Skip to content

Commit

Permalink
add test for search operator limit
Browse files Browse the repository at this point in the history
Signed-off-by: Robin Appelman <robin@icewind.nl>
  • Loading branch information
icewind1991 committed Sep 8, 2022
1 parent 6ba7a07 commit e83c16a
Showing 1 changed file with 76 additions and 25 deletions.
101 changes: 76 additions & 25 deletions apps/dav/tests/unit/Files/FileSearchBackendTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace OCA\DAV\Tests\Files;

use OC\Files\Search\SearchComparison;
Expand All @@ -44,6 +45,7 @@
use OCP\Share\IManager;
use SearchDAV\Backend\SearchPropertyDefinition;
use SearchDAV\Query\Limit;
use SearchDAV\Query\Operator;
use SearchDAV\Query\Query;
use Test\TestCase;

Expand Down Expand Up @@ -132,10 +134,10 @@ public function testSearchFilename() {
$this->user
))
->willReturn([
new \OC\Files\Node\Folder($this->rootFolder, $this->view, '/test/path')
new \OC\Files\Node\Folder($this->rootFolder, $this->view, '/test/path'),
]);

$query = $this->getBasicQuery(\SearchDAV\Query\Operator::OPERATION_EQUAL, '{DAV:}displayname', 'foo');
$query = $this->getBasicQuery(Operator::OPERATION_EQUAL, '{DAV:}displayname', 'foo');
$result = $this->search->search($query);

$this->assertCount(1, $result);
Expand All @@ -161,10 +163,10 @@ public function testSearchMimetype() {
$this->user
))
->willReturn([
new \OC\Files\Node\Folder($this->rootFolder, $this->view, '/test/path')
new \OC\Files\Node\Folder($this->rootFolder, $this->view, '/test/path'),
]);

$query = $this->getBasicQuery(\SearchDAV\Query\Operator::OPERATION_EQUAL, '{DAV:}getcontenttype', 'foo');
$query = $this->getBasicQuery(Operator::OPERATION_EQUAL, '{DAV:}getcontenttype', 'foo');
$result = $this->search->search($query);

$this->assertCount(1, $result);
Expand All @@ -190,10 +192,10 @@ public function testSearchSize() {
$this->user
))
->willReturn([
new \OC\Files\Node\Folder($this->rootFolder, $this->view, '/test/path')
new \OC\Files\Node\Folder($this->rootFolder, $this->view, '/test/path'),
]);

$query = $this->getBasicQuery(\SearchDAV\Query\Operator::OPERATION_GREATER_THAN, FilesPlugin::SIZE_PROPERTYNAME, 10);
$query = $this->getBasicQuery(Operator::OPERATION_GREATER_THAN, FilesPlugin::SIZE_PROPERTYNAME, 10);
$result = $this->search->search($query);

$this->assertCount(1, $result);
Expand All @@ -219,10 +221,10 @@ public function testSearchMtime() {
$this->user
))
->willReturn([
new \OC\Files\Node\Folder($this->rootFolder, $this->view, '/test/path')
new \OC\Files\Node\Folder($this->rootFolder, $this->view, '/test/path'),
]);

$query = $this->getBasicQuery(\SearchDAV\Query\Operator::OPERATION_GREATER_THAN, '{DAV:}getlastmodified', 10);
$query = $this->getBasicQuery(Operator::OPERATION_GREATER_THAN, '{DAV:}getlastmodified', 10);
$result = $this->search->search($query);

$this->assertCount(1, $result);
Expand All @@ -248,10 +250,10 @@ public function testSearchIsCollection() {
$this->user
))
->willReturn([
new \OC\Files\Node\Folder($this->rootFolder, $this->view, '/test/path')
new \OC\Files\Node\Folder($this->rootFolder, $this->view, '/test/path'),
]);

$query = $this->getBasicQuery(\SearchDAV\Query\Operator::OPERATION_IS_COLLECTION, 'yes');
$query = $this->getBasicQuery(Operator::OPERATION_IS_COLLECTION, 'yes');
$result = $this->search->search($query);

$this->assertCount(1, $result);
Expand All @@ -269,7 +271,7 @@ public function testSearchInvalidProp() {
$this->searchFolder->expects($this->never())
->method('search');

$query = $this->getBasicQuery(\SearchDAV\Query\Operator::OPERATION_EQUAL, '{DAV:}getetag', 'foo');
$query = $this->getBasicQuery(Operator::OPERATION_EQUAL, '{DAV:}getetag', 'foo');
$this->search->search($query);
}

Expand All @@ -280,12 +282,12 @@ private function getBasicQuery($type, $property, $value = null) {
$orderBy = [];
$select = [];
if (is_null($value)) {
$where = new \SearchDAV\Query\Operator(
$where = new Operator(
$type,
[new \SearchDAV\Query\Literal($property)]
);
} else {
$where = new \SearchDAV\Query\Operator(
$where = new Operator(
$type,
[new SearchPropertyDefinition($property, true, true, true), new \SearchDAV\Query\Literal($value)]
);
Expand All @@ -305,7 +307,7 @@ public function testSearchNonFolder() {
->method('getNodeForPath')
->willReturn($davNode);

$query = $this->getBasicQuery(\SearchDAV\Query\Operator::OPERATION_EQUAL, '{DAV:}displayname', 'foo');
$query = $this->getBasicQuery(Operator::OPERATION_EQUAL, '{DAV:}displayname', 'foo');
$this->search->search($query);
}

Expand All @@ -321,11 +323,11 @@ public function testSearchLimitOwnerBasic() {
->willReturnCallback(function ($query) use (&$receivedQuery) {
$receivedQuery = $query;
return [
new \OC\Files\Node\Folder($this->rootFolder, $this->view, '/test/path')
new \OC\Files\Node\Folder($this->rootFolder, $this->view, '/test/path'),
];
});

$query = $this->getBasicQuery(\SearchDAV\Query\Operator::OPERATION_EQUAL, FilesPlugin::OWNER_ID_PROPERTYNAME, $this->user->getUID());
$query = $this->getBasicQuery(Operator::OPERATION_EQUAL, FilesPlugin::OWNER_ID_PROPERTYNAME, $this->user->getUID());
$this->search->search($query);

$this->assertNotNull($receivedQuery);
Expand All @@ -350,22 +352,22 @@ public function testSearchLimitOwnerNested() {
->willReturnCallback(function ($query) use (&$receivedQuery) {
$receivedQuery = $query;
return [
new \OC\Files\Node\Folder($this->rootFolder, $this->view, '/test/path')
new \OC\Files\Node\Folder($this->rootFolder, $this->view, '/test/path'),
];
});

$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,
$query = $this->getBasicQuery(Operator::OPERATION_EQUAL, FilesPlugin::OWNER_ID_PROPERTYNAME, $this->user->getUID());
$query->where = new Operator(
Operator::OPERATION_AND,
[
new \SearchDAV\Query\Operator(
\SearchDAV\Query\Operator::OPERATION_EQUAL,
new Operator(
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 Operator(
Operator::OPERATION_EQUAL,
[new SearchPropertyDefinition(FilesPlugin::OWNER_ID_PROPERTYNAME, true, true, true), new \SearchDAV\Query\Literal($this->user->getUID())]
)
),
]
);
$this->search->search($query);
Expand All @@ -385,4 +387,53 @@ public function testSearchLimitOwnerNested() {
$this->assertEquals(ISearchBinaryOperator::OPERATOR_AND, $operator->getType());
$this->assertEmpty($operator->getArguments());
}

public function testSearchOperatorLimit() {
$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);
}
}

0 comments on commit e83c16a

Please sign in to comment.