1
0

FilesReportPluginTest.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\DAV\Tests\unit\Connector\Sabre;
  8. use OC\Files\View;
  9. use OCA\DAV\Connector\Sabre\Directory;
  10. use OCA\DAV\Connector\Sabre\FilesPlugin;
  11. use OCA\DAV\Connector\Sabre\FilesReportPlugin as FilesReportPluginImplementation;
  12. use OCP\App\IAppManager;
  13. use OCP\Files\File;
  14. use OCP\Files\FileInfo;
  15. use OCP\Files\Folder;
  16. use OCP\Files\IFilenameValidator;
  17. use OCP\IConfig;
  18. use OCP\IGroupManager;
  19. use OCP\IPreview;
  20. use OCP\IRequest;
  21. use OCP\ITagManager;
  22. use OCP\ITags;
  23. use OCP\IUser;
  24. use OCP\IUserSession;
  25. use OCP\SystemTag\ISystemTag;
  26. use OCP\SystemTag\ISystemTagManager;
  27. use OCP\SystemTag\ISystemTagObjectMapper;
  28. use OCP\SystemTag\TagNotFoundException;
  29. use PHPUnit\Framework\MockObject\MockObject;
  30. use Sabre\DAV\INode;
  31. use Sabre\DAV\Tree;
  32. use Sabre\HTTP\ResponseInterface;
  33. class FilesReportPluginTest extends \Test\TestCase {
  34. private \Sabre\DAV\Server&MockObject $server;
  35. private Tree&MockObject $tree;
  36. private ISystemTagObjectMapper&MockObject $tagMapper;
  37. private ISystemTagManager&MockObject $tagManager;
  38. private ITags&MockObject $privateTags;
  39. private ITagManager&MockObject $privateTagManager;
  40. private IUserSession&MockObject $userSession;
  41. private FilesReportPluginImplementation $plugin;
  42. private View&MockObject $view;
  43. private IGroupManager&MockObject $groupManager;
  44. private Folder&MockObject $userFolder;
  45. private IPreview&MockObject $previewManager;
  46. private IAppManager&MockObject $appManager;
  47. protected function setUp(): void {
  48. parent::setUp();
  49. $this->tree = $this->getMockBuilder(Tree::class)
  50. ->disableOriginalConstructor()
  51. ->getMock();
  52. $this->view = $this->getMockBuilder(View::class)
  53. ->disableOriginalConstructor()
  54. ->getMock();
  55. $this->server = $this->getMockBuilder('\Sabre\DAV\Server')
  56. ->setConstructorArgs([$this->tree])
  57. ->onlyMethods(['getRequestUri', 'getBaseUri'])
  58. ->getMock();
  59. $this->server->expects($this->any())
  60. ->method('getBaseUri')
  61. ->willReturn('http://example.com/owncloud/remote.php/dav');
  62. $this->groupManager = $this->getMockBuilder(IGroupManager::class)
  63. ->disableOriginalConstructor()
  64. ->getMock();
  65. $this->userFolder = $this->getMockBuilder(Folder::class)
  66. ->disableOriginalConstructor()
  67. ->getMock();
  68. $this->previewManager = $this->getMockBuilder(IPreview::class)
  69. ->disableOriginalConstructor()
  70. ->getMock();
  71. $this->appManager = $this->getMockBuilder(IAppManager::class)
  72. ->disableOriginalConstructor()
  73. ->getMock();
  74. $this->tagManager = $this->createMock(ISystemTagManager::class);
  75. $this->tagMapper = $this->createMock(ISystemTagObjectMapper::class);
  76. $this->userSession = $this->createMock(IUserSession::class);
  77. $this->privateTags = $this->createMock(ITags::class);
  78. $this->privateTagManager = $this->createMock(ITagManager::class);
  79. $this->privateTagManager->expects($this->any())
  80. ->method('load')
  81. ->with('files')
  82. ->willReturn($this->privateTags);
  83. $user = $this->getMockBuilder(IUser::class)
  84. ->disableOriginalConstructor()
  85. ->getMock();
  86. $user->expects($this->any())
  87. ->method('getUID')
  88. ->willReturn('testuser');
  89. $this->userSession->expects($this->any())
  90. ->method('getUser')
  91. ->willReturn($user);
  92. $this->plugin = new FilesReportPluginImplementation(
  93. $this->tree,
  94. $this->view,
  95. $this->tagManager,
  96. $this->tagMapper,
  97. $this->privateTagManager,
  98. $this->userSession,
  99. $this->groupManager,
  100. $this->userFolder,
  101. $this->appManager
  102. );
  103. }
  104. public function testOnReportInvalidNode(): void {
  105. $path = 'totally/unrelated/13';
  106. $this->tree->expects($this->any())
  107. ->method('getNodeForPath')
  108. ->with('/' . $path)
  109. ->willReturn(
  110. $this->getMockBuilder(INode::class)
  111. ->disableOriginalConstructor()
  112. ->getMock()
  113. );
  114. $this->server->expects($this->any())
  115. ->method('getRequestUri')
  116. ->willReturn($path);
  117. $this->plugin->initialize($this->server);
  118. $this->assertNull($this->plugin->onReport(FilesReportPluginImplementation::REPORT_NAME, [], '/' . $path));
  119. }
  120. public function testOnReportInvalidReportName(): void {
  121. $path = 'test';
  122. $this->tree->expects($this->any())
  123. ->method('getNodeForPath')
  124. ->with('/' . $path)
  125. ->willReturn(
  126. $this->getMockBuilder(INode::class)
  127. ->disableOriginalConstructor()
  128. ->getMock()
  129. );
  130. $this->server->expects($this->any())
  131. ->method('getRequestUri')
  132. ->willReturn($path);
  133. $this->plugin->initialize($this->server);
  134. $this->assertNull($this->plugin->onReport('{whoever}whatever', [], '/' . $path));
  135. }
  136. public function testOnReport(): void {
  137. $path = 'test';
  138. $parameters = [
  139. [
  140. 'name' => '{DAV:}prop',
  141. 'value' => [
  142. ['name' => '{DAV:}getcontentlength', 'value' => ''],
  143. ['name' => '{http://owncloud.org/ns}size', 'value' => ''],
  144. ],
  145. ],
  146. [
  147. 'name' => '{http://owncloud.org/ns}filter-rules',
  148. 'value' => [
  149. ['name' => '{http://owncloud.org/ns}systemtag', 'value' => '123'],
  150. ['name' => '{http://owncloud.org/ns}systemtag', 'value' => '456'],
  151. ],
  152. ],
  153. ];
  154. $this->groupManager->expects($this->any())
  155. ->method('isAdmin')
  156. ->willReturn(true);
  157. $reportTargetNode = $this->getMockBuilder(Directory::class)
  158. ->disableOriginalConstructor()
  159. ->getMock();
  160. $reportTargetNode->expects($this->any())
  161. ->method('getPath')
  162. ->willReturn('');
  163. $response = $this->getMockBuilder(ResponseInterface::class)
  164. ->disableOriginalConstructor()
  165. ->getMock();
  166. $response->expects($this->once())
  167. ->method('setHeader')
  168. ->with('Content-Type', 'application/xml; charset=utf-8');
  169. $response->expects($this->once())
  170. ->method('setStatus')
  171. ->with(207);
  172. $response->expects($this->once())
  173. ->method('setBody');
  174. $this->tree->expects($this->any())
  175. ->method('getNodeForPath')
  176. ->with('/' . $path)
  177. ->willReturn($reportTargetNode);
  178. $filesNode1 = $this->createMock(File::class);
  179. $filesNode1->expects($this->any())
  180. ->method('getSize')
  181. ->willReturn(12);
  182. $filesNode2 = $this->createMock(Folder::class);
  183. $filesNode2->expects($this->any())
  184. ->method('getSize')
  185. ->willReturn(10);
  186. $tag123 = $this->createMock(ISystemTag::class);
  187. $tag123->expects($this->any())
  188. ->method('getName')
  189. ->willReturn('OneTwoThree');
  190. $tag123->expects($this->any())
  191. ->method('isUserVisible')
  192. ->willReturn(true);
  193. $tag456 = $this->createMock(ISystemTag::class);
  194. $tag456->expects($this->any())
  195. ->method('getName')
  196. ->willReturn('FourFiveSix');
  197. $tag456->expects($this->any())
  198. ->method('isUserVisible')
  199. ->willReturn(true);
  200. $this->tagManager->expects($this->once())
  201. ->method('getTagsByIds')
  202. ->with(['123', '456'])
  203. ->willReturn([$tag123, $tag456]);
  204. $this->userFolder->expects($this->exactly(2))
  205. ->method('searchBySystemTag')
  206. ->withConsecutive(
  207. ['OneTwoThree'],
  208. ['FourFiveSix'],
  209. )
  210. ->willReturnOnConsecutiveCalls(
  211. [$filesNode1],
  212. [$filesNode2],
  213. );
  214. $this->server->expects($this->any())
  215. ->method('getRequestUri')
  216. ->willReturn($path);
  217. $this->server->httpResponse = $response;
  218. $this->plugin->initialize($this->server);
  219. $this->assertFalse($this->plugin->onReport(FilesReportPluginImplementation::REPORT_NAME, $parameters, '/' . $path));
  220. }
  221. public function testFindNodesByFileIdsRoot(): void {
  222. $filesNode1 = $this->getMockBuilder(Folder::class)
  223. ->disableOriginalConstructor()
  224. ->getMock();
  225. $filesNode1->expects($this->once())
  226. ->method('getName')
  227. ->willReturn('first node');
  228. $filesNode2 = $this->getMockBuilder(File::class)
  229. ->disableOriginalConstructor()
  230. ->getMock();
  231. $filesNode2->expects($this->once())
  232. ->method('getName')
  233. ->willReturn('second node');
  234. $reportTargetNode = $this->getMockBuilder(Directory::class)
  235. ->disableOriginalConstructor()
  236. ->getMock();
  237. $reportTargetNode->expects($this->any())
  238. ->method('getPath')
  239. ->willReturn('/');
  240. $this->userFolder->expects($this->exactly(2))
  241. ->method('getFirstNodeById')
  242. ->withConsecutive(
  243. ['111'],
  244. ['222'],
  245. )
  246. ->willReturnOnConsecutiveCalls(
  247. $filesNode1,
  248. $filesNode2,
  249. );
  250. /** @var Directory&MockObject $reportTargetNode */
  251. $result = $this->plugin->findNodesByFileIds($reportTargetNode, ['111', '222']);
  252. $this->assertCount(2, $result);
  253. $this->assertInstanceOf('\OCA\DAV\Connector\Sabre\Directory', $result[0]);
  254. $this->assertEquals('first node', $result[0]->getName());
  255. $this->assertInstanceOf('\OCA\DAV\Connector\Sabre\File', $result[1]);
  256. $this->assertEquals('second node', $result[1]->getName());
  257. }
  258. public function testFindNodesByFileIdsSubDir(): void {
  259. $filesNode1 = $this->getMockBuilder(Folder::class)
  260. ->disableOriginalConstructor()
  261. ->getMock();
  262. $filesNode1->expects($this->once())
  263. ->method('getName')
  264. ->willReturn('first node');
  265. $filesNode2 = $this->getMockBuilder(File::class)
  266. ->disableOriginalConstructor()
  267. ->getMock();
  268. $filesNode2->expects($this->once())
  269. ->method('getName')
  270. ->willReturn('second node');
  271. $reportTargetNode = $this->getMockBuilder(Directory::class)
  272. ->disableOriginalConstructor()
  273. ->getMock();
  274. $reportTargetNode->expects($this->any())
  275. ->method('getPath')
  276. ->willReturn('/sub1/sub2');
  277. $subNode = $this->getMockBuilder(Folder::class)
  278. ->disableOriginalConstructor()
  279. ->getMock();
  280. $this->userFolder->expects($this->once())
  281. ->method('get')
  282. ->with('/sub1/sub2')
  283. ->willReturn($subNode);
  284. $subNode->expects($this->exactly(2))
  285. ->method('getFirstNodeById')
  286. ->withConsecutive(
  287. ['111'],
  288. ['222'],
  289. )
  290. ->willReturnOnConsecutiveCalls(
  291. $filesNode1,
  292. $filesNode2,
  293. );
  294. /** @var Directory&MockObject $reportTargetNode */
  295. $result = $this->plugin->findNodesByFileIds($reportTargetNode, ['111', '222']);
  296. $this->assertCount(2, $result);
  297. $this->assertInstanceOf('\OCA\DAV\Connector\Sabre\Directory', $result[0]);
  298. $this->assertEquals('first node', $result[0]->getName());
  299. $this->assertInstanceOf('\OCA\DAV\Connector\Sabre\File', $result[1]);
  300. $this->assertEquals('second node', $result[1]->getName());
  301. }
  302. public function testPrepareResponses(): void {
  303. $requestedProps = ['{DAV:}getcontentlength', '{http://owncloud.org/ns}fileid', '{DAV:}resourcetype'];
  304. $fileInfo = $this->createMock(FileInfo::class);
  305. $fileInfo->method('isReadable')->willReturn(true);
  306. $node1 = $this->getMockBuilder(Directory::class)
  307. ->disableOriginalConstructor()
  308. ->getMock();
  309. $node2 = $this->getMockBuilder(\OCA\DAV\Connector\Sabre\File::class)
  310. ->disableOriginalConstructor()
  311. ->getMock();
  312. $node1->expects($this->once())
  313. ->method('getInternalFileId')
  314. ->willReturn('111');
  315. $node1->expects($this->any())
  316. ->method('getPath')
  317. ->willReturn('/node1');
  318. $node1->method('getFileInfo')->willReturn($fileInfo);
  319. $node2->expects($this->once())
  320. ->method('getInternalFileId')
  321. ->willReturn('222');
  322. $node2->expects($this->once())
  323. ->method('getSize')
  324. ->willReturn(1024);
  325. $node2->expects($this->any())
  326. ->method('getPath')
  327. ->willReturn('/sub/node2');
  328. $node2->method('getFileInfo')->willReturn($fileInfo);
  329. $config = $this->getMockBuilder(IConfig::class)
  330. ->disableOriginalConstructor()
  331. ->getMock();
  332. $validator = $this->createMock(IFilenameValidator::class);
  333. $this->server->addPlugin(
  334. new FilesPlugin(
  335. $this->tree,
  336. $config,
  337. $this->createMock(IRequest::class),
  338. $this->previewManager,
  339. $this->createMock(IUserSession::class),
  340. $validator,
  341. )
  342. );
  343. $this->plugin->initialize($this->server);
  344. $responses = $this->plugin->prepareResponses('/files/username', $requestedProps, [$node1, $node2]);
  345. $this->assertCount(2, $responses);
  346. $this->assertEquals('http://example.com/owncloud/remote.php/dav/files/username/node1', $responses[0]->getHref());
  347. $this->assertEquals('http://example.com/owncloud/remote.php/dav/files/username/sub/node2', $responses[1]->getHref());
  348. $props1 = $responses[0]->getResponseProperties();
  349. $this->assertEquals('111', $props1[200]['{http://owncloud.org/ns}fileid']);
  350. $this->assertNull($props1[404]['{DAV:}getcontentlength']);
  351. $this->assertInstanceOf('\Sabre\DAV\Xml\Property\ResourceType', $props1[200]['{DAV:}resourcetype']);
  352. $resourceType1 = $props1[200]['{DAV:}resourcetype']->getValue();
  353. $this->assertEquals('{DAV:}collection', $resourceType1[0]);
  354. $props2 = $responses[1]->getResponseProperties();
  355. $this->assertEquals('1024', $props2[200]['{DAV:}getcontentlength']);
  356. $this->assertEquals('222', $props2[200]['{http://owncloud.org/ns}fileid']);
  357. $this->assertInstanceOf('\Sabre\DAV\Xml\Property\ResourceType', $props2[200]['{DAV:}resourcetype']);
  358. $this->assertCount(0, $props2[200]['{DAV:}resourcetype']->getValue());
  359. }
  360. public function testProcessFilterRulesSingle(): void {
  361. $this->groupManager->expects($this->any())
  362. ->method('isAdmin')
  363. ->willReturn(true);
  364. $rules = [
  365. ['name' => '{http://owncloud.org/ns}systemtag', 'value' => '123'],
  366. ];
  367. $filesNode1 = $this->createMock(File::class);
  368. $filesNode1->expects($this->any())
  369. ->method('getSize')
  370. ->willReturn(12);
  371. $filesNode2 = $this->createMock(Folder::class);
  372. $filesNode2->expects($this->any())
  373. ->method('getSize')
  374. ->willReturn(10);
  375. $tag123 = $this->createMock(ISystemTag::class);
  376. $tag123->expects($this->any())
  377. ->method('getName')
  378. ->willReturn('OneTwoThree');
  379. $tag123->expects($this->any())
  380. ->method('isUserVisible')
  381. ->willReturn(true);
  382. $this->tagManager->expects($this->once())
  383. ->method('getTagsByIds')
  384. ->with(['123'])
  385. ->willReturn([$tag123]);
  386. $this->userFolder->expects($this->once())
  387. ->method('searchBySystemTag')
  388. ->with('OneTwoThree')
  389. ->willReturn([$filesNode1, $filesNode2]);
  390. $this->assertEquals([$filesNode1, $filesNode2], $this->invokePrivate($this->plugin, 'processFilterRulesForFileNodes', [$rules, 0, 0]));
  391. }
  392. public function testProcessFilterRulesAndCondition(): void {
  393. $this->groupManager->expects($this->any())
  394. ->method('isAdmin')
  395. ->willReturn(true);
  396. $filesNode1 = $this->createMock(File::class);
  397. $filesNode1->expects($this->any())
  398. ->method('getSize')
  399. ->willReturn(12);
  400. $filesNode1->expects($this->any())
  401. ->method('getId')
  402. ->willReturn(111);
  403. $filesNode2 = $this->createMock(Folder::class);
  404. $filesNode2->expects($this->any())
  405. ->method('getSize')
  406. ->willReturn(10);
  407. $filesNode2->expects($this->any())
  408. ->method('getId')
  409. ->willReturn(222);
  410. $filesNode3 = $this->createMock(File::class);
  411. $filesNode3->expects($this->any())
  412. ->method('getSize')
  413. ->willReturn(14);
  414. $filesNode3->expects($this->any())
  415. ->method('getId')
  416. ->willReturn(333);
  417. $tag123 = $this->createMock(ISystemTag::class);
  418. $tag123->expects($this->any())
  419. ->method('getName')
  420. ->willReturn('OneTwoThree');
  421. $tag123->expects($this->any())
  422. ->method('isUserVisible')
  423. ->willReturn(true);
  424. $tag456 = $this->createMock(ISystemTag::class);
  425. $tag456->expects($this->any())
  426. ->method('getName')
  427. ->willReturn('FourFiveSix');
  428. $tag456->expects($this->any())
  429. ->method('isUserVisible')
  430. ->willReturn(true);
  431. $this->tagManager->expects($this->once())
  432. ->method('getTagsByIds')
  433. ->with(['123', '456'])
  434. ->willReturn([$tag123, $tag456]);
  435. $this->userFolder->expects($this->exactly(2))
  436. ->method('searchBySystemTag')
  437. ->withConsecutive(
  438. ['OneTwoThree'],
  439. ['FourFiveSix'],
  440. )
  441. ->willReturnOnConsecutiveCalls(
  442. [$filesNode1, $filesNode2],
  443. [$filesNode2, $filesNode3],
  444. );
  445. $rules = [
  446. ['name' => '{http://owncloud.org/ns}systemtag', 'value' => '123'],
  447. ['name' => '{http://owncloud.org/ns}systemtag', 'value' => '456'],
  448. ];
  449. $this->assertEquals([$filesNode2], array_values($this->invokePrivate($this->plugin, 'processFilterRulesForFileNodes', [$rules, null, null])));
  450. }
  451. public function testProcessFilterRulesAndConditionWithOneEmptyResult(): void {
  452. $this->groupManager->expects($this->any())
  453. ->method('isAdmin')
  454. ->willReturn(true);
  455. $filesNode1 = $this->createMock(File::class);
  456. $filesNode1->expects($this->any())
  457. ->method('getSize')
  458. ->willReturn(12);
  459. $filesNode1->expects($this->any())
  460. ->method('getId')
  461. ->willReturn(111);
  462. $filesNode2 = $this->createMock(Folder::class);
  463. $filesNode2->expects($this->any())
  464. ->method('getSize')
  465. ->willReturn(10);
  466. $filesNode2->expects($this->any())
  467. ->method('getId')
  468. ->willReturn(222);
  469. $tag123 = $this->createMock(ISystemTag::class);
  470. $tag123->expects($this->any())
  471. ->method('getName')
  472. ->willReturn('OneTwoThree');
  473. $tag123->expects($this->any())
  474. ->method('isUserVisible')
  475. ->willReturn(true);
  476. $tag456 = $this->createMock(ISystemTag::class);
  477. $tag456->expects($this->any())
  478. ->method('getName')
  479. ->willReturn('FourFiveSix');
  480. $tag456->expects($this->any())
  481. ->method('isUserVisible')
  482. ->willReturn(true);
  483. $this->tagManager->expects($this->once())
  484. ->method('getTagsByIds')
  485. ->with(['123', '456'])
  486. ->willReturn([$tag123, $tag456]);
  487. $this->userFolder->expects($this->exactly(2))
  488. ->method('searchBySystemTag')
  489. ->withConsecutive(
  490. ['OneTwoThree'],
  491. ['FourFiveSix'],
  492. )
  493. ->willReturnOnConsecutiveCalls(
  494. [$filesNode1, $filesNode2],
  495. [],
  496. );
  497. $rules = [
  498. ['name' => '{http://owncloud.org/ns}systemtag', 'value' => '123'],
  499. ['name' => '{http://owncloud.org/ns}systemtag', 'value' => '456'],
  500. ];
  501. $this->assertEquals([], $this->invokePrivate($this->plugin, 'processFilterRulesForFileNodes', [$rules, null, null]));
  502. }
  503. public function testProcessFilterRulesAndConditionWithFirstEmptyResult(): void {
  504. $this->groupManager->expects($this->any())
  505. ->method('isAdmin')
  506. ->willReturn(true);
  507. $filesNode1 = $this->createMock(File::class);
  508. $filesNode1->expects($this->any())
  509. ->method('getSize')
  510. ->willReturn(12);
  511. $filesNode1->expects($this->any())
  512. ->method('getId')
  513. ->willReturn(111);
  514. $filesNode2 = $this->createMock(Folder::class);
  515. $filesNode2->expects($this->any())
  516. ->method('getSize')
  517. ->willReturn(10);
  518. $filesNode2->expects($this->any())
  519. ->method('getId')
  520. ->willReturn(222);
  521. $tag123 = $this->createMock(ISystemTag::class);
  522. $tag123->expects($this->any())
  523. ->method('getName')
  524. ->willReturn('OneTwoThree');
  525. $tag123->expects($this->any())
  526. ->method('isUserVisible')
  527. ->willReturn(true);
  528. $tag456 = $this->createMock(ISystemTag::class);
  529. $tag456->expects($this->any())
  530. ->method('getName')
  531. ->willReturn('FourFiveSix');
  532. $tag456->expects($this->any())
  533. ->method('isUserVisible')
  534. ->willReturn(true);
  535. $this->tagManager->expects($this->once())
  536. ->method('getTagsByIds')
  537. ->with(['123', '456'])
  538. ->willReturn([$tag123, $tag456]);
  539. $this->userFolder->expects($this->once())
  540. ->method('searchBySystemTag')
  541. ->with('OneTwoThree')
  542. ->willReturnOnConsecutiveCalls(
  543. [],
  544. [$filesNode1, $filesNode2],
  545. );
  546. $rules = [
  547. ['name' => '{http://owncloud.org/ns}systemtag', 'value' => '123'],
  548. ['name' => '{http://owncloud.org/ns}systemtag', 'value' => '456'],
  549. ];
  550. $this->assertEquals([], $this->invokePrivate($this->plugin, 'processFilterRulesForFileNodes', [$rules, null, null]));
  551. }
  552. public function testProcessFilterRulesAndConditionWithEmptyMidResult(): void {
  553. $this->groupManager->expects($this->any())
  554. ->method('isAdmin')
  555. ->willReturn(true);
  556. $filesNode1 = $this->createMock(File::class);
  557. $filesNode1->expects($this->any())
  558. ->method('getSize')
  559. ->willReturn(12);
  560. $filesNode1->expects($this->any())
  561. ->method('getId')
  562. ->willReturn(111);
  563. $filesNode2 = $this->createMock(Folder::class);
  564. $filesNode2->expects($this->any())
  565. ->method('getSize')
  566. ->willReturn(10);
  567. $filesNode2->expects($this->any())
  568. ->method('getId')
  569. ->willReturn(222);
  570. $filesNode3 = $this->createMock(Folder::class);
  571. $filesNode3->expects($this->any())
  572. ->method('getSize')
  573. ->willReturn(13);
  574. $filesNode3->expects($this->any())
  575. ->method('getId')
  576. ->willReturn(333);
  577. $tag123 = $this->createMock(ISystemTag::class);
  578. $tag123->expects($this->any())
  579. ->method('getName')
  580. ->willReturn('OneTwoThree');
  581. $tag123->expects($this->any())
  582. ->method('isUserVisible')
  583. ->willReturn(true);
  584. $tag456 = $this->createMock(ISystemTag::class);
  585. $tag456->expects($this->any())
  586. ->method('getName')
  587. ->willReturn('FourFiveSix');
  588. $tag456->expects($this->any())
  589. ->method('isUserVisible')
  590. ->willReturn(true);
  591. $tag789 = $this->createMock(ISystemTag::class);
  592. $tag789->expects($this->any())
  593. ->method('getName')
  594. ->willReturn('SevenEightNein');
  595. $tag789->expects($this->any())
  596. ->method('isUserVisible')
  597. ->willReturn(true);
  598. $this->tagManager->expects($this->once())
  599. ->method('getTagsByIds')
  600. ->with(['123', '456', '789'])
  601. ->willReturn([$tag123, $tag456, $tag789]);
  602. $this->userFolder->expects($this->exactly(2))
  603. ->method('searchBySystemTag')
  604. ->withConsecutive(['OneTwoThree'], ['FourFiveSix'], ['SevenEightNein'])
  605. ->willReturnOnConsecutiveCalls(
  606. [$filesNode1, $filesNode2],
  607. [$filesNode3],
  608. [$filesNode1, $filesNode2],
  609. );
  610. $rules = [
  611. ['name' => '{http://owncloud.org/ns}systemtag', 'value' => '123'],
  612. ['name' => '{http://owncloud.org/ns}systemtag', 'value' => '456'],
  613. ['name' => '{http://owncloud.org/ns}systemtag', 'value' => '789'],
  614. ];
  615. $this->assertEquals([], array_values($this->invokePrivate($this->plugin, 'processFilterRulesForFileNodes', [$rules, null, null])));
  616. }
  617. public function testProcessFilterRulesInvisibleTagAsAdmin(): void {
  618. $this->groupManager->expects($this->any())
  619. ->method('isAdmin')
  620. ->willReturn(true);
  621. $filesNode1 = $this->createMock(File::class);
  622. $filesNode1->expects($this->any())
  623. ->method('getSize')
  624. ->willReturn(12);
  625. $filesNode1->expects($this->any())
  626. ->method('getId')
  627. ->willReturn(111);
  628. $filesNode2 = $this->createMock(Folder::class);
  629. $filesNode2->expects($this->any())
  630. ->method('getSize')
  631. ->willReturn(10);
  632. $filesNode2->expects($this->any())
  633. ->method('getId')
  634. ->willReturn(222);
  635. $filesNode3 = $this->createMock(Folder::class);
  636. $filesNode3->expects($this->any())
  637. ->method('getSize')
  638. ->willReturn(13);
  639. $filesNode3->expects($this->any())
  640. ->method('getId')
  641. ->willReturn(333);
  642. $tag123 = $this->createMock(ISystemTag::class);
  643. $tag123->expects($this->any())
  644. ->method('getName')
  645. ->willReturn('OneTwoThree');
  646. $tag123->expects($this->any())
  647. ->method('isUserVisible')
  648. ->willReturn(true);
  649. $tag456 = $this->createMock(ISystemTag::class);
  650. $tag456->expects($this->any())
  651. ->method('getName')
  652. ->willReturn('FourFiveSix');
  653. $tag456->expects($this->any())
  654. ->method('isUserVisible')
  655. ->willReturn(false);
  656. $this->tagManager->expects($this->once())
  657. ->method('getTagsByIds')
  658. ->with(['123', '456'])
  659. ->willReturn([$tag123, $tag456]);
  660. $this->userFolder->expects($this->exactly(2))
  661. ->method('searchBySystemTag')
  662. ->withConsecutive(['OneTwoThree'], ['FourFiveSix'])
  663. ->willReturnOnConsecutiveCalls(
  664. [$filesNode1, $filesNode2],
  665. [$filesNode2, $filesNode3],
  666. );
  667. $rules = [
  668. ['name' => '{http://owncloud.org/ns}systemtag', 'value' => '123'],
  669. ['name' => '{http://owncloud.org/ns}systemtag', 'value' => '456'],
  670. ];
  671. $this->assertEquals([$filesNode2], array_values($this->invokePrivate($this->plugin, 'processFilterRulesForFileNodes', [$rules, null, null])));
  672. }
  673. public function testProcessFilterRulesInvisibleTagAsUser(): void {
  674. $this->expectException(TagNotFoundException::class);
  675. $this->groupManager->expects($this->any())
  676. ->method('isAdmin')
  677. ->willReturn(false);
  678. $tag123 = $this->createMock(ISystemTag::class);
  679. $tag123->expects($this->any())
  680. ->method('getName')
  681. ->willReturn('OneTwoThree');
  682. $tag123->expects($this->any())
  683. ->method('isUserVisible')
  684. ->willReturn(true);
  685. $tag456 = $this->createMock(ISystemTag::class);
  686. $tag456->expects($this->any())
  687. ->method('getName')
  688. ->willReturn('FourFiveSix');
  689. $tag456->expects($this->any())
  690. ->method('isUserVisible')
  691. ->willReturn(false);
  692. $this->tagManager->expects($this->once())
  693. ->method('getTagsByIds')
  694. ->with(['123', '456'])
  695. ->willThrowException(new TagNotFoundException());
  696. $this->userFolder->expects($this->never())
  697. ->method('searchBySystemTag');
  698. $rules = [
  699. ['name' => '{http://owncloud.org/ns}systemtag', 'value' => '123'],
  700. ['name' => '{http://owncloud.org/ns}systemtag', 'value' => '456'],
  701. ];
  702. $this->invokePrivate($this->plugin, 'processFilterRulesForFileNodes', [$rules, null, null]);
  703. }
  704. public function testProcessFilterRulesVisibleTagAsUser(): void {
  705. $this->groupManager->expects($this->any())
  706. ->method('isAdmin')
  707. ->willReturn(false);
  708. $tag1 = $this->createMock(ISystemTag::class);
  709. $tag1->expects($this->any())
  710. ->method('getId')
  711. ->willReturn('123');
  712. $tag1->expects($this->any())
  713. ->method('isUserVisible')
  714. ->willReturn(true);
  715. $tag1->expects($this->any())
  716. ->method('getName')
  717. ->willReturn('OneTwoThree');
  718. $tag2 = $this->createMock(ISystemTag::class);
  719. $tag2->expects($this->any())
  720. ->method('getId')
  721. ->willReturn('123');
  722. $tag2->expects($this->any())
  723. ->method('isUserVisible')
  724. ->willReturn(true);
  725. $tag2->expects($this->any())
  726. ->method('getName')
  727. ->willReturn('FourFiveSix');
  728. $this->tagManager->expects($this->once())
  729. ->method('getTagsByIds')
  730. ->with(['123', '456'])
  731. ->willReturn([$tag1, $tag2]);
  732. $filesNode1 = $this->createMock(File::class);
  733. $filesNode1->expects($this->any())
  734. ->method('getId')
  735. ->willReturn(111);
  736. $filesNode1->expects($this->any())
  737. ->method('getSize')
  738. ->willReturn(12);
  739. $filesNode2 = $this->createMock(Folder::class);
  740. $filesNode2->expects($this->any())
  741. ->method('getId')
  742. ->willReturn(222);
  743. $filesNode2->expects($this->any())
  744. ->method('getSize')
  745. ->willReturn(10);
  746. $filesNode3 = $this->createMock(Folder::class);
  747. $filesNode3->expects($this->any())
  748. ->method('getId')
  749. ->willReturn(333);
  750. $filesNode3->expects($this->any())
  751. ->method('getSize')
  752. ->willReturn(33);
  753. $this->tagManager->expects($this->once())
  754. ->method('getTagsByIds')
  755. ->with(['123', '456'])
  756. ->willReturn([$tag1, $tag2]);
  757. // main assertion: only user visible tags are being passed through.
  758. $this->userFolder->expects($this->exactly(2))
  759. ->method('searchBySystemTag')
  760. ->withConsecutive(['OneTwoThree'], ['FourFiveSix'])
  761. ->willReturnOnConsecutiveCalls(
  762. [$filesNode1, $filesNode2],
  763. [$filesNode2, $filesNode3],
  764. );
  765. $rules = [
  766. ['name' => '{http://owncloud.org/ns}systemtag', 'value' => '123'],
  767. ['name' => '{http://owncloud.org/ns}systemtag', 'value' => '456'],
  768. ];
  769. $this->assertEquals([$filesNode2], array_values($this->invokePrivate($this->plugin, 'processFilterRulesForFileNodes', [$rules, null, null])));
  770. }
  771. public function testProcessFavoriteFilter(): void {
  772. $rules = [
  773. ['name' => '{http://owncloud.org/ns}favorite', 'value' => '1'],
  774. ];
  775. $this->privateTags->expects($this->once())
  776. ->method('getFavorites')
  777. ->willReturn(['456', '789']);
  778. $this->assertEquals(['456', '789'], array_values($this->invokePrivate($this->plugin, 'processFilterRulesForFileIDs', [$rules])));
  779. }
  780. public function filesBaseUriProvider() {
  781. return [
  782. ['', '', ''],
  783. ['files/username', '', '/files/username'],
  784. ['files/username/test', '/test', '/files/username'],
  785. ['files/username/test/sub', '/test/sub', '/files/username'],
  786. ['test', '/test', ''],
  787. ];
  788. }
  789. /**
  790. * @dataProvider filesBaseUriProvider
  791. */
  792. public function testFilesBaseUri($uri, $reportPath, $expectedUri): void {
  793. $this->assertEquals($expectedUri, $this->invokePrivate($this->plugin, 'getFilesBaseUri', [$uri, $reportPath]));
  794. }
  795. }