You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FilesReportPluginTest.php 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  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 Sabre\DAV\INode;
  47. use Sabre\DAV\Tree;
  48. use Sabre\HTTP\ResponseInterface;
  49. class FilesReportPluginTest extends \Test\TestCase {
  50. /** @var \Sabre\DAV\Server|\PHPUnit\Framework\MockObject\MockObject */
  51. private $server;
  52. /** @var \Sabre\DAV\Tree|\PHPUnit\Framework\MockObject\MockObject */
  53. private $tree;
  54. /** @var ISystemTagObjectMapper|\PHPUnit\Framework\MockObject\MockObject */
  55. private $tagMapper;
  56. /** @var ISystemTagManager|\PHPUnit\Framework\MockObject\MockObject */
  57. private $tagManager;
  58. /** @var ITags|\PHPUnit\Framework\MockObject\MockObject */
  59. private $privateTags;
  60. /** @var \OCP\IUserSession */
  61. private $userSession;
  62. /** @var FilesReportPluginImplementation */
  63. private $plugin;
  64. /** @var View|\PHPUnit\Framework\MockObject\MockObject **/
  65. private $view;
  66. /** @var IGroupManager|\PHPUnit\Framework\MockObject\MockObject **/
  67. private $groupManager;
  68. /** @var Folder|\PHPUnit\Framework\MockObject\MockObject **/
  69. private $userFolder;
  70. /** @var IPreview|\PHPUnit\Framework\MockObject\MockObject * */
  71. private $previewManager;
  72. /** @var IAppManager|\PHPUnit\Framework\MockObject\MockObject * */
  73. private $appManager;
  74. protected function setUp(): void {
  75. parent::setUp();
  76. $this->tree = $this->getMockBuilder(Tree::class)
  77. ->disableOriginalConstructor()
  78. ->getMock();
  79. $this->view = $this->getMockBuilder(View::class)
  80. ->disableOriginalConstructor()
  81. ->getMock();
  82. $this->server = $this->getMockBuilder('\Sabre\DAV\Server')
  83. ->setConstructorArgs([$this->tree])
  84. ->setMethods(['getRequestUri', 'getBaseUri'])
  85. ->getMock();
  86. $this->server->expects($this->any())
  87. ->method('getBaseUri')
  88. ->willReturn('http://example.com/owncloud/remote.php/dav');
  89. $this->groupManager = $this->getMockBuilder(IGroupManager::class)
  90. ->disableOriginalConstructor()
  91. ->getMock();
  92. $this->userFolder = $this->getMockBuilder(Folder::class)
  93. ->disableOriginalConstructor()
  94. ->getMock();
  95. $this->previewManager = $this->getMockBuilder(IPreview::class)
  96. ->disableOriginalConstructor()
  97. ->getMock();
  98. $this->appManager = $this->getMockBuilder(IAppManager::class)
  99. ->disableOriginalConstructor()
  100. ->getMock();
  101. $this->tagManager = $this->createMock(ISystemTagManager::class);
  102. $this->tagMapper = $this->createMock(ISystemTagObjectMapper::class);
  103. $this->userSession = $this->createMock(IUserSession::class);
  104. $this->privateTags = $this->createMock(ITags::class);
  105. $privateTagManager = $this->createMock(ITagManager::class);
  106. $privateTagManager->expects($this->any())
  107. ->method('load')
  108. ->with('files')
  109. ->willReturn($this->privateTags);
  110. $user = $this->getMockBuilder(IUser::class)
  111. ->disableOriginalConstructor()
  112. ->getMock();
  113. $user->expects($this->any())
  114. ->method('getUID')
  115. ->willReturn('testuser');
  116. $this->userSession->expects($this->any())
  117. ->method('getUser')
  118. ->willReturn($user);
  119. $this->plugin = new FilesReportPluginImplementation(
  120. $this->tree,
  121. $this->view,
  122. $this->tagManager,
  123. $this->tagMapper,
  124. $privateTagManager,
  125. $this->userSession,
  126. $this->groupManager,
  127. $this->userFolder,
  128. $this->appManager
  129. );
  130. }
  131. public function testOnReportInvalidNode() {
  132. $path = 'totally/unrelated/13';
  133. $this->tree->expects($this->any())
  134. ->method('getNodeForPath')
  135. ->with('/' . $path)
  136. ->willReturn(
  137. $this->getMockBuilder(INode::class)
  138. ->disableOriginalConstructor()
  139. ->getMock()
  140. );
  141. $this->server->expects($this->any())
  142. ->method('getRequestUri')
  143. ->willReturn($path);
  144. $this->plugin->initialize($this->server);
  145. $this->assertNull($this->plugin->onReport(FilesReportPluginImplementation::REPORT_NAME, [], '/' . $path));
  146. }
  147. public function testOnReportInvalidReportName() {
  148. $path = 'test';
  149. $this->tree->expects($this->any())
  150. ->method('getNodeForPath')
  151. ->with('/' . $path)
  152. ->willReturn(
  153. $this->getMockBuilder(INode::class)
  154. ->disableOriginalConstructor()
  155. ->getMock()
  156. );
  157. $this->server->expects($this->any())
  158. ->method('getRequestUri')
  159. ->willReturn($path);
  160. $this->plugin->initialize($this->server);
  161. $this->assertNull($this->plugin->onReport('{whoever}whatever', [], '/' . $path));
  162. }
  163. public function testOnReport() {
  164. $path = 'test';
  165. $parameters = [
  166. [
  167. 'name' => '{DAV:}prop',
  168. 'value' => [
  169. ['name' => '{DAV:}getcontentlength', 'value' => ''],
  170. ['name' => '{http://owncloud.org/ns}size', 'value' => ''],
  171. ],
  172. ],
  173. [
  174. 'name' => '{http://owncloud.org/ns}filter-rules',
  175. 'value' => [
  176. ['name' => '{http://owncloud.org/ns}systemtag', 'value' => '123'],
  177. ['name' => '{http://owncloud.org/ns}systemtag', 'value' => '456'],
  178. ],
  179. ],
  180. ];
  181. $this->groupManager->expects($this->any())
  182. ->method('isAdmin')
  183. ->willReturn(true);
  184. $this->tagMapper->expects($this->at(0))
  185. ->method('getObjectIdsForTags')
  186. ->with('123', 'files')
  187. ->willReturn(['111', '222']);
  188. $this->tagMapper->expects($this->at(1))
  189. ->method('getObjectIdsForTags')
  190. ->with('456', 'files')
  191. ->willReturn(['111', '222', '333']);
  192. $reportTargetNode = $this->getMockBuilder(Directory::class)
  193. ->disableOriginalConstructor()
  194. ->getMock();
  195. $response = $this->getMockBuilder(ResponseInterface::class)
  196. ->disableOriginalConstructor()
  197. ->getMock();
  198. $response->expects($this->once())
  199. ->method('setHeader')
  200. ->with('Content-Type', 'application/xml; charset=utf-8');
  201. $response->expects($this->once())
  202. ->method('setStatus')
  203. ->with(207);
  204. $response->expects($this->once())
  205. ->method('setBody');
  206. $this->tree->expects($this->any())
  207. ->method('getNodeForPath')
  208. ->with('/' . $path)
  209. ->willReturn($reportTargetNode);
  210. $filesNode1 = $this->getMockBuilder(Folder::class)
  211. ->disableOriginalConstructor()
  212. ->getMock();
  213. $filesNode2 = $this->getMockBuilder(File::class)
  214. ->disableOriginalConstructor()
  215. ->getMock();
  216. $this->userFolder->expects($this->at(0))
  217. ->method('getById')
  218. ->with('111')
  219. ->willReturn([$filesNode1]);
  220. $this->userFolder->expects($this->at(1))
  221. ->method('getById')
  222. ->with('222')
  223. ->willReturn([$filesNode2]);
  224. $this->server->expects($this->any())
  225. ->method('getRequestUri')
  226. ->willReturn($path);
  227. $this->server->httpResponse = $response;
  228. $this->plugin->initialize($this->server);
  229. $this->assertFalse($this->plugin->onReport(FilesReportPluginImplementation::REPORT_NAME, $parameters, '/' . $path));
  230. }
  231. public function testFindNodesByFileIdsRoot() {
  232. $filesNode1 = $this->getMockBuilder(Folder::class)
  233. ->disableOriginalConstructor()
  234. ->getMock();
  235. $filesNode1->expects($this->once())
  236. ->method('getName')
  237. ->willReturn('first node');
  238. $filesNode2 = $this->getMockBuilder(File::class)
  239. ->disableOriginalConstructor()
  240. ->getMock();
  241. $filesNode2->expects($this->once())
  242. ->method('getName')
  243. ->willReturn('second node');
  244. $reportTargetNode = $this->getMockBuilder(Directory::class)
  245. ->disableOriginalConstructor()
  246. ->getMock();
  247. $reportTargetNode->expects($this->any())
  248. ->method('getPath')
  249. ->willReturn('/');
  250. $this->userFolder->expects($this->at(0))
  251. ->method('getById')
  252. ->with('111')
  253. ->willReturn([$filesNode1]);
  254. $this->userFolder->expects($this->at(1))
  255. ->method('getById')
  256. ->with('222')
  257. ->willReturn([$filesNode2]);
  258. /** @var \OCA\DAV\Connector\Sabre\Directory|\PHPUnit\Framework\MockObject\MockObject $reportTargetNode */
  259. $result = $this->plugin->findNodesByFileIds($reportTargetNode, ['111', '222']);
  260. $this->assertCount(2, $result);
  261. $this->assertInstanceOf('\OCA\DAV\Connector\Sabre\Directory', $result[0]);
  262. $this->assertEquals('first node', $result[0]->getName());
  263. $this->assertInstanceOf('\OCA\DAV\Connector\Sabre\File', $result[1]);
  264. $this->assertEquals('second node', $result[1]->getName());
  265. }
  266. public function testFindNodesByFileIdsSubDir() {
  267. $filesNode1 = $this->getMockBuilder(Folder::class)
  268. ->disableOriginalConstructor()
  269. ->getMock();
  270. $filesNode1->expects($this->once())
  271. ->method('getName')
  272. ->willReturn('first node');
  273. $filesNode2 = $this->getMockBuilder(File::class)
  274. ->disableOriginalConstructor()
  275. ->getMock();
  276. $filesNode2->expects($this->once())
  277. ->method('getName')
  278. ->willReturn('second node');
  279. $reportTargetNode = $this->getMockBuilder(Directory::class)
  280. ->disableOriginalConstructor()
  281. ->getMock();
  282. $reportTargetNode->expects($this->any())
  283. ->method('getPath')
  284. ->willReturn('/sub1/sub2');
  285. $subNode = $this->getMockBuilder(Folder::class)
  286. ->disableOriginalConstructor()
  287. ->getMock();
  288. $this->userFolder->expects($this->at(0))
  289. ->method('get')
  290. ->with('/sub1/sub2')
  291. ->willReturn($subNode);
  292. $subNode->expects($this->at(0))
  293. ->method('getById')
  294. ->with('111')
  295. ->willReturn([$filesNode1]);
  296. $subNode->expects($this->at(1))
  297. ->method('getById')
  298. ->with('222')
  299. ->willReturn([$filesNode2]);
  300. /** @var \OCA\DAV\Connector\Sabre\Directory|\PHPUnit\Framework\MockObject\MockObject $reportTargetNode */
  301. $result = $this->plugin->findNodesByFileIds($reportTargetNode, ['111', '222']);
  302. $this->assertCount(2, $result);
  303. $this->assertInstanceOf('\OCA\DAV\Connector\Sabre\Directory', $result[0]);
  304. $this->assertEquals('first node', $result[0]->getName());
  305. $this->assertInstanceOf('\OCA\DAV\Connector\Sabre\File', $result[1]);
  306. $this->assertEquals('second node', $result[1]->getName());
  307. }
  308. public function testPrepareResponses() {
  309. $requestedProps = ['{DAV:}getcontentlength', '{http://owncloud.org/ns}fileid', '{DAV:}resourcetype'];
  310. $fileInfo = $this->createMock(FileInfo::class);
  311. $fileInfo->method('isReadable')->willReturn(true);
  312. $node1 = $this->getMockBuilder(Directory::class)
  313. ->disableOriginalConstructor()
  314. ->getMock();
  315. $node2 = $this->getMockBuilder(\OCA\DAV\Connector\Sabre\File::class)
  316. ->disableOriginalConstructor()
  317. ->getMock();
  318. $node1->expects($this->once())
  319. ->method('getInternalFileId')
  320. ->willReturn('111');
  321. $node1->expects($this->any())
  322. ->method('getPath')
  323. ->willReturn('/node1');
  324. $node1->method('getFileInfo')->willReturn($fileInfo);
  325. $node2->expects($this->once())
  326. ->method('getInternalFileId')
  327. ->willReturn('222');
  328. $node2->expects($this->once())
  329. ->method('getSize')
  330. ->willReturn(1024);
  331. $node2->expects($this->any())
  332. ->method('getPath')
  333. ->willReturn('/sub/node2');
  334. $node2->method('getFileInfo')->willReturn($fileInfo);
  335. $config = $this->getMockBuilder(IConfig::class)
  336. ->disableOriginalConstructor()
  337. ->getMock();
  338. $this->server->addPlugin(
  339. new \OCA\DAV\Connector\Sabre\FilesPlugin(
  340. $this->tree,
  341. $config,
  342. $this->getMockBuilder(IRequest::class)
  343. ->disableOriginalConstructor()
  344. ->getMock(),
  345. $this->previewManager
  346. )
  347. );
  348. $this->plugin->initialize($this->server);
  349. $responses = $this->plugin->prepareResponses('/files/username', $requestedProps, [$node1, $node2]);
  350. $this->assertCount(2, $responses);
  351. $this->assertEquals(200, $responses[0]->getHttpStatus());
  352. $this->assertEquals(200, $responses[1]->getHttpStatus());
  353. $this->assertEquals('http://example.com/owncloud/remote.php/dav/files/username/node1', $responses[0]->getHref());
  354. $this->assertEquals('http://example.com/owncloud/remote.php/dav/files/username/sub/node2', $responses[1]->getHref());
  355. $props1 = $responses[0]->getResponseProperties();
  356. $this->assertEquals('111', $props1[200]['{http://owncloud.org/ns}fileid']);
  357. $this->assertNull($props1[404]['{DAV:}getcontentlength']);
  358. $this->assertInstanceOf('\Sabre\DAV\Xml\Property\ResourceType', $props1[200]['{DAV:}resourcetype']);
  359. $resourceType1 = $props1[200]['{DAV:}resourcetype']->getValue();
  360. $this->assertEquals('{DAV:}collection', $resourceType1[0]);
  361. $props2 = $responses[1]->getResponseProperties();
  362. $this->assertEquals('1024', $props2[200]['{DAV:}getcontentlength']);
  363. $this->assertEquals('222', $props2[200]['{http://owncloud.org/ns}fileid']);
  364. $this->assertInstanceOf('\Sabre\DAV\Xml\Property\ResourceType', $props2[200]['{DAV:}resourcetype']);
  365. $this->assertCount(0, $props2[200]['{DAV:}resourcetype']->getValue());
  366. }
  367. public function testProcessFilterRulesSingle() {
  368. $this->groupManager->expects($this->any())
  369. ->method('isAdmin')
  370. ->willReturn(true);
  371. $this->tagMapper->expects($this->exactly(1))
  372. ->method('getObjectIdsForTags')
  373. ->withConsecutive(
  374. ['123', 'files']
  375. )
  376. ->willReturnMap([
  377. ['123', 'files', 0, '', ['111', '222']],
  378. ]);
  379. $rules = [
  380. ['name' => '{http://owncloud.org/ns}systemtag', 'value' => '123'],
  381. ];
  382. $this->assertEquals(['111', '222'], $this->invokePrivate($this->plugin, 'processFilterRules', [$rules]));
  383. }
  384. public function testProcessFilterRulesAndCondition() {
  385. $this->groupManager->expects($this->any())
  386. ->method('isAdmin')
  387. ->willReturn(true);
  388. $this->tagMapper->expects($this->exactly(2))
  389. ->method('getObjectIdsForTags')
  390. ->withConsecutive(
  391. ['123', 'files'],
  392. ['456', 'files']
  393. )
  394. ->willReturnMap([
  395. ['123', 'files', 0, '', ['111', '222']],
  396. ['456', 'files', 0, '', ['222', '333']],
  397. ]);
  398. $rules = [
  399. ['name' => '{http://owncloud.org/ns}systemtag', 'value' => '123'],
  400. ['name' => '{http://owncloud.org/ns}systemtag', 'value' => '456'],
  401. ];
  402. $this->assertEquals(['222'], array_values($this->invokePrivate($this->plugin, 'processFilterRules', [$rules])));
  403. }
  404. public function testProcessFilterRulesAndConditionWithOneEmptyResult() {
  405. $this->groupManager->expects($this->any())
  406. ->method('isAdmin')
  407. ->willReturn(true);
  408. $this->tagMapper->expects($this->exactly(2))
  409. ->method('getObjectIdsForTags')
  410. ->withConsecutive(
  411. ['123', 'files'],
  412. ['456', 'files']
  413. )
  414. ->willReturnMap([
  415. ['123', 'files', 0, '', ['111', '222']],
  416. ['456', 'files', 0, '', []],
  417. ]);
  418. $rules = [
  419. ['name' => '{http://owncloud.org/ns}systemtag', 'value' => '123'],
  420. ['name' => '{http://owncloud.org/ns}systemtag', 'value' => '456'],
  421. ];
  422. $this->assertEquals([], array_values($this->invokePrivate($this->plugin, 'processFilterRules', [$rules])));
  423. }
  424. public function testProcessFilterRulesAndConditionWithFirstEmptyResult() {
  425. $this->groupManager->expects($this->any())
  426. ->method('isAdmin')
  427. ->willReturn(true);
  428. $this->tagMapper->expects($this->exactly(1))
  429. ->method('getObjectIdsForTags')
  430. ->withConsecutive(
  431. ['123', 'files'],
  432. ['456', 'files']
  433. )
  434. ->willReturnMap([
  435. ['123', 'files', 0, '', []],
  436. ['456', 'files', 0, '', ['111', '222']],
  437. ]);
  438. $rules = [
  439. ['name' => '{http://owncloud.org/ns}systemtag', 'value' => '123'],
  440. ['name' => '{http://owncloud.org/ns}systemtag', 'value' => '456'],
  441. ];
  442. $this->assertEquals([], array_values($this->invokePrivate($this->plugin, 'processFilterRules', [$rules])));
  443. }
  444. public function testProcessFilterRulesAndConditionWithEmptyMidResult() {
  445. $this->groupManager->expects($this->any())
  446. ->method('isAdmin')
  447. ->willReturn(true);
  448. $this->tagMapper->expects($this->exactly(2))
  449. ->method('getObjectIdsForTags')
  450. ->withConsecutive(
  451. ['123', 'files'],
  452. ['456', 'files'],
  453. ['789', 'files']
  454. )
  455. ->willReturnMap([
  456. ['123', 'files', 0, '', ['111', '222']],
  457. ['456', 'files', 0, '', ['333']],
  458. ['789', 'files', 0, '', ['111', '222']],
  459. ]);
  460. $rules = [
  461. ['name' => '{http://owncloud.org/ns}systemtag', 'value' => '123'],
  462. ['name' => '{http://owncloud.org/ns}systemtag', 'value' => '456'],
  463. ['name' => '{http://owncloud.org/ns}systemtag', 'value' => '789'],
  464. ];
  465. $this->assertEquals([], array_values($this->invokePrivate($this->plugin, 'processFilterRules', [$rules])));
  466. }
  467. public function testProcessFilterRulesInvisibleTagAsAdmin() {
  468. $this->groupManager->expects($this->any())
  469. ->method('isAdmin')
  470. ->willReturn(true);
  471. $tag1 = $this->getMockBuilder(ISystemTag::class)
  472. ->disableOriginalConstructor()
  473. ->getMock();
  474. $tag1->expects($this->any())
  475. ->method('getId')
  476. ->willReturn('123');
  477. $tag1->expects($this->any())
  478. ->method('isUserVisible')
  479. ->willReturn(true);
  480. $tag2 = $this->getMockBuilder(ISystemTag::class)
  481. ->disableOriginalConstructor()
  482. ->getMock();
  483. $tag2->expects($this->any())
  484. ->method('getId')
  485. ->willReturn('123');
  486. $tag2->expects($this->any())
  487. ->method('isUserVisible')
  488. ->willReturn(false);
  489. // no need to fetch tags to check permissions
  490. $this->tagManager->expects($this->never())
  491. ->method('getTagsByIds');
  492. $this->tagMapper->expects($this->at(0))
  493. ->method('getObjectIdsForTags')
  494. ->with('123')
  495. ->willReturn(['111', '222']);
  496. $this->tagMapper->expects($this->at(1))
  497. ->method('getObjectIdsForTags')
  498. ->with('456')
  499. ->willReturn(['222', '333']);
  500. $rules = [
  501. ['name' => '{http://owncloud.org/ns}systemtag', 'value' => '123'],
  502. ['name' => '{http://owncloud.org/ns}systemtag', 'value' => '456'],
  503. ];
  504. $this->assertEquals(['222'], array_values($this->invokePrivate($this->plugin, 'processFilterRules', [$rules])));
  505. }
  506. public function testProcessFilterRulesInvisibleTagAsUser() {
  507. $this->expectException(\OCP\SystemTag\TagNotFoundException::class);
  508. $this->groupManager->expects($this->any())
  509. ->method('isAdmin')
  510. ->willReturn(false);
  511. $tag1 = $this->getMockBuilder(ISystemTag::class)
  512. ->disableOriginalConstructor()
  513. ->getMock();
  514. $tag1->expects($this->any())
  515. ->method('getId')
  516. ->willReturn('123');
  517. $tag1->expects($this->any())
  518. ->method('isUserVisible')
  519. ->willReturn(true);
  520. $tag2 = $this->getMockBuilder(ISystemTag::class)
  521. ->disableOriginalConstructor()
  522. ->getMock();
  523. $tag2->expects($this->any())
  524. ->method('getId')
  525. ->willReturn('123');
  526. $tag2->expects($this->any())
  527. ->method('isUserVisible')
  528. ->willReturn(false); // invisible
  529. $this->tagManager->expects($this->once())
  530. ->method('getTagsByIds')
  531. ->with(['123', '456'])
  532. ->willReturn([$tag1, $tag2]);
  533. $rules = [
  534. ['name' => '{http://owncloud.org/ns}systemtag', 'value' => '123'],
  535. ['name' => '{http://owncloud.org/ns}systemtag', 'value' => '456'],
  536. ];
  537. $this->invokePrivate($this->plugin, 'processFilterRules', [$rules]);
  538. }
  539. public function testProcessFilterRulesVisibleTagAsUser() {
  540. $this->groupManager->expects($this->any())
  541. ->method('isAdmin')
  542. ->willReturn(false);
  543. $tag1 = $this->getMockBuilder(ISystemTag::class)
  544. ->disableOriginalConstructor()
  545. ->getMock();
  546. $tag1->expects($this->any())
  547. ->method('getId')
  548. ->willReturn('123');
  549. $tag1->expects($this->any())
  550. ->method('isUserVisible')
  551. ->willReturn(true);
  552. $tag2 = $this->getMockBuilder(ISystemTag::class)
  553. ->disableOriginalConstructor()
  554. ->getMock();
  555. $tag2->expects($this->any())
  556. ->method('getId')
  557. ->willReturn('123');
  558. $tag2->expects($this->any())
  559. ->method('isUserVisible')
  560. ->willReturn(true);
  561. $this->tagManager->expects($this->once())
  562. ->method('getTagsByIds')
  563. ->with(['123', '456'])
  564. ->willReturn([$tag1, $tag2]);
  565. $this->tagMapper->expects($this->at(0))
  566. ->method('getObjectIdsForTags')
  567. ->with('123')
  568. ->willReturn(['111', '222']);
  569. $this->tagMapper->expects($this->at(1))
  570. ->method('getObjectIdsForTags')
  571. ->with('456')
  572. ->willReturn(['222', '333']);
  573. $rules = [
  574. ['name' => '{http://owncloud.org/ns}systemtag', 'value' => '123'],
  575. ['name' => '{http://owncloud.org/ns}systemtag', 'value' => '456'],
  576. ];
  577. $this->assertEquals(['222'], array_values($this->invokePrivate($this->plugin, 'processFilterRules', [$rules])));
  578. }
  579. public function testProcessFavoriteFilter() {
  580. $rules = [
  581. ['name' => '{http://owncloud.org/ns}favorite', 'value' => '1'],
  582. ];
  583. $this->privateTags->expects($this->once())
  584. ->method('getFavorites')
  585. ->willReturn(['456', '789']);
  586. $this->assertEquals(['456', '789'], array_values($this->invokePrivate($this->plugin, 'processFilterRules', [$rules])));
  587. }
  588. public function filesBaseUriProvider() {
  589. return [
  590. ['', '', ''],
  591. ['files/username', '', '/files/username'],
  592. ['files/username/test', '/test', '/files/username'],
  593. ['files/username/test/sub', '/test/sub', '/files/username'],
  594. ['test', '/test', ''],
  595. ];
  596. }
  597. /**
  598. * @dataProvider filesBaseUriProvider
  599. */
  600. public function testFilesBaseUri($uri, $reportPath, $expectedUri) {
  601. $this->assertEquals($expectedUri, $this->invokePrivate($this->plugin, 'getFilesBaseUri', [$uri, $reportPath]));
  602. }
  603. }