privatedata.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. class Test_OC_OCS_Privatedata extends \Test\TestCase {
  23. private $appKey;
  24. protected function setUp() {
  25. parent::setUp();
  26. \OC::$server->getSession()->set('user_id', 'user1');
  27. $this->appKey = $this->getUniqueID('app');
  28. }
  29. public function testGetEmptyOne() {
  30. $params = array('app' => $this->appKey, 'key' => '123');
  31. $result = OC_OCS_Privatedata::get($params);
  32. $this->assertOcsResult(0, $result);
  33. }
  34. public function testGetEmptyAll() {
  35. $params = array('app' => $this->appKey);
  36. $result = OC_OCS_Privatedata::get($params);
  37. $this->assertOcsResult(0, $result);
  38. }
  39. public function testSetOne() {
  40. $_POST = array('value' => 123456789);
  41. $params = array('app' => $this->appKey, 'key' => 'k-1');
  42. $result = OC_OCS_Privatedata::set($params);
  43. $this->assertEquals(100, $result->getStatusCode());
  44. $result = OC_OCS_Privatedata::get($params);
  45. $this->assertOcsResult(1, $result);
  46. }
  47. public function testSetExisting() {
  48. $_POST = array('value' => 123456789);
  49. $params = array('app' => $this->appKey, 'key' => 'k-10');
  50. $result = OC_OCS_Privatedata::set($params);
  51. $this->assertEquals(100, $result->getStatusCode());
  52. $result = OC_OCS_Privatedata::get($params);
  53. $this->assertOcsResult(1, $result);
  54. $data = $result->getData();
  55. $data = $data[0];
  56. $this->assertEquals('123456789', $data['value']);
  57. $_POST = array('value' => 'updated');
  58. $params = array('app' => $this->appKey, 'key' => 'k-10');
  59. $result = OC_OCS_Privatedata::set($params);
  60. $this->assertEquals(100, $result->getStatusCode());
  61. $result = OC_OCS_Privatedata::get($params);
  62. $this->assertOcsResult(1, $result);
  63. $data = $result->getData();
  64. $data = $data[0];
  65. $this->assertEquals('updated', $data['value']);
  66. }
  67. public function testSetSameValue() {
  68. $_POST = array('value' => 123456789);
  69. $params = array('app' => $this->appKey, 'key' => 'k-10');
  70. $result = OC_OCS_Privatedata::set($params);
  71. $this->assertEquals(100, $result->getStatusCode());
  72. $result = OC_OCS_Privatedata::get($params);
  73. $this->assertOcsResult(1, $result);
  74. $data = $result->getData();
  75. $data = $data[0];
  76. $this->assertEquals('123456789', $data['value']);
  77. // set the same value again
  78. $_POST = array('value' => 123456789);
  79. $params = array('app' => $this->appKey, 'key' => 'k-10');
  80. $result = OC_OCS_Privatedata::set($params);
  81. $this->assertEquals(100, $result->getStatusCode());
  82. $result = OC_OCS_Privatedata::get($params);
  83. $this->assertOcsResult(1, $result);
  84. $data = $result->getData();
  85. $data = $data[0];
  86. $this->assertEquals('123456789', $data['value']);
  87. }
  88. public function testSetMany() {
  89. $_POST = array('value' => 123456789);
  90. // set key 'k-1'
  91. $params = array('app' => $this->appKey, 'key' => 'k-1');
  92. $result = OC_OCS_Privatedata::set($params);
  93. $this->assertEquals(100, $result->getStatusCode());
  94. // set key 'k-2'
  95. $params = array('app' => $this->appKey, 'key' => 'k-2');
  96. $result = OC_OCS_Privatedata::set($params);
  97. $this->assertEquals(100, $result->getStatusCode());
  98. // query for all
  99. $params = array('app' => $this->appKey);
  100. $result = OC_OCS_Privatedata::get($params);
  101. $this->assertOcsResult(2, $result);
  102. }
  103. public function testDelete() {
  104. $_POST = array('value' => 123456789);
  105. // set key 'k-1'
  106. $params = array('app' => $this->appKey, 'key' => 'k-3');
  107. $result = OC_OCS_Privatedata::set($params);
  108. $this->assertEquals(100, $result->getStatusCode());
  109. $result = OC_OCS_Privatedata::delete($params);
  110. $this->assertEquals(100, $result->getStatusCode());
  111. $result = OC_OCS_Privatedata::get($params);
  112. $this->assertOcsResult(0, $result);
  113. }
  114. /**
  115. * @dataProvider deleteWithEmptyKeysProvider
  116. */
  117. public function testDeleteWithEmptyKeys($params) {
  118. $result = OC_OCS_Privatedata::delete($params);
  119. $this->assertEquals(101, $result->getStatusCode());
  120. }
  121. public function deleteWithEmptyKeysProvider() {
  122. return array(
  123. array(array()),
  124. array(array('app' => '123')),
  125. array(array('key' => '123')),
  126. );
  127. }
  128. /**
  129. * @param \OC_OCS_Result $result
  130. * @param integer $expectedArraySize
  131. */
  132. public function assertOcsResult($expectedArraySize, $result) {
  133. $this->assertEquals(100, $result->getStatusCode());
  134. $data = $result->getData();
  135. $this->assertTrue(is_array($data));
  136. $this->assertEquals($expectedArraySize, sizeof($data));
  137. }
  138. }