more tests

This commit is contained in:
Robin Appelman 2017-02-22 16:43:46 +01:00
commit b42b650266
13 changed files with 442 additions and 12 deletions

View file

@ -32,6 +32,7 @@ use Sabre\DAV\Xml\Element\Response;
use Sabre\DAV\Xml\Response\MultiStatus;
use Sabre\HTTP\RequestInterface;
use Sabre\HTTP\ResponseInterface;
use Sabre\Xml\ParseException;
use Sabre\Xml\Writer;
use SearchDAV\Backend\ISearchBackend;
use SearchDAV\Backend\SearchPropertyDefinition;
@ -55,11 +56,11 @@ class SearchPlugin extends ServerPlugin {
public function __construct(ISearchBackend $searchBackend) {
$this->searchBackend = $searchBackend;
$this->queryParser = new QueryParser();
}
public function initialize(Server $server) {
$this->server = $server;
$this->queryParser = new QueryParser($this->server->xml);
$server->on('method:SEARCH', [$this, 'searchHandler']);
$server->on('afterMethod:OPTIONS', [$this, 'optionHandler']);
$server->on('propFind', [$this, 'propFindHandler']);
@ -98,7 +99,7 @@ class SearchPlugin extends ServerPlugin {
}
public function optionHandler(RequestInterface $request, ResponseInterface $response) {
if ($request->getPath() === '') {
if ($request->getPath() === $this->searchBackend->getArbiterPath()) {
$response->addHeader('DASL', '<DAV:basicsearch>');
}
}
@ -111,11 +112,21 @@ class SearchPlugin extends ServerPlugin {
return;
}
$xml = $this->queryParser->parse(
$request->getBody(),
$request->getUrl(),
$documentType
);
if ($request->getPath() !== $this->searchBackend->getArbiterPath()) {
return;
}
try {
$xml = $this->queryParser->parse(
$request->getBody(),
$request->getUrl(),
$documentType
);
} catch (ParseException $e) {
$response->setStatus(400);
$response->setBody('Parse error: ' . $e->getMessage());
return false;
}
switch ($documentType) {
case '{DAV:}searchrequest':

View file

@ -21,6 +21,7 @@
namespace SearchDAV\XML;
use Sabre\Xml\ParseException;
use Sabre\Xml\Reader;
use Sabre\Xml\XmlDeserializable;
@ -62,10 +63,15 @@ class BasicSearch implements XmlDeserializable {
$search = new self();
$elements = \Sabre\Xml\Deserializer\keyValue($reader);
$search->select = isset($elements['{DAV:}select']) ? $elements['{DAV:}select'] : null;
$search->from = isset($elements['{DAV:}from']) ? $elements['{DAV:}from'] : null;
if (!isset($elements['{DAV:}from'])) {
throw new ParseException('Missing {DAV:}from when parsing {DAV:}basicsearch');
}
$search->select = isset($elements['{DAV:}select']) ? $elements['{DAV:}select'] : [];
$search->from = $elements['{DAV:}from'];
$search->where = isset($elements['{DAV:}where']) ? $elements['{DAV:}where'] : null;
$search->orderBy = isset($elements['{DAV:}orderby']) ? $elements['{DAV:}orderby'] : null;
$search->orderBy = isset($elements['{DAV:}orderby']) ? $elements['{DAV:}orderby'] : [];
return $search;
}

View file

@ -49,7 +49,7 @@ class QueryDiscoverResponse extends Response {
if ($this->schema) {
$writer->writeElement('{DAV:}query-schema', [
'{DAV:basicsearchschema}' => $this->schema
'{DAV:}basicsearchschema' => $this->schema
]);
}
}

View file

@ -53,10 +53,12 @@ class Scope implements XmlDeserializable {
/**
* @param string $href
* @param int|string $depth
* @param string|null $path
*/
public function __construct($href = '', $depth = 1) {
public function __construct($href = '', $depth = 1, $path = null) {
$this->href = $href;
$this->depth = $depth;
$this->path = $path;
}
static function xmlDeserialize(Reader $reader) {

View file

@ -55,4 +55,52 @@ class QueryParserTest extends \PHPUnit_Framework_TestCase {
new Order('{DAV:}getcontentlength', Order::ASC)
], $search->orderBy);
}
public function testParseNoOrder() {
$query = file_get_contents(__DIR__ . '/noorder.xml');
$parser = new QueryParser();
$xml = $parser->parse($query, null, $rootElementName);
$this->assertEquals('{DAV:}searchrequest', $rootElementName);
$this->assertArrayHasKey('{DAV:}basicsearch', $xml);
/** @var BasicSearch $search */
$search = $xml['{DAV:}basicsearch'];
$this->assertInstanceOf(BasicSearch::class, $search);
$this->assertEquals(['{DAV:}getcontentlength'], $search->select);
$this->assertEquals([
new Scope('/container1/', 'infinity'),
new Scope('/container2/', 1),
], $search->from);
$this->assertEquals(new Operator(Operator::OPERATION_IS_COLLECTION, []), $search->where);
$this->assertEquals([], $search->orderBy);
}
/**
* @expectedException \Sabre\XML\ParseException
*/
public function testParseNoWhere() {
$query = file_get_contents(__DIR__ . '/nowhere.xml');
$parser = new QueryParser();
$parser->parse($query, null, $rootElementName);
}
/**
* @expectedException \Sabre\XML\ParseException
*/
public function testParseNoFrom() {
$query = file_get_contents(__DIR__ . '/nofrom.xml');
$parser = new QueryParser();
$parser->parse($query, null, $rootElementName);
}
/**
* @expectedException \Sabre\XML\ParseException
*/
public function testParseNoSelect() {
$query = file_get_contents(__DIR__ . '/noselect.xml');
$parser = new QueryParser();
$parser->parse($query, null, $rootElementName);
}
}

199
tests/SearchPluginTest.php Normal file
View file

@ -0,0 +1,199 @@
<?php
/**
* @copyright Copyright (c) 2017 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/>.
*
*/
namespace SearchDAV\Test;
use Sabre\DAV\FS\Directory;
use Sabre\DAV\Server;
use Sabre\DAV\Xml\Service;
use Sabre\HTTP\Request;
use Sabre\HTTP\Response;
use Sabre\VObject\Parser\XML;
use SearchDAV\Backend\ISearchBackend;
use SearchDAV\Backend\SearchPropertyDefinition;
use SearchDAV\Backend\SearchResult;
use SearchDAV\DAV\SearchPlugin;
use SearchDAV\XML\BasicSearch;
use SearchDAV\XML\Literal;
use SearchDAV\XML\Operator;
use SearchDAV\XML\Order;
use SearchDAV\XML\Scope;
class SearchPluginTest extends \PHPUnit_Framework_TestCase {
/** @var ISearchBackend|\PHPUnit_Framework_MockObject_MockObject */
private $searchBackend;
protected function setUp() {
parent::setUp();
$this->searchBackend = $this->getMockBuilder(ISearchBackend::class)
->getMock();
}
public function testHandleParseException() {
$this->searchBackend->expects($this->any())
->method('getArbiterPath')
->willReturn('foo');
$request = new Request('SEARCH', 'foo', [
'Content-Type' => 'text/xml'
], fopen(__DIR__ . '/nofrom.xml', 'r'));
$response = new Response();
$plugin = new SearchPlugin($this->searchBackend);
$plugin->searchHandler($request, $response);
$this->assertEquals(400, $response->getStatus());
}
public function testHTTPMethods() {
$this->searchBackend->expects($this->any())
->method('getArbiterPath')
->willReturn('foo');
$plugin = new SearchPlugin($this->searchBackend);
$server = new Server();
$server->setBaseUri('/index.php');
$plugin->initialize($server);
$this->assertEquals([], $plugin->getHTTPMethods('bar'));
$this->assertEquals(['SEARCH'], $plugin->getHTTPMethods('foo'));
$this->assertEquals([], $plugin->getHTTPMethods('http://example.com/index.php/bar'));
$this->assertEquals(['SEARCH'], $plugin->getHTTPMethods('http://example.com/index.php/foo'));
}
public function testOptionHandler() {
$this->searchBackend->expects($this->any())
->method('getArbiterPath')
->willReturn('foo');
$plugin = new SearchPlugin($this->searchBackend);
$request = new Request('OPTIONS', '/index.php/bar');
$request->setBaseUrl('/index.php');
$response = new Response();
$plugin->optionHandler($request, $response);
$this->assertEquals(false, $response->hasHeader('DASL'));
$request = new Request('OPTIONS', '/index.php/foo');
$request->setBaseUrl('/index.php');
$response = new Response();
$plugin->optionHandler($request, $response);
$this->assertEquals(true, $response->hasHeader('DASL'));
}
public function testSchemaDiscovery() {
$this->searchBackend->expects($this->any())
->method('getArbiterPath')
->willReturn('foo');
$plugin = new SearchPlugin($this->searchBackend);
$server = new Server();
$plugin->initialize($server);
$request = new Request('SEARCH', '/index.php/foo', [
'Content-Type' => 'text/xml'
]);
$request->setBaseUrl('/index.php');
$request->setBody(fopen(__DIR__ . '/discover.xml', 'r'));
$response = new Response();
$this->searchBackend->expects($this->once())
->method('isValidScope')
->willReturn(true);
$this->searchBackend->expects($this->once())
->method('getPropertyDefinitionsForScope')
->willReturn([
new SearchPropertyDefinition('{DAV:}getcontentlength', true, true, true, SearchPropertyDefinition::DATATYPE_NONNEGATIVE_INTEGER),
new SearchPropertyDefinition('{DAV:}getcontenttype', true, true, true),
new SearchPropertyDefinition('{DAV:}displayname', true, true, true),
new SearchPropertyDefinition('{http://ns.nextcloud.com:}fileid', false, true, true, SearchPropertyDefinition::DATATYPE_NONNEGATIVE_INTEGER),
]);
$plugin->searchHandler($request, $response);
$parser = new Service();
$parsedResponse = $parser->parse($response->getBody());
$expected = $parser->parse(fopen(__DIR__ . '/discoverresponse.xml', 'r'));
$this->assertEquals($expected, $parsedResponse);
}
public function testSearchQuery() {
$this->searchBackend->expects($this->any())
->method('getArbiterPath')
->willReturn('foo');
$plugin = new SearchPlugin($this->searchBackend);
$server = new Server();
$plugin->initialize($server);
$request = new Request('SEARCH', '/index.php/foo', [
'Content-Type' => 'text/xml'
]);
$request->setBaseUrl('/index.php');
$request->setBody(fopen(__DIR__ . '/basicquery.xml', 'r'));
$response = new Response();
$this->searchBackend->expects($this->any())
->method('isValidScope')
->willReturn(true);
$query = new BasicSearch();
$query->orderBy = [
new Order('{DAV:}getcontentlength', Order::ASC)
];
$query->select = ['{DAV:}getcontentlength'];
$query->from = [
new Scope('/container1/', 'infinity', '/container1/')
];
$query->where = new Operator(Operator::OPERATION_GREATER_THAN, [
'{DAV:}getcontentlength',
new Literal(10000)
]);
$this->searchBackend->expects($this->once())
->method('search')
->with($query)
->willReturn([
new SearchResult(
new Directory('/foo'),
'/foo'
)
]);
$plugin->searchHandler($request, $response);
$parser = new Service();
$parsedResponse = $parser->parse($response->getBody());
$expected = $parser->parse(fopen(__DIR__ . '/searchresult.xml', 'r'));
$this->assertEquals($expected, $parsedResponse);
}
}

11
tests/discover.xml Normal file
View file

@ -0,0 +1,11 @@
<?xml version="1.0"?>
<query-schema-discovery xmlns="DAV:">
<basicsearch>
<from>
<scope>
<href>/test</href>
<depth>infinity</depth>
</scope>
</from>
</basicsearch>
</query-schema-discovery>

View file

@ -0,0 +1,41 @@
<?xml version="1.0"?>
<d:multistatus xmlns:d="DAV:" xmlns:s="http://sabredav.org/ns"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<d:response>
<d:status>HTTP/1.1 200 OK</d:status>
<d:href>/test</d:href>
<d:query-schema>
<d:basicsearchschema>
<d:properties>
<d:propdesc>
<d:prop>{DAV:}getcontentlength</d:prop>
<d:dataType>
<xs:nonNegativeInteger/>
</d:dataType>
<d:searchable/>
<d:sortable/>
<d:selectable/>
</d:propdesc>
<d:propdesc>
<d:prop>{DAV:}getcontenttype</d:prop>
<d:prop>{DAV:}displayname</d:prop>
<d:dataType>
<xs:string/>
</d:dataType>
<d:searchable/>
<d:sortable/>
<d:selectable/>
</d:propdesc>
<d:propdesc>
<d:prop>{http://ns.nextcloud.com:}fileid</d:prop>
<d:dataType>
<xs:nonNegativeInteger/>
</d:dataType>
<d:sortable/>
<d:selectable/>
</d:propdesc>
</d:properties>
</d:basicsearchschema>
</d:query-schema>
</d:response>
</d:multistatus>

26
tests/nofrom.xml Normal file
View file

@ -0,0 +1,26 @@
<?xml version="1.0"?>
<d:searchrequest xmlns:d="DAV:">
<d:basicsearch>
<d:select>
<d:prop>
<d:getcontentlength/>
</d:prop>
</d:select>
<d:where>
<d:gt>
<d:prop>
<d:getcontentlength/>
</d:prop>
<d:literal>10000</d:literal>
</d:gt>
</d:where>
<d:orderby>
<d:order>
<d:prop>
<d:getcontentlength/>
</d:prop>
<d:ascending/>
</d:order>
</d:orderby>
</d:basicsearch>
</d:searchrequest>

23
tests/noorder.xml Normal file
View file

@ -0,0 +1,23 @@
<?xml version="1.0"?>
<d:searchrequest xmlns:d="DAV:">
<d:basicsearch>
<d:select>
<d:prop>
<d:getcontentlength/>
</d:prop>
</d:select>
<d:from>
<d:scope>
<d:href>/container1/</d:href>
<d:depth>infinity</d:depth>
</d:scope>
<d:scope>
<d:href>/container2/</d:href>
<d:depth>1</d:depth>
</d:scope>
</d:from>
<d:where>
<d:is-collection/>
</d:where>
</d:basicsearch>
</d:searchrequest>

27
tests/noselect.xml Normal file
View file

@ -0,0 +1,27 @@
<?xml version="1.0"?>
<d:searchrequest xmlns:d="DAV:">
<d:basicsearch>
<d:from>
<d:scope>
<d:href>/container1/</d:href>
<d:depth>infinity</d:depth>
</d:scope>
</d:from>
<d:where>
<d:gt>
<d:prop>
<d:getcontentlength/>
</d:prop>
<d:literal>10000</d:literal>
</d:gt>
</d:where>
<d:orderby>
<d:order>
<d:prop>
<d:getcontentlength/>
</d:prop>
<d:ascending/>
</d:order>
</d:orderby>
</d:basicsearch>
</d:searchrequest>

24
tests/nowhere.xml Normal file
View file

@ -0,0 +1,24 @@
<?xml version="1.0"?>
<d:searchrequest xmlns:d="DAV:">
<d:basicsearch>
<d:select>
<d:prop>
<d:getcontentlength/>
</d:prop>
</d:select>
<d:from>
<d:scope>
<d:href>/container1/</d:href>
<d:depth>infinity</d:depth>
</d:scope>
</d:from>
<d:orderby>
<d:order>
<d:prop>
<d:getcontentlength/>
</d:prop>
<d:ascending/>
</d:order>
</d:orderby>
</d:basicsearch>
</d:searchrequest>

12
tests/searchresult.xml Normal file
View file

@ -0,0 +1,12 @@
<?xml version="1.0"?>
<d:multistatus xmlns:d="DAV:" xmlns:s="http://sabredav.org/ns">
<d:response>
<d:href>foo/</d:href>
<d:propstat>
<d:prop>
<d:getcontentlength/>
</d:prop>
<d:status>HTTP/1.1 404 Not Found</d:status>
</d:propstat>
</d:response>
</d:multistatus>