privatedata.php 4.1 KB

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