custompropertiesbackend.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. <?php
  2. namespace Tests\Connector\Sabre;
  3. /**
  4. * Copyright (c) 2015 Vincent Petry <pvince81@owncloud.com>
  5. * This file is licensed under the Affero General Public License version 3 or
  6. * later.
  7. * See the COPYING-README file.
  8. */
  9. class CustomPropertiesBackend extends \Test\TestCase {
  10. /**
  11. * @var \Sabre\DAV\Server
  12. */
  13. private $server;
  14. /**
  15. * @var \Sabre\DAV\ObjectTree
  16. */
  17. private $tree;
  18. /**
  19. * @var \OC\Connector\Sabre\CustomPropertiesBackend
  20. */
  21. private $plugin;
  22. /**
  23. * @var \OCP\IUser
  24. */
  25. private $user;
  26. public function setUp() {
  27. parent::setUp();
  28. $this->server = new \Sabre\DAV\Server();
  29. $this->tree = $this->getMockBuilder('\Sabre\DAV\Tree')
  30. ->disableOriginalConstructor()
  31. ->getMock();
  32. $userId = $this->getUniqueID('testcustompropertiesuser');
  33. $this->user = $this->getMock('\OCP\IUser');
  34. $this->user->expects($this->any())
  35. ->method('getUID')
  36. ->will($this->returnValue($userId));
  37. $this->plugin = new \OC\Connector\Sabre\CustomPropertiesBackend(
  38. $this->tree,
  39. \OC::$server->getDatabaseConnection(),
  40. $this->user
  41. );
  42. }
  43. public function tearDown() {
  44. $connection = \OC::$server->getDatabaseConnection();
  45. $deleteStatement = $connection->prepare(
  46. 'DELETE FROM `*PREFIX*properties`' .
  47. ' WHERE `userid` = ?'
  48. );
  49. $deleteStatement->execute(
  50. array(
  51. $this->user->getUID(),
  52. )
  53. );
  54. $deleteStatement->closeCursor();
  55. }
  56. private function createTestNode($class) {
  57. $node = $this->getMockBuilder($class)
  58. ->disableOriginalConstructor()
  59. ->getMock();
  60. $node->expects($this->any())
  61. ->method('getId')
  62. ->will($this->returnValue(123));
  63. $node->expects($this->any())
  64. ->method('getPath')
  65. ->will($this->returnValue('/dummypath'));
  66. return $node;
  67. }
  68. private function applyDefaultProps($path = '/dummypath') {
  69. // properties to set
  70. $propPatch = new \Sabre\DAV\PropPatch(array(
  71. 'customprop' => 'value1',
  72. 'customprop2' => 'value2',
  73. ));
  74. $this->plugin->propPatch(
  75. $path,
  76. $propPatch
  77. );
  78. $propPatch->commit();
  79. $this->assertEmpty($propPatch->getRemainingMutations());
  80. $result = $propPatch->getResult();
  81. $this->assertEquals(200, $result['customprop']);
  82. $this->assertEquals(200, $result['customprop2']);
  83. }
  84. /**
  85. * Test that propFind on a missing file soft fails
  86. */
  87. public function testPropFindMissingFileSoftFail() {
  88. $this->tree->expects($this->at(0))
  89. ->method('getNodeForPath')
  90. ->with('/dummypath')
  91. ->will($this->throwException(new \Sabre\DAV\Exception\NotFound()));
  92. $this->tree->expects($this->at(1))
  93. ->method('getNodeForPath')
  94. ->with('/dummypath')
  95. ->will($this->throwException(new \Sabre\DAV\Exception\ServiceUnavailable()));
  96. $propFind = new \Sabre\DAV\PropFind(
  97. '/dummypath',
  98. array(
  99. 'customprop',
  100. 'customprop2',
  101. 'unsetprop',
  102. ),
  103. 0
  104. );
  105. $this->plugin->propFind(
  106. '/dummypath',
  107. $propFind
  108. );
  109. $this->plugin->propFind(
  110. '/dummypath',
  111. $propFind
  112. );
  113. // no exception, soft fail
  114. $this->assertTrue(true);
  115. }
  116. /**
  117. * Test setting/getting properties
  118. */
  119. public function testSetGetPropertiesForFile() {
  120. $node = $this->createTestNode('\OC\Connector\Sabre\File');
  121. $this->tree->expects($this->any())
  122. ->method('getNodeForPath')
  123. ->with('/dummypath')
  124. ->will($this->returnValue($node));
  125. $this->applyDefaultProps();
  126. $propFind = new \Sabre\DAV\PropFind(
  127. '/dummypath',
  128. array(
  129. 'customprop',
  130. 'customprop2',
  131. 'unsetprop',
  132. ),
  133. 0
  134. );
  135. $this->plugin->propFind(
  136. '/dummypath',
  137. $propFind
  138. );
  139. $this->assertEquals('value1', $propFind->get('customprop'));
  140. $this->assertEquals('value2', $propFind->get('customprop2'));
  141. $this->assertEquals(array('unsetprop'), $propFind->get404Properties());
  142. }
  143. /**
  144. * Test getting properties from directory
  145. */
  146. public function testGetPropertiesForDirectory() {
  147. $rootNode = $this->createTestNode('\OC\Connector\Sabre\Directory');
  148. $nodeSub = $this->getMockBuilder('\OC\Connector\Sabre\File')
  149. ->disableOriginalConstructor()
  150. ->getMock();
  151. $nodeSub->expects($this->any())
  152. ->method('getId')
  153. ->will($this->returnValue(456));
  154. $nodeSub->expects($this->any())
  155. ->method('getPath')
  156. ->will($this->returnValue('/dummypath/test.txt'));
  157. $rootNode->expects($this->once())
  158. ->method('getChildren')
  159. ->will($this->returnValue(array($nodeSub)));
  160. $this->tree->expects($this->at(0))
  161. ->method('getNodeForPath')
  162. ->with('/dummypath')
  163. ->will($this->returnValue($rootNode));
  164. $this->tree->expects($this->at(1))
  165. ->method('getNodeForPath')
  166. ->with('/dummypath/test.txt')
  167. ->will($this->returnValue($nodeSub));
  168. $this->tree->expects($this->at(2))
  169. ->method('getNodeForPath')
  170. ->with('/dummypath')
  171. ->will($this->returnValue($rootNode));
  172. $this->tree->expects($this->at(3))
  173. ->method('getNodeForPath')
  174. ->with('/dummypath/test.txt')
  175. ->will($this->returnValue($nodeSub));
  176. $this->applyDefaultProps('/dummypath');
  177. $this->applyDefaultProps('/dummypath/test.txt');
  178. $propNames = array(
  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(array('unsetprop'), $propFindRoot->get404Properties());
  206. $this->assertEquals('value1', $propFindSub->get('customprop'));
  207. $this->assertEquals('value2', $propFindSub->get('customprop2'));
  208. $this->assertEquals(array('unsetprop'), $propFindSub->get404Properties());
  209. }
  210. /**
  211. * Test delete property
  212. */
  213. public function testDeleteProperty() {
  214. $node = $this->createTestNode('\OC\Connector\Sabre\File');
  215. $this->tree->expects($this->any())
  216. ->method('getNodeForPath')
  217. ->with('/dummypath')
  218. ->will($this->returnValue($node));
  219. $this->applyDefaultProps();
  220. $propPatch = new \Sabre\DAV\PropPatch(array(
  221. 'customprop' => null,
  222. ));
  223. $this->plugin->propPatch(
  224. '/dummypath',
  225. $propPatch
  226. );
  227. $propPatch->commit();
  228. $this->assertEmpty($propPatch->getRemainingMutations());
  229. $result = $propPatch->getResult();
  230. $this->assertEquals(204, $result['customprop']);
  231. }
  232. }