privatedata.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. /**
  3. * ownCloud
  4. *
  5. * @author Thomas Müller
  6. * @copyright 2013 Thomas Müller deepdiver@owncloud.com
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  10. * License as published by the Free Software Foundation; either
  11. * version 3 of the License, or any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public
  19. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. /**
  23. * Class Test_OC_OCS_Privatedata
  24. *
  25. * @group DB
  26. */
  27. class Test_OC_OCS_Privatedata extends \Test\TestCase {
  28. private $appKey;
  29. protected function setUp() {
  30. parent::setUp();
  31. \OC::$server->getSession()->set('user_id', 'user1');
  32. $this->appKey = $this->getUniqueID('app');
  33. }
  34. public function testGetEmptyOne() {
  35. $params = array('app' => $this->appKey, 'key' => '123');
  36. $result = OC_OCS_Privatedata::get($params);
  37. $this->assertOcsResult(0, $result);
  38. }
  39. public function testGetEmptyAll() {
  40. $params = array('app' => $this->appKey);
  41. $result = OC_OCS_Privatedata::get($params);
  42. $this->assertOcsResult(0, $result);
  43. }
  44. public function testSetOne() {
  45. $_POST = array('value' => 123456789);
  46. $params = array('app' => $this->appKey, 'key' => 'k-1');
  47. $result = OC_OCS_Privatedata::set($params);
  48. $this->assertEquals(100, $result->getStatusCode());
  49. $result = OC_OCS_Privatedata::get($params);
  50. $this->assertOcsResult(1, $result);
  51. }
  52. public function testSetExisting() {
  53. $_POST = array('value' => 123456789);
  54. $params = array('app' => $this->appKey, 'key' => 'k-10');
  55. $result = OC_OCS_Privatedata::set($params);
  56. $this->assertEquals(100, $result->getStatusCode());
  57. $result = OC_OCS_Privatedata::get($params);
  58. $this->assertOcsResult(1, $result);
  59. $data = $result->getData();
  60. $data = $data[0];
  61. $this->assertEquals('123456789', $data['value']);
  62. $_POST = array('value' => 'updated');
  63. $params = array('app' => $this->appKey, 'key' => 'k-10');
  64. $result = OC_OCS_Privatedata::set($params);
  65. $this->assertEquals(100, $result->getStatusCode());
  66. $result = OC_OCS_Privatedata::get($params);
  67. $this->assertOcsResult(1, $result);
  68. $data = $result->getData();
  69. $data = $data[0];
  70. $this->assertEquals('updated', $data['value']);
  71. }
  72. public function testSetSameValue() {
  73. $_POST = array('value' => 123456789);
  74. $params = array('app' => $this->appKey, 'key' => 'k-10');
  75. $result = OC_OCS_Privatedata::set($params);
  76. $this->assertEquals(100, $result->getStatusCode());
  77. $result = OC_OCS_Privatedata::get($params);
  78. $this->assertOcsResult(1, $result);
  79. $data = $result->getData();
  80. $data = $data[0];
  81. $this->assertEquals('123456789', $data['value']);
  82. // set the same value again
  83. $_POST = array('value' => 123456789);
  84. $params = array('app' => $this->appKey, 'key' => 'k-10');
  85. $result = OC_OCS_Privatedata::set($params);
  86. $this->assertEquals(100, $result->getStatusCode());
  87. $result = OC_OCS_Privatedata::get($params);
  88. $this->assertOcsResult(1, $result);
  89. $data = $result->getData();
  90. $data = $data[0];
  91. $this->assertEquals('123456789', $data['value']);
  92. }
  93. public function testSetMany() {
  94. $_POST = array('value' => 123456789);
  95. // set key 'k-1'
  96. $params = array('app' => $this->appKey, 'key' => 'k-1');
  97. $result = OC_OCS_Privatedata::set($params);
  98. $this->assertEquals(100, $result->getStatusCode());
  99. // set key 'k-2'
  100. $params = array('app' => $this->appKey, 'key' => 'k-2');
  101. $result = OC_OCS_Privatedata::set($params);
  102. $this->assertEquals(100, $result->getStatusCode());
  103. // query for all
  104. $params = array('app' => $this->appKey);
  105. $result = OC_OCS_Privatedata::get($params);
  106. $this->assertOcsResult(2, $result);
  107. }
  108. public function testDelete() {
  109. $_POST = array('value' => 123456789);
  110. // set key 'k-1'
  111. $params = array('app' => $this->appKey, 'key' => 'k-3');
  112. $result = OC_OCS_Privatedata::set($params);
  113. $this->assertEquals(100, $result->getStatusCode());
  114. $result = OC_OCS_Privatedata::delete($params);
  115. $this->assertEquals(100, $result->getStatusCode());
  116. $result = OC_OCS_Privatedata::get($params);
  117. $this->assertOcsResult(0, $result);
  118. }
  119. /**
  120. * @dataProvider deleteWithEmptyKeysProvider
  121. */
  122. public function testDeleteWithEmptyKeys($params) {
  123. $result = OC_OCS_Privatedata::delete($params);
  124. $this->assertEquals(101, $result->getStatusCode());
  125. }
  126. public function deleteWithEmptyKeysProvider() {
  127. return array(
  128. array(array()),
  129. array(array('app' => '123')),
  130. array(array('key' => '123')),
  131. );
  132. }
  133. /**
  134. * @param \OC_OCS_Result $result
  135. * @param integer $expectedArraySize
  136. */
  137. public function assertOcsResult($expectedArraySize, $result) {
  138. $this->assertEquals(100, $result->getStatusCode());
  139. $data = $result->getData();
  140. $this->assertTrue(is_array($data));
  141. $this->assertEquals($expectedArraySize, sizeof($data));
  142. }
  143. }