FilesReportPluginTest.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963
  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('getById')
  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('getById')
  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(200, $responses[0]->getHttpStatus());
  375. $this->assertEquals(200, $responses[1]->getHttpStatus());
  376. $this->assertEquals('http://example.com/owncloud/remote.php/dav/files/username/node1', $responses[0]->getHref());
  377. $this->assertEquals('http://example.com/owncloud/remote.php/dav/files/username/sub/node2', $responses[1]->getHref());
  378. $props1 = $responses[0]->getResponseProperties();
  379. $this->assertEquals('111', $props1[200]['{http://owncloud.org/ns}fileid']);
  380. $this->assertNull($props1[404]['{DAV:}getcontentlength']);
  381. $this->assertInstanceOf('\Sabre\DAV\Xml\Property\ResourceType', $props1[200]['{DAV:}resourcetype']);
  382. $resourceType1 = $props1[200]['{DAV:}resourcetype']->getValue();
  383. $this->assertEquals('{DAV:}collection', $resourceType1[0]);
  384. $props2 = $responses[1]->getResponseProperties();
  385. $this->assertEquals('1024', $props2[200]['{DAV:}getcontentlength']);
  386. $this->assertEquals('222', $props2[200]['{http://owncloud.org/ns}fileid']);
  387. $this->assertInstanceOf('\Sabre\DAV\Xml\Property\ResourceType', $props2[200]['{DAV:}resourcetype']);
  388. $this->assertCount(0, $props2[200]['{DAV:}resourcetype']->getValue());
  389. }
  390. public function testProcessFilterRulesSingle(): void {
  391. $this->groupManager->expects($this->any())
  392. ->method('isAdmin')
  393. ->willReturn(true);
  394. $rules = [
  395. ['name' => '{http://owncloud.org/ns}systemtag', 'value' => '123'],
  396. ];
  397. $filesNode1 = $this->createMock(File::class);
  398. $filesNode1->expects($this->any())
  399. ->method('getSize')
  400. ->willReturn(12);
  401. $filesNode2 = $this->createMock(Folder::class);
  402. $filesNode2->expects($this->any())
  403. ->method('getSize')
  404. ->willReturn(10);
  405. $tag123 = $this->createMock(ISystemTag::class);
  406. $tag123->expects($this->any())
  407. ->method('getName')
  408. ->willReturn('OneTwoThree');
  409. $tag123->expects($this->any())
  410. ->method('isUserVisible')
  411. ->willReturn(true);
  412. $this->tagManager->expects($this->once())
  413. ->method('getTagsByIds')
  414. ->with(['123'])
  415. ->willReturn([$tag123]);
  416. $this->userFolder->expects($this->once())
  417. ->method('searchBySystemTag')
  418. ->with('OneTwoThree')
  419. ->willReturn([$filesNode1, $filesNode2]);
  420. $this->assertEquals([$filesNode1, $filesNode2], $this->invokePrivate($this->plugin, 'processFilterRulesForFileNodes', [$rules, 0, 0]));
  421. }
  422. public function testProcessFilterRulesAndCondition(): void {
  423. $this->groupManager->expects($this->any())
  424. ->method('isAdmin')
  425. ->willReturn(true);
  426. $filesNode1 = $this->createMock(File::class);
  427. $filesNode1->expects($this->any())
  428. ->method('getSize')
  429. ->willReturn(12);
  430. $filesNode1->expects($this->any())
  431. ->method('getId')
  432. ->willReturn(111);
  433. $filesNode2 = $this->createMock(Folder::class);
  434. $filesNode2->expects($this->any())
  435. ->method('getSize')
  436. ->willReturn(10);
  437. $filesNode2->expects($this->any())
  438. ->method('getId')
  439. ->willReturn(222);
  440. $filesNode3 = $this->createMock(File::class);
  441. $filesNode3->expects($this->any())
  442. ->method('getSize')
  443. ->willReturn(14);
  444. $filesNode3->expects($this->any())
  445. ->method('getId')
  446. ->willReturn(333);
  447. $tag123 = $this->createMock(ISystemTag::class);
  448. $tag123->expects($this->any())
  449. ->method('getName')
  450. ->willReturn('OneTwoThree');
  451. $tag123->expects($this->any())
  452. ->method('isUserVisible')
  453. ->willReturn(true);
  454. $tag456 = $this->createMock(ISystemTag::class);
  455. $tag456->expects($this->any())
  456. ->method('getName')
  457. ->willReturn('FourFiveSix');
  458. $tag456->expects($this->any())
  459. ->method('isUserVisible')
  460. ->willReturn(true);
  461. $this->tagManager->expects($this->once())
  462. ->method('getTagsByIds')
  463. ->with(['123', '456'])
  464. ->willReturn([$tag123, $tag456]);
  465. $this->userFolder->expects($this->exactly(2))
  466. ->method('searchBySystemTag')
  467. ->withConsecutive(
  468. ['OneTwoThree'],
  469. ['FourFiveSix'],
  470. )
  471. ->willReturnOnConsecutiveCalls(
  472. [$filesNode1, $filesNode2],
  473. [$filesNode2, $filesNode3],
  474. );
  475. $rules = [
  476. ['name' => '{http://owncloud.org/ns}systemtag', 'value' => '123'],
  477. ['name' => '{http://owncloud.org/ns}systemtag', 'value' => '456'],
  478. ];
  479. $this->assertEquals([$filesNode2], array_values($this->invokePrivate($this->plugin, 'processFilterRulesForFileNodes', [$rules, null, null])));
  480. }
  481. public function testProcessFilterRulesAndConditionWithOneEmptyResult(): void {
  482. $this->groupManager->expects($this->any())
  483. ->method('isAdmin')
  484. ->willReturn(true);
  485. $filesNode1 = $this->createMock(File::class);
  486. $filesNode1->expects($this->any())
  487. ->method('getSize')
  488. ->willReturn(12);
  489. $filesNode1->expects($this->any())
  490. ->method('getId')
  491. ->willReturn(111);
  492. $filesNode2 = $this->createMock(Folder::class);
  493. $filesNode2->expects($this->any())
  494. ->method('getSize')
  495. ->willReturn(10);
  496. $filesNode2->expects($this->any())
  497. ->method('getId')
  498. ->willReturn(222);
  499. $tag123 = $this->createMock(ISystemTag::class);
  500. $tag123->expects($this->any())
  501. ->method('getName')
  502. ->willReturn('OneTwoThree');
  503. $tag123->expects($this->any())
  504. ->method('isUserVisible')
  505. ->willReturn(true);
  506. $tag456 = $this->createMock(ISystemTag::class);
  507. $tag456->expects($this->any())
  508. ->method('getName')
  509. ->willReturn('FourFiveSix');
  510. $tag456->expects($this->any())
  511. ->method('isUserVisible')
  512. ->willReturn(true);
  513. $this->tagManager->expects($this->once())
  514. ->method('getTagsByIds')
  515. ->with(['123', '456'])
  516. ->willReturn([$tag123, $tag456]);
  517. $this->userFolder->expects($this->exactly(2))
  518. ->method('searchBySystemTag')
  519. ->withConsecutive(
  520. ['OneTwoThree'],
  521. ['FourFiveSix'],
  522. )
  523. ->willReturnOnConsecutiveCalls(
  524. [$filesNode1, $filesNode2],
  525. [],
  526. );
  527. $rules = [
  528. ['name' => '{http://owncloud.org/ns}systemtag', 'value' => '123'],
  529. ['name' => '{http://owncloud.org/ns}systemtag', 'value' => '456'],
  530. ];
  531. $this->assertEquals([], $this->invokePrivate($this->plugin, 'processFilterRulesForFileNodes', [$rules, null, null]));
  532. }
  533. public function testProcessFilterRulesAndConditionWithFirstEmptyResult(): void {
  534. $this->groupManager->expects($this->any())
  535. ->method('isAdmin')
  536. ->willReturn(true);
  537. $filesNode1 = $this->createMock(File::class);
  538. $filesNode1->expects($this->any())
  539. ->method('getSize')
  540. ->willReturn(12);
  541. $filesNode1->expects($this->any())
  542. ->method('getId')
  543. ->willReturn(111);
  544. $filesNode2 = $this->createMock(Folder::class);
  545. $filesNode2->expects($this->any())
  546. ->method('getSize')
  547. ->willReturn(10);
  548. $filesNode2->expects($this->any())
  549. ->method('getId')
  550. ->willReturn(222);
  551. $tag123 = $this->createMock(ISystemTag::class);
  552. $tag123->expects($this->any())
  553. ->method('getName')
  554. ->willReturn('OneTwoThree');
  555. $tag123->expects($this->any())
  556. ->method('isUserVisible')
  557. ->willReturn(true);
  558. $tag456 = $this->createMock(ISystemTag::class);
  559. $tag456->expects($this->any())
  560. ->method('getName')
  561. ->willReturn('FourFiveSix');
  562. $tag456->expects($this->any())
  563. ->method('isUserVisible')
  564. ->willReturn(true);
  565. $this->tagManager->expects($this->once())
  566. ->method('getTagsByIds')
  567. ->with(['123', '456'])
  568. ->willReturn([$tag123, $tag456]);
  569. $this->userFolder->expects($this->once())
  570. ->method('searchBySystemTag')
  571. ->with('OneTwoThree')
  572. ->willReturnOnConsecutiveCalls(
  573. [],
  574. [$filesNode1, $filesNode2],
  575. );
  576. $rules = [
  577. ['name' => '{http://owncloud.org/ns}systemtag', 'value' => '123'],
  578. ['name' => '{http://owncloud.org/ns}systemtag', 'value' => '456'],
  579. ];
  580. $this->assertEquals([], $this->invokePrivate($this->plugin, 'processFilterRulesForFileNodes', [$rules, null, null]));
  581. }
  582. public function testProcessFilterRulesAndConditionWithEmptyMidResult(): void {
  583. $this->groupManager->expects($this->any())
  584. ->method('isAdmin')
  585. ->willReturn(true);
  586. $filesNode1 = $this->createMock(File::class);
  587. $filesNode1->expects($this->any())
  588. ->method('getSize')
  589. ->willReturn(12);
  590. $filesNode1->expects($this->any())
  591. ->method('getId')
  592. ->willReturn(111);
  593. $filesNode2 = $this->createMock(Folder::class);
  594. $filesNode2->expects($this->any())
  595. ->method('getSize')
  596. ->willReturn(10);
  597. $filesNode2->expects($this->any())
  598. ->method('getId')
  599. ->willReturn(222);
  600. $filesNode3 = $this->createMock(Folder::class);
  601. $filesNode3->expects($this->any())
  602. ->method('getSize')
  603. ->willReturn(13);
  604. $filesNode3->expects($this->any())
  605. ->method('getId')
  606. ->willReturn(333);
  607. $tag123 = $this->createMock(ISystemTag::class);
  608. $tag123->expects($this->any())
  609. ->method('getName')
  610. ->willReturn('OneTwoThree');
  611. $tag123->expects($this->any())
  612. ->method('isUserVisible')
  613. ->willReturn(true);
  614. $tag456 = $this->createMock(ISystemTag::class);
  615. $tag456->expects($this->any())
  616. ->method('getName')
  617. ->willReturn('FourFiveSix');
  618. $tag456->expects($this->any())
  619. ->method('isUserVisible')
  620. ->willReturn(true);
  621. $tag789 = $this->createMock(ISystemTag::class);
  622. $tag789->expects($this->any())
  623. ->method('getName')
  624. ->willReturn('SevenEightNein');
  625. $tag789->expects($this->any())
  626. ->method('isUserVisible')
  627. ->willReturn(true);
  628. $this->tagManager->expects($this->once())
  629. ->method('getTagsByIds')
  630. ->with(['123', '456', '789'])
  631. ->willReturn([$tag123, $tag456, $tag789]);
  632. $this->userFolder->expects($this->exactly(2))
  633. ->method('searchBySystemTag')
  634. ->withConsecutive(['OneTwoThree'], ['FourFiveSix'], ['SevenEightNein'])
  635. ->willReturnOnConsecutiveCalls(
  636. [$filesNode1, $filesNode2],
  637. [$filesNode3],
  638. [$filesNode1, $filesNode2],
  639. );
  640. $rules = [
  641. ['name' => '{http://owncloud.org/ns}systemtag', 'value' => '123'],
  642. ['name' => '{http://owncloud.org/ns}systemtag', 'value' => '456'],
  643. ['name' => '{http://owncloud.org/ns}systemtag', 'value' => '789'],
  644. ];
  645. $this->assertEquals([], array_values($this->invokePrivate($this->plugin, 'processFilterRulesForFileNodes', [$rules, null, null])));
  646. }
  647. public function testProcessFilterRulesInvisibleTagAsAdmin(): void {
  648. $this->groupManager->expects($this->any())
  649. ->method('isAdmin')
  650. ->willReturn(true);
  651. $filesNode1 = $this->createMock(File::class);
  652. $filesNode1->expects($this->any())
  653. ->method('getSize')
  654. ->willReturn(12);
  655. $filesNode1->expects($this->any())
  656. ->method('getId')
  657. ->willReturn(111);
  658. $filesNode2 = $this->createMock(Folder::class);
  659. $filesNode2->expects($this->any())
  660. ->method('getSize')
  661. ->willReturn(10);
  662. $filesNode2->expects($this->any())
  663. ->method('getId')
  664. ->willReturn(222);
  665. $filesNode3 = $this->createMock(Folder::class);
  666. $filesNode3->expects($this->any())
  667. ->method('getSize')
  668. ->willReturn(13);
  669. $filesNode3->expects($this->any())
  670. ->method('getId')
  671. ->willReturn(333);
  672. $tag123 = $this->createMock(ISystemTag::class);
  673. $tag123->expects($this->any())
  674. ->method('getName')
  675. ->willReturn('OneTwoThree');
  676. $tag123->expects($this->any())
  677. ->method('isUserVisible')
  678. ->willReturn(true);
  679. $tag456 = $this->createMock(ISystemTag::class);
  680. $tag456->expects($this->any())
  681. ->method('getName')
  682. ->willReturn('FourFiveSix');
  683. $tag456->expects($this->any())
  684. ->method('isUserVisible')
  685. ->willReturn(false);
  686. $this->tagManager->expects($this->once())
  687. ->method('getTagsByIds')
  688. ->with(['123', '456'])
  689. ->willReturn([$tag123, $tag456]);
  690. $this->userFolder->expects($this->exactly(2))
  691. ->method('searchBySystemTag')
  692. ->withConsecutive(['OneTwoThree'], ['FourFiveSix'])
  693. ->willReturnOnConsecutiveCalls(
  694. [$filesNode1, $filesNode2],
  695. [$filesNode2, $filesNode3],
  696. );
  697. $rules = [
  698. ['name' => '{http://owncloud.org/ns}systemtag', 'value' => '123'],
  699. ['name' => '{http://owncloud.org/ns}systemtag', 'value' => '456'],
  700. ];
  701. $this->assertEquals([$filesNode2], array_values($this->invokePrivate($this->plugin, 'processFilterRulesForFileNodes', [$rules, null, null])));
  702. }
  703. public function testProcessFilterRulesInvisibleTagAsUser(): void {
  704. $this->expectException(TagNotFoundException::class);
  705. $this->groupManager->expects($this->any())
  706. ->method('isAdmin')
  707. ->willReturn(false);
  708. $tag123 = $this->createMock(ISystemTag::class);
  709. $tag123->expects($this->any())
  710. ->method('getName')
  711. ->willReturn('OneTwoThree');
  712. $tag123->expects($this->any())
  713. ->method('isUserVisible')
  714. ->willReturn(true);
  715. $tag456 = $this->createMock(ISystemTag::class);
  716. $tag456->expects($this->any())
  717. ->method('getName')
  718. ->willReturn('FourFiveSix');
  719. $tag456->expects($this->any())
  720. ->method('isUserVisible')
  721. ->willReturn(false);
  722. $this->tagManager->expects($this->once())
  723. ->method('getTagsByIds')
  724. ->with(['123', '456'])
  725. ->willThrowException(new TagNotFoundException());
  726. $this->userFolder->expects($this->never())
  727. ->method('searchBySystemTag');
  728. $rules = [
  729. ['name' => '{http://owncloud.org/ns}systemtag', 'value' => '123'],
  730. ['name' => '{http://owncloud.org/ns}systemtag', 'value' => '456'],
  731. ];
  732. $this->invokePrivate($this->plugin, 'processFilterRulesForFileNodes', [$rules, null, null]);
  733. }
  734. public function testProcessFilterRulesVisibleTagAsUser(): void {
  735. $this->groupManager->expects($this->any())
  736. ->method('isAdmin')
  737. ->willReturn(false);
  738. $tag1 = $this->createMock(ISystemTag::class);
  739. $tag1->expects($this->any())
  740. ->method('getId')
  741. ->willReturn('123');
  742. $tag1->expects($this->any())
  743. ->method('isUserVisible')
  744. ->willReturn(true);
  745. $tag1->expects($this->any())
  746. ->method('getName')
  747. ->willReturn('OneTwoThree');
  748. $tag2 = $this->createMock(ISystemTag::class);
  749. $tag2->expects($this->any())
  750. ->method('getId')
  751. ->willReturn('123');
  752. $tag2->expects($this->any())
  753. ->method('isUserVisible')
  754. ->willReturn(true);
  755. $tag2->expects($this->any())
  756. ->method('getName')
  757. ->willReturn('FourFiveSix');
  758. $this->tagManager->expects($this->once())
  759. ->method('getTagsByIds')
  760. ->with(['123', '456'])
  761. ->willReturn([$tag1, $tag2]);
  762. $filesNode1 = $this->createMock(File::class);
  763. $filesNode1->expects($this->any())
  764. ->method('getId')
  765. ->willReturn(111);
  766. $filesNode1->expects($this->any())
  767. ->method('getSize')
  768. ->willReturn(12);
  769. $filesNode2 = $this->createMock(Folder::class);
  770. $filesNode2->expects($this->any())
  771. ->method('getId')
  772. ->willReturn(222);
  773. $filesNode2->expects($this->any())
  774. ->method('getSize')
  775. ->willReturn(10);
  776. $filesNode3 = $this->createMock(Folder::class);
  777. $filesNode3->expects($this->any())
  778. ->method('getId')
  779. ->willReturn(333);
  780. $filesNode3->expects($this->any())
  781. ->method('getSize')
  782. ->willReturn(33);
  783. $this->tagManager->expects($this->once())
  784. ->method('getTagsByIds')
  785. ->with(['123', '456'])
  786. ->willReturn([$tag1, $tag2]);
  787. // main assertion: only user visible tags are being passed through.
  788. $this->userFolder->expects($this->exactly(2))
  789. ->method('searchBySystemTag')
  790. ->withConsecutive(['OneTwoThree'], ['FourFiveSix'])
  791. ->willReturnOnConsecutiveCalls(
  792. [$filesNode1, $filesNode2],
  793. [$filesNode2, $filesNode3],
  794. );
  795. $rules = [
  796. ['name' => '{http://owncloud.org/ns}systemtag', 'value' => '123'],
  797. ['name' => '{http://owncloud.org/ns}systemtag', 'value' => '456'],
  798. ];
  799. $this->assertEquals([$filesNode2], array_values($this->invokePrivate($this->plugin, 'processFilterRulesForFileNodes', [$rules, null, null])));
  800. }
  801. public function testProcessFavoriteFilter(): void {
  802. $rules = [
  803. ['name' => '{http://owncloud.org/ns}favorite', 'value' => '1'],
  804. ];
  805. $this->privateTags->expects($this->once())
  806. ->method('getFavorites')
  807. ->willReturn(['456', '789']);
  808. $this->assertEquals(['456', '789'], array_values($this->invokePrivate($this->plugin, 'processFilterRulesForFileIDs', [$rules])));
  809. }
  810. public function filesBaseUriProvider() {
  811. return [
  812. ['', '', ''],
  813. ['files/username', '', '/files/username'],
  814. ['files/username/test', '/test', '/files/username'],
  815. ['files/username/test/sub', '/test/sub', '/files/username'],
  816. ['test', '/test', ''],
  817. ];
  818. }
  819. /**
  820. * @dataProvider filesBaseUriProvider
  821. */
  822. public function testFilesBaseUri($uri, $reportPath, $expectedUri): void {
  823. $this->assertEquals($expectedUri, $this->invokePrivate($this->plugin, 'getFilesBaseUri', [$uri, $reportPath]));
  824. }
  825. }