preferences.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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_Preferences extends PHPUnit_Framework_TestCase {
  10. public static function setUpBeforeClass() {
  11. $query = \OC_DB::prepare('INSERT INTO `*PREFIX*preferences` VALUES(?, ?, ?, ?)');
  12. $query->execute(array("Someuser", "someapp", "somekey", "somevalue"));
  13. $query->execute(array("Someuser", "getusersapp", "somekey", "somevalue"));
  14. $query->execute(array("Anotheruser", "getusersapp", "somekey", "someothervalue"));
  15. $query->execute(array("Anuser", "getusersapp", "somekey", "somevalue"));
  16. $query->execute(array("Someuser", "getappsapp", "somekey", "somevalue"));
  17. $query->execute(array("Someuser", "getkeysapp", "firstkey", "somevalue"));
  18. $query->execute(array("Someuser", "getkeysapp", "anotherkey", "somevalue"));
  19. $query->execute(array("Someuser", "getkeysapp", "key-tastic", "somevalue"));
  20. $query->execute(array("Someuser", "getvalueapp", "key", "a value for a key"));
  21. $query->execute(array("Deleteuser", "deleteapp", "deletekey", "somevalue"));
  22. $query->execute(array("Deleteuser", "deleteapp", "somekey", "somevalue"));
  23. $query->execute(array("Deleteuser", "someapp", "somekey", "somevalue"));
  24. }
  25. public static function tearDownAfterClass() {
  26. $query = \OC_DB::prepare('DELETE FROM `*PREFIX*preferences` WHERE `userid` = ?');
  27. $query->execute(array('Someuser'));
  28. $query->execute(array('Anotheruser'));
  29. $query->execute(array('Anuser'));
  30. }
  31. public function testGetUsers() {
  32. $query = \OC_DB::prepare('SELECT DISTINCT `userid` FROM `*PREFIX*preferences`');
  33. $result = $query->execute();
  34. $expected = array();
  35. while ($row = $result->fetchRow()) {
  36. $expected[] = $row['userid'];
  37. }
  38. $this->assertEquals($expected, \OC_Preferences::getUsers());
  39. }
  40. public function testGetApps() {
  41. $query = \OC_DB::prepare('SELECT DISTINCT `appid` FROM `*PREFIX*preferences` WHERE `userid` = ?');
  42. $result = $query->execute(array('Someuser'));
  43. $expected = array();
  44. while ($row = $result->fetchRow()) {
  45. $expected[] = $row['appid'];
  46. }
  47. $this->assertEquals($expected, \OC_Preferences::getApps('Someuser'));
  48. }
  49. public function testGetKeys() {
  50. $query = \OC_DB::prepare('SELECT DISTINCT `configkey` FROM `*PREFIX*preferences` WHERE `userid` = ? AND `appid` = ?');
  51. $result = $query->execute(array('Someuser', 'getkeysapp'));
  52. $expected = array();
  53. while ($row = $result->fetchRow()) {
  54. $expected[] = $row['configkey'];
  55. }
  56. $this->assertEquals($expected, \OC_Preferences::getKeys('Someuser', 'getkeysapp'));
  57. }
  58. public function testGetValue() {
  59. $this->assertNull(\OC_Preferences::getValue('nonexistant', 'nonexistant', 'nonexistant'));
  60. $this->assertEquals('default', \OC_Preferences::getValue('nonexistant', 'nonexistant', 'nonexistant', 'default'));
  61. $query = \OC_DB::prepare('SELECT `configvalue` FROM `*PREFIX*preferences` WHERE `userid` = ? AND `appid` = ? AND `configkey` = ?');
  62. $result = $query->execute(array('Someuser', 'getvalueapp', 'key'));
  63. $row = $result->fetchRow();
  64. $expected = $row['configvalue'];
  65. $this->assertEquals($expected, \OC_Preferences::getValue('Someuser', 'getvalueapp', 'key'));
  66. }
  67. public function testSetValue() {
  68. $this->assertTrue(\OC_Preferences::setValue('Someuser', 'setvalueapp', 'newkey', 'newvalue'));
  69. $query = \OC_DB::prepare('SELECT `configvalue` FROM `*PREFIX*preferences` WHERE `userid` = ? AND `appid` = ? AND `configkey` = ?');
  70. $result = $query->execute(array('Someuser', 'setvalueapp', 'newkey'));
  71. $row = $result->fetchRow();
  72. $value = $row['configvalue'];
  73. $this->assertEquals('newvalue', $value);
  74. $this->assertTrue(\OC_Preferences::setValue('Someuser', 'setvalueapp', 'newkey', 'othervalue'));
  75. $query = \OC_DB::prepare('SELECT `configvalue` FROM `*PREFIX*preferences` WHERE `userid` = ? AND `appid` = ? AND `configkey` = ?');
  76. $result = $query->execute(array('Someuser', 'setvalueapp', 'newkey'));
  77. $row = $result->fetchRow();
  78. $value = $row['configvalue'];
  79. $this->assertEquals('othervalue', $value);
  80. }
  81. public function testDeleteKey() {
  82. $this->assertTrue(\OC_Preferences::deleteKey('Deleteuser', 'deleteapp', 'deletekey'));
  83. $query = \OC_DB::prepare('SELECT `configvalue` FROM `*PREFIX*preferences` WHERE `userid` = ? AND `appid` = ? AND `configkey` = ?');
  84. $result = $query->execute(array('Deleteuser', 'deleteapp', 'deletekey'));
  85. $this->assertEquals(0, count($result->fetchAll()));
  86. }
  87. public function testDeleteApp() {
  88. $this->assertTrue(\OC_Preferences::deleteApp('Deleteuser', 'deleteapp'));
  89. $query = \OC_DB::prepare('SELECT `configvalue` FROM `*PREFIX*preferences` WHERE `userid` = ? AND `appid` = ?');
  90. $result = $query->execute(array('Deleteuser', 'deleteapp'));
  91. $this->assertEquals(0, count($result->fetchAll()));
  92. }
  93. public function testDeleteUser() {
  94. $this->assertTrue(\OC_Preferences::deleteUser('Deleteuser'));
  95. $query = \OC_DB::prepare('SELECT `configvalue` FROM `*PREFIX*preferences` WHERE `userid` = ?');
  96. $result = $query->execute(array('Deleteuser'));
  97. $this->assertEquals(0, count($result->fetchAll()));
  98. }
  99. public function testDeleteAppFromAllUsers() {
  100. $this->assertTrue(\OC_Preferences::deleteAppFromAllUsers('someapp'));
  101. $query = \OC_DB::prepare('SELECT `configvalue` FROM `*PREFIX*preferences` WHERE `appid` = ?');
  102. $result = $query->execute(array('someapp'));
  103. $this->assertEquals(0, count($result->fetchAll()));
  104. }
  105. }
  106. class Test_Preferences_Object extends PHPUnit_Framework_TestCase {
  107. public function testGetUsers()
  108. {
  109. $statementMock = $this->getMock('\Doctrine\DBAL\Statement', array(), array(), '', false);
  110. $statementMock->expects($this->exactly(2))
  111. ->method('fetchColumn')
  112. ->will($this->onConsecutiveCalls('foo', false));
  113. $connectionMock = $this->getMock('\OC\DB\Connection', array(), array(), '', false);
  114. $connectionMock->expects($this->once())
  115. ->method('executeQuery')
  116. ->with($this->equalTo('SELECT DISTINCT `userid` FROM `*PREFIX*preferences`'))
  117. ->will($this->returnValue($statementMock));
  118. $preferences = new OC\Preferences($connectionMock);
  119. $apps = $preferences->getUsers();
  120. $this->assertEquals(array('foo'), $apps);
  121. }
  122. public function testSetValue()
  123. {
  124. $connectionMock = $this->getMock('\OC\DB\Connection', array(), array(), '', false);
  125. $connectionMock->expects($this->exactly(2))
  126. ->method('fetchColumn')
  127. ->with($this->equalTo('SELECT COUNT(*) FROM `*PREFIX*preferences`'
  128. .' WHERE `userid` = ? AND `appid` = ? AND `configkey` = ?'),
  129. $this->equalTo(array('grg', 'bar', 'foo')))
  130. ->will($this->onConsecutiveCalls(0, 1));
  131. $connectionMock->expects($this->once())
  132. ->method('insert')
  133. ->with($this->equalTo('*PREFIX*preferences'),
  134. $this->equalTo(
  135. array(
  136. 'userid' => 'grg',
  137. 'appid' => 'bar',
  138. 'configkey' => 'foo',
  139. 'configvalue' => 'v1',
  140. )
  141. ));
  142. $connectionMock->expects($this->once())
  143. ->method('update')
  144. ->with($this->equalTo('*PREFIX*preferences'),
  145. $this->equalTo(
  146. array(
  147. 'configvalue' => 'v2',
  148. )),
  149. $this->equalTo(
  150. array(
  151. 'userid' => 'grg',
  152. 'appid' => 'bar',
  153. 'configkey' => 'foo',
  154. )
  155. ));
  156. $preferences = new OC\Preferences($connectionMock);
  157. $preferences->setValue('grg', 'bar', 'foo', 'v1');
  158. $preferences->setValue('grg', 'bar', 'foo', 'v2');
  159. }
  160. public function testGetUserValues()
  161. {
  162. $query = \OC_DB::prepare('INSERT INTO `*PREFIX*preferences` VALUES(?, ?, ?, ?)');
  163. $query->execute(array('SomeUser', 'testGetUserValues', 'somekey', 'somevalue'));
  164. $query->execute(array('AnotherUser', 'testGetUserValues', 'somekey', 'someothervalue'));
  165. $query->execute(array('AUser', 'testGetUserValues', 'somekey', 'somevalue'));
  166. $preferences = new OC\Preferences(\OC_DB::getConnection());
  167. $users = array('SomeUser', 'AnotherUser', 'NoValueSet');
  168. $values = $preferences->getValueForUsers('testGetUserValues', 'somekey', $users);
  169. $this->assertUserValues($values);
  170. // Add a lot of users so the array is chunked
  171. for ($i = 1; $i <= 75; $i++) {
  172. array_unshift($users, 'NoValueBefore#' . $i);
  173. array_push($users, 'NoValueAfter#' . $i);
  174. }
  175. $values = $preferences->getValueForUsers('testGetUserValues', 'somekey', $users);
  176. $this->assertUserValues($values);
  177. // Clean DB after the test
  178. $query = \OC_DB::prepare('DELETE FROM `*PREFIX*preferences` WHERE `appid` = ?');
  179. $query->execute(array('testGetUserValues'));
  180. }
  181. protected function assertUserValues($values) {
  182. $this->assertEquals(2, sizeof($values));
  183. $this->assertArrayHasKey('SomeUser', $values);
  184. $this->assertEquals('somevalue', $values['SomeUser']);
  185. $this->assertArrayHasKey('AnotherUser', $values);
  186. $this->assertEquals('someothervalue', $values['AnotherUser']);
  187. }
  188. public function testDeleteKey()
  189. {
  190. $connectionMock = $this->getMock('\OC\DB\Connection', array(), array(), '', false);
  191. $connectionMock->expects($this->once())
  192. ->method('delete')
  193. ->with($this->equalTo('*PREFIX*preferences'),
  194. $this->equalTo(
  195. array(
  196. 'userid' => 'grg',
  197. 'appid' => 'bar',
  198. 'configkey' => 'foo',
  199. )
  200. ));
  201. $preferences = new OC\Preferences($connectionMock);
  202. $preferences->deleteKey('grg', 'bar', 'foo');
  203. }
  204. public function testDeleteApp()
  205. {
  206. $connectionMock = $this->getMock('\OC\DB\Connection', array(), array(), '', false);
  207. $connectionMock->expects($this->once())
  208. ->method('delete')
  209. ->with($this->equalTo('*PREFIX*preferences'),
  210. $this->equalTo(
  211. array(
  212. 'userid' => 'grg',
  213. 'appid' => 'bar',
  214. )
  215. ));
  216. $preferences = new OC\Preferences($connectionMock);
  217. $preferences->deleteApp('grg', 'bar');
  218. }
  219. public function testDeleteUser()
  220. {
  221. $connectionMock = $this->getMock('\OC\DB\Connection', array(), array(), '', false);
  222. $connectionMock->expects($this->once())
  223. ->method('delete')
  224. ->with($this->equalTo('*PREFIX*preferences'),
  225. $this->equalTo(
  226. array(
  227. 'userid' => 'grg',
  228. )
  229. ));
  230. $preferences = new OC\Preferences($connectionMock);
  231. $preferences->deleteUser('grg');
  232. }
  233. public function testDeleteAppFromAllUsers()
  234. {
  235. $connectionMock = $this->getMock('\OC\DB\Connection', array(), array(), '', false);
  236. $connectionMock->expects($this->once())
  237. ->method('delete')
  238. ->with($this->equalTo('*PREFIX*preferences'),
  239. $this->equalTo(
  240. array(
  241. 'appid' => 'bar',
  242. )
  243. ));
  244. $preferences = new OC\Preferences($connectionMock);
  245. $preferences->deleteAppFromAllUsers('bar');
  246. }
  247. }