2017-02-21 20:18:41 +03:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @copyright Copyright (c) 2017 Robin Appelman <robin@icewind.nl>
|
|
|
|
*
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Robin Appelman <robin@icewind.nl>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2017-11-06 17:56:42 +03:00
|
|
|
*
|
2017-02-21 20:18:41 +03:00
|
|
|
* @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
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2017-02-21 20:18:41 +03:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OCA\DAV\Tests\Files;
|
|
|
|
|
|
|
|
use OC\Files\Search\SearchComparison;
|
|
|
|
use OC\Files\Search\SearchQuery;
|
|
|
|
use OC\Files\View;
|
2017-08-26 11:52:22 +03:00
|
|
|
use OCA\DAV\Connector\Sabre\CachingTree;
|
2017-02-21 20:18:41 +03:00
|
|
|
use OCA\DAV\Connector\Sabre\Directory;
|
|
|
|
use OCA\DAV\Connector\Sabre\File;
|
|
|
|
use OCA\DAV\Connector\Sabre\FilesPlugin;
|
|
|
|
use OCA\DAV\Files\FileSearchBackend;
|
|
|
|
use OCP\Files\FileInfo;
|
|
|
|
use OCP\Files\Folder;
|
|
|
|
use OCP\Files\IRootFolder;
|
2019-11-08 17:05:21 +03:00
|
|
|
use OCP\Files\Search\ISearchBinaryOperator;
|
2017-02-21 20:18:41 +03:00
|
|
|
use OCP\Files\Search\ISearchComparison;
|
2019-11-08 17:05:21 +03:00
|
|
|
use OCP\Files\Search\ISearchQuery;
|
2017-02-21 20:18:41 +03:00
|
|
|
use OCP\IUser;
|
|
|
|
use OCP\Share\IManager;
|
2018-02-07 16:55:50 +03:00
|
|
|
use SearchDAV\Backend\SearchPropertyDefinition;
|
|
|
|
use SearchDAV\Query\Limit;
|
|
|
|
use SearchDAV\Query\Query;
|
2017-02-21 20:18:41 +03:00
|
|
|
use SearchDAV\XML\Operator;
|
|
|
|
use Test\TestCase;
|
|
|
|
|
|
|
|
class FileSearchBackendTest extends TestCase {
|
2017-08-26 11:52:22 +03:00
|
|
|
/** @var CachingTree|\PHPUnit_Framework_MockObject_MockObject */
|
2017-02-21 20:18:41 +03:00
|
|
|
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;
|
|
|
|
|
2019-11-21 18:40:38 +03:00
|
|
|
protected function setUp(): void {
|
2017-02-21 20:18:41 +03:00
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->user = $this->createMock(IUser::class);
|
|
|
|
$this->user->expects($this->any())
|
|
|
|
->method('getUID')
|
|
|
|
->willReturn('test');
|
|
|
|
|
2017-08-26 11:52:22 +03:00
|
|
|
$this->tree = $this->getMockBuilder(CachingTree::class)
|
2017-02-21 20:18:41 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$this->view = $this->getMockBuilder(View::class)
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$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);
|
|
|
|
|
|
|
|
$this->search = new FileSearchBackend($this->tree, $this->user, $this->rootFolder, $this->shareManager, $this->view);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSearchFilename() {
|
|
|
|
$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,
|
2017-03-08 17:17:39 +03:00
|
|
|
[],
|
|
|
|
$this->user
|
2017-02-21 20:18:41 +03:00
|
|
|
))
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn([
|
2017-02-21 20:18:41 +03:00
|
|
|
new \OC\Files\Node\Folder($this->rootFolder, $this->view, '/test/path')
|
2020-03-26 00:21:27 +03:00
|
|
|
]);
|
2017-02-21 20:18:41 +03:00
|
|
|
|
2018-02-07 16:55:50 +03:00
|
|
|
$query = $this->getBasicQuery(\SearchDAV\Query\Operator::OPERATION_EQUAL, '{DAV:}displayname', 'foo');
|
2017-02-21 20:18:41 +03:00
|
|
|
$result = $this->search->search($query);
|
|
|
|
|
|
|
|
$this->assertCount(1, $result);
|
|
|
|
$this->assertEquals('/files/test/test/path', $result[0]->href);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSearchMimetype() {
|
|
|
|
$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,
|
2017-03-08 17:17:39 +03:00
|
|
|
[],
|
|
|
|
$this->user
|
2017-02-21 20:18:41 +03:00
|
|
|
))
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn([
|
2017-02-21 20:18:41 +03:00
|
|
|
new \OC\Files\Node\Folder($this->rootFolder, $this->view, '/test/path')
|
2020-03-26 00:21:27 +03:00
|
|
|
]);
|
2017-02-21 20:18:41 +03:00
|
|
|
|
2018-02-07 16:55:50 +03:00
|
|
|
$query = $this->getBasicQuery(\SearchDAV\Query\Operator::OPERATION_EQUAL, '{DAV:}getcontenttype', 'foo');
|
2017-02-21 20:18:41 +03:00
|
|
|
$result = $this->search->search($query);
|
|
|
|
|
|
|
|
$this->assertCount(1, $result);
|
|
|
|
$this->assertEquals('/files/test/test/path', $result[0]->href);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSearchSize() {
|
|
|
|
$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,
|
2017-03-08 17:17:39 +03:00
|
|
|
[],
|
|
|
|
$this->user
|
2017-02-21 20:18:41 +03:00
|
|
|
))
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn([
|
2017-02-21 20:18:41 +03:00
|
|
|
new \OC\Files\Node\Folder($this->rootFolder, $this->view, '/test/path')
|
2020-03-26 00:21:27 +03:00
|
|
|
]);
|
2017-02-21 20:18:41 +03:00
|
|
|
|
2018-02-07 16:55:50 +03:00
|
|
|
$query = $this->getBasicQuery(\SearchDAV\Query\Operator::OPERATION_GREATER_THAN, FilesPlugin::SIZE_PROPERTYNAME, 10);
|
2017-02-21 20:18:41 +03:00
|
|
|
$result = $this->search->search($query);
|
|
|
|
|
|
|
|
$this->assertCount(1, $result);
|
|
|
|
$this->assertEquals('/files/test/test/path', $result[0]->href);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSearchMtime() {
|
|
|
|
$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,
|
2017-03-08 17:17:39 +03:00
|
|
|
[],
|
|
|
|
$this->user
|
2017-02-21 20:18:41 +03:00
|
|
|
))
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn([
|
2017-02-21 20:18:41 +03:00
|
|
|
new \OC\Files\Node\Folder($this->rootFolder, $this->view, '/test/path')
|
2020-03-26 00:21:27 +03:00
|
|
|
]);
|
2017-02-21 20:18:41 +03:00
|
|
|
|
2018-02-07 16:55:50 +03:00
|
|
|
$query = $this->getBasicQuery(\SearchDAV\Query\Operator::OPERATION_GREATER_THAN, '{DAV:}getlastmodified', 10);
|
2017-02-21 20:18:41 +03:00
|
|
|
$result = $this->search->search($query);
|
|
|
|
|
|
|
|
$this->assertCount(1, $result);
|
|
|
|
$this->assertEquals('/files/test/test/path', $result[0]->href);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSearchIsCollection() {
|
|
|
|
$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,
|
2017-03-08 17:17:39 +03:00
|
|
|
[],
|
|
|
|
$this->user
|
2017-02-21 20:18:41 +03:00
|
|
|
))
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn([
|
2017-02-21 20:18:41 +03:00
|
|
|
new \OC\Files\Node\Folder($this->rootFolder, $this->view, '/test/path')
|
2020-03-26 00:21:27 +03:00
|
|
|
]);
|
2017-02-21 20:18:41 +03:00
|
|
|
|
2018-02-07 16:55:50 +03:00
|
|
|
$query = $this->getBasicQuery(\SearchDAV\Query\Operator::OPERATION_IS_COLLECTION, 'yes');
|
2017-02-21 20:18:41 +03:00
|
|
|
$result = $this->search->search($query);
|
|
|
|
|
|
|
|
$this->assertCount(1, $result);
|
|
|
|
$this->assertEquals('/files/test/test/path', $result[0]->href);
|
|
|
|
}
|
|
|
|
|
2019-11-27 17:27:18 +03:00
|
|
|
|
2017-02-21 20:18:41 +03:00
|
|
|
public function testSearchInvalidProp() {
|
2019-11-27 17:27:18 +03:00
|
|
|
$this->expectException(\InvalidArgumentException::class);
|
|
|
|
|
2017-02-21 20:18:41 +03:00
|
|
|
$this->tree->expects($this->any())
|
|
|
|
->method('getNodeForPath')
|
|
|
|
->willReturn($this->davFolder);
|
|
|
|
|
|
|
|
$this->searchFolder->expects($this->never())
|
|
|
|
->method('search');
|
|
|
|
|
2018-02-07 16:55:50 +03:00
|
|
|
$query = $this->getBasicQuery(\SearchDAV\Query\Operator::OPERATION_EQUAL, '{DAV:}getetag', 'foo');
|
2017-02-21 20:18:41 +03:00
|
|
|
$this->search->search($query);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getBasicQuery($type, $property, $value = null) {
|
2018-02-07 16:55:50 +03:00
|
|
|
$scope = new \SearchDAV\Query\Scope('/', 'infinite');
|
2017-02-21 20:18:41 +03:00
|
|
|
$scope->path = '/';
|
2018-02-07 16:55:50 +03:00
|
|
|
$from = [$scope];
|
|
|
|
$orderBy = [];
|
|
|
|
$select = [];
|
2017-02-21 20:18:41 +03:00
|
|
|
if (is_null($value)) {
|
2018-02-07 16:55:50 +03:00
|
|
|
$where = new \SearchDAV\Query\Operator(
|
2017-02-21 20:18:41 +03:00
|
|
|
$type,
|
2018-02-07 16:55:50 +03:00
|
|
|
[new \SearchDAV\Query\Literal($property)]
|
2017-02-21 20:18:41 +03:00
|
|
|
);
|
|
|
|
} else {
|
2018-02-07 16:55:50 +03:00
|
|
|
$where = new \SearchDAV\Query\Operator(
|
2017-02-21 20:18:41 +03:00
|
|
|
$type,
|
2018-02-07 16:55:50 +03:00
|
|
|
[new SearchPropertyDefinition($property, true, true, true), new \SearchDAV\Query\Literal($value)]
|
2017-02-21 20:18:41 +03:00
|
|
|
);
|
|
|
|
}
|
2018-02-07 16:55:50 +03:00
|
|
|
$limit = new Limit();
|
|
|
|
|
|
|
|
return new Query($select, $from, $where, $orderBy, $limit);
|
2017-02-21 20:18:41 +03:00
|
|
|
}
|
|
|
|
|
2019-11-27 17:27:18 +03:00
|
|
|
|
2017-02-21 20:18:41 +03:00
|
|
|
public function testSearchNonFolder() {
|
2019-11-27 17:27:18 +03:00
|
|
|
$this->expectException(\InvalidArgumentException::class);
|
|
|
|
|
2017-02-21 20:18:41 +03:00
|
|
|
$davNode = $this->createMock(File::class);
|
|
|
|
|
|
|
|
$this->tree->expects($this->any())
|
|
|
|
->method('getNodeForPath')
|
|
|
|
->willReturn($davNode);
|
|
|
|
|
2018-02-07 16:55:50 +03:00
|
|
|
$query = $this->getBasicQuery(\SearchDAV\Query\Operator::OPERATION_EQUAL, '{DAV:}displayname', 'foo');
|
2017-02-21 20:18:41 +03:00
|
|
|
$this->search->search($query);
|
|
|
|
}
|
2019-11-08 17:05:21 +03:00
|
|
|
|
|
|
|
public function testSearchLimitOwnerBasic() {
|
|
|
|
$this->tree->expects($this->any())
|
|
|
|
->method('getNodeForPath')
|
|
|
|
->willReturn($this->davFolder);
|
|
|
|
|
|
|
|
/** @var ISearchQuery|null $receivedQuery */
|
|
|
|
$receivedQuery = null;
|
|
|
|
$this->searchFolder
|
|
|
|
->method('search')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturnCallback(function ($query) use (&$receivedQuery) {
|
2019-11-08 17:05:21 +03:00
|
|
|
$receivedQuery = $query;
|
|
|
|
return [
|
|
|
|
new \OC\Files\Node\Folder($this->rootFolder, $this->view, '/test/path')
|
|
|
|
];
|
2020-03-26 00:21:27 +03:00
|
|
|
});
|
2019-11-08 17:05:21 +03:00
|
|
|
|
|
|
|
$query = $this->getBasicQuery(\SearchDAV\Query\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() {
|
|
|
|
$this->tree->expects($this->any())
|
|
|
|
->method('getNodeForPath')
|
|
|
|
->willReturn($this->davFolder);
|
|
|
|
|
|
|
|
/** @var ISearchQuery|null $receivedQuery */
|
|
|
|
$receivedQuery = null;
|
|
|
|
$this->searchFolder
|
|
|
|
->method('search')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturnCallback(function ($query) use (&$receivedQuery) {
|
2019-11-08 17:05:21 +03:00
|
|
|
$receivedQuery = $query;
|
|
|
|
return [
|
|
|
|
new \OC\Files\Node\Folder($this->rootFolder, $this->view, '/test/path')
|
|
|
|
];
|
2020-03-26 00:21:27 +03:00
|
|
|
});
|
2019-11-08 17:05:21 +03:00
|
|
|
|
|
|
|
$query = $this->getBasicQuery(\SearchDAV\Query\Operator::OPERATION_EQUAL, FilesPlugin::OWNER_ID_PROPERTYNAME, $this->user->getUID());
|
|
|
|
$query->where = new \SearchDAV\Query\Operator(
|
|
|
|
\SearchDAV\Query\Operator::OPERATION_AND,
|
|
|
|
[
|
|
|
|
new \SearchDAV\Query\Operator(
|
|
|
|
\SearchDAV\Query\Operator::OPERATION_EQUAL,
|
|
|
|
[new SearchPropertyDefinition('{DAV:}getcontenttype', true, true, true), new \SearchDAV\Query\Literal('image/png')]
|
|
|
|
),
|
|
|
|
new \SearchDAV\Query\Operator(
|
|
|
|
\SearchDAV\Query\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());
|
|
|
|
}
|
2017-02-21 20:18:41 +03:00
|
|
|
}
|