CustomPropertiesBackendTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\DAV\Tests\unit\Connector\Sabre;
  8. use OCA\DAV\CalDAV\DefaultCalendarValidator;
  9. use OCA\DAV\Connector\Sabre\Directory;
  10. use OCA\DAV\Connector\Sabre\File;
  11. use OCA\DAV\DAV\CustomPropertiesBackend;
  12. use OCP\IUser;
  13. use PHPUnit\Framework\MockObject\MockObject;
  14. use Sabre\DAV\Tree;
  15. /**
  16. * Class CustomPropertiesBackend
  17. *
  18. * @group DB
  19. *
  20. * @package OCA\DAV\Tests\unit\Connector\Sabre
  21. */
  22. class CustomPropertiesBackendTest extends \Test\TestCase {
  23. /**
  24. * @var \Sabre\DAV\Server
  25. */
  26. private $server;
  27. /**
  28. * @var \Sabre\DAV\Tree
  29. */
  30. private $tree;
  31. /**
  32. * @var CustomPropertiesBackend
  33. */
  34. private $plugin;
  35. /**
  36. * @var IUser
  37. */
  38. private $user;
  39. /** @property MockObject|DefaultCalendarValidator */
  40. private $defaultCalendarValidator;
  41. protected function setUp(): void {
  42. parent::setUp();
  43. $this->server = new \Sabre\DAV\Server();
  44. $this->tree = $this->getMockBuilder(Tree::class)
  45. ->disableOriginalConstructor()
  46. ->getMock();
  47. $userId = $this->getUniqueID('testcustompropertiesuser');
  48. $this->user = $this->getMockBuilder(IUser::class)
  49. ->disableOriginalConstructor()
  50. ->getMock();
  51. $this->user->expects($this->any())
  52. ->method('getUID')
  53. ->willReturn($userId);
  54. $this->defaultCalendarValidator = $this->createMock(DefaultCalendarValidator::class);
  55. $this->plugin = new CustomPropertiesBackend(
  56. $this->server,
  57. $this->tree,
  58. \OC::$server->getDatabaseConnection(),
  59. $this->user,
  60. $this->defaultCalendarValidator,
  61. );
  62. }
  63. protected function tearDown(): void {
  64. $connection = \OC::$server->getDatabaseConnection();
  65. $deleteStatement = $connection->prepare(
  66. 'DELETE FROM `*PREFIX*properties`' .
  67. ' WHERE `userid` = ?'
  68. );
  69. $deleteStatement->execute(
  70. [
  71. $this->user->getUID(),
  72. ]
  73. );
  74. $deleteStatement->closeCursor();
  75. }
  76. private function createTestNode($class) {
  77. $node = $this->getMockBuilder($class)
  78. ->disableOriginalConstructor()
  79. ->getMock();
  80. $node->expects($this->any())
  81. ->method('getId')
  82. ->willReturn(123);
  83. $node->expects($this->any())
  84. ->method('getPath')
  85. ->willReturn('/dummypath');
  86. return $node;
  87. }
  88. private function applyDefaultProps($path = '/dummypath'): void {
  89. // properties to set
  90. $propPatch = new \Sabre\DAV\PropPatch([
  91. 'customprop' => 'value1',
  92. 'customprop2' => 'value2',
  93. ]);
  94. $this->plugin->propPatch(
  95. $path,
  96. $propPatch
  97. );
  98. $propPatch->commit();
  99. $this->assertEmpty($propPatch->getRemainingMutations());
  100. $result = $propPatch->getResult();
  101. $this->assertEquals(200, $result['customprop']);
  102. $this->assertEquals(200, $result['customprop2']);
  103. }
  104. /**
  105. * Test that propFind on a missing file soft fails
  106. */
  107. public function testPropFindMissingFileSoftFail(): void {
  108. $propFind = new \Sabre\DAV\PropFind(
  109. '/dummypath',
  110. [
  111. 'customprop',
  112. 'customprop2',
  113. 'unsetprop',
  114. ],
  115. 0
  116. );
  117. $this->plugin->propFind(
  118. '/dummypath',
  119. $propFind
  120. );
  121. $this->plugin->propFind(
  122. '/dummypath',
  123. $propFind
  124. );
  125. // assert that the above didn't throw exceptions
  126. $this->assertTrue(true);
  127. }
  128. /**
  129. * Test setting/getting properties
  130. */
  131. public function testSetGetPropertiesForFile(): void {
  132. $this->applyDefaultProps();
  133. $propFind = new \Sabre\DAV\PropFind(
  134. '/dummypath',
  135. [
  136. 'customprop',
  137. 'customprop2',
  138. 'unsetprop',
  139. ],
  140. 0
  141. );
  142. $this->plugin->propFind(
  143. '/dummypath',
  144. $propFind
  145. );
  146. $this->assertEquals('value1', $propFind->get('customprop'));
  147. $this->assertEquals('value2', $propFind->get('customprop2'));
  148. $this->assertEquals(['unsetprop'], $propFind->get404Properties());
  149. }
  150. /**
  151. * Test getting properties from directory
  152. */
  153. public function testGetPropertiesForDirectory(): void {
  154. $this->applyDefaultProps('/dummypath');
  155. $this->applyDefaultProps('/dummypath/test.txt');
  156. $propNames = [
  157. 'customprop',
  158. 'customprop2',
  159. 'unsetprop',
  160. ];
  161. $propFindRoot = new \Sabre\DAV\PropFind(
  162. '/dummypath',
  163. $propNames,
  164. 1
  165. );
  166. $propFindSub = new \Sabre\DAV\PropFind(
  167. '/dummypath/test.txt',
  168. $propNames,
  169. 0
  170. );
  171. $this->plugin->propFind(
  172. '/dummypath',
  173. $propFindRoot
  174. );
  175. $this->plugin->propFind(
  176. '/dummypath/test.txt',
  177. $propFindSub
  178. );
  179. // TODO: find a way to assert that no additional SQL queries were
  180. // run while doing the second propFind
  181. $this->assertEquals('value1', $propFindRoot->get('customprop'));
  182. $this->assertEquals('value2', $propFindRoot->get('customprop2'));
  183. $this->assertEquals(['unsetprop'], $propFindRoot->get404Properties());
  184. $this->assertEquals('value1', $propFindSub->get('customprop'));
  185. $this->assertEquals('value2', $propFindSub->get('customprop2'));
  186. $this->assertEquals(['unsetprop'], $propFindSub->get404Properties());
  187. }
  188. /**
  189. * Test delete property
  190. */
  191. public function testDeleteProperty(): void {
  192. $this->applyDefaultProps();
  193. $propPatch = new \Sabre\DAV\PropPatch([
  194. 'customprop' => null,
  195. ]);
  196. $this->plugin->propPatch(
  197. '/dummypath',
  198. $propPatch
  199. );
  200. $propPatch->commit();
  201. $this->assertEmpty($propPatch->getRemainingMutations());
  202. $result = $propPatch->getResult();
  203. $this->assertEquals(204, $result['customprop']);
  204. }
  205. }