CustomPropertiesBackendTest.php 7.4 KB

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