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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724
  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. $reportTargetNode->expects($this->any())
  196. ->method('getPath')
  197. ->willReturn('');
  198. $response = $this->getMockBuilder(ResponseInterface::class)
  199. ->disableOriginalConstructor()
  200. ->getMock();
  201. $response->expects($this->once())
  202. ->method('setHeader')
  203. ->with('Content-Type', 'application/xml; charset=utf-8');
  204. $response->expects($this->once())
  205. ->method('setStatus')
  206. ->with(207);
  207. $response->expects($this->once())
  208. ->method('setBody');
  209. $this->tree->expects($this->any())
  210. ->method('getNodeForPath')
  211. ->with('/' . $path)
  212. ->willReturn($reportTargetNode);
  213. $filesNode1 = $this->getMockBuilder(Folder::class)
  214. ->disableOriginalConstructor()
  215. ->getMock();
  216. $filesNode2 = $this->getMockBuilder(File::class)
  217. ->disableOriginalConstructor()
  218. ->getMock();
  219. $this->userFolder->expects($this->at(0))
  220. ->method('getById')
  221. ->with('111')
  222. ->willReturn([$filesNode1]);
  223. $this->userFolder->expects($this->at(1))
  224. ->method('getById')
  225. ->with('222')
  226. ->willReturn([$filesNode2]);
  227. $this->server->expects($this->any())
  228. ->method('getRequestUri')
  229. ->willReturn($path);
  230. $this->server->httpResponse = $response;
  231. $this->plugin->initialize($this->server);
  232. $this->assertFalse($this->plugin->onReport(FilesReportPluginImplementation::REPORT_NAME, $parameters, '/' . $path));
  233. }
  234. public function testFindNodesByFileIdsRoot() {
  235. $filesNode1 = $this->getMockBuilder(Folder::class)
  236. ->disableOriginalConstructor()
  237. ->getMock();
  238. $filesNode1->expects($this->once())
  239. ->method('getName')
  240. ->willReturn('first node');
  241. $filesNode2 = $this->getMockBuilder(File::class)
  242. ->disableOriginalConstructor()
  243. ->getMock();
  244. $filesNode2->expects($this->once())
  245. ->method('getName')
  246. ->willReturn('second node');
  247. $reportTargetNode = $this->getMockBuilder(Directory::class)
  248. ->disableOriginalConstructor()
  249. ->getMock();
  250. $reportTargetNode->expects($this->any())
  251. ->method('getPath')
  252. ->willReturn('/');
  253. $this->userFolder->expects($this->at(0))
  254. ->method('getById')
  255. ->with('111')
  256. ->willReturn([$filesNode1]);
  257. $this->userFolder->expects($this->at(1))
  258. ->method('getById')
  259. ->with('222')
  260. ->willReturn([$filesNode2]);
  261. /** @var \OCA\DAV\Connector\Sabre\Directory|\PHPUnit\Framework\MockObject\MockObject $reportTargetNode */
  262. $result = $this->plugin->findNodesByFileIds($reportTargetNode, ['111', '222']);
  263. $this->assertCount(2, $result);
  264. $this->assertInstanceOf('\OCA\DAV\Connector\Sabre\Directory', $result[0]);
  265. $this->assertEquals('first node', $result[0]->getName());
  266. $this->assertInstanceOf('\OCA\DAV\Connector\Sabre\File', $result[1]);
  267. $this->assertEquals('second node', $result[1]->getName());
  268. }
  269. public function testFindNodesByFileIdsSubDir() {
  270. $filesNode1 = $this->getMockBuilder(Folder::class)
  271. ->disableOriginalConstructor()
  272. ->getMock();
  273. $filesNode1->expects($this->once())
  274. ->method('getName')
  275. ->willReturn('first node');
  276. $filesNode2 = $this->getMockBuilder(File::class)
  277. ->disableOriginalConstructor()
  278. ->getMock();
  279. $filesNode2->expects($this->once())
  280. ->method('getName')
  281. ->willReturn('second node');
  282. $reportTargetNode = $this->getMockBuilder(Directory::class)
  283. ->disableOriginalConstructor()
  284. ->getMock();
  285. $reportTargetNode->expects($this->any())
  286. ->method('getPath')
  287. ->willReturn('/sub1/sub2');
  288. $subNode = $this->getMockBuilder(Folder::class)
  289. ->disableOriginalConstructor()
  290. ->getMock();
  291. $this->userFolder->expects($this->at(0))
  292. ->method('get')
  293. ->with('/sub1/sub2')
  294. ->willReturn($subNode);
  295. $subNode->expects($this->at(0))
  296. ->method('getById')
  297. ->with('111')
  298. ->willReturn([$filesNode1]);
  299. $subNode->expects($this->at(1))
  300. ->method('getById')
  301. ->with('222')
  302. ->willReturn([$filesNode2]);
  303. /** @var \OCA\DAV\Connector\Sabre\Directory|\PHPUnit\Framework\MockObject\MockObject $reportTargetNode */
  304. $result = $this->plugin->findNodesByFileIds($reportTargetNode, ['111', '222']);
  305. $this->assertCount(2, $result);
  306. $this->assertInstanceOf('\OCA\DAV\Connector\Sabre\Directory', $result[0]);
  307. $this->assertEquals('first node', $result[0]->getName());
  308. $this->assertInstanceOf('\OCA\DAV\Connector\Sabre\File', $result[1]);
  309. $this->assertEquals('second node', $result[1]->getName());
  310. }
  311. public function testPrepareResponses() {
  312. $requestedProps = ['{DAV:}getcontentlength', '{http://owncloud.org/ns}fileid', '{DAV:}resourcetype'];
  313. $fileInfo = $this->createMock(FileInfo::class);
  314. $fileInfo->method('isReadable')->willReturn(true);
  315. $node1 = $this->getMockBuilder(Directory::class)
  316. ->disableOriginalConstructor()
  317. ->getMock();
  318. $node2 = $this->getMockBuilder(\OCA\DAV\Connector\Sabre\File::class)
  319. ->disableOriginalConstructor()
  320. ->getMock();
  321. $node1->expects($this->once())
  322. ->method('getInternalFileId')
  323. ->willReturn('111');
  324. $node1->expects($this->any())
  325. ->method('getPath')
  326. ->willReturn('/node1');
  327. $node1->method('getFileInfo')->willReturn($fileInfo);
  328. $node2->expects($this->once())
  329. ->method('getInternalFileId')
  330. ->willReturn('222');
  331. $node2->expects($this->once())
  332. ->method('getSize')
  333. ->willReturn(1024);
  334. $node2->expects($this->any())
  335. ->method('getPath')
  336. ->willReturn('/sub/node2');
  337. $node2->method('getFileInfo')->willReturn($fileInfo);
  338. $config = $this->getMockBuilder(IConfig::class)
  339. ->disableOriginalConstructor()
  340. ->getMock();
  341. $this->server->addPlugin(
  342. new \OCA\DAV\Connector\Sabre\FilesPlugin(
  343. $this->tree,
  344. $config,
  345. $this->createMock(IRequest::class),
  346. $this->previewManager,
  347. $this->createMock(IUserSession::class)
  348. )
  349. );
  350. $this->plugin->initialize($this->server);
  351. $responses = $this->plugin->prepareResponses('/files/username', $requestedProps, [$node1, $node2]);
  352. $this->assertCount(2, $responses);
  353. $this->assertEquals(200, $responses[0]->getHttpStatus());
  354. $this->assertEquals(200, $responses[1]->getHttpStatus());
  355. $this->assertEquals('http://example.com/owncloud/remote.php/dav/files/username/node1', $responses[0]->getHref());
  356. $this->assertEquals('http://example.com/owncloud/remote.php/dav/files/username/sub/node2', $responses[1]->getHref());
  357. $props1 = $responses[0]->getResponseProperties();
  358. $this->assertEquals('111', $props1[200]['{http://owncloud.org/ns}fileid']);
  359. $this->assertNull($props1[404]['{DAV:}getcontentlength']);
  360. $this->assertInstanceOf('\Sabre\DAV\Xml\Property\ResourceType', $props1[200]['{DAV:}resourcetype']);
  361. $resourceType1 = $props1[200]['{DAV:}resourcetype']->getValue();
  362. $this->assertEquals('{DAV:}collection', $resourceType1[0]);
  363. $props2 = $responses[1]->getResponseProperties();
  364. $this->assertEquals('1024', $props2[200]['{DAV:}getcontentlength']);
  365. $this->assertEquals('222', $props2[200]['{http://owncloud.org/ns}fileid']);
  366. $this->assertInstanceOf('\Sabre\DAV\Xml\Property\ResourceType', $props2[200]['{DAV:}resourcetype']);
  367. $this->assertCount(0, $props2[200]['{DAV:}resourcetype']->getValue());
  368. }
  369. public function testProcessFilterRulesSingle() {
  370. $this->groupManager->expects($this->any())
  371. ->method('isAdmin')
  372. ->willReturn(true);
  373. $this->tagMapper->expects($this->exactly(1))
  374. ->method('getObjectIdsForTags')
  375. ->withConsecutive(
  376. ['123', 'files']
  377. )
  378. ->willReturnMap([
  379. ['123', 'files', 0, '', ['111', '222']],
  380. ]);
  381. $rules = [
  382. ['name' => '{http://owncloud.org/ns}systemtag', 'value' => '123'],
  383. ];
  384. $this->assertEquals(['111', '222'], $this->invokePrivate($this->plugin, 'processFilterRules', [$rules]));
  385. }
  386. public function testProcessFilterRulesAndCondition() {
  387. $this->groupManager->expects($this->any())
  388. ->method('isAdmin')
  389. ->willReturn(true);
  390. $this->tagMapper->expects($this->exactly(2))
  391. ->method('getObjectIdsForTags')
  392. ->withConsecutive(
  393. ['123', 'files'],
  394. ['456', 'files']
  395. )
  396. ->willReturnMap([
  397. ['123', 'files', 0, '', ['111', '222']],
  398. ['456', 'files', 0, '', ['222', '333']],
  399. ]);
  400. $rules = [
  401. ['name' => '{http://owncloud.org/ns}systemtag', 'value' => '123'],
  402. ['name' => '{http://owncloud.org/ns}systemtag', 'value' => '456'],
  403. ];
  404. $this->assertEquals(['222'], array_values($this->invokePrivate($this->plugin, 'processFilterRules', [$rules])));
  405. }
  406. public function testProcessFilterRulesAndConditionWithOneEmptyResult() {
  407. $this->groupManager->expects($this->any())
  408. ->method('isAdmin')
  409. ->willReturn(true);
  410. $this->tagMapper->expects($this->exactly(2))
  411. ->method('getObjectIdsForTags')
  412. ->withConsecutive(
  413. ['123', 'files'],
  414. ['456', 'files']
  415. )
  416. ->willReturnMap([
  417. ['123', 'files', 0, '', ['111', '222']],
  418. ['456', 'files', 0, '', []],
  419. ]);
  420. $rules = [
  421. ['name' => '{http://owncloud.org/ns}systemtag', 'value' => '123'],
  422. ['name' => '{http://owncloud.org/ns}systemtag', 'value' => '456'],
  423. ];
  424. $this->assertEquals([], array_values($this->invokePrivate($this->plugin, 'processFilterRules', [$rules])));
  425. }
  426. public function testProcessFilterRulesAndConditionWithFirstEmptyResult() {
  427. $this->groupManager->expects($this->any())
  428. ->method('isAdmin')
  429. ->willReturn(true);
  430. $this->tagMapper->expects($this->exactly(1))
  431. ->method('getObjectIdsForTags')
  432. ->withConsecutive(
  433. ['123', 'files'],
  434. ['456', 'files']
  435. )
  436. ->willReturnMap([
  437. ['123', 'files', 0, '', []],
  438. ['456', 'files', 0, '', ['111', '222']],
  439. ]);
  440. $rules = [
  441. ['name' => '{http://owncloud.org/ns}systemtag', 'value' => '123'],
  442. ['name' => '{http://owncloud.org/ns}systemtag', 'value' => '456'],
  443. ];
  444. $this->assertEquals([], array_values($this->invokePrivate($this->plugin, 'processFilterRules', [$rules])));
  445. }
  446. public function testProcessFilterRulesAndConditionWithEmptyMidResult() {
  447. $this->groupManager->expects($this->any())
  448. ->method('isAdmin')
  449. ->willReturn(true);
  450. $this->tagMapper->expects($this->exactly(2))
  451. ->method('getObjectIdsForTags')
  452. ->withConsecutive(
  453. ['123', 'files'],
  454. ['456', 'files'],
  455. ['789', 'files']
  456. )
  457. ->willReturnMap([
  458. ['123', 'files', 0, '', ['111', '222']],
  459. ['456', 'files', 0, '', ['333']],
  460. ['789', 'files', 0, '', ['111', '222']],
  461. ]);
  462. $rules = [
  463. ['name' => '{http://owncloud.org/ns}systemtag', 'value' => '123'],
  464. ['name' => '{http://owncloud.org/ns}systemtag', 'value' => '456'],
  465. ['name' => '{http://owncloud.org/ns}systemtag', 'value' => '789'],
  466. ];
  467. $this->assertEquals([], array_values($this->invokePrivate($this->plugin, 'processFilterRules', [$rules])));
  468. }
  469. public function testProcessFilterRulesInvisibleTagAsAdmin() {
  470. $this->groupManager->expects($this->any())
  471. ->method('isAdmin')
  472. ->willReturn(true);
  473. $tag1 = $this->getMockBuilder(ISystemTag::class)
  474. ->disableOriginalConstructor()
  475. ->getMock();
  476. $tag1->expects($this->any())
  477. ->method('getId')
  478. ->willReturn('123');
  479. $tag1->expects($this->any())
  480. ->method('isUserVisible')
  481. ->willReturn(true);
  482. $tag2 = $this->getMockBuilder(ISystemTag::class)
  483. ->disableOriginalConstructor()
  484. ->getMock();
  485. $tag2->expects($this->any())
  486. ->method('getId')
  487. ->willReturn('123');
  488. $tag2->expects($this->any())
  489. ->method('isUserVisible')
  490. ->willReturn(false);
  491. // no need to fetch tags to check permissions
  492. $this->tagManager->expects($this->never())
  493. ->method('getTagsByIds');
  494. $this->tagMapper->expects($this->at(0))
  495. ->method('getObjectIdsForTags')
  496. ->with('123')
  497. ->willReturn(['111', '222']);
  498. $this->tagMapper->expects($this->at(1))
  499. ->method('getObjectIdsForTags')
  500. ->with('456')
  501. ->willReturn(['222', '333']);
  502. $rules = [
  503. ['name' => '{http://owncloud.org/ns}systemtag', 'value' => '123'],
  504. ['name' => '{http://owncloud.org/ns}systemtag', 'value' => '456'],
  505. ];
  506. $this->assertEquals(['222'], array_values($this->invokePrivate($this->plugin, 'processFilterRules', [$rules])));
  507. }
  508. public function testProcessFilterRulesInvisibleTagAsUser() {
  509. $this->expectException(\OCP\SystemTag\TagNotFoundException::class);
  510. $this->groupManager->expects($this->any())
  511. ->method('isAdmin')
  512. ->willReturn(false);
  513. $tag1 = $this->getMockBuilder(ISystemTag::class)
  514. ->disableOriginalConstructor()
  515. ->getMock();
  516. $tag1->expects($this->any())
  517. ->method('getId')
  518. ->willReturn('123');
  519. $tag1->expects($this->any())
  520. ->method('isUserVisible')
  521. ->willReturn(true);
  522. $tag2 = $this->getMockBuilder(ISystemTag::class)
  523. ->disableOriginalConstructor()
  524. ->getMock();
  525. $tag2->expects($this->any())
  526. ->method('getId')
  527. ->willReturn('123');
  528. $tag2->expects($this->any())
  529. ->method('isUserVisible')
  530. ->willReturn(false); // invisible
  531. $this->tagManager->expects($this->once())
  532. ->method('getTagsByIds')
  533. ->with(['123', '456'])
  534. ->willReturn([$tag1, $tag2]);
  535. $rules = [
  536. ['name' => '{http://owncloud.org/ns}systemtag', 'value' => '123'],
  537. ['name' => '{http://owncloud.org/ns}systemtag', 'value' => '456'],
  538. ];
  539. $this->invokePrivate($this->plugin, 'processFilterRules', [$rules]);
  540. }
  541. public function testProcessFilterRulesVisibleTagAsUser() {
  542. $this->groupManager->expects($this->any())
  543. ->method('isAdmin')
  544. ->willReturn(false);
  545. $tag1 = $this->getMockBuilder(ISystemTag::class)
  546. ->disableOriginalConstructor()
  547. ->getMock();
  548. $tag1->expects($this->any())
  549. ->method('getId')
  550. ->willReturn('123');
  551. $tag1->expects($this->any())
  552. ->method('isUserVisible')
  553. ->willReturn(true);
  554. $tag2 = $this->getMockBuilder(ISystemTag::class)
  555. ->disableOriginalConstructor()
  556. ->getMock();
  557. $tag2->expects($this->any())
  558. ->method('getId')
  559. ->willReturn('123');
  560. $tag2->expects($this->any())
  561. ->method('isUserVisible')
  562. ->willReturn(true);
  563. $this->tagManager->expects($this->once())
  564. ->method('getTagsByIds')
  565. ->with(['123', '456'])
  566. ->willReturn([$tag1, $tag2]);
  567. $this->tagMapper->expects($this->at(0))
  568. ->method('getObjectIdsForTags')
  569. ->with('123')
  570. ->willReturn(['111', '222']);
  571. $this->tagMapper->expects($this->at(1))
  572. ->method('getObjectIdsForTags')
  573. ->with('456')
  574. ->willReturn(['222', '333']);
  575. $rules = [
  576. ['name' => '{http://owncloud.org/ns}systemtag', 'value' => '123'],
  577. ['name' => '{http://owncloud.org/ns}systemtag', 'value' => '456'],
  578. ];
  579. $this->assertEquals(['222'], array_values($this->invokePrivate($this->plugin, 'processFilterRules', [$rules])));
  580. }
  581. public function testProcessFavoriteFilter() {
  582. $rules = [
  583. ['name' => '{http://owncloud.org/ns}favorite', 'value' => '1'],
  584. ];
  585. $this->privateTags->expects($this->once())
  586. ->method('getFavorites')
  587. ->willReturn(['456', '789']);
  588. $this->assertEquals(['456', '789'], array_values($this->invokePrivate($this->plugin, 'processFilterRules', [$rules])));
  589. }
  590. public function filesBaseUriProvider() {
  591. return [
  592. ['', '', ''],
  593. ['files/username', '', '/files/username'],
  594. ['files/username/test', '/test', '/files/username'],
  595. ['files/username/test/sub', '/test/sub', '/files/username'],
  596. ['test', '/test', ''],
  597. ];
  598. }
  599. /**
  600. * @dataProvider filesBaseUriProvider
  601. */
  602. public function testFilesBaseUri($uri, $reportPath, $expectedUri) {
  603. $this->assertEquals($expectedUri, $this->invokePrivate($this->plugin, 'getFilesBaseUri', [$uri, $reportPath]));
  604. }
  605. }