preferences.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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, $result->numRows());
  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, $result->numRows());
  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, $result->numRows());
  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, $result->numRows());
  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 testGetApps()
  123. {
  124. $statementMock = $this->getMock('\Doctrine\DBAL\Statement', array(), array(), '', false);
  125. $statementMock->expects($this->exactly(2))
  126. ->method('fetchColumn')
  127. ->will($this->onConsecutiveCalls('foo', false));
  128. $connectionMock = $this->getMock('\OC\DB\Connection', array(), array(), '', false);
  129. $connectionMock->expects($this->once())
  130. ->method('executeQuery')
  131. ->with($this->equalTo('SELECT DISTINCT `appid` FROM `*PREFIX*preferences` WHERE `userid` = ?'),
  132. $this->equalTo(array('bar')))
  133. ->will($this->returnValue($statementMock));
  134. $preferences = new OC\Preferences($connectionMock);
  135. $apps = $preferences->getApps('bar');
  136. $this->assertEquals(array('foo'), $apps);
  137. }
  138. public function testGetKeys()
  139. {
  140. $statementMock = $this->getMock('\Doctrine\DBAL\Statement', array(), array(), '', false);
  141. $statementMock->expects($this->exactly(2))
  142. ->method('fetchColumn')
  143. ->will($this->onConsecutiveCalls('foo', false));
  144. $connectionMock = $this->getMock('\OC\DB\Connection', array(), array(), '', false);
  145. $connectionMock->expects($this->once())
  146. ->method('executeQuery')
  147. ->with($this->equalTo('SELECT `configkey` FROM `*PREFIX*preferences` WHERE `userid` = ? AND `appid` = ?'),
  148. $this->equalTo(array('bar', 'moo')))
  149. ->will($this->returnValue($statementMock));
  150. $preferences = new OC\Preferences($connectionMock);
  151. $keys = $preferences->getKeys('bar', 'moo');
  152. $this->assertEquals(array('foo'), $keys);
  153. }
  154. public function testGetValue()
  155. {
  156. $connectionMock = $this->getMock('\OC\DB\Connection', array(), array(), '', false);
  157. $connectionMock->expects($this->exactly(2))
  158. ->method('fetchAssoc')
  159. ->with($this->equalTo('SELECT `configvalue` FROM `*PREFIX*preferences` WHERE `userid` = ? AND `appid` = ? AND `configkey` = ?'),
  160. $this->equalTo(array('grg', 'bar', 'red')))
  161. ->will($this->onConsecutiveCalls(array('configvalue'=>'foo'), null));
  162. $preferences = new OC\Preferences($connectionMock);
  163. $value = $preferences->getValue('grg', 'bar', 'red');
  164. $this->assertEquals('foo', $value);
  165. $value = $preferences->getValue('grg', 'bar', 'red', 'def');
  166. $this->assertEquals('def', $value);
  167. }
  168. public function testSetValue()
  169. {
  170. $connectionMock = $this->getMock('\OC\DB\Connection', array(), array(), '', false);
  171. $connectionMock->expects($this->exactly(2))
  172. ->method('fetchColumn')
  173. ->with($this->equalTo('SELECT COUNT(*) FROM `*PREFIX*preferences`'
  174. .' WHERE `userid` = ? AND `appid` = ? AND `configkey` = ?'),
  175. $this->equalTo(array('grg', 'bar', 'foo')))
  176. ->will($this->onConsecutiveCalls(0, 1));
  177. $connectionMock->expects($this->once())
  178. ->method('insert')
  179. ->with($this->equalTo('*PREFIX*preferences'),
  180. $this->equalTo(
  181. array(
  182. 'userid' => 'grg',
  183. 'appid' => 'bar',
  184. 'configkey' => 'foo',
  185. 'configvalue' => 'v1',
  186. )
  187. ));
  188. $connectionMock->expects($this->once())
  189. ->method('update')
  190. ->with($this->equalTo('*PREFIX*preferences'),
  191. $this->equalTo(
  192. array(
  193. 'configvalue' => 'v2',
  194. )),
  195. $this->equalTo(
  196. array(
  197. 'userid' => 'grg',
  198. 'appid' => 'bar',
  199. 'configkey' => 'foo',
  200. )
  201. ));
  202. $preferences = new OC\Preferences($connectionMock);
  203. $preferences->setValue('grg', 'bar', 'foo', 'v1');
  204. $preferences->setValue('grg', 'bar', 'foo', 'v2');
  205. }
  206. public function testDeleteKey()
  207. {
  208. $connectionMock = $this->getMock('\OC\DB\Connection', array(), array(), '', false);
  209. $connectionMock->expects($this->once())
  210. ->method('delete')
  211. ->with($this->equalTo('*PREFIX*preferences'),
  212. $this->equalTo(
  213. array(
  214. 'userid' => 'grg',
  215. 'appid' => 'bar',
  216. 'configkey' => 'foo',
  217. )
  218. ));
  219. $preferences = new OC\Preferences($connectionMock);
  220. $preferences->deleteKey('grg', 'bar', 'foo');
  221. }
  222. public function testDeleteApp()
  223. {
  224. $connectionMock = $this->getMock('\OC\DB\Connection', array(), array(), '', false);
  225. $connectionMock->expects($this->once())
  226. ->method('delete')
  227. ->with($this->equalTo('*PREFIX*preferences'),
  228. $this->equalTo(
  229. array(
  230. 'userid' => 'grg',
  231. 'appid' => 'bar',
  232. )
  233. ));
  234. $preferences = new OC\Preferences($connectionMock);
  235. $preferences->deleteApp('grg', 'bar');
  236. }
  237. public function testDeleteUser()
  238. {
  239. $connectionMock = $this->getMock('\OC\DB\Connection', array(), array(), '', false);
  240. $connectionMock->expects($this->once())
  241. ->method('delete')
  242. ->with($this->equalTo('*PREFIX*preferences'),
  243. $this->equalTo(
  244. array(
  245. 'userid' => 'grg',
  246. )
  247. ));
  248. $preferences = new OC\Preferences($connectionMock);
  249. $preferences->deleteUser('grg');
  250. }
  251. public function testDeleteAppFromAllUsers()
  252. {
  253. $connectionMock = $this->getMock('\OC\DB\Connection', array(), array(), '', false);
  254. $connectionMock->expects($this->once())
  255. ->method('delete')
  256. ->with($this->equalTo('*PREFIX*preferences'),
  257. $this->equalTo(
  258. array(
  259. 'appid' => 'bar',
  260. )
  261. ));
  262. $preferences = new OC\Preferences($connectionMock);
  263. $preferences->deleteAppFromAllUsers('bar');
  264. }
  265. }