diff options
Diffstat (limited to 'apps/dav/tests/unit/Files/FileSearchBackendTest.php')
-rw-r--r-- | apps/dav/tests/unit/Files/FileSearchBackendTest.php | 324 |
1 files changed, 219 insertions, 105 deletions
diff --git a/apps/dav/tests/unit/Files/FileSearchBackendTest.php b/apps/dav/tests/unit/Files/FileSearchBackendTest.php index 14df99a278a..c6d6f85347b 100644 --- a/apps/dav/tests/unit/Files/FileSearchBackendTest.php +++ b/apps/dav/tests/unit/Files/FileSearchBackendTest.php @@ -1,75 +1,52 @@ <?php + +declare(strict_types=1); /** - * @copyright Copyright (c) 2017 Robin Appelman <robin@icewind.nl> - * - * @author Morris Jobke <hey@morrisjobke.de> - * @author Robin Appelman <robin@icewind.nl> - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * + * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ -namespace OCA\DAV\Tests\Files; +namespace OCA\DAV\Tests\unit\Files; use OC\Files\Search\SearchComparison; use OC\Files\Search\SearchQuery; use OC\Files\View; -use OCA\DAV\Connector\Sabre\CachingTree; use OCA\DAV\Connector\Sabre\Directory; use OCA\DAV\Connector\Sabre\File; use OCA\DAV\Connector\Sabre\FilesPlugin; +use OCA\DAV\Connector\Sabre\ObjectTree; +use OCA\DAV\Connector\Sabre\Server; use OCA\DAV\Files\FileSearchBackend; use OCP\Files\FileInfo; use OCP\Files\Folder; use OCP\Files\IRootFolder; +use OCP\Files\Search\ISearchBinaryOperator; use OCP\Files\Search\ISearchComparison; +use OCP\Files\Search\ISearchQuery; +use OCP\FilesMetadata\IFilesMetadataManager; use OCP\IUser; use OCP\Share\IManager; -use SearchDAV\XML\BasicSearch; -use SearchDAV\XML\Literal; -use SearchDAV\XML\Operator; -use SearchDAV\XML\Scope; +use PHPUnit\Framework\MockObject\MockObject; +use SearchDAV\Backend\SearchPropertyDefinition; +use SearchDAV\Query\Limit; +use SearchDAV\Query\Literal; +use SearchDAV\Query\Operator; +use SearchDAV\Query\Query; +use SearchDAV\Query\Scope; use Test\TestCase; class FileSearchBackendTest extends TestCase { - /** @var CachingTree|\PHPUnit_Framework_MockObject_MockObject */ - private $tree; - - /** @var IUser */ - private $user; - - /** @var IRootFolder|\PHPUnit_Framework_MockObject_MockObject */ - private $rootFolder; - - /** @var IManager|\PHPUnit_Framework_MockObject_MockObject */ - private $shareManager; - - /** @var View|\PHPUnit_Framework_MockObject_MockObject */ - private $view; - - /** @var Folder|\PHPUnit_Framework_MockObject_MockObject */ - private $searchFolder; - - /** @var FileSearchBackend */ - private $search; - - /** @var Directory|\PHPUnit_Framework_MockObject_MockObject */ - private $davFolder; - - protected function setUp() { + private ObjectTree&MockObject $tree; + private Server&MockObject $server; + private IUser&MockObject $user; + private IRootFolder&MockObject $rootFolder; + private IManager&MockObject $shareManager; + private View&MockObject $view; + private Folder&MockObject $searchFolder; + private Directory&MockObject $davFolder; + private FileSearchBackend $search; + + protected function setUp(): void { parent::setUp(); $this->user = $this->createMock(IUser::class); @@ -77,28 +54,23 @@ class FileSearchBackendTest extends TestCase { ->method('getUID') ->willReturn('test'); - $this->tree = $this->getMockBuilder(CachingTree::class) - ->disableOriginalConstructor() - ->getMock(); - - $this->view = $this->getMockBuilder(View::class) - ->disableOriginalConstructor() - ->getMock(); - - $this->view->expects($this->any()) - ->method('getRelativePath') - ->willReturnArgument(0); - + $this->tree = $this->createMock(ObjectTree::class); + $this->server = $this->createMock(Server::class); + $this->view = $this->createMock(View::class); $this->rootFolder = $this->createMock(IRootFolder::class); - $this->shareManager = $this->createMock(IManager::class); - $this->searchFolder = $this->createMock(Folder::class); - $fileInfo = $this->createMock(FileInfo::class); - $this->davFolder = $this->createMock(Directory::class); + $this->view->expects($this->any()) + ->method('getRoot') + ->willReturn(''); + + $this->view->expects($this->any()) + ->method('getRelativePath') + ->willReturnArgument(0); + $this->davFolder->expects($this->any()) ->method('getFileInfo') ->willReturn($fileInfo); @@ -107,10 +79,12 @@ class FileSearchBackendTest extends TestCase { ->method('get') ->willReturn($this->searchFolder); - $this->search = new FileSearchBackend($this->tree, $this->user, $this->rootFolder, $this->shareManager, $this->view); + $filesMetadataManager = $this->createMock(IFilesMetadataManager::class); + + $this->search = new FileSearchBackend($this->server, $this->tree, $this->user, $this->rootFolder, $this->shareManager, $this->view, $filesMetadataManager); } - public function testSearchFilename() { + public function testSearchFilename(): void { $this->tree->expects($this->any()) ->method('getNodeForPath') ->willReturn($this->davFolder); @@ -128,9 +102,9 @@ class FileSearchBackendTest extends TestCase { [], $this->user )) - ->will($this->returnValue([ - new \OC\Files\Node\Folder($this->rootFolder, $this->view, '/test/path') - ])); + ->willReturn([ + new \OC\Files\Node\Folder($this->rootFolder, $this->view, '/test/path'), + ]); $query = $this->getBasicQuery(Operator::OPERATION_EQUAL, '{DAV:}displayname', 'foo'); $result = $this->search->search($query); @@ -139,7 +113,7 @@ class FileSearchBackendTest extends TestCase { $this->assertEquals('/files/test/test/path', $result[0]->href); } - public function testSearchMimetype() { + public function testSearchMimetype(): void { $this->tree->expects($this->any()) ->method('getNodeForPath') ->willReturn($this->davFolder); @@ -157,9 +131,9 @@ class FileSearchBackendTest extends TestCase { [], $this->user )) - ->will($this->returnValue([ - new \OC\Files\Node\Folder($this->rootFolder, $this->view, '/test/path') - ])); + ->willReturn([ + new \OC\Files\Node\Folder($this->rootFolder, $this->view, '/test/path'), + ]); $query = $this->getBasicQuery(Operator::OPERATION_EQUAL, '{DAV:}getcontenttype', 'foo'); $result = $this->search->search($query); @@ -168,7 +142,7 @@ class FileSearchBackendTest extends TestCase { $this->assertEquals('/files/test/test/path', $result[0]->href); } - public function testSearchSize() { + public function testSearchSize(): void { $this->tree->expects($this->any()) ->method('getNodeForPath') ->willReturn($this->davFolder); @@ -186,9 +160,9 @@ class FileSearchBackendTest extends TestCase { [], $this->user )) - ->will($this->returnValue([ - new \OC\Files\Node\Folder($this->rootFolder, $this->view, '/test/path') - ])); + ->willReturn([ + new \OC\Files\Node\Folder($this->rootFolder, $this->view, '/test/path'), + ]); $query = $this->getBasicQuery(Operator::OPERATION_GREATER_THAN, FilesPlugin::SIZE_PROPERTYNAME, 10); $result = $this->search->search($query); @@ -197,7 +171,7 @@ class FileSearchBackendTest extends TestCase { $this->assertEquals('/files/test/test/path', $result[0]->href); } - public function testSearchMtime() { + public function testSearchMtime(): void { $this->tree->expects($this->any()) ->method('getNodeForPath') ->willReturn($this->davFolder); @@ -215,9 +189,9 @@ class FileSearchBackendTest extends TestCase { [], $this->user )) - ->will($this->returnValue([ - new \OC\Files\Node\Folder($this->rootFolder, $this->view, '/test/path') - ])); + ->willReturn([ + new \OC\Files\Node\Folder($this->rootFolder, $this->view, '/test/path'), + ]); $query = $this->getBasicQuery(Operator::OPERATION_GREATER_THAN, '{DAV:}getlastmodified', 10); $result = $this->search->search($query); @@ -226,7 +200,7 @@ class FileSearchBackendTest extends TestCase { $this->assertEquals('/files/test/test/path', $result[0]->href); } - public function testSearchIsCollection() { + public function testSearchIsCollection(): void { $this->tree->expects($this->any()) ->method('getNodeForPath') ->willReturn($this->davFolder); @@ -244,9 +218,9 @@ class FileSearchBackendTest extends TestCase { [], $this->user )) - ->will($this->returnValue([ - new \OC\Files\Node\Folder($this->rootFolder, $this->view, '/test/path') - ])); + ->willReturn([ + new \OC\Files\Node\Folder($this->rootFolder, $this->view, '/test/path'), + ]); $query = $this->getBasicQuery(Operator::OPERATION_IS_COLLECTION, 'yes'); $result = $this->search->search($query); @@ -255,10 +229,10 @@ class FileSearchBackendTest extends TestCase { $this->assertEquals('/files/test/test/path', $result[0]->href); } - /** - * @expectedException \InvalidArgumentException - */ - public function testSearchInvalidProp() { + + public function testSearchInvalidProp(): void { + $this->expectException(\InvalidArgumentException::class); + $this->tree->expects($this->any()) ->method('getNodeForPath') ->willReturn($this->davFolder); @@ -270,31 +244,32 @@ class FileSearchBackendTest extends TestCase { $this->search->search($query); } - private function getBasicQuery($type, $property, $value = null) { - $query = new BasicSearch(); + private function getBasicQuery(string $type, string $property, int|string|null $value = null) { $scope = new Scope('/', 'infinite'); $scope->path = '/'; - $query->from = [$scope]; - $query->orderBy = []; - $query->select = []; + $from = [$scope]; + $orderBy = []; + $select = []; if (is_null($value)) { - $query->where = new Operator( + $where = new Operator( $type, [new Literal($property)] ); } else { - $query->where = new Operator( + $where = new Operator( $type, - [$property, new Literal($value)] + [new SearchPropertyDefinition($property, true, true, true), new Literal($value)] ); } - return $query; + $limit = new Limit(); + + return new Query($select, $from, $where, $orderBy, $limit); } - /** - * @expectedException \InvalidArgumentException - */ - public function testSearchNonFolder() { + + public function testSearchNonFolder(): void { + $this->expectException(\InvalidArgumentException::class); + $davNode = $this->createMock(File::class); $this->tree->expects($this->any()) @@ -304,4 +279,143 @@ class FileSearchBackendTest extends TestCase { $query = $this->getBasicQuery(Operator::OPERATION_EQUAL, '{DAV:}displayname', 'foo'); $this->search->search($query); } + + public function testSearchLimitOwnerBasic(): void { + $this->tree->expects($this->any()) + ->method('getNodeForPath') + ->willReturn($this->davFolder); + + /** @var ISearchQuery|null $receivedQuery */ + $receivedQuery = null; + $this->searchFolder + ->method('search') + ->willReturnCallback(function ($query) use (&$receivedQuery) { + $receivedQuery = $query; + return [ + new \OC\Files\Node\Folder($this->rootFolder, $this->view, '/test/path'), + ]; + }); + + $query = $this->getBasicQuery(Operator::OPERATION_EQUAL, FilesPlugin::OWNER_ID_PROPERTYNAME, $this->user->getUID()); + $this->search->search($query); + + $this->assertNotNull($receivedQuery); + $this->assertTrue($receivedQuery->limitToHome()); + + /** @var ISearchBinaryOperator $operator */ + $operator = $receivedQuery->getSearchOperation(); + $this->assertInstanceOf(ISearchBinaryOperator::class, $operator); + $this->assertEquals(ISearchBinaryOperator::OPERATOR_AND, $operator->getType()); + $this->assertEmpty($operator->getArguments()); + } + + public function testSearchLimitOwnerNested(): void { + $this->tree->expects($this->any()) + ->method('getNodeForPath') + ->willReturn($this->davFolder); + + /** @var ISearchQuery|null $receivedQuery */ + $receivedQuery = null; + $this->searchFolder + ->method('search') + ->willReturnCallback(function ($query) use (&$receivedQuery) { + $receivedQuery = $query; + return [ + new \OC\Files\Node\Folder($this->rootFolder, $this->view, '/test/path'), + ]; + }); + + $query = $this->getBasicQuery(Operator::OPERATION_EQUAL, FilesPlugin::OWNER_ID_PROPERTYNAME, $this->user->getUID()); + $query->where = new Operator( + Operator::OPERATION_AND, + [ + new Operator( + Operator::OPERATION_EQUAL, + [new SearchPropertyDefinition('{DAV:}getcontenttype', true, true, true), new Literal('image/png')] + ), + new Operator( + Operator::OPERATION_EQUAL, + [new SearchPropertyDefinition(FilesPlugin::OWNER_ID_PROPERTYNAME, true, true, true), new Literal($this->user->getUID())] + ), + ] + ); + $this->search->search($query); + + $this->assertNotNull($receivedQuery); + $this->assertTrue($receivedQuery->limitToHome()); + + /** @var ISearchBinaryOperator $operator */ + $operator = $receivedQuery->getSearchOperation(); + $this->assertInstanceOf(ISearchBinaryOperator::class, $operator); + $this->assertEquals(ISearchBinaryOperator::OPERATOR_AND, $operator->getType()); + $this->assertCount(2, $operator->getArguments()); + + /** @var ISearchBinaryOperator $operator */ + $operator = $operator->getArguments()[1]; + $this->assertInstanceOf(ISearchBinaryOperator::class, $operator); + $this->assertEquals(ISearchBinaryOperator::OPERATOR_AND, $operator->getType()); + $this->assertEmpty($operator->getArguments()); + } + + public function testSearchOperatorLimit(): void { + $this->tree->expects($this->any()) + ->method('getNodeForPath') + ->willReturn($this->davFolder); + + $innerOperator = new Operator( + Operator::OPERATION_EQUAL, + [new SearchPropertyDefinition('{DAV:}getcontenttype', true, true, true), new Literal('image/png')] + ); + // 5 child operators + $level1Operator = new Operator( + Operator::OPERATION_AND, + [ + $innerOperator, + $innerOperator, + $innerOperator, + $innerOperator, + $innerOperator, + ] + ); + // 5^2 = 25 child operators + $level2Operator = new Operator( + Operator::OPERATION_AND, + [ + $level1Operator, + $level1Operator, + $level1Operator, + $level1Operator, + $level1Operator, + ] + ); + // 5^3 = 125 child operators + $level3Operator = new Operator( + Operator::OPERATION_AND, + [ + $level2Operator, + $level2Operator, + $level2Operator, + $level2Operator, + $level2Operator, + ] + ); + + $query = $this->getBasicQuery(Operator::OPERATION_EQUAL, FilesPlugin::OWNER_ID_PROPERTYNAME, $this->user->getUID()); + $query->where = $level3Operator; + $this->expectException(\InvalidArgumentException::class); + $this->search->search($query); + } + + public function testPreloadPropertyFor(): void { + $node1 = $this->createMock(File::class); + $node2 = $this->createMock(Directory::class); + $nodes = [$node1, $node2]; + $requestProperties = ['{DAV:}getcontenttype', '{DAV:}getlastmodified']; + + $this->server->expects($this->once()) + ->method('emit') + ->with('preloadProperties', [$nodes, $requestProperties]); + + $this->search->preloadPropertyFor($nodes, $requestProperties); + } } |