appconfig.php 7.3 KB

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