CustomPropertiesBackendTest.php 6.0 KB

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