FilesReportPluginTest.php 28 KB

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