TagsPluginTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Daniel Kesselberg <mail@danielkesselberg.de>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. * @author Thomas Müller <thomas.mueller@tmit.eu>
  11. * @author Vincent Petry <vincent@nextcloud.com>
  12. *
  13. * @license AGPL-3.0
  14. *
  15. * This code is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License, version 3,
  17. * as published by the Free Software Foundation.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License, version 3,
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>
  26. *
  27. */
  28. namespace OCA\DAV\Tests\unit\Connector\Sabre;
  29. use OCA\DAV\Connector\Sabre\Directory;
  30. use OCA\DAV\Connector\Sabre\File;
  31. use OCA\DAV\Connector\Sabre\Node;
  32. use OCA\DAV\Upload\UploadFile;
  33. use OCP\ITagManager;
  34. use OCP\ITags;
  35. use Sabre\DAV\Tree;
  36. /**
  37. * Copyright (c) 2014 Vincent Petry <pvince81@owncloud.com>
  38. * This file is licensed under the Affero General Public License version 3 or
  39. * later.
  40. * See the COPYING-README file.
  41. */
  42. class TagsPluginTest extends \Test\TestCase {
  43. public const TAGS_PROPERTYNAME = \OCA\DAV\Connector\Sabre\TagsPlugin::TAGS_PROPERTYNAME;
  44. public const FAVORITE_PROPERTYNAME = \OCA\DAV\Connector\Sabre\TagsPlugin::FAVORITE_PROPERTYNAME;
  45. public const TAG_FAVORITE = \OCA\DAV\Connector\Sabre\TagsPlugin::TAG_FAVORITE;
  46. /**
  47. * @var \Sabre\DAV\Server
  48. */
  49. private $server;
  50. /**
  51. * @var Tree
  52. */
  53. private $tree;
  54. /**
  55. * @var \OCP\ITagManager
  56. */
  57. private $tagManager;
  58. /**
  59. * @var \OCP\ITags
  60. */
  61. private $tagger;
  62. /**
  63. * @var \OCA\DAV\Connector\Sabre\TagsPlugin
  64. */
  65. private $plugin;
  66. protected function setUp(): void {
  67. parent::setUp();
  68. $this->server = new \Sabre\DAV\Server();
  69. $this->tree = $this->getMockBuilder(Tree::class)
  70. ->disableOriginalConstructor()
  71. ->getMock();
  72. $this->tagger = $this->getMockBuilder(ITags::class)
  73. ->disableOriginalConstructor()
  74. ->getMock();
  75. $this->tagManager = $this->getMockBuilder(ITagManager::class)
  76. ->disableOriginalConstructor()
  77. ->getMock();
  78. $this->tagManager->expects($this->any())
  79. ->method('load')
  80. ->with('files')
  81. ->willReturn($this->tagger);
  82. $this->plugin = new \OCA\DAV\Connector\Sabre\TagsPlugin($this->tree, $this->tagManager);
  83. $this->plugin->initialize($this->server);
  84. }
  85. /**
  86. * @dataProvider tagsGetPropertiesDataProvider
  87. */
  88. public function testGetProperties($tags, $requestedProperties, $expectedProperties) {
  89. $node = $this->getMockBuilder(Node::class)
  90. ->disableOriginalConstructor()
  91. ->getMock();
  92. $node->expects($this->any())
  93. ->method('getId')
  94. ->willReturn(123);
  95. $expectedCallCount = 0;
  96. if (count($requestedProperties) > 0) {
  97. $expectedCallCount = 1;
  98. }
  99. $this->tagger->expects($this->exactly($expectedCallCount))
  100. ->method('getTagsForObjects')
  101. ->with($this->equalTo([123]))
  102. ->willReturn([123 => $tags]);
  103. $propFind = new \Sabre\DAV\PropFind(
  104. '/dummyPath',
  105. $requestedProperties,
  106. 0
  107. );
  108. $this->plugin->handleGetProperties(
  109. $propFind,
  110. $node
  111. );
  112. $result = $propFind->getResultForMultiStatus();
  113. $this->assertEmpty($result[404]);
  114. unset($result[404]);
  115. $this->assertEquals($expectedProperties, $result);
  116. }
  117. /**
  118. * @dataProvider tagsGetPropertiesDataProvider
  119. */
  120. public function testPreloadThenGetProperties($tags, $requestedProperties, $expectedProperties) {
  121. $node1 = $this->getMockBuilder(File::class)
  122. ->disableOriginalConstructor()
  123. ->getMock();
  124. $node1->expects($this->any())
  125. ->method('getId')
  126. ->willReturn(111);
  127. $node2 = $this->getMockBuilder(File::class)
  128. ->disableOriginalConstructor()
  129. ->getMock();
  130. $node2->expects($this->any())
  131. ->method('getId')
  132. ->willReturn(222);
  133. $expectedCallCount = 0;
  134. if (count($requestedProperties) > 0) {
  135. // this guarantees that getTagsForObjects
  136. // is only called once and then the tags
  137. // are cached
  138. $expectedCallCount = 1;
  139. }
  140. $node = $this->getMockBuilder(Directory::class)
  141. ->disableOriginalConstructor()
  142. ->getMock();
  143. $node->expects($this->any())
  144. ->method('getId')
  145. ->willReturn(123);
  146. $node->expects($this->exactly($expectedCallCount))
  147. ->method('getChildren')
  148. ->willReturn([$node1, $node2]);
  149. $this->tagger->expects($this->exactly($expectedCallCount))
  150. ->method('getTagsForObjects')
  151. ->with($this->equalTo([123, 111, 222]))
  152. ->willReturn(
  153. [
  154. 111 => $tags,
  155. 123 => $tags
  156. ]
  157. );
  158. // simulate sabre recursive PROPFIND traversal
  159. $propFindRoot = new \Sabre\DAV\PropFind(
  160. '/subdir',
  161. $requestedProperties,
  162. 1
  163. );
  164. $propFind1 = new \Sabre\DAV\PropFind(
  165. '/subdir/test.txt',
  166. $requestedProperties,
  167. 0
  168. );
  169. $propFind2 = new \Sabre\DAV\PropFind(
  170. '/subdir/test2.txt',
  171. $requestedProperties,
  172. 0
  173. );
  174. $this->plugin->handleGetProperties(
  175. $propFindRoot,
  176. $node
  177. );
  178. $this->plugin->handleGetProperties(
  179. $propFind1,
  180. $node1
  181. );
  182. $this->plugin->handleGetProperties(
  183. $propFind2,
  184. $node2
  185. );
  186. $result = $propFind1->getResultForMultiStatus();
  187. $this->assertEmpty($result[404]);
  188. unset($result[404]);
  189. $this->assertEquals($expectedProperties, $result);
  190. }
  191. public function tagsGetPropertiesDataProvider() {
  192. return [
  193. // request both, receive both
  194. [
  195. ['tag1', 'tag2', self::TAG_FAVORITE],
  196. [self::TAGS_PROPERTYNAME, self::FAVORITE_PROPERTYNAME],
  197. [
  198. 200 => [
  199. self::TAGS_PROPERTYNAME => new \OCA\DAV\Connector\Sabre\TagList(['tag1', 'tag2']),
  200. self::FAVORITE_PROPERTYNAME => true,
  201. ]
  202. ]
  203. ],
  204. // request tags alone
  205. [
  206. ['tag1', 'tag2', self::TAG_FAVORITE],
  207. [self::TAGS_PROPERTYNAME],
  208. [
  209. 200 => [
  210. self::TAGS_PROPERTYNAME => new \OCA\DAV\Connector\Sabre\TagList(['tag1', 'tag2']),
  211. ]
  212. ]
  213. ],
  214. // request fav alone
  215. [
  216. ['tag1', 'tag2', self::TAG_FAVORITE],
  217. [self::FAVORITE_PROPERTYNAME],
  218. [
  219. 200 => [
  220. self::FAVORITE_PROPERTYNAME => true,
  221. ]
  222. ]
  223. ],
  224. // request none
  225. [
  226. ['tag1', 'tag2', self::TAG_FAVORITE],
  227. [],
  228. [
  229. 200 => []
  230. ],
  231. ],
  232. // request both with none set, receive both
  233. [
  234. [],
  235. [self::TAGS_PROPERTYNAME, self::FAVORITE_PROPERTYNAME],
  236. [
  237. 200 => [
  238. self::TAGS_PROPERTYNAME => new \OCA\DAV\Connector\Sabre\TagList([]),
  239. self::FAVORITE_PROPERTYNAME => false,
  240. ]
  241. ]
  242. ],
  243. ];
  244. }
  245. public function testGetPropertiesSkipChunks(): void {
  246. $sabreNode = $this->getMockBuilder(UploadFile::class)
  247. ->disableOriginalConstructor()
  248. ->getMock();
  249. $propFind = new \Sabre\DAV\PropFind(
  250. '/dummyPath',
  251. [self::TAGS_PROPERTYNAME, self::TAG_FAVORITE],
  252. 0
  253. );
  254. $this->plugin->handleGetProperties(
  255. $propFind,
  256. $sabreNode
  257. );
  258. $result = $propFind->getResultForMultiStatus();
  259. $this->assertCount(2, $result[404]);
  260. }
  261. public function testUpdateTags() {
  262. // this test will replace the existing tags "tagremove" with "tag1" and "tag2"
  263. // and keep "tagkeep"
  264. $node = $this->getMockBuilder(Node::class)
  265. ->disableOriginalConstructor()
  266. ->getMock();
  267. $node->expects($this->any())
  268. ->method('getId')
  269. ->willReturn(123);
  270. $this->tree->expects($this->any())
  271. ->method('getNodeForPath')
  272. ->with('/dummypath')
  273. ->willReturn($node);
  274. $this->tagger->expects($this->at(0))
  275. ->method('getTagsForObjects')
  276. ->with($this->equalTo([123]))
  277. ->willReturn([123 => ['tagkeep', 'tagremove', self::TAG_FAVORITE]]);
  278. // then tag as tag1 and tag2
  279. $this->tagger->expects($this->at(1))
  280. ->method('tagAs')
  281. ->with(123, 'tag1');
  282. $this->tagger->expects($this->at(2))
  283. ->method('tagAs')
  284. ->with(123, 'tag2');
  285. // it will untag tag3
  286. $this->tagger->expects($this->at(3))
  287. ->method('unTag')
  288. ->with(123, 'tagremove');
  289. // properties to set
  290. $propPatch = new \Sabre\DAV\PropPatch([
  291. self::TAGS_PROPERTYNAME => new \OCA\DAV\Connector\Sabre\TagList(['tag1', 'tag2', 'tagkeep'])
  292. ]);
  293. $this->plugin->handleUpdateProperties(
  294. '/dummypath',
  295. $propPatch
  296. );
  297. $propPatch->commit();
  298. // all requested properties removed, as they were processed already
  299. $this->assertEmpty($propPatch->getRemainingMutations());
  300. $result = $propPatch->getResult();
  301. $this->assertEquals(200, $result[self::TAGS_PROPERTYNAME]);
  302. $this->assertFalse(isset($result[self::FAVORITE_PROPERTYNAME]));
  303. }
  304. public function testUpdateTagsFromScratch() {
  305. $node = $this->getMockBuilder(Node::class)
  306. ->disableOriginalConstructor()
  307. ->getMock();
  308. $node->expects($this->any())
  309. ->method('getId')
  310. ->willReturn(123);
  311. $this->tree->expects($this->any())
  312. ->method('getNodeForPath')
  313. ->with('/dummypath')
  314. ->willReturn($node);
  315. $this->tagger->expects($this->at(0))
  316. ->method('getTagsForObjects')
  317. ->with($this->equalTo([123]))
  318. ->willReturn([]);
  319. // then tag as tag1 and tag2
  320. $this->tagger->expects($this->at(1))
  321. ->method('tagAs')
  322. ->with(123, 'tag1');
  323. $this->tagger->expects($this->at(2))
  324. ->method('tagAs')
  325. ->with(123, 'tag2');
  326. // properties to set
  327. $propPatch = new \Sabre\DAV\PropPatch([
  328. self::TAGS_PROPERTYNAME => new \OCA\DAV\Connector\Sabre\TagList(['tag1', 'tag2', 'tagkeep'])
  329. ]);
  330. $this->plugin->handleUpdateProperties(
  331. '/dummypath',
  332. $propPatch
  333. );
  334. $propPatch->commit();
  335. // all requested properties removed, as they were processed already
  336. $this->assertEmpty($propPatch->getRemainingMutations());
  337. $result = $propPatch->getResult();
  338. $this->assertEquals(200, $result[self::TAGS_PROPERTYNAME]);
  339. $this->assertFalse(false, isset($result[self::FAVORITE_PROPERTYNAME]));
  340. }
  341. public function testUpdateFav() {
  342. // this test will replace the existing tags "tagremove" with "tag1" and "tag2"
  343. // and keep "tagkeep"
  344. $node = $this->getMockBuilder(Node::class)
  345. ->disableOriginalConstructor()
  346. ->getMock();
  347. $node->expects($this->any())
  348. ->method('getId')
  349. ->willReturn(123);
  350. $this->tree->expects($this->any())
  351. ->method('getNodeForPath')
  352. ->with('/dummypath')
  353. ->willReturn($node);
  354. // set favorite tag
  355. $this->tagger->expects($this->once())
  356. ->method('tagAs')
  357. ->with(123, self::TAG_FAVORITE);
  358. // properties to set
  359. $propPatch = new \Sabre\DAV\PropPatch([
  360. self::FAVORITE_PROPERTYNAME => true
  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. // unfavorite now
  373. // set favorite tag
  374. $this->tagger->expects($this->once())
  375. ->method('unTag')
  376. ->with(123, self::TAG_FAVORITE);
  377. // properties to set
  378. $propPatch = new \Sabre\DAV\PropPatch([
  379. self::FAVORITE_PROPERTYNAME => false
  380. ]);
  381. $this->plugin->handleUpdateProperties(
  382. '/dummypath',
  383. $propPatch
  384. );
  385. $propPatch->commit();
  386. // all requested properties removed, as they were processed already
  387. $this->assertEmpty($propPatch->getRemainingMutations());
  388. $result = $propPatch->getResult();
  389. $this->assertFalse(false, isset($result[self::TAGS_PROPERTYNAME]));
  390. $this->assertEquals(200, isset($result[self::FAVORITE_PROPERTYNAME]));
  391. }
  392. }