appconfig.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. /**
  3. * Copyright (c) 2013 Christopher Schäpers <christopher@schaepers.it>
  4. * Copyright (c) 2013 Bart Visscher <bartv@thisnet.nl>
  5. * This file is licensed under the Affero General Public License version 3 or
  6. * later.
  7. * See the COPYING-README file.
  8. */
  9. class Test_Appconfig extends \Test\TestCase {
  10. public static function setUpBeforeClass() {
  11. parent::setUpBeforeClass();
  12. $query = \OC_DB::prepare('DELETE FROM `*PREFIX*appconfig` WHERE `appid` = ?');
  13. $query->execute(array('testapp'));
  14. $query->execute(array('someapp'));
  15. $query->execute(array('123456'));
  16. $query->execute(array('anotherapp'));
  17. $query = \OC_DB::prepare('INSERT INTO `*PREFIX*appconfig` VALUES (?, ?, ?)');
  18. $query->execute(array('testapp', 'enabled', 'true'));
  19. $query->execute(array('testapp', 'installed_version', '1.2.3'));
  20. $query->execute(array('testapp', 'depends_on', 'someapp'));
  21. $query->execute(array('testapp', 'deletethis', 'deletethis'));
  22. $query->execute(array('testapp', 'key', 'value'));
  23. $query->execute(array('someapp', 'key', 'value'));
  24. $query->execute(array('someapp', 'otherkey', 'othervalue'));
  25. $query->execute(array('123456', 'key', 'value'));
  26. $query->execute(array('123456', 'enabled', 'false'));
  27. $query->execute(array('anotherapp', 'key', 'value'));
  28. $query->execute(array('anotherapp', 'enabled', 'false'));
  29. }
  30. public static function tearDownAfterClass() {
  31. $query = \OC_DB::prepare('DELETE FROM `*PREFIX*appconfig` WHERE `appid` = ?');
  32. $query->execute(array('testapp'));
  33. $query->execute(array('someapp'));
  34. $query->execute(array('123456'));
  35. $query->execute(array('anotherapp'));
  36. parent::tearDownAfterClass();
  37. }
  38. public function testGetApps() {
  39. $query = \OC_DB::prepare('SELECT DISTINCT `appid` FROM `*PREFIX*appconfig` ORDER BY `appid`');
  40. $result = $query->execute();
  41. $expected = array();
  42. while ($row = $result->fetchRow()) {
  43. $expected[] = $row['appid'];
  44. }
  45. sort($expected);
  46. $apps = \OC_Appconfig::getApps();
  47. $this->assertEquals($expected, $apps);
  48. }
  49. public function testGetKeys() {
  50. $query = \OC_DB::prepare('SELECT `configkey` FROM `*PREFIX*appconfig` WHERE `appid` = ?');
  51. $result = $query->execute(array('testapp'));
  52. $expected = array();
  53. while($row = $result->fetchRow()) {
  54. $expected[] = $row["configkey"];
  55. }
  56. sort($expected);
  57. $keys = \OC_Appconfig::getKeys('testapp');
  58. $this->assertEquals($expected, $keys);
  59. }
  60. public function testGetValue() {
  61. $query = \OC_DB::prepare('SELECT `configvalue` FROM `*PREFIX*appconfig` WHERE `appid` = ? AND `configkey` = ?');
  62. $result = $query->execute(array('testapp', 'installed_version'));
  63. $expected = $result->fetchRow();
  64. $value = \OC_Appconfig::getValue('testapp', 'installed_version');
  65. $this->assertEquals($expected['configvalue'], $value);
  66. $value = \OC_Appconfig::getValue('testapp', 'nonexistant');
  67. $this->assertNull($value);
  68. $value = \OC_Appconfig::getValue('testapp', 'nonexistant', 'default');
  69. $this->assertEquals('default', $value);
  70. }
  71. public function testHasKey() {
  72. $value = \OC_Appconfig::hasKey('testapp', 'installed_version');
  73. $this->assertTrue($value);
  74. $value = \OC_Appconfig::hasKey('nonexistant', 'nonexistant');
  75. $this->assertFalse($value);
  76. }
  77. public function testSetValue() {
  78. \OC_Appconfig::setValue('testapp', 'installed_version', '1.33.7');
  79. $query = \OC_DB::prepare('SELECT `configvalue` FROM `*PREFIX*appconfig` WHERE `appid` = ? AND `configkey` = ?');
  80. $result = $query->execute(array('testapp', 'installed_version'));
  81. $value = $result->fetchRow();
  82. $this->assertEquals('1.33.7', $value['configvalue']);
  83. \OC_Appconfig::setValue('someapp', 'somekey', 'somevalue');
  84. $query = \OC_DB::prepare('SELECT `configvalue` FROM `*PREFIX*appconfig` WHERE `appid` = ? AND `configkey` = ?');
  85. $result = $query->execute(array('someapp', 'somekey'));
  86. $value = $result->fetchRow();
  87. $this->assertEquals('somevalue', $value['configvalue']);
  88. }
  89. public function testSetValueUnchanged() {
  90. $statementMock = $this->getMock('\Doctrine\DBAL\Statement', array(), array(), '', false);
  91. $statementMock->expects($this->once())
  92. ->method('fetch')
  93. ->will($this->returnValue(false));
  94. $connectionMock = $this->getMock('\OC\DB\Connection', array(), array(), '', false);
  95. $connectionMock->expects($this->once())
  96. ->method('executeQuery')
  97. ->with($this->equalTo('SELECT `configvalue`, `configkey` FROM `*PREFIX*appconfig`'
  98. .' WHERE `appid` = ?'), $this->equalTo(array('bar')))
  99. ->will($this->returnValue($statementMock));
  100. $connectionMock->expects($this->once())
  101. ->method('insert')
  102. ->with($this->equalTo('*PREFIX*appconfig'),
  103. $this->equalTo(
  104. array(
  105. 'appid' => 'bar',
  106. 'configkey' => 'foo',
  107. 'configvalue' => 'v1',
  108. )
  109. ));
  110. $connectionMock->expects($this->never())
  111. ->method('update');
  112. $appconfig = new OC\AppConfig($connectionMock);
  113. $appconfig->setValue('bar', 'foo', 'v1');
  114. $appconfig->setValue('bar', 'foo', 'v1');
  115. $appconfig->setValue('bar', 'foo', 'v1');
  116. }
  117. public function testSetValueUnchanged2() {
  118. $statementMock = $this->getMock('\Doctrine\DBAL\Statement', array(), array(), '', false);
  119. $statementMock->expects($this->once())
  120. ->method('fetch')
  121. ->will($this->returnValue(false));
  122. $connectionMock = $this->getMock('\OC\DB\Connection', array(), array(), '', false);
  123. $connectionMock->expects($this->once())
  124. ->method('executeQuery')
  125. ->with($this->equalTo('SELECT `configvalue`, `configkey` FROM `*PREFIX*appconfig`'
  126. .' WHERE `appid` = ?'), $this->equalTo(array('bar')))
  127. ->will($this->returnValue($statementMock));
  128. $connectionMock->expects($this->once())
  129. ->method('insert')
  130. ->with($this->equalTo('*PREFIX*appconfig'),
  131. $this->equalTo(
  132. array(
  133. 'appid' => 'bar',
  134. 'configkey' => 'foo',
  135. 'configvalue' => 'v1',
  136. )
  137. ));
  138. $connectionMock->expects($this->once())
  139. ->method('update')
  140. ->with($this->equalTo('*PREFIX*appconfig'),
  141. $this->equalTo(array('configvalue' => 'v2')),
  142. $this->equalTo(array('appid' => 'bar', 'configkey' => 'foo'))
  143. );
  144. $appconfig = new OC\AppConfig($connectionMock);
  145. $appconfig->setValue('bar', 'foo', 'v1');
  146. $appconfig->setValue('bar', 'foo', 'v2');
  147. $appconfig->setValue('bar', 'foo', 'v2');
  148. }
  149. public function testDeleteKey() {
  150. \OC_Appconfig::deleteKey('testapp', 'deletethis');
  151. $query = \OC_DB::prepare('SELECT `configvalue` FROM `*PREFIX*appconfig` WHERE `appid` = ? AND `configkey` = ?');
  152. $query->execute(array('testapp', 'deletethis'));
  153. $result = (bool)$query->fetchRow();
  154. $this->assertFalse($result);
  155. }
  156. public function testDeleteApp() {
  157. \OC_Appconfig::deleteApp('someapp');
  158. $query = \OC_DB::prepare('SELECT `configkey` FROM `*PREFIX*appconfig` WHERE `appid` = ?');
  159. $query->execute(array('someapp'));
  160. $result = (bool)$query->fetchRow();
  161. $this->assertFalse($result);
  162. }
  163. public function testGetValues() {
  164. $this->assertFalse(\OC_Appconfig::getValues('testapp', 'enabled'));
  165. $query = \OC_DB::prepare('SELECT `configkey`, `configvalue` FROM `*PREFIX*appconfig` WHERE `appid` = ?');
  166. $query->execute(array('testapp'));
  167. $expected = array();
  168. while ($row = $query->fetchRow()) {
  169. $expected[$row['configkey']] = $row['configvalue'];
  170. }
  171. $values = \OC_Appconfig::getValues('testapp', false);
  172. $this->assertEquals($expected, $values);
  173. $query = \OC_DB::prepare('SELECT `appid`, `configvalue` FROM `*PREFIX*appconfig` WHERE `configkey` = ?');
  174. $query->execute(array('enabled'));
  175. $expected = array();
  176. while ($row = $query->fetchRow()) {
  177. $expected[$row['appid']] = $row['configvalue'];
  178. }
  179. $values = \OC_Appconfig::getValues(false, 'enabled');
  180. $this->assertEquals($expected, $values);
  181. }
  182. }