1
0

PrivatedataTest.php 5.0 KB

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