TagsPluginTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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. use OCA\DAV\Connector\Sabre\Directory;
  28. use OCA\DAV\Connector\Sabre\File;
  29. use OCA\DAV\Connector\Sabre\Node;
  30. use OCP\ITagManager;
  31. use OCP\ITags;
  32. use Sabre\DAV\Tree;
  33. /**
  34. * Copyright (c) 2014 Vincent Petry <pvince81@owncloud.com>
  35. * This file is licensed under the Affero General Public License version 3 or
  36. * later.
  37. * See the COPYING-README file.
  38. */
  39. class TagsPluginTest extends \Test\TestCase {
  40. const TAGS_PROPERTYNAME = \OCA\DAV\Connector\Sabre\TagsPlugin::TAGS_PROPERTYNAME;
  41. const FAVORITE_PROPERTYNAME = \OCA\DAV\Connector\Sabre\TagsPlugin::FAVORITE_PROPERTYNAME;
  42. const TAG_FAVORITE = \OCA\DAV\Connector\Sabre\TagsPlugin::TAG_FAVORITE;
  43. /**
  44. * @var \Sabre\DAV\Server
  45. */
  46. private $server;
  47. /**
  48. * @var Tree
  49. */
  50. private $tree;
  51. /**
  52. * @var \OCP\ITagManager
  53. */
  54. private $tagManager;
  55. /**
  56. * @var \OCP\ITags
  57. */
  58. private $tagger;
  59. /**
  60. * @var \OCA\DAV\Connector\Sabre\TagsPlugin
  61. */
  62. private $plugin;
  63. protected function setUp(): void {
  64. parent::setUp();
  65. $this->server = new \Sabre\DAV\Server();
  66. $this->tree = $this->getMockBuilder(Tree::class)
  67. ->disableOriginalConstructor()
  68. ->getMock();
  69. $this->tagger = $this->getMockBuilder(ITags::class)
  70. ->disableOriginalConstructor()
  71. ->getMock();
  72. $this->tagManager = $this->getMockBuilder(ITagManager::class)
  73. ->disableOriginalConstructor()
  74. ->getMock();
  75. $this->tagManager->expects($this->any())
  76. ->method('load')
  77. ->with('files')
  78. ->will($this->returnValue($this->tagger));
  79. $this->plugin = new \OCA\DAV\Connector\Sabre\TagsPlugin($this->tree, $this->tagManager);
  80. $this->plugin->initialize($this->server);
  81. }
  82. /**
  83. * @dataProvider tagsGetPropertiesDataProvider
  84. */
  85. public function testGetProperties($tags, $requestedProperties, $expectedProperties) {
  86. $node = $this->getMockBuilder(Node::class)
  87. ->disableOriginalConstructor()
  88. ->getMock();
  89. $node->expects($this->any())
  90. ->method('getId')
  91. ->will($this->returnValue(123));
  92. $expectedCallCount = 0;
  93. if (count($requestedProperties) > 0) {
  94. $expectedCallCount = 1;
  95. }
  96. $this->tagger->expects($this->exactly($expectedCallCount))
  97. ->method('getTagsForObjects')
  98. ->with($this->equalTo(array(123)))
  99. ->will($this->returnValue(array(123 => $tags)));
  100. $propFind = new \Sabre\DAV\PropFind(
  101. '/dummyPath',
  102. $requestedProperties,
  103. 0
  104. );
  105. $this->plugin->handleGetProperties(
  106. $propFind,
  107. $node
  108. );
  109. $result = $propFind->getResultForMultiStatus();
  110. $this->assertEmpty($result[404]);
  111. unset($result[404]);
  112. $this->assertEquals($expectedProperties, $result);
  113. }
  114. /**
  115. * @dataProvider tagsGetPropertiesDataProvider
  116. */
  117. public function testPreloadThenGetProperties($tags, $requestedProperties, $expectedProperties) {
  118. $node1 = $this->getMockBuilder(File::class)
  119. ->disableOriginalConstructor()
  120. ->getMock();
  121. $node1->expects($this->any())
  122. ->method('getId')
  123. ->will($this->returnValue(111));
  124. $node2 = $this->getMockBuilder(File::class)
  125. ->disableOriginalConstructor()
  126. ->getMock();
  127. $node2->expects($this->any())
  128. ->method('getId')
  129. ->will($this->returnValue(222));
  130. $expectedCallCount = 0;
  131. if (count($requestedProperties) > 0) {
  132. // this guarantees that getTagsForObjects
  133. // is only called once and then the tags
  134. // are cached
  135. $expectedCallCount = 1;
  136. }
  137. $node = $this->getMockBuilder(Directory::class)
  138. ->disableOriginalConstructor()
  139. ->getMock();
  140. $node->expects($this->any())
  141. ->method('getId')
  142. ->will($this->returnValue(123));
  143. $node->expects($this->exactly($expectedCallCount))
  144. ->method('getChildren')
  145. ->will($this->returnValue(array($node1, $node2)));
  146. $this->tagger->expects($this->exactly($expectedCallCount))
  147. ->method('getTagsForObjects')
  148. ->with($this->equalTo(array(123, 111, 222)))
  149. ->will($this->returnValue(
  150. array(
  151. 111 => $tags,
  152. 123 => $tags
  153. )
  154. ));
  155. // simulate sabre recursive PROPFIND traversal
  156. $propFindRoot = new \Sabre\DAV\PropFind(
  157. '/subdir',
  158. $requestedProperties,
  159. 1
  160. );
  161. $propFind1 = new \Sabre\DAV\PropFind(
  162. '/subdir/test.txt',
  163. $requestedProperties,
  164. 0
  165. );
  166. $propFind2 = new \Sabre\DAV\PropFind(
  167. '/subdir/test2.txt',
  168. $requestedProperties,
  169. 0
  170. );
  171. $this->plugin->handleGetProperties(
  172. $propFindRoot,
  173. $node
  174. );
  175. $this->plugin->handleGetProperties(
  176. $propFind1,
  177. $node1
  178. );
  179. $this->plugin->handleGetProperties(
  180. $propFind2,
  181. $node2
  182. );
  183. $result = $propFind1->getResultForMultiStatus();
  184. $this->assertEmpty($result[404]);
  185. unset($result[404]);
  186. $this->assertEquals($expectedProperties, $result);
  187. }
  188. function tagsGetPropertiesDataProvider() {
  189. return array(
  190. // request both, receive both
  191. array(
  192. array('tag1', 'tag2', self::TAG_FAVORITE),
  193. array(self::TAGS_PROPERTYNAME, self::FAVORITE_PROPERTYNAME),
  194. array(
  195. 200 => array(
  196. self::TAGS_PROPERTYNAME => new \OCA\DAV\Connector\Sabre\TagList(array('tag1', 'tag2')),
  197. self::FAVORITE_PROPERTYNAME => true,
  198. )
  199. )
  200. ),
  201. // request tags alone
  202. array(
  203. array('tag1', 'tag2', self::TAG_FAVORITE),
  204. array(self::TAGS_PROPERTYNAME),
  205. array(
  206. 200 => array(
  207. self::TAGS_PROPERTYNAME => new \OCA\DAV\Connector\Sabre\TagList(array('tag1', 'tag2')),
  208. )
  209. )
  210. ),
  211. // request fav alone
  212. array(
  213. array('tag1', 'tag2', self::TAG_FAVORITE),
  214. array(self::FAVORITE_PROPERTYNAME),
  215. array(
  216. 200 => array(
  217. self::FAVORITE_PROPERTYNAME => true,
  218. )
  219. )
  220. ),
  221. // request none
  222. array(
  223. array('tag1', 'tag2', self::TAG_FAVORITE),
  224. array(),
  225. array(
  226. 200 => array()
  227. ),
  228. ),
  229. // request both with none set, receive both
  230. array(
  231. array(),
  232. array(self::TAGS_PROPERTYNAME, self::FAVORITE_PROPERTYNAME),
  233. array(
  234. 200 => array(
  235. self::TAGS_PROPERTYNAME => new \OCA\DAV\Connector\Sabre\TagList(array()),
  236. self::FAVORITE_PROPERTYNAME => false,
  237. )
  238. )
  239. ),
  240. );
  241. }
  242. public function testUpdateTags() {
  243. // this test will replace the existing tags "tagremove" with "tag1" and "tag2"
  244. // and keep "tagkeep"
  245. $node = $this->getMockBuilder(Node::class)
  246. ->disableOriginalConstructor()
  247. ->getMock();
  248. $node->expects($this->any())
  249. ->method('getId')
  250. ->will($this->returnValue(123));
  251. $this->tree->expects($this->any())
  252. ->method('getNodeForPath')
  253. ->with('/dummypath')
  254. ->will($this->returnValue($node));
  255. $this->tagger->expects($this->at(0))
  256. ->method('getTagsForObjects')
  257. ->with($this->equalTo(array(123)))
  258. ->will($this->returnValue(array(123 => array('tagkeep', 'tagremove', self::TAG_FAVORITE))));
  259. // then tag as tag1 and tag2
  260. $this->tagger->expects($this->at(1))
  261. ->method('tagAs')
  262. ->with(123, 'tag1');
  263. $this->tagger->expects($this->at(2))
  264. ->method('tagAs')
  265. ->with(123, 'tag2');
  266. // it will untag tag3
  267. $this->tagger->expects($this->at(3))
  268. ->method('unTag')
  269. ->with(123, 'tagremove');
  270. // properties to set
  271. $propPatch = new \Sabre\DAV\PropPatch(array(
  272. self::TAGS_PROPERTYNAME => new \OCA\DAV\Connector\Sabre\TagList(array('tag1', 'tag2', 'tagkeep'))
  273. ));
  274. $this->plugin->handleUpdateProperties(
  275. '/dummypath',
  276. $propPatch
  277. );
  278. $propPatch->commit();
  279. // all requested properties removed, as they were processed already
  280. $this->assertEmpty($propPatch->getRemainingMutations());
  281. $result = $propPatch->getResult();
  282. $this->assertEquals(200, $result[self::TAGS_PROPERTYNAME]);
  283. $this->assertFalse(isset($result[self::FAVORITE_PROPERTYNAME]));
  284. }
  285. public function testUpdateTagsFromScratch() {
  286. $node = $this->getMockBuilder(Node::class)
  287. ->disableOriginalConstructor()
  288. ->getMock();
  289. $node->expects($this->any())
  290. ->method('getId')
  291. ->will($this->returnValue(123));
  292. $this->tree->expects($this->any())
  293. ->method('getNodeForPath')
  294. ->with('/dummypath')
  295. ->will($this->returnValue($node));
  296. $this->tagger->expects($this->at(0))
  297. ->method('getTagsForObjects')
  298. ->with($this->equalTo(array(123)))
  299. ->will($this->returnValue(array()));
  300. // then tag as tag1 and tag2
  301. $this->tagger->expects($this->at(1))
  302. ->method('tagAs')
  303. ->with(123, 'tag1');
  304. $this->tagger->expects($this->at(2))
  305. ->method('tagAs')
  306. ->with(123, 'tag2');
  307. // properties to set
  308. $propPatch = new \Sabre\DAV\PropPatch(array(
  309. self::TAGS_PROPERTYNAME => new \OCA\DAV\Connector\Sabre\TagList(array('tag1', 'tag2', 'tagkeep'))
  310. ));
  311. $this->plugin->handleUpdateProperties(
  312. '/dummypath',
  313. $propPatch
  314. );
  315. $propPatch->commit();
  316. // all requested properties removed, as they were processed already
  317. $this->assertEmpty($propPatch->getRemainingMutations());
  318. $result = $propPatch->getResult();
  319. $this->assertEquals(200, $result[self::TAGS_PROPERTYNAME]);
  320. $this->assertFalse(false, isset($result[self::FAVORITE_PROPERTYNAME]));
  321. }
  322. public function testUpdateFav() {
  323. // this test will replace the existing tags "tagremove" with "tag1" and "tag2"
  324. // and keep "tagkeep"
  325. $node = $this->getMockBuilder(Node::class)
  326. ->disableOriginalConstructor()
  327. ->getMock();
  328. $node->expects($this->any())
  329. ->method('getId')
  330. ->will($this->returnValue(123));
  331. $this->tree->expects($this->any())
  332. ->method('getNodeForPath')
  333. ->with('/dummypath')
  334. ->will($this->returnValue($node));
  335. // set favorite tag
  336. $this->tagger->expects($this->once())
  337. ->method('tagAs')
  338. ->with(123, self::TAG_FAVORITE);
  339. // properties to set
  340. $propPatch = new \Sabre\DAV\PropPatch(array(
  341. self::FAVORITE_PROPERTYNAME => true
  342. ));
  343. $this->plugin->handleUpdateProperties(
  344. '/dummypath',
  345. $propPatch
  346. );
  347. $propPatch->commit();
  348. // all requested properties removed, as they were processed already
  349. $this->assertEmpty($propPatch->getRemainingMutations());
  350. $result = $propPatch->getResult();
  351. $this->assertFalse(false, isset($result[self::TAGS_PROPERTYNAME]));
  352. $this->assertEquals(200, isset($result[self::FAVORITE_PROPERTYNAME]));
  353. // unfavorite now
  354. // set favorite tag
  355. $this->tagger->expects($this->once())
  356. ->method('unTag')
  357. ->with(123, self::TAG_FAVORITE);
  358. // properties to set
  359. $propPatch = new \Sabre\DAV\PropPatch(array(
  360. self::FAVORITE_PROPERTYNAME => false
  361. ));
  362. $this->plugin->handleUpdateProperties(
  363. '/dummypath',
  364. $propPatch
  365. );
  366. $propPatch->commit();
  367. // all requested properties removed, as they were processed already
  368. $this->assertEmpty($propPatch->getRemainingMutations());
  369. $result = $propPatch->getResult();
  370. $this->assertFalse(false, isset($result[self::TAGS_PROPERTYNAME]));
  371. $this->assertEquals(200, isset($result[self::FAVORITE_PROPERTYNAME]));
  372. }
  373. }