add more tests

This commit is contained in:
Robin Appelman 2017-02-22 18:14:08 +01:00
commit 5c2a3f1b02
11 changed files with 337 additions and 5 deletions

View file

@ -55,8 +55,10 @@ class DiscoverHandler {
}
public function handelDiscoverRequest($xml, RequestInterface $request, ResponseInterface $response) {
if (!$xml['{DAV:}basicsearch']) {
throw new BadRequest('Unexpected xml content for query-schema-discovery, expected basicsearch');
if (!isset($xml['{DAV:}basicsearch'])) {
$response->setStatus(400);
$response->setBody('Unexpected xml content for query-schema-discovery, expected basicsearch');
return false;
}
/** @var BasicSearch $query */
$query = $xml['{DAV:}basicsearch'];

View file

@ -51,8 +51,10 @@ class SearchHandler {
}
public function handleSearchRequest($xml, ResponseInterface $response) {
if (!$xml['{DAV:}basicsearch']) {
throw new BadRequest('Unexpected xml content for searchrequest, expected basicsearch');
if (!isset($xml['{DAV:}basicsearch'])) {
$response->setStatus(400);
$response->setBody('Unexpected xml content for searchrequest, expected basicsearch');
return false;
}
/** @var BasicSearch $query */
$query = $xml['{DAV:}basicsearch'];

View file

@ -135,7 +135,9 @@ class SearchPlugin extends ServerPlugin {
case '{DAV:}query-schema-discovery':
return $this->discover->handelDiscoverRequest($xml, $request, $response);
default:
throw new BadRequest('Unexpected document type: ' . $documentType . ' for this Content-Type');
$response->setStatus(400);
$response->setBody('Unexpected document type: ' . $documentType . ' for this Content-Type, expected {DAV:}searchrequest or {DAV:}query-schema-discovery');
return false;
}
}
}

51
tests/PathHelperTest.php Normal file
View file

@ -0,0 +1,51 @@
<?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\Server;
use SearchDAV\DAV\PathHelper;
class PathHelperTest extends \PHPUnit_Framework_TestCase {
public function uriProvider(){
return [
['/', '', ''],
['/index.php/', 'foo', 'foo'],
['/index.php/', 'http://example.com/index.php/foo', 'foo'],
['/index.php/', 'http://example.com/foo', null]
];
}
/**
* @dataProvider uriProvider
*
* @param $baseUri
* @param $uri
* @param $expected
*/
public function testGetPathFromUri($baseUri, $uri, $expected) {
$server = new Server();
$server->setBaseUri($baseUri);
$pathHelper = new PathHelper($server);
$this->assertEquals($expected, $pathHelper->getPathFromUri($uri));
}
}

View file

@ -22,12 +22,14 @@
namespace SearchDAV\Test;
use Sabre\Xml\Service;
use SearchDAV\DAV\QueryParser;
use SearchDAV\XML\BasicSearch;
use SearchDAV\XML\Literal;
use SearchDAV\XML\Operator;
use SearchDAV\XML\Order;
use SearchDAV\XML\Scope;
use SearchDAV\XML\SupportedQueryGrammar;
class QueryParserTest extends \PHPUnit_Framework_TestCase {
@ -85,4 +87,14 @@ class QueryParserTest extends \PHPUnit_Framework_TestCase {
$parser = new QueryParser();
$parser->parse($query, null, $rootElementName);
}
public function testSerializeSupportedGrammar() {
$supportedGrammar = new SupportedQueryGrammar();
$parser = new QueryParser();
$serialized = $parser->write('{DAV:}supported-query-grammar-set', $supportedGrammar);
$xml = new Service();
$this->assertEquals($xml->parse(fopen(__DIR__ . '/supportedgrammar.xml', 'r')), $xml->parse($serialized));
}
}

View file

@ -23,6 +23,8 @@ namespace SearchDAV\Test;
use Sabre\DAV\FS\Directory;
use Sabre\DAV\INode;
use Sabre\DAV\PropFind;
use Sabre\DAV\Server;
use Sabre\DAV\Xml\Service;
use Sabre\HTTP\Request;
@ -37,6 +39,7 @@ use SearchDAV\XML\Literal;
use SearchDAV\XML\Operator;
use SearchDAV\XML\Order;
use SearchDAV\XML\Scope;
use SearchDAV\XML\SupportedQueryGrammar;
class SearchPluginTest extends \PHPUnit_Framework_TestCase {
/** @var ISearchBackend|\PHPUnit_Framework_MockObject_MockObject */
@ -49,6 +52,53 @@ class SearchPluginTest extends \PHPUnit_Framework_TestCase {
->getMock();
}
public function testNoXmlBody() {
$this->searchBackend->expects($this->any())
->method('getArbiterPath')
->willReturn('foo');
$request = new Request('SEARCH', 'foo', [
'Content-Type' => 'text/plain'
], fopen(__DIR__ . '/nofrom.xml', 'r'));
$response = new Response();
$plugin = new SearchPlugin($this->searchBackend);
$this->assertNotEquals(false, $plugin->searchHandler($request, $response));
}
public function testNotArbiterPath() {
$this->searchBackend->expects($this->any())
->method('getArbiterPath')
->willReturn('foo');
$request = new Request('SEARCH', 'bar', [
'Content-Type' => 'text/xml'
], fopen(__DIR__ . '/nofrom.xml', 'r'));
$response = new Response();
$plugin = new SearchPlugin($this->searchBackend);
$this->assertNotEquals(false, $plugin->searchHandler($request, $response));
}
public function testInvalidType() {
$this->searchBackend->expects($this->any())
->method('getArbiterPath')
->willReturn('foo');
$request = new Request('SEARCH', 'foo', [
'Content-Type' => 'text/xml'
], fopen(__DIR__ . '/invalidtype.xml', 'r'));
$response = new Response();
$plugin = new SearchPlugin($this->searchBackend);
$plugin->searchHandler($request, $response);
$this->assertEquals(400, $response->getStatus());
}
public function testHandleParseException() {
$this->searchBackend->expects($this->any())
->method('getArbiterPath')
@ -146,6 +196,64 @@ class SearchPluginTest extends \PHPUnit_Framework_TestCase {
$this->assertEquals($expected, $parsedResponse);
}
public function testSchemaDiscoveryInvalidScope() {
$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(false);
$this->searchBackend->expects($this->never())
->method('getPropertyDefinitionsForScope');
$plugin->searchHandler($request, $response);
$parser = new Service();
$parsedResponse = $parser->parse($response->getBody());
$expected = $parser->parse(fopen(__DIR__ . '/invalidscoperesponse.xml', 'r'));
$this->assertEquals($expected, $parsedResponse);
}
public function testSchemaDiscoveryInvalid() {
$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__ . '/invaliddiscover.xml', 'r'));
$response = new Response();
$this->searchBackend->expects($this->never())
->method('isValidScope');
$this->searchBackend->expects($this->never())
->method('getPropertyDefinitionsForScope');
$plugin->searchHandler($request, $response);
$this->assertEquals(400, $response->getStatus());
}
public function testSearchQuery() {
$this->searchBackend->expects($this->any())
->method('getArbiterPath')
@ -252,4 +360,81 @@ class SearchPluginTest extends \PHPUnit_Framework_TestCase {
$this->assertEquals(400, $response->getStatus());
}
public function testSearchQueryNoSelect() {
$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__ . '/noselect.xml', 'r'));
$response = new Response();
$this->searchBackend->expects($this->any())
->method('isValidScope')
->willReturn(true);
$this->searchBackend->expects($this->never())
->method('search');
$plugin->searchHandler($request, $response);
$this->assertEquals(400, $response->getStatus());
}
public function testSearchInvalid() {
$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__ . '/invalid.xml', 'r'));
$response = new Response();
$this->searchBackend->expects($this->any())
->method('isValidScope')
->willReturn(true);
$this->searchBackend->expects($this->never())
->method('search');
$plugin->searchHandler($request, $response);
$this->assertEquals(400, $response->getStatus());
}
public function testPropFindHandler() {
$propFind = new PropFind('bar', ['{DAV:}supported-query-grammar-set']);
$this->searchBackend->expects($this->any())
->method('getArbiterPath')
->willReturn('foo');
$plugin = new SearchPlugin($this->searchBackend);
/** @var INode $node */
$node = $this->getMockBuilder(INode::class)->getMock();
$plugin->propFindHandler($propFind, $node);
$this->assertEquals(null, $propFind->get('{DAV:}supported-query-grammar-set'));
$propFind = new PropFind('foo', ['{DAV:}supported-query-grammar-set']);
$plugin->propFindHandler($propFind, $node);
$this->assertEquals(new SupportedQueryGrammar(), $propFind->get('{DAV:}supported-query-grammar-set'));
}
}

30
tests/invalid.xml Normal file
View file

@ -0,0 +1,30 @@
<?xml version="1.0"?>
<d:searchrequest xmlns:d="DAV:">
<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: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:searchrequest>

View file

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

View file

@ -0,0 +1,8 @@
<?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 404 Not Found</d:status>
<d:href>/test</d:href>
</d:response>
</d:multistatus>

22
tests/invalidtype.xml Normal file
View file

@ -0,0 +1,22 @@
<?xml version="1.0"?>
<d:basicsearch xmlns:d="DAV:">
<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>

View file

@ -0,0 +1,9 @@
<?xml version="1.0"?>
<d:supported-query-grammar-set xmlns:d="DAV:" xmlns:s="http://sabredav.org/ns"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<d:supported-query-grammar>
<d:grammar>
<d:basicsearch/>
</d:grammar>
</d:supported-query-grammar>
</d:supported-query-grammar-set>