CustomPropertiesBackendTest.php 7.3 KB

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