diff options
author | Arthur Schiwon <blizzz@arthur-schiwon.de> | 2023-07-11 14:22:44 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-07-11 14:22:44 +0200 |
commit | 59466b3424066012ab808fa0a36c6611321fa3cb (patch) | |
tree | a37691a67f57381f28c52a528d5e09820ba989d5 /apps | |
parent | 99ff886d5d5c2a20a204cd3bce3eba08bfdbbe7a (diff) | |
parent | 2ca8c7102bcf65d3f5ce58696d9bade504416c87 (diff) | |
download | nextcloud-server-59466b3424066012ab808fa0a36c6611321fa3cb.tar.gz nextcloud-server-59466b3424066012ab808fa0a36c6611321fa3cb.zip |
Merge pull request #39233 from nextcloud/backport/39202/stable25
[stable25] use more efficient tag retrieval on DAV report request
Diffstat (limited to 'apps')
-rw-r--r-- | apps/dav/lib/Connector/Sabre/FilesReportPlugin.php | 142 | ||||
-rw-r--r-- | apps/dav/tests/unit/Connector/Sabre/FilesReportPluginTest.php | 608 |
2 files changed, 509 insertions, 241 deletions
diff --git a/apps/dav/lib/Connector/Sabre/FilesReportPlugin.php b/apps/dav/lib/Connector/Sabre/FilesReportPlugin.php index 4876e9ad8f3..8ae457b2e59 100644 --- a/apps/dav/lib/Connector/Sabre/FilesReportPlugin.php +++ b/apps/dav/lib/Connector/Sabre/FilesReportPlugin.php @@ -30,6 +30,7 @@ namespace OCA\DAV\Connector\Sabre; use OC\Files\View; use OCP\App\IAppManager; use OCP\Files\Folder; +use OCP\Files\Node as INode; use OCP\IGroupManager; use OCP\ITagManager; use OCP\IUserSession; @@ -45,9 +46,9 @@ use Sabre\DAV\Xml\Element\Response; use Sabre\DAV\Xml\Response\MultiStatus; class FilesReportPlugin extends ServerPlugin { - // namespace public const NS_OWNCLOUD = 'http://owncloud.org/ns'; + public const NS_NEXTCLOUD = 'http://nextcloud.org/ns'; public const REPORT_NAME = '{http://owncloud.org/ns}filter-files'; public const SYSTEMTAG_PROPERTYNAME = '{http://owncloud.org/ns}systemtag'; public const CIRCLE_PROPERTYNAME = '{http://owncloud.org/ns}circle'; @@ -186,6 +187,7 @@ class FilesReportPlugin extends ServerPlugin { } $ns = '{' . $this::NS_OWNCLOUD . '}'; + $ncns = '{' . $this::NS_NEXTCLOUD . '}'; $requestedProps = []; $filterRules = []; @@ -199,6 +201,14 @@ class FilesReportPlugin extends ServerPlugin { foreach ($reportProps['value'] as $propVal) { $requestedProps[] = $propVal['name']; } + } elseif ($name === '{DAV:}limit') { + foreach ($reportProps['value'] as $propVal) { + if ($propVal['name'] === '{DAV:}nresults') { + $limit = (int)$propVal['value']; + } elseif ($propVal['name'] === $ncns . 'firstresult') { + $offset = (int)$propVal['value']; + } + } } } @@ -209,13 +219,32 @@ class FilesReportPlugin extends ServerPlugin { // gather all file ids matching filter try { - $resultFileIds = $this->processFilterRules($filterRules); + $resultFileIds = $this->processFilterRulesForFileIDs($filterRules); + // no logic in circles and favorites for paging, we always have all results, and slice later on + $resultFileIds = array_slice($resultFileIds, $offset ?? 0, $limit ?? null); + // fetching nodes has paging on DB level – therefore we cannot mix and slice the results, similar + // to user backends. I.e. the final result may return more results than requested. + $resultNodes = $this->processFilterRulesForFileNodes($filterRules, $limit ?? null, $offset ?? null); } catch (TagNotFoundException $e) { throw new PreconditionFailed('Cannot filter by non-existing tag', 0, $e); } + $results = []; + foreach ($resultNodes as $entry) { + if ($entry) { + $results[] = $this->wrapNode($entry); + } + } + // find sabre nodes by file id, restricted to the root node path - $results = $this->findNodesByFileIds($reportTargetNode, $resultFileIds); + $additionalNodes = $this->findNodesByFileIds($reportTargetNode, $resultFileIds); + if ($additionalNodes && $results) { + $results = array_uintersect($results, $additionalNodes, function (Node $a, Node $b): int { + return $a->getId() - $b->getId(); + }); + } elseif (!$results && $additionalNodes) { + $results = $additionalNodes; + } $filesUri = $this->getFilesBaseUri($uri, $reportTargetNode->getPath()); $responses = $this->prepareResponses($filesUri, $requestedProps, $results); @@ -261,19 +290,13 @@ class FilesReportPlugin extends ServerPlugin { * * @param array $filterRules * @return array array of unique file id results - * - * @throws TagNotFoundException whenever a tag was not found */ - protected function processFilterRules($filterRules) { + protected function processFilterRulesForFileIDs(array $filterRules): array { $ns = '{' . $this::NS_OWNCLOUD . '}'; - $resultFileIds = null; - $systemTagIds = []; + $resultFileIds = []; $circlesIds = []; $favoriteFilter = null; foreach ($filterRules as $filterRule) { - if ($filterRule['name'] === $ns . 'systemtag') { - $systemTagIds[] = $filterRule['value']; - } if ($filterRule['name'] === self::CIRCLE_PROPERTYNAME) { $circlesIds[] = $filterRule['value']; } @@ -289,15 +312,6 @@ class FilesReportPlugin extends ServerPlugin { } } - if (!empty($systemTagIds)) { - $fileIds = $this->getSystemTagFileIds($systemTagIds); - if (empty($resultFileIds)) { - $resultFileIds = $fileIds; - } else { - $resultFileIds = array_intersect($fileIds, $resultFileIds); - } - } - if (!empty($circlesIds)) { $fileIds = $this->getCirclesFileIds($circlesIds); if (empty($resultFileIds)) { @@ -310,47 +324,48 @@ class FilesReportPlugin extends ServerPlugin { return $resultFileIds; } - private function getSystemTagFileIds($systemTagIds) { - $resultFileIds = null; - - // check user permissions, if applicable - if (!$this->isAdmin()) { - // check visibility/permission - $tags = $this->tagManager->getTagsByIds($systemTagIds); - $unknownTagIds = []; - foreach ($tags as $tag) { - if (!$tag->isUserVisible()) { - $unknownTagIds[] = $tag->getId(); - } - } - - if (!empty($unknownTagIds)) { - throw new TagNotFoundException('Tag with ids ' . implode(', ', $unknownTagIds) . ' not found'); + protected function processFilterRulesForFileNodes(array $filterRules, ?int $limit, ?int $offset): array { + $systemTagIds = []; + foreach ($filterRules as $filterRule) { + if ($filterRule['name'] === self::SYSTEMTAG_PROPERTYNAME) { + $systemTagIds[] = $filterRule['value']; } } - // fetch all file ids and intersect them - foreach ($systemTagIds as $systemTagId) { - $fileIds = $this->tagMapper->getObjectIdsForTags($systemTagId, 'files'); + $nodes = []; - if (empty($fileIds)) { - // This tag has no files, nothing can ever show up - return []; - } + // type check to ensure searchBySystemTag is available, it is not + // exposed in API yet + if (!empty($systemTagIds) && method_exists($this->userFolder, 'searchBySystemTag')) { + $tags = $this->tagManager->getTagsByIds($systemTagIds, $this->userSession->getUser()); - // first run ? - if ($resultFileIds === null) { - $resultFileIds = $fileIds; - } else { - $resultFileIds = array_intersect($resultFileIds, $fileIds); + // For we run DB queries per tag and require intersection, we cannot apply limit and offset for DB queries on multi tag search. + $oneTagSearch = count($tags) === 1; + $dbLimit = $oneTagSearch ? $limit ?? 0 : 0; + $dbOffset = $oneTagSearch ? $offset ?? 0 : 0; + + foreach ($tags as $tag) { + $tagName = $tag->getName(); + $tmpNodes = $this->userFolder->searchBySystemTag($tagName, $this->userSession->getUser()->getUID(), $dbLimit, $dbOffset); + if (count($nodes) === 0) { + $nodes = $tmpNodes; + } else { + $nodes = array_uintersect($nodes, $tmpNodes, function (INode $a, INode $b): int { + return $a->getId() - $b->getId(); + }); + } + if ($nodes === []) { + // there cannot be a common match when nodes are empty early. + return $nodes; + } } - if (empty($resultFileIds)) { - // Empty intersection, nothing can show up anymore - return []; + if (!$oneTagSearch && ($limit !== null || $offset !== null)) { + $nodes = array_slice($nodes, $offset, $limit); } } - return $resultFileIds; + + return $nodes; } /** @@ -406,7 +421,10 @@ class FilesReportPlugin extends ServerPlugin { * @param array $fileIds file ids * @return Node[] array of Sabre nodes */ - public function findNodesByFileIds($rootNode, $fileIds) { + public function findNodesByFileIds(Node $rootNode, array $fileIds): array { + if (empty($fileIds)) { + return []; + } $folder = $this->userFolder; if (trim($rootNode->getPath(), '/') !== '') { $folder = $folder->get($rootNode->getPath()); @@ -417,11 +435,7 @@ class FilesReportPlugin extends ServerPlugin { $entry = $folder->getById($fileId); if ($entry) { $entry = current($entry); - if ($entry instanceof \OCP\Files\File) { - $results[] = new File($this->fileView, $entry); - } elseif ($entry instanceof \OCP\Files\Folder) { - $results[] = new Directory($this->fileView, $entry); - } + $results[] = $this->wrapNode($entry); } } @@ -429,6 +443,18 @@ class FilesReportPlugin extends ServerPlugin { } /** + * @param \OCP\Files\File|\OCP\Files\Folder $node + * @return File|Directory + */ + protected function wrapNode($node): \OCA\DAV\Connector\Sabre\Node { + if ($node instanceof \OCP\Files\File) { + return new File($this->fileView, $node); + } else { + return new Directory($this->fileView, $node); + } + } + + /** * Returns whether the currently logged in user is an administrator */ private function isAdmin() { diff --git a/apps/dav/tests/unit/Connector/Sabre/FilesReportPluginTest.php b/apps/dav/tests/unit/Connector/Sabre/FilesReportPluginTest.php index f73434b33b6..db355d6500c 100644 --- a/apps/dav/tests/unit/Connector/Sabre/FilesReportPluginTest.php +++ b/apps/dav/tests/unit/Connector/Sabre/FilesReportPluginTest.php @@ -44,45 +44,50 @@ use OCP\IUserSession; use OCP\SystemTag\ISystemTag; use OCP\SystemTag\ISystemTagManager; use OCP\SystemTag\ISystemTagObjectMapper; +use OCP\SystemTag\TagNotFoundException; +use PHPUnit\Framework\MockObject\MockObject; use Sabre\DAV\INode; use Sabre\DAV\Tree; use Sabre\HTTP\ResponseInterface; class FilesReportPluginTest extends \Test\TestCase { - /** @var \Sabre\DAV\Server|\PHPUnit\Framework\MockObject\MockObject */ + /** @var \Sabre\DAV\Server|MockObject */ private $server; - /** @var \Sabre\DAV\Tree|\PHPUnit\Framework\MockObject\MockObject */ + /** @var \Sabre\DAV\Tree|MockObject */ private $tree; - /** @var ISystemTagObjectMapper|\PHPUnit\Framework\MockObject\MockObject */ + /** @var ISystemTagObjectMapper|MockObject */ private $tagMapper; - /** @var ISystemTagManager|\PHPUnit\Framework\MockObject\MockObject */ + /** @var ISystemTagManager|MockObject */ private $tagManager; - /** @var ITags|\PHPUnit\Framework\MockObject\MockObject */ + /** @var ITags|MockObject */ private $privateTags; + /** @var ITagManager|MockObject|(object&MockObject)|(ITagManager&object&MockObject)|(ITagManager&MockObject) */ + private $privateTagManager; + /** @var \OCP\IUserSession */ private $userSession; /** @var FilesReportPluginImplementation */ private $plugin; - /** @var View|\PHPUnit\Framework\MockObject\MockObject **/ + /** @var View|MockObject **/ private $view; - /** @var IGroupManager|\PHPUnit\Framework\MockObject\MockObject **/ + /** @var IGroupManager|MockObject **/ private $groupManager; - /** @var Folder|\PHPUnit\Framework\MockObject\MockObject **/ + /** @var Folder|MockObject **/ private $userFolder; - /** @var IPreview|\PHPUnit\Framework\MockObject\MockObject * */ + /** @var IPreview|MockObject * */ private $previewManager; - /** @var IAppManager|\PHPUnit\Framework\MockObject\MockObject * */ + /** @var IAppManager|MockObject * */ private $appManager; protected function setUp(): void { @@ -110,6 +115,8 @@ class FilesReportPluginTest extends \Test\TestCase { $this->userFolder = $this->getMockBuilder(Folder::class) ->disableOriginalConstructor() + ->addMethods(['searchBySystemTag']) + ->onlyMethods(get_class_methods(Folder::class)) ->getMock(); $this->previewManager = $this->getMockBuilder(IPreview::class) @@ -124,8 +131,8 @@ class FilesReportPluginTest extends \Test\TestCase { $this->tagMapper = $this->createMock(ISystemTagObjectMapper::class); $this->userSession = $this->createMock(IUserSession::class); $this->privateTags = $this->createMock(ITags::class); - $privateTagManager = $this->createMock(ITagManager::class); - $privateTagManager->expects($this->any()) + $this->privateTagManager = $this->createMock(ITagManager::class); + $this->privateTagManager->expects($this->any()) ->method('load') ->with('files') ->willReturn($this->privateTags); @@ -145,7 +152,7 @@ class FilesReportPluginTest extends \Test\TestCase { $this->view, $this->tagManager, $this->tagMapper, - $privateTagManager, + $this->privateTagManager, $this->userSession, $this->groupManager, $this->userFolder, @@ -153,7 +160,7 @@ class FilesReportPluginTest extends \Test\TestCase { ); } - public function testOnReportInvalidNode() { + public function testOnReportInvalidNode(): void { $path = 'totally/unrelated/13'; $this->tree->expects($this->any()) @@ -173,7 +180,7 @@ class FilesReportPluginTest extends \Test\TestCase { $this->assertNull($this->plugin->onReport(FilesReportPluginImplementation::REPORT_NAME, [], '/' . $path)); } - public function testOnReportInvalidReportName() { + public function testOnReportInvalidReportName(): void { $path = 'test'; $this->tree->expects($this->any()) @@ -193,7 +200,7 @@ class FilesReportPluginTest extends \Test\TestCase { $this->assertNull($this->plugin->onReport('{whoever}whatever', [], '/' . $path)); } - public function testOnReport() { + public function testOnReport(): void { $path = 'test'; $parameters = [ @@ -217,15 +224,6 @@ class FilesReportPluginTest extends \Test\TestCase { ->method('isAdmin') ->willReturn(true); - $this->tagMapper->expects($this->at(0)) - ->method('getObjectIdsForTags') - ->with('123', 'files') - ->willReturn(['111', '222']); - $this->tagMapper->expects($this->at(1)) - ->method('getObjectIdsForTags') - ->with('456', 'files') - ->willReturn(['111', '222', '333']); - $reportTargetNode = $this->getMockBuilder(Directory::class) ->disableOriginalConstructor() ->getMock(); @@ -253,21 +251,45 @@ class FilesReportPluginTest extends \Test\TestCase { ->with('/' . $path) ->willReturn($reportTargetNode); - $filesNode1 = $this->getMockBuilder(Folder::class) - ->disableOriginalConstructor() - ->getMock(); - $filesNode2 = $this->getMockBuilder(File::class) - ->disableOriginalConstructor() - ->getMock(); + $filesNode1 = $this->createMock(File::class); + $filesNode1->expects($this->any()) + ->method('getSize') + ->willReturn(12); + $filesNode2 = $this->createMock(Folder::class); + $filesNode2->expects($this->any()) + ->method('getSize') + ->willReturn(10); - $this->userFolder->expects($this->at(0)) - ->method('getById') - ->with('111') - ->willReturn([$filesNode1]); - $this->userFolder->expects($this->at(1)) - ->method('getById') - ->with('222') - ->willReturn([$filesNode2]); + $tag123 = $this->createMock(ISystemTag::class); + $tag123->expects($this->any()) + ->method('getName') + ->willReturn('OneTwoThree'); + $tag123->expects($this->any()) + ->method('isUserVisible') + ->willReturn(true); + $tag456 = $this->createMock(ISystemTag::class); + $tag456->expects($this->any()) + ->method('getName') + ->willReturn('FourFiveSix'); + $tag456->expects($this->any()) + ->method('isUserVisible') + ->willReturn(true); + + $this->tagManager->expects($this->once()) + ->method('getTagsByIds') + ->with(['123', '456']) + ->willReturn([$tag123, $tag456]); + + $this->userFolder->expects($this->exactly(2)) + ->method('searchBySystemTag') + ->withConsecutive( + ['OneTwoThree'], + ['FourFiveSix'], + ) + ->willReturnOnConsecutiveCalls( + [$filesNode1], + [$filesNode2], + ); $this->server->expects($this->any()) ->method('getRequestUri') @@ -278,7 +300,7 @@ class FilesReportPluginTest extends \Test\TestCase { $this->assertFalse($this->plugin->onReport(FilesReportPluginImplementation::REPORT_NAME, $parameters, '/' . $path)); } - public function testFindNodesByFileIdsRoot() { + public function testFindNodesByFileIdsRoot(): void { $filesNode1 = $this->getMockBuilder(Folder::class) ->disableOriginalConstructor() ->getMock(); @@ -300,16 +322,18 @@ class FilesReportPluginTest extends \Test\TestCase { ->method('getPath') ->willReturn('/'); - $this->userFolder->expects($this->at(0)) - ->method('getById') - ->with('111') - ->willReturn([$filesNode1]); - $this->userFolder->expects($this->at(1)) + $this->userFolder->expects($this->exactly(2)) ->method('getById') - ->with('222') - ->willReturn([$filesNode2]); + ->withConsecutive( + ['111'], + ['222'], + ) + ->willReturnOnConsecutiveCalls( + [$filesNode1], + [$filesNode2], + ); - /** @var \OCA\DAV\Connector\Sabre\Directory|\PHPUnit\Framework\MockObject\MockObject $reportTargetNode */ + /** @var \OCA\DAV\Connector\Sabre\Directory|MockObject $reportTargetNode */ $result = $this->plugin->findNodesByFileIds($reportTargetNode, ['111', '222']); $this->assertCount(2, $result); @@ -319,7 +343,7 @@ class FilesReportPluginTest extends \Test\TestCase { $this->assertEquals('second node', $result[1]->getName()); } - public function testFindNodesByFileIdsSubDir() { + public function testFindNodesByFileIdsSubDir(): void { $filesNode1 = $this->getMockBuilder(Folder::class) ->disableOriginalConstructor() ->getMock(); @@ -346,21 +370,23 @@ class FilesReportPluginTest extends \Test\TestCase { ->disableOriginalConstructor() ->getMock(); - $this->userFolder->expects($this->at(0)) + $this->userFolder->expects($this->once()) ->method('get') ->with('/sub1/sub2') ->willReturn($subNode); - $subNode->expects($this->at(0)) + $subNode->expects($this->exactly(2)) ->method('getById') - ->with('111') - ->willReturn([$filesNode1]); - $subNode->expects($this->at(1)) - ->method('getById') - ->with('222') - ->willReturn([$filesNode2]); + ->withConsecutive( + ['111'], + ['222'], + ) + ->willReturnOnConsecutiveCalls( + [$filesNode1], + [$filesNode2], + ); - /** @var \OCA\DAV\Connector\Sabre\Directory|\PHPUnit\Framework\MockObject\MockObject $reportTargetNode */ + /** @var \OCA\DAV\Connector\Sabre\Directory|MockObject $reportTargetNode */ $result = $this->plugin->findNodesByFileIds($reportTargetNode, ['111', '222']); $this->assertCount(2, $result); @@ -370,7 +396,7 @@ class FilesReportPluginTest extends \Test\TestCase { $this->assertEquals('second node', $result[1]->getName()); } - public function testPrepareResponses() { + public function testPrepareResponses(): void { $requestedProps = ['{DAV:}getcontentlength', '{http://owncloud.org/ns}fileid', '{DAV:}resourcetype']; $fileInfo = $this->createMock(FileInfo::class); @@ -439,116 +465,288 @@ class FilesReportPluginTest extends \Test\TestCase { $this->assertCount(0, $props2[200]['{DAV:}resourcetype']->getValue()); } - public function testProcessFilterRulesSingle() { + public function testProcessFilterRulesSingle(): void { $this->groupManager->expects($this->any()) ->method('isAdmin') ->willReturn(true); - $this->tagMapper->expects($this->exactly(1)) - ->method('getObjectIdsForTags') - ->withConsecutive( - ['123', 'files'] - ) - ->willReturnMap([ - ['123', 'files', 0, '', ['111', '222']], - ]); - $rules = [ ['name' => '{http://owncloud.org/ns}systemtag', 'value' => '123'], ]; - $this->assertEquals(['111', '222'], $this->invokePrivate($this->plugin, 'processFilterRules', [$rules])); + $filesNode1 = $this->createMock(File::class); + $filesNode1->expects($this->any()) + ->method('getSize') + ->willReturn(12); + $filesNode2 = $this->createMock(Folder::class); + $filesNode2->expects($this->any()) + ->method('getSize') + ->willReturn(10); + + $tag123 = $this->createMock(ISystemTag::class); + $tag123->expects($this->any()) + ->method('getName') + ->willReturn('OneTwoThree'); + $tag123->expects($this->any()) + ->method('isUserVisible') + ->willReturn(true); + + $this->tagManager->expects($this->once()) + ->method('getTagsByIds') + ->with(['123']) + ->willReturn([$tag123]); + + $this->userFolder->expects($this->once()) + ->method('searchBySystemTag') + ->with('OneTwoThree') + ->willReturn([$filesNode1, $filesNode2]); + + $this->assertEquals([$filesNode1, $filesNode2], $this->invokePrivate($this->plugin, 'processFilterRulesForFileNodes', [$rules, 0, 0])); } - public function testProcessFilterRulesAndCondition() { + public function testProcessFilterRulesAndCondition(): void { $this->groupManager->expects($this->any()) ->method('isAdmin') ->willReturn(true); - $this->tagMapper->expects($this->exactly(2)) - ->method('getObjectIdsForTags') + $filesNode1 = $this->createMock(File::class); + $filesNode1->expects($this->any()) + ->method('getSize') + ->willReturn(12); + $filesNode1->expects($this->any()) + ->method('getId') + ->willReturn(111); + $filesNode2 = $this->createMock(Folder::class); + $filesNode2->expects($this->any()) + ->method('getSize') + ->willReturn(10); + $filesNode2->expects($this->any()) + ->method('getId') + ->willReturn(222); + $filesNode3 = $this->createMock(File::class); + $filesNode3->expects($this->any()) + ->method('getSize') + ->willReturn(14); + $filesNode3->expects($this->any()) + ->method('getId') + ->willReturn(333); + + $tag123 = $this->createMock(ISystemTag::class); + $tag123->expects($this->any()) + ->method('getName') + ->willReturn('OneTwoThree'); + $tag123->expects($this->any()) + ->method('isUserVisible') + ->willReturn(true); + $tag456 = $this->createMock(ISystemTag::class); + $tag456->expects($this->any()) + ->method('getName') + ->willReturn('FourFiveSix'); + $tag456->expects($this->any()) + ->method('isUserVisible') + ->willReturn(true); + + $this->tagManager->expects($this->once()) + ->method('getTagsByIds') + ->with(['123', '456']) + ->willReturn([$tag123, $tag456]); + + $this->userFolder->expects($this->exactly(2)) + ->method('searchBySystemTag') ->withConsecutive( - ['123', 'files'], - ['456', 'files'] + ['OneTwoThree'], + ['FourFiveSix'], ) - ->willReturnMap([ - ['123', 'files', 0, '', ['111', '222']], - ['456', 'files', 0, '', ['222', '333']], - ]); + ->willReturnOnConsecutiveCalls( + [$filesNode1, $filesNode2], + [$filesNode2, $filesNode3], + ); $rules = [ ['name' => '{http://owncloud.org/ns}systemtag', 'value' => '123'], ['name' => '{http://owncloud.org/ns}systemtag', 'value' => '456'], ]; - $this->assertEquals(['222'], array_values($this->invokePrivate($this->plugin, 'processFilterRules', [$rules]))); + $this->assertEquals([$filesNode2], array_values($this->invokePrivate($this->plugin, 'processFilterRulesForFileNodes', [$rules, null, null]))); } - public function testProcessFilterRulesAndConditionWithOneEmptyResult() { + public function testProcessFilterRulesAndConditionWithOneEmptyResult(): void { $this->groupManager->expects($this->any()) ->method('isAdmin') ->willReturn(true); - $this->tagMapper->expects($this->exactly(2)) - ->method('getObjectIdsForTags') + $filesNode1 = $this->createMock(File::class); + $filesNode1->expects($this->any()) + ->method('getSize') + ->willReturn(12); + $filesNode1->expects($this->any()) + ->method('getId') + ->willReturn(111); + $filesNode2 = $this->createMock(Folder::class); + $filesNode2->expects($this->any()) + ->method('getSize') + ->willReturn(10); + $filesNode2->expects($this->any()) + ->method('getId') + ->willReturn(222); + + $tag123 = $this->createMock(ISystemTag::class); + $tag123->expects($this->any()) + ->method('getName') + ->willReturn('OneTwoThree'); + $tag123->expects($this->any()) + ->method('isUserVisible') + ->willReturn(true); + $tag456 = $this->createMock(ISystemTag::class); + $tag456->expects($this->any()) + ->method('getName') + ->willReturn('FourFiveSix'); + $tag456->expects($this->any()) + ->method('isUserVisible') + ->willReturn(true); + + $this->tagManager->expects($this->once()) + ->method('getTagsByIds') + ->with(['123', '456']) + ->willReturn([$tag123, $tag456]); + + $this->userFolder->expects($this->exactly(2)) + ->method('searchBySystemTag') ->withConsecutive( - ['123', 'files'], - ['456', 'files'] + ['OneTwoThree'], + ['FourFiveSix'], ) - ->willReturnMap([ - ['123', 'files', 0, '', ['111', '222']], - ['456', 'files', 0, '', []], - ]); + ->willReturnOnConsecutiveCalls( + [$filesNode1, $filesNode2], + [], + ); $rules = [ ['name' => '{http://owncloud.org/ns}systemtag', 'value' => '123'], ['name' => '{http://owncloud.org/ns}systemtag', 'value' => '456'], ]; - $this->assertEquals([], array_values($this->invokePrivate($this->plugin, 'processFilterRules', [$rules]))); + $this->assertEquals([], $this->invokePrivate($this->plugin, 'processFilterRulesForFileNodes', [$rules, null, null])); } - public function testProcessFilterRulesAndConditionWithFirstEmptyResult() { + public function testProcessFilterRulesAndConditionWithFirstEmptyResult(): void { $this->groupManager->expects($this->any()) ->method('isAdmin') ->willReturn(true); - $this->tagMapper->expects($this->exactly(1)) - ->method('getObjectIdsForTags') - ->withConsecutive( - ['123', 'files'], - ['456', 'files'] - ) - ->willReturnMap([ - ['123', 'files', 0, '', []], - ['456', 'files', 0, '', ['111', '222']], - ]); + $filesNode1 = $this->createMock(File::class); + $filesNode1->expects($this->any()) + ->method('getSize') + ->willReturn(12); + $filesNode1->expects($this->any()) + ->method('getId') + ->willReturn(111); + $filesNode2 = $this->createMock(Folder::class); + $filesNode2->expects($this->any()) + ->method('getSize') + ->willReturn(10); + $filesNode2->expects($this->any()) + ->method('getId') + ->willReturn(222); + + $tag123 = $this->createMock(ISystemTag::class); + $tag123->expects($this->any()) + ->method('getName') + ->willReturn('OneTwoThree'); + $tag123->expects($this->any()) + ->method('isUserVisible') + ->willReturn(true); + $tag456 = $this->createMock(ISystemTag::class); + $tag456->expects($this->any()) + ->method('getName') + ->willReturn('FourFiveSix'); + $tag456->expects($this->any()) + ->method('isUserVisible') + ->willReturn(true); + + $this->tagManager->expects($this->once()) + ->method('getTagsByIds') + ->with(['123', '456']) + ->willReturn([$tag123, $tag456]); + + $this->userFolder->expects($this->once()) + ->method('searchBySystemTag') + ->with('OneTwoThree') + ->willReturnOnConsecutiveCalls( + [], + [$filesNode1, $filesNode2], + ); $rules = [ ['name' => '{http://owncloud.org/ns}systemtag', 'value' => '123'], ['name' => '{http://owncloud.org/ns}systemtag', 'value' => '456'], ]; - $this->assertEquals([], array_values($this->invokePrivate($this->plugin, 'processFilterRules', [$rules]))); + $this->assertEquals([], $this->invokePrivate($this->plugin, 'processFilterRulesForFileNodes', [$rules, null, null])); } - public function testProcessFilterRulesAndConditionWithEmptyMidResult() { + public function testProcessFilterRulesAndConditionWithEmptyMidResult(): void { $this->groupManager->expects($this->any()) ->method('isAdmin') ->willReturn(true); - $this->tagMapper->expects($this->exactly(2)) - ->method('getObjectIdsForTags') - ->withConsecutive( - ['123', 'files'], - ['456', 'files'], - ['789', 'files'] - ) - ->willReturnMap([ - ['123', 'files', 0, '', ['111', '222']], - ['456', 'files', 0, '', ['333']], - ['789', 'files', 0, '', ['111', '222']], - ]); + $filesNode1 = $this->createMock(File::class); + $filesNode1->expects($this->any()) + ->method('getSize') + ->willReturn(12); + $filesNode1->expects($this->any()) + ->method('getId') + ->willReturn(111); + $filesNode2 = $this->createMock(Folder::class); + $filesNode2->expects($this->any()) + ->method('getSize') + ->willReturn(10); + $filesNode2->expects($this->any()) + ->method('getId') + ->willReturn(222); + $filesNode3 = $this->createMock(Folder::class); + $filesNode3->expects($this->any()) + ->method('getSize') + ->willReturn(13); + $filesNode3->expects($this->any()) + ->method('getId') + ->willReturn(333); + + $tag123 = $this->createMock(ISystemTag::class); + $tag123->expects($this->any()) + ->method('getName') + ->willReturn('OneTwoThree'); + $tag123->expects($this->any()) + ->method('isUserVisible') + ->willReturn(true); + $tag456 = $this->createMock(ISystemTag::class); + $tag456->expects($this->any()) + ->method('getName') + ->willReturn('FourFiveSix'); + $tag456->expects($this->any()) + ->method('isUserVisible') + ->willReturn(true); + $tag789 = $this->createMock(ISystemTag::class); + $tag789->expects($this->any()) + ->method('getName') + ->willReturn('SevenEightNein'); + $tag789->expects($this->any()) + ->method('isUserVisible') + ->willReturn(true); + + $this->tagManager->expects($this->once()) + ->method('getTagsByIds') + ->with(['123', '456', '789']) + ->willReturn([$tag123, $tag456, $tag789]); + + $this->userFolder->expects($this->exactly(2)) + ->method('searchBySystemTag') + ->withConsecutive(['OneTwoThree'], ['FourFiveSix'], ['SevenEightNein']) + ->willReturnOnConsecutiveCalls( + [$filesNode1, $filesNode2], + [$filesNode3], + [$filesNode1, $filesNode2], + ); $rules = [ ['name' => '{http://owncloud.org/ns}systemtag', 'value' => '123'], @@ -556,144 +754,188 @@ class FilesReportPluginTest extends \Test\TestCase { ['name' => '{http://owncloud.org/ns}systemtag', 'value' => '789'], ]; - $this->assertEquals([], array_values($this->invokePrivate($this->plugin, 'processFilterRules', [$rules]))); + $this->assertEquals([], array_values($this->invokePrivate($this->plugin, 'processFilterRulesForFileNodes', [$rules, null, null]))); } - public function testProcessFilterRulesInvisibleTagAsAdmin() { + public function testProcessFilterRulesInvisibleTagAsAdmin(): void { $this->groupManager->expects($this->any()) ->method('isAdmin') ->willReturn(true); - $tag1 = $this->getMockBuilder(ISystemTag::class) - ->disableOriginalConstructor() - ->getMock(); - $tag1->expects($this->any()) + $filesNode1 = $this->createMock(File::class); + $filesNode1->expects($this->any()) + ->method('getSize') + ->willReturn(12); + $filesNode1->expects($this->any()) ->method('getId') - ->willReturn('123'); - $tag1->expects($this->any()) + ->willReturn(111); + $filesNode2 = $this->createMock(Folder::class); + $filesNode2->expects($this->any()) + ->method('getSize') + ->willReturn(10); + $filesNode2->expects($this->any()) + ->method('getId') + ->willReturn(222); + $filesNode3 = $this->createMock(Folder::class); + $filesNode3->expects($this->any()) + ->method('getSize') + ->willReturn(13); + $filesNode3->expects($this->any()) + ->method('getId') + ->willReturn(333); + + $tag123 = $this->createMock(ISystemTag::class); + $tag123->expects($this->any()) + ->method('getName') + ->willReturn('OneTwoThree'); + $tag123->expects($this->any()) ->method('isUserVisible') ->willReturn(true); - - $tag2 = $this->getMockBuilder(ISystemTag::class) - ->disableOriginalConstructor() - ->getMock(); - $tag2->expects($this->any()) - ->method('getId') - ->willReturn('123'); - $tag2->expects($this->any()) + $tag456 = $this->createMock(ISystemTag::class); + $tag456->expects($this->any()) + ->method('getName') + ->willReturn('FourFiveSix'); + $tag456->expects($this->any()) ->method('isUserVisible') ->willReturn(false); - // no need to fetch tags to check permissions - $this->tagManager->expects($this->never()) - ->method('getTagsByIds'); - - $this->tagMapper->expects($this->at(0)) - ->method('getObjectIdsForTags') - ->with('123') - ->willReturn(['111', '222']); - $this->tagMapper->expects($this->at(1)) - ->method('getObjectIdsForTags') - ->with('456') - ->willReturn(['222', '333']); + $this->tagManager->expects($this->once()) + ->method('getTagsByIds') + ->with(['123', '456']) + ->willReturn([$tag123, $tag456]); + + $this->userFolder->expects($this->exactly(2)) + ->method('searchBySystemTag') + ->withConsecutive(['OneTwoThree'], ['FourFiveSix']) + ->willReturnOnConsecutiveCalls( + [$filesNode1, $filesNode2], + [$filesNode2, $filesNode3], + ); $rules = [ ['name' => '{http://owncloud.org/ns}systemtag', 'value' => '123'], ['name' => '{http://owncloud.org/ns}systemtag', 'value' => '456'], ]; - $this->assertEquals(['222'], array_values($this->invokePrivate($this->plugin, 'processFilterRules', [$rules]))); + $this->assertEquals([$filesNode2], array_values($this->invokePrivate($this->plugin, 'processFilterRulesForFileNodes', [$rules, null, null]))); } - public function testProcessFilterRulesInvisibleTagAsUser() { - $this->expectException(\OCP\SystemTag\TagNotFoundException::class); + public function testProcessFilterRulesInvisibleTagAsUser(): void { + $this->expectException(TagNotFoundException::class); $this->groupManager->expects($this->any()) ->method('isAdmin') ->willReturn(false); - $tag1 = $this->getMockBuilder(ISystemTag::class) - ->disableOriginalConstructor() - ->getMock(); - $tag1->expects($this->any()) - ->method('getId') - ->willReturn('123'); - $tag1->expects($this->any()) + $tag123 = $this->createMock(ISystemTag::class); + $tag123->expects($this->any()) + ->method('getName') + ->willReturn('OneTwoThree'); + $tag123->expects($this->any()) ->method('isUserVisible') ->willReturn(true); - - $tag2 = $this->getMockBuilder(ISystemTag::class) - ->disableOriginalConstructor() - ->getMock(); - $tag2->expects($this->any()) - ->method('getId') - ->willReturn('123'); - $tag2->expects($this->any()) + $tag456 = $this->createMock(ISystemTag::class); + $tag456->expects($this->any()) + ->method('getName') + ->willReturn('FourFiveSix'); + $tag456->expects($this->any()) ->method('isUserVisible') - ->willReturn(false); // invisible + ->willReturn(false); $this->tagManager->expects($this->once()) ->method('getTagsByIds') ->with(['123', '456']) - ->willReturn([$tag1, $tag2]); + ->willThrowException(new TagNotFoundException()); + + $this->userFolder->expects($this->never()) + ->method('searchBySystemTag'); $rules = [ ['name' => '{http://owncloud.org/ns}systemtag', 'value' => '123'], ['name' => '{http://owncloud.org/ns}systemtag', 'value' => '456'], ]; - $this->invokePrivate($this->plugin, 'processFilterRules', [$rules]); + $this->invokePrivate($this->plugin, 'processFilterRulesForFileNodes', [$rules, null, null]); } - public function testProcessFilterRulesVisibleTagAsUser() { + public function testProcessFilterRulesVisibleTagAsUser(): void { $this->groupManager->expects($this->any()) ->method('isAdmin') ->willReturn(false); - $tag1 = $this->getMockBuilder(ISystemTag::class) - ->disableOriginalConstructor() - ->getMock(); + $tag1 = $this->createMock(ISystemTag::class); $tag1->expects($this->any()) ->method('getId') ->willReturn('123'); $tag1->expects($this->any()) ->method('isUserVisible') ->willReturn(true); + $tag1->expects($this->any()) + ->method('getName') + ->willReturn('OneTwoThree'); - $tag2 = $this->getMockBuilder(ISystemTag::class) - ->disableOriginalConstructor() - ->getMock(); + $tag2 = $this->createMock(ISystemTag::class); $tag2->expects($this->any()) ->method('getId') ->willReturn('123'); $tag2->expects($this->any()) ->method('isUserVisible') ->willReturn(true); + $tag2->expects($this->any()) + ->method('getName') + ->willReturn('FourFiveSix'); $this->tagManager->expects($this->once()) ->method('getTagsByIds') ->with(['123', '456']) ->willReturn([$tag1, $tag2]); - $this->tagMapper->expects($this->at(0)) - ->method('getObjectIdsForTags') - ->with('123') - ->willReturn(['111', '222']); - $this->tagMapper->expects($this->at(1)) - ->method('getObjectIdsForTags') - ->with('456') - ->willReturn(['222', '333']); + $filesNode1 = $this->createMock(File::class); + $filesNode1->expects($this->any()) + ->method('getId') + ->willReturn(111); + $filesNode1->expects($this->any()) + ->method('getSize') + ->willReturn(12); + $filesNode2 = $this->createMock(Folder::class); + $filesNode2->expects($this->any()) + ->method('getId') + ->willReturn(222); + $filesNode2->expects($this->any()) + ->method('getSize') + ->willReturn(10); + $filesNode3 = $this->createMock(Folder::class); + $filesNode3->expects($this->any()) + ->method('getId') + ->willReturn(333); + $filesNode3->expects($this->any()) + ->method('getSize') + ->willReturn(33); + + $this->tagManager->expects($this->once()) + ->method('getTagsByIds') + ->with(['123', '456']) + ->willReturn([$tag1, $tag2]); + + // main assertion: only user visible tags are being passed through. + $this->userFolder->expects($this->exactly(2)) + ->method('searchBySystemTag') + ->withConsecutive(['OneTwoThree'], ['FourFiveSix']) + ->willReturnOnConsecutiveCalls( + [$filesNode1, $filesNode2], + [$filesNode2, $filesNode3], + ); $rules = [ ['name' => '{http://owncloud.org/ns}systemtag', 'value' => '123'], ['name' => '{http://owncloud.org/ns}systemtag', 'value' => '456'], ]; - $this->assertEquals(['222'], array_values($this->invokePrivate($this->plugin, 'processFilterRules', [$rules]))); + $this->assertEquals([$filesNode2], array_values($this->invokePrivate($this->plugin, 'processFilterRulesForFileNodes', [$rules, null, null]))); } - public function testProcessFavoriteFilter() { + public function testProcessFavoriteFilter(): void { $rules = [ ['name' => '{http://owncloud.org/ns}favorite', 'value' => '1'], ]; @@ -702,7 +944,7 @@ class FilesReportPluginTest extends \Test\TestCase { ->method('getFavorites') ->willReturn(['456', '789']); - $this->assertEquals(['456', '789'], array_values($this->invokePrivate($this->plugin, 'processFilterRules', [$rules]))); + $this->assertEquals(['456', '789'], array_values($this->invokePrivate($this->plugin, 'processFilterRulesForFileIDs', [$rules]))); } public function filesBaseUriProvider() { @@ -718,7 +960,7 @@ class FilesReportPluginTest extends \Test\TestCase { /** * @dataProvider filesBaseUriProvider */ - public function testFilesBaseUri($uri, $reportPath, $expectedUri) { + public function testFilesBaseUri($uri, $reportPath, $expectedUri): void { $this->assertEquals($expectedUri, $this->invokePrivate($this->plugin, 'getFilesBaseUri', [$uri, $reportPath])); } } |