preferences.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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_Preferences extends PHPUnit_Framework_TestCase {
  9. public static function setUpBeforeClass() {
  10. $query = \OC_DB::prepare('INSERT INTO `*PREFIX*preferences` VALUES(?, ?, ?, ?)');
  11. $query->execute(array("Someuser", "someapp", "somekey", "somevalue"));
  12. $query->execute(array("Someuser", "getusersapp", "somekey", "somevalue"));
  13. $query->execute(array("Anotheruser", "getusersapp", "somekey", "someothervalue"));
  14. $query->execute(array("Anuser", "getusersapp", "somekey", "somevalue"));
  15. $query->execute(array("Someuser", "getappsapp", "somekey", "somevalue"));
  16. $query->execute(array("Someuser", "getkeysapp", "firstkey", "somevalue"));
  17. $query->execute(array("Someuser", "getkeysapp", "anotherkey", "somevalue"));
  18. $query->execute(array("Someuser", "getkeysapp", "key-tastic", "somevalue"));
  19. $query->execute(array("Someuser", "getvalueapp", "key", "a value for a key"));
  20. $query->execute(array("Deleteuser", "deleteapp", "deletekey", "somevalue"));
  21. $query->execute(array("Deleteuser", "deleteapp", "somekey", "somevalue"));
  22. $query->execute(array("Deleteuser", "someapp", "somekey", "somevalue"));
  23. }
  24. public static function tearDownAfterClass() {
  25. $query = \OC_DB::prepare('DELETE FROM `*PREFIX*preferences` WHERE `userid` = ?');
  26. $query->execute(array('Someuser'));
  27. $query->execute(array('Anotheruser'));
  28. $query->execute(array('Anuser'));
  29. }
  30. public function testGetUsers() {
  31. $query = \OC_DB::prepare('SELECT DISTINCT `userid` FROM `*PREFIX*preferences`');
  32. $result = $query->execute();
  33. $expected = array();
  34. while ($row = $result->fetchRow()) {
  35. $expected[] = $row['userid'];
  36. }
  37. $this->assertEquals($expected, \OC_Preferences::getUsers());
  38. }
  39. public function testGetApps() {
  40. $query = \OC_DB::prepare('SELECT DISTINCT `appid` FROM `*PREFIX*preferences` WHERE `userid` = ?');
  41. $result = $query->execute(array('Someuser'));
  42. $expected = array();
  43. while ($row = $result->fetchRow()) {
  44. $expected[] = $row['appid'];
  45. }
  46. $this->assertEquals($expected, \OC_Preferences::getApps('Someuser'));
  47. }
  48. public function testGetKeys() {
  49. $query = \OC_DB::prepare('SELECT DISTINCT `configkey` FROM `*PREFIX*preferences` WHERE `userid` = ? AND `appid` = ?');
  50. $result = $query->execute(array('Someuser', 'getkeysapp'));
  51. $expected = array();
  52. while ($row = $result->fetchRow()) {
  53. $expected[] = $row['configkey'];
  54. }
  55. $this->assertEquals($expected, \OC_Preferences::getKeys('Someuser', 'getkeysapp'));
  56. }
  57. public function testGetValue() {
  58. $this->assertNull(\OC_Preferences::getValue('nonexistant', 'nonexistant', 'nonexistant'));
  59. $this->assertEquals('default', \OC_Preferences::getValue('nonexistant', 'nonexistant', 'nonexistant', 'default'));
  60. $query = \OC_DB::prepare('SELECT `configvalue` FROM `*PREFIX*preferences` WHERE `userid` = ? AND `appid` = ? AND `configkey` = ?');
  61. $result = $query->execute(array('Someuser', 'getvalueapp', 'key'));
  62. $row = $result->fetchRow();
  63. $expected = $row['configvalue'];
  64. $this->assertEquals($expected, \OC_Preferences::getValue('Someuser', 'getvalueapp', 'key'));
  65. }
  66. public function testSetValue() {
  67. $this->assertTrue(\OC_Preferences::setValue('Someuser', 'setvalueapp', 'newkey', 'newvalue'));
  68. $query = \OC_DB::prepare('SELECT `configvalue` FROM `*PREFIX*preferences` WHERE `userid` = ? AND `appid` = ? AND `configkey` = ?');
  69. $result = $query->execute(array('Someuser', 'setvalueapp', 'newkey'));
  70. $row = $result->fetchRow();
  71. $value = $row['configvalue'];
  72. $this->assertEquals('newvalue', $value);
  73. $this->assertTrue(\OC_Preferences::setValue('Someuser', 'setvalueapp', 'newkey', 'othervalue'));
  74. $query = \OC_DB::prepare('SELECT `configvalue` FROM `*PREFIX*preferences` WHERE `userid` = ? AND `appid` = ? AND `configkey` = ?');
  75. $result = $query->execute(array('Someuser', 'setvalueapp', 'newkey'));
  76. $row = $result->fetchRow();
  77. $value = $row['configvalue'];
  78. $this->assertEquals('othervalue', $value);
  79. }
  80. public function testDeleteKey() {
  81. $this->assertTrue(\OC_Preferences::deleteKey('Deleteuser', 'deleteapp', 'deletekey'));
  82. $query = \OC_DB::prepare('SELECT `configvalue` FROM `*PREFIX*preferences` WHERE `userid` = ? AND `appid` = ? AND `configkey` = ?');
  83. $result = $query->execute(array('Deleteuser', 'deleteapp', 'deletekey'));
  84. $this->assertEquals(0, $result->numRows());
  85. }
  86. public function testDeleteApp() {
  87. $this->assertTrue(\OC_Preferences::deleteApp('Deleteuser', 'deleteapp'));
  88. $query = \OC_DB::prepare('SELECT `configvalue` FROM `*PREFIX*preferences` WHERE `userid` = ? AND `appid` = ?');
  89. $result = $query->execute(array('Deleteuser', 'deleteapp'));
  90. $this->assertEquals(0, $result->numRows());
  91. }
  92. public function testDeleteUser() {
  93. $this->assertTrue(\OC_Preferences::deleteUser('Deleteuser'));
  94. $query = \OC_DB::prepare('SELECT `configvalue` FROM `*PREFIX*preferences` WHERE `userid` = ?');
  95. $result = $query->execute(array('Deleteuser'));
  96. $this->assertEquals(0, $result->numRows());
  97. }
  98. public function testDeleteAppFromAllUsers() {
  99. $this->assertTrue(\OC_Preferences::deleteAppFromAllUsers('someapp'));
  100. $query = \OC_DB::prepare('SELECT `configvalue` FROM `*PREFIX*preferences` WHERE `appid` = ?');
  101. $result = $query->execute(array('someapp'));
  102. $this->assertEquals(0, $result->numRows());
  103. }
  104. }