CustomPropertiesBackendTest.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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->server,
  80. $this->tree,
  81. \OC::$server->getDatabaseConnection(),
  82. $this->user
  83. );
  84. }
  85. protected function tearDown(): void {
  86. $connection = \OC::$server->getDatabaseConnection();
  87. $deleteStatement = $connection->prepare(
  88. 'DELETE FROM `*PREFIX*properties`' .
  89. ' WHERE `userid` = ?'
  90. );
  91. $deleteStatement->execute(
  92. [
  93. $this->user->getUID(),
  94. ]
  95. );
  96. $deleteStatement->closeCursor();
  97. }
  98. private function createTestNode($class) {
  99. $node = $this->getMockBuilder($class)
  100. ->disableOriginalConstructor()
  101. ->getMock();
  102. $node->expects($this->any())
  103. ->method('getId')
  104. ->willReturn(123);
  105. $node->expects($this->any())
  106. ->method('getPath')
  107. ->willReturn('/dummypath');
  108. return $node;
  109. }
  110. private function applyDefaultProps($path = '/dummypath'): void {
  111. // properties to set
  112. $propPatch = new \Sabre\DAV\PropPatch([
  113. 'customprop' => 'value1',
  114. 'customprop2' => 'value2',
  115. ]);
  116. $this->plugin->propPatch(
  117. $path,
  118. $propPatch
  119. );
  120. $propPatch->commit();
  121. $this->assertEmpty($propPatch->getRemainingMutations());
  122. $result = $propPatch->getResult();
  123. $this->assertEquals(200, $result['customprop']);
  124. $this->assertEquals(200, $result['customprop2']);
  125. }
  126. /**
  127. * Test that propFind on a missing file soft fails
  128. */
  129. public function testPropFindMissingFileSoftFail(): void {
  130. $propFind = new \Sabre\DAV\PropFind(
  131. '/dummypath',
  132. [
  133. 'customprop',
  134. 'customprop2',
  135. 'unsetprop',
  136. ],
  137. 0
  138. );
  139. $this->plugin->propFind(
  140. '/dummypath',
  141. $propFind
  142. );
  143. $this->plugin->propFind(
  144. '/dummypath',
  145. $propFind
  146. );
  147. // assert that the above didn't throw exceptions
  148. $this->assertTrue(true);
  149. }
  150. /**
  151. * Test setting/getting properties
  152. */
  153. public function testSetGetPropertiesForFile(): void {
  154. $this->applyDefaultProps();
  155. $propFind = new \Sabre\DAV\PropFind(
  156. '/dummypath',
  157. [
  158. 'customprop',
  159. 'customprop2',
  160. 'unsetprop',
  161. ],
  162. 0
  163. );
  164. $this->plugin->propFind(
  165. '/dummypath',
  166. $propFind
  167. );
  168. $this->assertEquals('value1', $propFind->get('customprop'));
  169. $this->assertEquals('value2', $propFind->get('customprop2'));
  170. $this->assertEquals(['unsetprop'], $propFind->get404Properties());
  171. }
  172. /**
  173. * Test getting properties from directory
  174. */
  175. public function testGetPropertiesForDirectory(): void {
  176. $this->applyDefaultProps('/dummypath');
  177. $this->applyDefaultProps('/dummypath/test.txt');
  178. $propNames = [
  179. 'customprop',
  180. 'customprop2',
  181. 'unsetprop',
  182. ];
  183. $propFindRoot = new \Sabre\DAV\PropFind(
  184. '/dummypath',
  185. $propNames,
  186. 1
  187. );
  188. $propFindSub = new \Sabre\DAV\PropFind(
  189. '/dummypath/test.txt',
  190. $propNames,
  191. 0
  192. );
  193. $this->plugin->propFind(
  194. '/dummypath',
  195. $propFindRoot
  196. );
  197. $this->plugin->propFind(
  198. '/dummypath/test.txt',
  199. $propFindSub
  200. );
  201. // TODO: find a way to assert that no additional SQL queries were
  202. // run while doing the second propFind
  203. $this->assertEquals('value1', $propFindRoot->get('customprop'));
  204. $this->assertEquals('value2', $propFindRoot->get('customprop2'));
  205. $this->assertEquals(['unsetprop'], $propFindRoot->get404Properties());
  206. $this->assertEquals('value1', $propFindSub->get('customprop'));
  207. $this->assertEquals('value2', $propFindSub->get('customprop2'));
  208. $this->assertEquals(['unsetprop'], $propFindSub->get404Properties());
  209. }
  210. /**
  211. * Test delete property
  212. */
  213. public function testDeleteProperty(): void {
  214. $this->applyDefaultProps();
  215. $propPatch = new \Sabre\DAV\PropPatch([
  216. 'customprop' => null,
  217. ]);
  218. $this->plugin->propPatch(
  219. '/dummypath',
  220. $propPatch
  221. );
  222. $propPatch->commit();
  223. $this->assertEmpty($propPatch->getRemainingMutations());
  224. $result = $propPatch->getResult();
  225. $this->assertEquals(204, $result['customprop']);
  226. }
  227. }