123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923 |
- <?php
- /**
- * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
- * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
- * SPDX-License-Identifier: AGPL-3.0-only
- */
- namespace OCA\DAV\Tests\unit\Connector\Sabre;
- use OC\Files\View;
- use OCA\DAV\Connector\Sabre\Directory;
- use OCA\DAV\Connector\Sabre\FilesPlugin;
- use OCA\DAV\Connector\Sabre\FilesReportPlugin as FilesReportPluginImplementation;
- use OCP\App\IAppManager;
- use OCP\Files\File;
- use OCP\Files\FileInfo;
- use OCP\Files\Folder;
- use OCP\Files\IFilenameValidator;
- use OCP\IConfig;
- use OCP\IGroupManager;
- use OCP\IPreview;
- use OCP\IRequest;
- use OCP\ITagManager;
- use OCP\ITags;
- use OCP\IUser;
- 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 {
- private \Sabre\DAV\Server&MockObject $server;
- private Tree&MockObject $tree;
- private ISystemTagObjectMapper&MockObject $tagMapper;
- private ISystemTagManager&MockObject $tagManager;
- private ITags&MockObject $privateTags;
- private ITagManager&MockObject $privateTagManager;
- private IUserSession&MockObject $userSession;
- private FilesReportPluginImplementation $plugin;
- private View&MockObject $view;
- private IGroupManager&MockObject $groupManager;
- private Folder&MockObject $userFolder;
- private IPreview&MockObject $previewManager;
- private IAppManager&MockObject $appManager;
- protected function setUp(): void {
- parent::setUp();
- $this->tree = $this->getMockBuilder(Tree::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->view = $this->getMockBuilder(View::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->server = $this->getMockBuilder('\Sabre\DAV\Server')
- ->setConstructorArgs([$this->tree])
- ->onlyMethods(['getRequestUri', 'getBaseUri'])
- ->getMock();
- $this->server->expects($this->any())
- ->method('getBaseUri')
- ->willReturn('http://example.com/owncloud/remote.php/dav');
- $this->groupManager = $this->getMockBuilder(IGroupManager::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->userFolder = $this->getMockBuilder(Folder::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->previewManager = $this->getMockBuilder(IPreview::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->appManager = $this->getMockBuilder(IAppManager::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->tagManager = $this->createMock(ISystemTagManager::class);
- $this->tagMapper = $this->createMock(ISystemTagObjectMapper::class);
- $this->userSession = $this->createMock(IUserSession::class);
- $this->privateTags = $this->createMock(ITags::class);
- $this->privateTagManager = $this->createMock(ITagManager::class);
- $this->privateTagManager->expects($this->any())
- ->method('load')
- ->with('files')
- ->willReturn($this->privateTags);
- $user = $this->getMockBuilder(IUser::class)
- ->disableOriginalConstructor()
- ->getMock();
- $user->expects($this->any())
- ->method('getUID')
- ->willReturn('testuser');
- $this->userSession->expects($this->any())
- ->method('getUser')
- ->willReturn($user);
- $this->plugin = new FilesReportPluginImplementation(
- $this->tree,
- $this->view,
- $this->tagManager,
- $this->tagMapper,
- $this->privateTagManager,
- $this->userSession,
- $this->groupManager,
- $this->userFolder,
- $this->appManager
- );
- }
- public function testOnReportInvalidNode(): void {
- $path = 'totally/unrelated/13';
- $this->tree->expects($this->any())
- ->method('getNodeForPath')
- ->with('/' . $path)
- ->willReturn(
- $this->getMockBuilder(INode::class)
- ->disableOriginalConstructor()
- ->getMock()
- );
- $this->server->expects($this->any())
- ->method('getRequestUri')
- ->willReturn($path);
- $this->plugin->initialize($this->server);
- $this->assertNull($this->plugin->onReport(FilesReportPluginImplementation::REPORT_NAME, [], '/' . $path));
- }
- public function testOnReportInvalidReportName(): void {
- $path = 'test';
- $this->tree->expects($this->any())
- ->method('getNodeForPath')
- ->with('/' . $path)
- ->willReturn(
- $this->getMockBuilder(INode::class)
- ->disableOriginalConstructor()
- ->getMock()
- );
- $this->server->expects($this->any())
- ->method('getRequestUri')
- ->willReturn($path);
- $this->plugin->initialize($this->server);
- $this->assertNull($this->plugin->onReport('{whoever}whatever', [], '/' . $path));
- }
- public function testOnReport(): void {
- $path = 'test';
- $parameters = [
- [
- 'name' => '{DAV:}prop',
- 'value' => [
- ['name' => '{DAV:}getcontentlength', 'value' => ''],
- ['name' => '{http://owncloud.org/ns}size', 'value' => ''],
- ],
- ],
- [
- 'name' => '{http://owncloud.org/ns}filter-rules',
- 'value' => [
- ['name' => '{http://owncloud.org/ns}systemtag', 'value' => '123'],
- ['name' => '{http://owncloud.org/ns}systemtag', 'value' => '456'],
- ],
- ],
- ];
- $this->groupManager->expects($this->any())
- ->method('isAdmin')
- ->willReturn(true);
- $reportTargetNode = $this->getMockBuilder(Directory::class)
- ->disableOriginalConstructor()
- ->getMock();
- $reportTargetNode->expects($this->any())
- ->method('getPath')
- ->willReturn('');
- $response = $this->getMockBuilder(ResponseInterface::class)
- ->disableOriginalConstructor()
- ->getMock();
- $response->expects($this->once())
- ->method('setHeader')
- ->with('Content-Type', 'application/xml; charset=utf-8');
- $response->expects($this->once())
- ->method('setStatus')
- ->with(207);
- $response->expects($this->once())
- ->method('setBody');
- $this->tree->expects($this->any())
- ->method('getNodeForPath')
- ->with('/' . $path)
- ->willReturn($reportTargetNode);
- $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);
- $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')
- ->willReturn($path);
- $this->server->httpResponse = $response;
- $this->plugin->initialize($this->server);
- $this->assertFalse($this->plugin->onReport(FilesReportPluginImplementation::REPORT_NAME, $parameters, '/' . $path));
- }
- public function testFindNodesByFileIdsRoot(): void {
- $filesNode1 = $this->getMockBuilder(Folder::class)
- ->disableOriginalConstructor()
- ->getMock();
- $filesNode1->expects($this->once())
- ->method('getName')
- ->willReturn('first node');
- $filesNode2 = $this->getMockBuilder(File::class)
- ->disableOriginalConstructor()
- ->getMock();
- $filesNode2->expects($this->once())
- ->method('getName')
- ->willReturn('second node');
- $reportTargetNode = $this->getMockBuilder(Directory::class)
- ->disableOriginalConstructor()
- ->getMock();
- $reportTargetNode->expects($this->any())
- ->method('getPath')
- ->willReturn('/');
- $this->userFolder->expects($this->exactly(2))
- ->method('getFirstNodeById')
- ->withConsecutive(
- ['111'],
- ['222'],
- )
- ->willReturnOnConsecutiveCalls(
- $filesNode1,
- $filesNode2,
- );
- /** @var Directory&MockObject $reportTargetNode */
- $result = $this->plugin->findNodesByFileIds($reportTargetNode, ['111', '222']);
- $this->assertCount(2, $result);
- $this->assertInstanceOf('\OCA\DAV\Connector\Sabre\Directory', $result[0]);
- $this->assertEquals('first node', $result[0]->getName());
- $this->assertInstanceOf('\OCA\DAV\Connector\Sabre\File', $result[1]);
- $this->assertEquals('second node', $result[1]->getName());
- }
- public function testFindNodesByFileIdsSubDir(): void {
- $filesNode1 = $this->getMockBuilder(Folder::class)
- ->disableOriginalConstructor()
- ->getMock();
- $filesNode1->expects($this->once())
- ->method('getName')
- ->willReturn('first node');
- $filesNode2 = $this->getMockBuilder(File::class)
- ->disableOriginalConstructor()
- ->getMock();
- $filesNode2->expects($this->once())
- ->method('getName')
- ->willReturn('second node');
- $reportTargetNode = $this->getMockBuilder(Directory::class)
- ->disableOriginalConstructor()
- ->getMock();
- $reportTargetNode->expects($this->any())
- ->method('getPath')
- ->willReturn('/sub1/sub2');
- $subNode = $this->getMockBuilder(Folder::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->userFolder->expects($this->once())
- ->method('get')
- ->with('/sub1/sub2')
- ->willReturn($subNode);
- $subNode->expects($this->exactly(2))
- ->method('getFirstNodeById')
- ->withConsecutive(
- ['111'],
- ['222'],
- )
- ->willReturnOnConsecutiveCalls(
- $filesNode1,
- $filesNode2,
- );
- /** @var Directory&MockObject $reportTargetNode */
- $result = $this->plugin->findNodesByFileIds($reportTargetNode, ['111', '222']);
- $this->assertCount(2, $result);
- $this->assertInstanceOf('\OCA\DAV\Connector\Sabre\Directory', $result[0]);
- $this->assertEquals('first node', $result[0]->getName());
- $this->assertInstanceOf('\OCA\DAV\Connector\Sabre\File', $result[1]);
- $this->assertEquals('second node', $result[1]->getName());
- }
- public function testPrepareResponses(): void {
- $requestedProps = ['{DAV:}getcontentlength', '{http://owncloud.org/ns}fileid', '{DAV:}resourcetype'];
- $fileInfo = $this->createMock(FileInfo::class);
- $fileInfo->method('isReadable')->willReturn(true);
- $node1 = $this->getMockBuilder(Directory::class)
- ->disableOriginalConstructor()
- ->getMock();
- $node2 = $this->getMockBuilder(\OCA\DAV\Connector\Sabre\File::class)
- ->disableOriginalConstructor()
- ->getMock();
- $node1->expects($this->once())
- ->method('getInternalFileId')
- ->willReturn('111');
- $node1->expects($this->any())
- ->method('getPath')
- ->willReturn('/node1');
- $node1->method('getFileInfo')->willReturn($fileInfo);
- $node2->expects($this->once())
- ->method('getInternalFileId')
- ->willReturn('222');
- $node2->expects($this->once())
- ->method('getSize')
- ->willReturn(1024);
- $node2->expects($this->any())
- ->method('getPath')
- ->willReturn('/sub/node2');
- $node2->method('getFileInfo')->willReturn($fileInfo);
- $config = $this->getMockBuilder(IConfig::class)
- ->disableOriginalConstructor()
- ->getMock();
- $validator = $this->createMock(IFilenameValidator::class);
- $this->server->addPlugin(
- new FilesPlugin(
- $this->tree,
- $config,
- $this->createMock(IRequest::class),
- $this->previewManager,
- $this->createMock(IUserSession::class),
- $validator,
- )
- );
- $this->plugin->initialize($this->server);
- $responses = $this->plugin->prepareResponses('/files/username', $requestedProps, [$node1, $node2]);
- $this->assertCount(2, $responses);
- $this->assertEquals('http://example.com/owncloud/remote.php/dav/files/username/node1', $responses[0]->getHref());
- $this->assertEquals('http://example.com/owncloud/remote.php/dav/files/username/sub/node2', $responses[1]->getHref());
- $props1 = $responses[0]->getResponseProperties();
- $this->assertEquals('111', $props1[200]['{http://owncloud.org/ns}fileid']);
- $this->assertNull($props1[404]['{DAV:}getcontentlength']);
- $this->assertInstanceOf('\Sabre\DAV\Xml\Property\ResourceType', $props1[200]['{DAV:}resourcetype']);
- $resourceType1 = $props1[200]['{DAV:}resourcetype']->getValue();
- $this->assertEquals('{DAV:}collection', $resourceType1[0]);
- $props2 = $responses[1]->getResponseProperties();
- $this->assertEquals('1024', $props2[200]['{DAV:}getcontentlength']);
- $this->assertEquals('222', $props2[200]['{http://owncloud.org/ns}fileid']);
- $this->assertInstanceOf('\Sabre\DAV\Xml\Property\ResourceType', $props2[200]['{DAV:}resourcetype']);
- $this->assertCount(0, $props2[200]['{DAV:}resourcetype']->getValue());
- }
- public function testProcessFilterRulesSingle(): void {
- $this->groupManager->expects($this->any())
- ->method('isAdmin')
- ->willReturn(true);
- $rules = [
- ['name' => '{http://owncloud.org/ns}systemtag', 'value' => '123'],
- ];
- $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(): void {
- $this->groupManager->expects($this->any())
- ->method('isAdmin')
- ->willReturn(true);
- $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(
- ['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([$filesNode2], array_values($this->invokePrivate($this->plugin, 'processFilterRulesForFileNodes', [$rules, null, null])));
- }
- public function testProcessFilterRulesAndConditionWithOneEmptyResult(): void {
- $this->groupManager->expects($this->any())
- ->method('isAdmin')
- ->willReturn(true);
- $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(
- ['OneTwoThree'],
- ['FourFiveSix'],
- )
- ->willReturnOnConsecutiveCalls(
- [$filesNode1, $filesNode2],
- [],
- );
- $rules = [
- ['name' => '{http://owncloud.org/ns}systemtag', 'value' => '123'],
- ['name' => '{http://owncloud.org/ns}systemtag', 'value' => '456'],
- ];
- $this->assertEquals([], $this->invokePrivate($this->plugin, 'processFilterRulesForFileNodes', [$rules, null, null]));
- }
- public function testProcessFilterRulesAndConditionWithFirstEmptyResult(): void {
- $this->groupManager->expects($this->any())
- ->method('isAdmin')
- ->willReturn(true);
- $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([], $this->invokePrivate($this->plugin, 'processFilterRulesForFileNodes', [$rules, null, null]));
- }
- public function testProcessFilterRulesAndConditionWithEmptyMidResult(): void {
- $this->groupManager->expects($this->any())
- ->method('isAdmin')
- ->willReturn(true);
- $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'],
- ['name' => '{http://owncloud.org/ns}systemtag', 'value' => '456'],
- ['name' => '{http://owncloud.org/ns}systemtag', 'value' => '789'],
- ];
- $this->assertEquals([], array_values($this->invokePrivate($this->plugin, 'processFilterRulesForFileNodes', [$rules, null, null])));
- }
- public function testProcessFilterRulesInvisibleTagAsAdmin(): void {
- $this->groupManager->expects($this->any())
- ->method('isAdmin')
- ->willReturn(true);
- $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(false);
- $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([$filesNode2], array_values($this->invokePrivate($this->plugin, 'processFilterRulesForFileNodes', [$rules, null, null])));
- }
- public function testProcessFilterRulesInvisibleTagAsUser(): void {
- $this->expectException(TagNotFoundException::class);
- $this->groupManager->expects($this->any())
- ->method('isAdmin')
- ->willReturn(false);
- $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(false);
- $this->tagManager->expects($this->once())
- ->method('getTagsByIds')
- ->with(['123', '456'])
- ->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, 'processFilterRulesForFileNodes', [$rules, null, null]);
- }
- public function testProcessFilterRulesVisibleTagAsUser(): void {
- $this->groupManager->expects($this->any())
- ->method('isAdmin')
- ->willReturn(false);
- $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->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]);
- $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([$filesNode2], array_values($this->invokePrivate($this->plugin, 'processFilterRulesForFileNodes', [$rules, null, null])));
- }
- public function testProcessFavoriteFilter(): void {
- $rules = [
- ['name' => '{http://owncloud.org/ns}favorite', 'value' => '1'],
- ];
- $this->privateTags->expects($this->once())
- ->method('getFavorites')
- ->willReturn(['456', '789']);
- $this->assertEquals(['456', '789'], array_values($this->invokePrivate($this->plugin, 'processFilterRulesForFileIDs', [$rules])));
- }
- public function filesBaseUriProvider() {
- return [
- ['', '', ''],
- ['files/username', '', '/files/username'],
- ['files/username/test', '/test', '/files/username'],
- ['files/username/test/sub', '/test/sub', '/files/username'],
- ['test', '/test', ''],
- ];
- }
- /**
- * @dataProvider filesBaseUriProvider
- */
- public function testFilesBaseUri($uri, $reportPath, $expectedUri): void {
- $this->assertEquals($expectedUri, $this->invokePrivate($this->plugin, 'getFilesBaseUri', [$uri, $reportPath]));
- }
- }
|