You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CustomPropertiesBackendTest.php 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Robin Appelman <robin@icewind.nl>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. * @author Thomas Müller <thomas.mueller@tmit.eu>
  11. * @author Vincent Petry <vincent@nextcloud.com>
  12. *
  13. * @license AGPL-3.0
  14. *
  15. * This code is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License, version 3,
  17. * as published by the Free Software Foundation.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License, version 3,
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>
  26. *
  27. */
  28. namespace OCA\DAV\Tests\unit\Connector\Sabre;
  29. /**
  30. * Copyright (c) 2015 Vincent Petry <pvince81@owncloud.com>
  31. * This file is licensed under the Affero General Public License version 3 or
  32. * later.
  33. * See the COPYING-README file.
  34. */
  35. use OCA\DAV\Connector\Sabre\Directory;
  36. use OCA\DAV\Connector\Sabre\File;
  37. use OCP\IUser;
  38. use Sabre\DAV\Tree;
  39. /**
  40. * Class CustomPropertiesBackend
  41. *
  42. * @group DB
  43. *
  44. * @package OCA\DAV\Tests\unit\Connector\Sabre
  45. */
  46. class CustomPropertiesBackendTest extends \Test\TestCase {
  47. /**
  48. * @var \Sabre\DAV\Server
  49. */
  50. private $server;
  51. /**
  52. * @var \Sabre\DAV\Tree
  53. */
  54. private $tree;
  55. /**
  56. * @var \OCA\DAV\DAV\CustomPropertiesBackend
  57. */
  58. private $plugin;
  59. /**
  60. * @var \OCP\IUser
  61. */
  62. private $user;
  63. protected function setUp(): void {
  64. parent::setUp();
  65. $this->server = new \Sabre\DAV\Server();
  66. $this->tree = $this->getMockBuilder(Tree::class)
  67. ->disableOriginalConstructor()
  68. ->getMock();
  69. $userId = $this->getUniqueID('testcustompropertiesuser');
  70. $this->user = $this->getMockBuilder(IUser::class)
  71. ->disableOriginalConstructor()
  72. ->getMock();
  73. $this->user->expects($this->any())
  74. ->method('getUID')
  75. ->willReturn($userId);
  76. $this->plugin = new \OCA\DAV\DAV\CustomPropertiesBackend(
  77. $this->tree,
  78. \OC::$server->getDatabaseConnection(),
  79. $this->user
  80. );
  81. }
  82. protected function tearDown(): void {
  83. $connection = \OC::$server->getDatabaseConnection();
  84. $deleteStatement = $connection->prepare(
  85. 'DELETE FROM `*PREFIX*properties`' .
  86. ' WHERE `userid` = ?'
  87. );
  88. $deleteStatement->execute(
  89. [
  90. $this->user->getUID(),
  91. ]
  92. );
  93. $deleteStatement->closeCursor();
  94. }
  95. private function createTestNode($class) {
  96. $node = $this->getMockBuilder($class)
  97. ->disableOriginalConstructor()
  98. ->getMock();
  99. $node->expects($this->any())
  100. ->method('getId')
  101. ->willReturn(123);
  102. $node->expects($this->any())
  103. ->method('getPath')
  104. ->willReturn('/dummypath');
  105. return $node;
  106. }
  107. private function applyDefaultProps($path = '/dummypath') {
  108. // properties to set
  109. $propPatch = new \Sabre\DAV\PropPatch([
  110. 'customprop' => 'value1',
  111. 'customprop2' => 'value2',
  112. ]);
  113. $this->plugin->propPatch(
  114. $path,
  115. $propPatch
  116. );
  117. $propPatch->commit();
  118. $this->assertEmpty($propPatch->getRemainingMutations());
  119. $result = $propPatch->getResult();
  120. $this->assertEquals(200, $result['customprop']);
  121. $this->assertEquals(200, $result['customprop2']);
  122. }
  123. /**
  124. * Test that propFind on a missing file soft fails
  125. */
  126. public function testPropFindMissingFileSoftFail() {
  127. $propFind = new \Sabre\DAV\PropFind(
  128. '/dummypath',
  129. [
  130. 'customprop',
  131. 'customprop2',
  132. 'unsetprop',
  133. ],
  134. 0
  135. );
  136. $this->plugin->propFind(
  137. '/dummypath',
  138. $propFind
  139. );
  140. $this->plugin->propFind(
  141. '/dummypath',
  142. $propFind
  143. );
  144. // assert that the above didn't throw exceptions
  145. $this->assertTrue(true);
  146. }
  147. /**
  148. * Test setting/getting properties
  149. */
  150. public function testSetGetPropertiesForFile() {
  151. $this->applyDefaultProps();
  152. $propFind = new \Sabre\DAV\PropFind(
  153. '/dummypath',
  154. [
  155. 'customprop',
  156. 'customprop2',
  157. 'unsetprop',
  158. ],
  159. 0
  160. );
  161. $this->plugin->propFind(
  162. '/dummypath',
  163. $propFind
  164. );
  165. $this->assertEquals('value1', $propFind->get('customprop'));
  166. $this->assertEquals('value2', $propFind->get('customprop2'));
  167. $this->assertEquals(['unsetprop'], $propFind->get404Properties());
  168. }
  169. /**
  170. * Test getting properties from directory
  171. */
  172. public function testGetPropertiesForDirectory() {
  173. $this->applyDefaultProps('/dummypath');
  174. $this->applyDefaultProps('/dummypath/test.txt');
  175. $propNames = [
  176. 'customprop',
  177. 'customprop2',
  178. 'unsetprop',
  179. ];
  180. $propFindRoot = new \Sabre\DAV\PropFind(
  181. '/dummypath',
  182. $propNames,
  183. 1
  184. );
  185. $propFindSub = new \Sabre\DAV\PropFind(
  186. '/dummypath/test.txt',
  187. $propNames,
  188. 0
  189. );
  190. $this->plugin->propFind(
  191. '/dummypath',
  192. $propFindRoot
  193. );
  194. $this->plugin->propFind(
  195. '/dummypath/test.txt',
  196. $propFindSub
  197. );
  198. // TODO: find a way to assert that no additional SQL queries were
  199. // run while doing the second propFind
  200. $this->assertEquals('value1', $propFindRoot->get('customprop'));
  201. $this->assertEquals('value2', $propFindRoot->get('customprop2'));
  202. $this->assertEquals(['unsetprop'], $propFindRoot->get404Properties());
  203. $this->assertEquals('value1', $propFindSub->get('customprop'));
  204. $this->assertEquals('value2', $propFindSub->get('customprop2'));
  205. $this->assertEquals(['unsetprop'], $propFindSub->get404Properties());
  206. }
  207. /**
  208. * Test delete property
  209. */
  210. public function testDeleteProperty() {
  211. $this->applyDefaultProps();
  212. $propPatch = new \Sabre\DAV\PropPatch([
  213. 'customprop' => null,
  214. ]);
  215. $this->plugin->propPatch(
  216. '/dummypath',
  217. $propPatch
  218. );
  219. $propPatch->commit();
  220. $this->assertEmpty($propPatch->getRemainingMutations());
  221. $result = $propPatch->getResult();
  222. $this->assertEquals(204, $result['customprop']);
  223. }
  224. }