123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427 |
- <?php
- /**
- * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
- * SPDX-License-Identifier: AGPL-3.0-or-later
- */
- namespace OCA\DAV\Tests\Files;
- use OC\Files\Search\SearchComparison;
- use OC\Files\Search\SearchQuery;
- use OC\Files\View;
- 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\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\Backend\SearchPropertyDefinition;
- use SearchDAV\Query\Limit;
- use SearchDAV\Query\Operator;
- use SearchDAV\Query\Query;
- use Test\TestCase;
- class FileSearchBackendTest extends TestCase {
- /** @var ObjectTree|\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(): void {
- if (PHP_VERSION_ID >= 80400) {
- $this->markTestSkipped('SearchDAV is not yet PHP 8.4 compatible');
- }
- parent::setUp();
- $this->user = $this->createMock(IUser::class);
- $this->user->expects($this->any())
- ->method('getUID')
- ->willReturn('test');
- $this->tree = $this->getMockBuilder(ObjectTree::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->view = $this->createMock(View::class);
- $this->view->expects($this->any())
- ->method('getRoot')
- ->willReturn('');
- $this->view->expects($this->any())
- ->method('getRelativePath')
- ->willReturnArgument(0);
- $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->davFolder->expects($this->any())
- ->method('getFileInfo')
- ->willReturn($fileInfo);
- $this->rootFolder->expects($this->any())
- ->method('get')
- ->willReturn($this->searchFolder);
- $filesMetadataManager = $this->createMock(IFilesMetadataManager::class);
- $this->search = new FileSearchBackend($this->tree, $this->user, $this->rootFolder, $this->shareManager, $this->view, $filesMetadataManager);
- }
- public function testSearchFilename(): void {
- $this->tree->expects($this->any())
- ->method('getNodeForPath')
- ->willReturn($this->davFolder);
- $this->searchFolder->expects($this->once())
- ->method('search')
- ->with(new SearchQuery(
- new SearchComparison(
- ISearchComparison::COMPARE_EQUAL,
- 'name',
- 'foo'
- ),
- 0,
- 0,
- [],
- $this->user
- ))
- ->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);
- $this->assertCount(1, $result);
- $this->assertEquals('/files/test/test/path', $result[0]->href);
- }
- public function testSearchMimetype(): void {
- $this->tree->expects($this->any())
- ->method('getNodeForPath')
- ->willReturn($this->davFolder);
- $this->searchFolder->expects($this->once())
- ->method('search')
- ->with(new SearchQuery(
- new SearchComparison(
- ISearchComparison::COMPARE_EQUAL,
- 'mimetype',
- 'foo'
- ),
- 0,
- 0,
- [],
- $this->user
- ))
- ->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);
- $this->assertCount(1, $result);
- $this->assertEquals('/files/test/test/path', $result[0]->href);
- }
- public function testSearchSize(): void {
- $this->tree->expects($this->any())
- ->method('getNodeForPath')
- ->willReturn($this->davFolder);
- $this->searchFolder->expects($this->once())
- ->method('search')
- ->with(new SearchQuery(
- new SearchComparison(
- ISearchComparison::COMPARE_GREATER_THAN,
- 'size',
- 10
- ),
- 0,
- 0,
- [],
- $this->user
- ))
- ->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);
- $this->assertCount(1, $result);
- $this->assertEquals('/files/test/test/path', $result[0]->href);
- }
- public function testSearchMtime(): void {
- $this->tree->expects($this->any())
- ->method('getNodeForPath')
- ->willReturn($this->davFolder);
- $this->searchFolder->expects($this->once())
- ->method('search')
- ->with(new SearchQuery(
- new SearchComparison(
- ISearchComparison::COMPARE_GREATER_THAN,
- 'mtime',
- 10
- ),
- 0,
- 0,
- [],
- $this->user
- ))
- ->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);
- $this->assertCount(1, $result);
- $this->assertEquals('/files/test/test/path', $result[0]->href);
- }
- public function testSearchIsCollection(): void {
- $this->tree->expects($this->any())
- ->method('getNodeForPath')
- ->willReturn($this->davFolder);
- $this->searchFolder->expects($this->once())
- ->method('search')
- ->with(new SearchQuery(
- new SearchComparison(
- ISearchComparison::COMPARE_EQUAL,
- 'mimetype',
- FileInfo::MIMETYPE_FOLDER
- ),
- 0,
- 0,
- [],
- $this->user
- ))
- ->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);
- $this->assertCount(1, $result);
- $this->assertEquals('/files/test/test/path', $result[0]->href);
- }
- public function testSearchInvalidProp(): void {
- $this->expectException(\InvalidArgumentException::class);
- $this->tree->expects($this->any())
- ->method('getNodeForPath')
- ->willReturn($this->davFolder);
- $this->searchFolder->expects($this->never())
- ->method('search');
- $query = $this->getBasicQuery(Operator::OPERATION_EQUAL, '{DAV:}getetag', 'foo');
- $this->search->search($query);
- }
- private function getBasicQuery($type, $property, $value = null) {
- $scope = new \SearchDAV\Query\Scope('/', 'infinite');
- $scope->path = '/';
- $from = [$scope];
- $orderBy = [];
- $select = [];
- if (is_null($value)) {
- $where = new Operator(
- $type,
- [new \SearchDAV\Query\Literal($property)]
- );
- } else {
- $where = new Operator(
- $type,
- [new SearchPropertyDefinition($property, true, true, true), new \SearchDAV\Query\Literal($value)]
- );
- }
- $limit = new Limit();
- return new Query($select, $from, $where, $orderBy, $limit);
- }
- public function testSearchNonFolder(): void {
- $this->expectException(\InvalidArgumentException::class);
- $davNode = $this->createMock(File::class);
- $this->tree->expects($this->any())
- ->method('getNodeForPath')
- ->willReturn($davNode);
- $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 \SearchDAV\Query\Literal('image/png')]
- ),
- new Operator(
- Operator::OPERATION_EQUAL,
- [new SearchPropertyDefinition(FilesPlugin::OWNER_ID_PROPERTYNAME, true, true, true), new \SearchDAV\Query\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 \SearchDAV\Query\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);
- }
- }
|