appconfig.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. /**
  3. * Copyright (c) 2013 Christopher Schäpers <christopher@schaepers.it>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. class Test_Appconfig extends PHPUnit_Framework_TestCase {
  9. public static function setUpBeforeClass() {
  10. $query = \OC_DB::prepare('INSERT INTO `*PREFIX*appconfig` VALUES (?, ?, ?)');
  11. $query->execute(array('testapp', 'enabled', 'true'));
  12. $query->execute(array('testapp', 'installed_version', '1.2.3'));
  13. $query->execute(array('testapp', 'depends_on', 'someapp'));
  14. $query->execute(array('testapp', 'deletethis', 'deletethis'));
  15. $query->execute(array('testapp', 'key', 'value'));
  16. $query->execute(array('someapp', 'key', 'value'));
  17. $query->execute(array('someapp', 'otherkey', 'othervalue'));
  18. $query->execute(array('123456', 'key', 'value'));
  19. $query->execute(array('123456', 'enabled', 'false'));
  20. $query->execute(array('anotherapp', 'key', 'value'));
  21. $query->execute(array('anotherapp', 'enabled', 'false'));
  22. }
  23. public static function tearDownAfterClass() {
  24. $query = \OC_DB::prepare('DELETE FROM `*PREFIX*appconfig` WHERE `appid` = ?');
  25. $query->execute(array('testapp'));
  26. $query->execute(array('someapp'));
  27. $query->execute(array('123456'));
  28. $query->execute(array('anotherapp'));
  29. }
  30. public function testGetApps() {
  31. $query = \OC_DB::prepare('SELECT DISTINCT `appid` FROM `*PREFIX*appconfig`');
  32. $result = $query->execute();
  33. $expected = array();
  34. while ($row = $result->fetchRow()) {
  35. $expected[] = $row['appid'];
  36. }
  37. $apps = \OC_Appconfig::getApps();
  38. $this->assertEquals($expected, $apps);
  39. }
  40. public function testGetKeys() {
  41. $query = \OC_DB::prepare('SELECT `configkey` FROM `*PREFIX*appconfig` WHERE `appid` = ?');
  42. $result = $query->execute(array('testapp'));
  43. $expected = array();
  44. while($row = $result->fetchRow()) {
  45. $expected[] = $row["configkey"];
  46. }
  47. $keys = \OC_Appconfig::getKeys('testapp');
  48. $this->assertEquals($expected, $keys);
  49. }
  50. public function testGetValue() {
  51. $query = \OC_DB::prepare('SELECT `configvalue` FROM `*PREFIX*appconfig` WHERE `appid` = ? AND `configkey` = ?');
  52. $result = $query->execute(array('testapp', 'installed_version'));
  53. $expected = $result->fetchRow();
  54. $value = \OC_Appconfig::getValue('testapp', 'installed_version');
  55. $this->assertEquals($expected['configvalue'], $value);
  56. $value = \OC_Appconfig::getValue('testapp', 'nonexistant');
  57. $this->assertNull($value);
  58. $value = \OC_Appconfig::getValue('testapp', 'nonexistant', 'default');
  59. $this->assertEquals('default', $value);
  60. }
  61. public function testHasKey() {
  62. $value = \OC_Appconfig::hasKey('testapp', 'installed_version');
  63. $this->assertTrue($value);
  64. $value = \OC_Appconfig::hasKey('nonexistant', 'nonexistant');
  65. $this->assertFalse($value);
  66. }
  67. public function testSetValue() {
  68. \OC_Appconfig::setValue('testapp', 'installed_version', '1.33.7');
  69. $query = \OC_DB::prepare('SELECT `configvalue` FROM `*PREFIX*appconfig` WHERE `appid` = ? AND `configkey` = ?');
  70. $result = $query->execute(array('testapp', 'installed_version'));
  71. $value = $result->fetchRow();
  72. $this->assertEquals('1.33.7', $value['configvalue']);
  73. \OC_Appconfig::setValue('someapp', 'somekey', 'somevalue');
  74. $query = \OC_DB::prepare('SELECT `configvalue` FROM `*PREFIX*appconfig` WHERE `appid` = ? AND `configkey` = ?');
  75. $result = $query->execute(array('someapp', 'somekey'));
  76. $value = $result->fetchRow();
  77. $this->assertEquals('somevalue', $value['configvalue']);
  78. }
  79. public function testDeleteKey() {
  80. \OC_Appconfig::deleteKey('testapp', 'deletethis');
  81. $query = \OC_DB::prepare('SELECT `configvalue` FROM `*PREFIX*appconfig` WHERE `appid` = ? AND `configkey` = ?');
  82. $query->execute(array('testapp', 'deletethis'));
  83. $result = (bool)$query->fetchRow();
  84. $this->assertFalse($result);
  85. }
  86. public function testDeleteApp() {
  87. \OC_Appconfig::deleteApp('someapp');
  88. $query = \OC_DB::prepare('SELECT `configkey` FROM `*PREFIX*appconfig` WHERE `appid` = ?');
  89. $query->execute(array('someapp'));
  90. $result = (bool)$query->fetchRow();
  91. $this->assertFalse($result);
  92. }
  93. public function testGetValues() {
  94. $this->assertFalse(\OC_Appconfig::getValues('testapp', 'enabled'));
  95. $query = \OC_DB::prepare('SELECT `configkey`, `configvalue` FROM `*PREFIX*appconfig` WHERE `appid` = ?');
  96. $query->execute(array('testapp'));
  97. $expected = array();
  98. while ($row = $query->fetchRow()) {
  99. $expected[$row['configkey']] = $row['configvalue'];
  100. }
  101. $values = \OC_Appconfig::getValues('testapp', false);
  102. $this->assertEquals($expected, $values);
  103. $query = \OC_DB::prepare('SELECT `appid`, `configvalue` FROM `*PREFIX*appconfig` WHERE `configkey` = ?');
  104. $query->execute(array('enabled'));
  105. $expected = array();
  106. while ($row = $query->fetchRow()) {
  107. $expected[$row['appid']] = $row['configvalue'];
  108. }
  109. $values = \OC_Appconfig::getValues(false, 'enabled');
  110. $this->assertEquals($expected, $values);
  111. }
  112. }