From dd424fcb7b26745c6bebc323df282509c28bd563 Mon Sep 17 00:00:00 2001 From: Georg Ehrke Date: Tue, 25 Apr 2017 16:37:13 +0200 Subject: [PATCH] unit test CalDAV Search Plugin Signed-off-by: Georg Ehrke --- apps/dav/lib/CalDAV/Search/SearchPlugin.php | 6 +- .../Xml/Request/CalendarSearchReport.php | 14 +- .../Request/CalendarSearchReportTest.php | 335 ++++++++++++++++++ .../unit/CalDAV/Search/SearchPluginTest.php | 124 +++++++ 4 files changed, 469 insertions(+), 10 deletions(-) create mode 100644 apps/dav/tests/unit/CalDAV/Search/Request/CalendarSearchReportTest.php create mode 100644 apps/dav/tests/unit/CalDAV/Search/SearchPluginTest.php diff --git a/apps/dav/lib/CalDAV/Search/SearchPlugin.php b/apps/dav/lib/CalDAV/Search/SearchPlugin.php index ad36f39e23..d658a50437 100644 --- a/apps/dav/lib/CalDAV/Search/SearchPlugin.php +++ b/apps/dav/lib/CalDAV/Search/SearchPlugin.php @@ -86,9 +86,9 @@ class SearchPlugin extends ServerPlugin { * @param mixed $path * @return bool */ - function report($reportName, $report, $path) { + public function report($reportName, $report, $path) { switch ($reportName) { - case '{' . self::NS_Nextcloud . '}calendar-search' : + case '{' . self::NS_Nextcloud . '}calendar-search': $this->server->transactionType = 'report-nc-calendar-search'; $this->calendarSearch($report); return false; @@ -127,7 +127,7 @@ class SearchPlugin extends ServerPlugin { */ private function calendarSearch($report) { $node = $this->server->tree->getNodeForPath($this->server->getRequestUri()); - $depth = $this->server->getHTTPDepth(0); + $depth = $this->server->getHTTPDepth(2); // The default result is an empty array $result = []; diff --git a/apps/dav/lib/CalDAV/Search/Xml/Request/CalendarSearchReport.php b/apps/dav/lib/CalDAV/Search/Xml/Request/CalendarSearchReport.php index cda5d6dcc2..2c92a41e16 100644 --- a/apps/dav/lib/CalDAV/Search/Xml/Request/CalendarSearchReport.php +++ b/apps/dav/lib/CalDAV/Search/Xml/Request/CalendarSearchReport.php @@ -20,7 +20,6 @@ */ namespace OCA\DAV\CalDAV\Search\Xml\Request; -use Sabre\CalDAV\Plugin; use Sabre\DAV\Exception\BadRequest; use Sabre\Xml\Reader; use Sabre\Xml\XmlDeserializable; @@ -107,24 +106,21 @@ class CalendarSearchReport implements XmlDeserializable { switch ($elem['name']) { case '{DAV:}prop': $newProps['properties'] = array_keys($elem['value']); - if (isset($elem['value']['{' . Plugin::NS_CALDAV . '}calendar-data'])) { - $newProps += $elem['value']['{' . Plugin::NS_CALDAV . '}calendar-data']; - } break; case '{' . SearchPlugin::NS_Nextcloud . '}filter': foreach ($elem['value'] as $subElem) { if ($subElem['name'] === '{' . SearchPlugin::NS_Nextcloud . '}comp-filter') { - if (!is_array($newProps['filters']['comps'])) { + if (!isset($newProps['filters']['comps']) || !is_array($newProps['filters']['comps'])) { $newProps['filters']['comps'] = []; } $newProps['filters']['comps'][] = $subElem['value']; } elseif ($subElem['name'] === '{' . SearchPlugin::NS_Nextcloud . '}prop-filter') { - if (!is_array($newProps['filters']['props'])) { + if (!isset($newProps['filters']['props']) || !is_array($newProps['filters']['props'])) { $newProps['filters']['props'] = []; } $newProps['filters']['props'][] = $subElem['value']; } elseif ($subElem['name'] === '{' . SearchPlugin::NS_Nextcloud . '}param-filter') { - if (!is_array($newProps['filters']['params'])) { + if (!isset($newProps['filters']['params']) || !is_array($newProps['filters']['params'])) { $newProps['filters']['params'] = []; } $newProps['filters']['params'][] = $subElem['value']; @@ -153,6 +149,10 @@ class CalendarSearchReport implements XmlDeserializable { throw new BadRequest('{' . SearchPlugin::NS_Nextcloud . '}prop-filter or {' . SearchPlugin::NS_Nextcloud . '}param-filter given without any {' . SearchPlugin::NS_Nextcloud . '}comp-filter'); } + if (!isset($newProps['filters']['search-term'])) { + throw new BadRequest('{' . SearchPlugin::NS_Nextcloud . '}search-term is required for this request'); + } + $obj = new self(); foreach ($newProps as $key => $value) { diff --git a/apps/dav/tests/unit/CalDAV/Search/Request/CalendarSearchReportTest.php b/apps/dav/tests/unit/CalDAV/Search/Request/CalendarSearchReportTest.php new file mode 100644 index 0000000000..15c776db07 --- /dev/null +++ b/apps/dav/tests/unit/CalDAV/Search/Request/CalendarSearchReportTest.php @@ -0,0 +1,335 @@ + + * + * @copyright Copyright (c) 2017 Georg Ehrke + * @license GNU AGPL version 3 or any later version + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * 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, version 3, + * along with this program. If not, see + * + */ + +namespace OCA\DAV\Tests\unit\CalDAV\Search\Xml\Request; + +use OCA\DAV\CalDAV\Search\Xml\Request\CalendarSearchReport; +use Sabre\Xml\Reader; +use Test\TestCase; + +class CalendarSearchReportTest extends TestCase { + + private $elementMap = [ + '{http://nextcloud.com/ns}calendar-search' => + 'OCA\\DAV\\CalDAV\\Search\\Xml\\Request\\CalendarSearchReport', + ]; + + public function testFoo() { + $xml = << + + + + + + + + + + + + + foo + + 10 + 5 + +XML; + + $result = $this->parse($xml); + + $calendarSearchReport = new CalendarSearchReport(); + $calendarSearchReport->properties = [ + '{DAV:}getetag', + '{urn:ietf:params:xml:ns:caldav}calendar-data', + ]; + $calendarSearchReport->filters = [ + 'comps' => [ + 'VEVENT', + 'VTODO' + ], + 'props' => [ + 'SUMMARY', + 'LOCATION', + 'ATTENDEE' + ], + 'params' => [ + [ + 'property' => 'ATTENDEE', + 'parameter' => 'CN' + ] + ], + 'search-term' => 'foo' + ]; + $calendarSearchReport->limit = 10; + $calendarSearchReport->offset = 5; + + $this->assertEquals( + $calendarSearchReport, + $result['value'] + ); + } + + public function testNoLimitOffset() { + $xml = << + + + + + + + + + foo + + +XML; + + $result = $this->parse($xml); + + $calendarSearchReport = new CalendarSearchReport(); + $calendarSearchReport->properties = [ + '{DAV:}getetag', + '{urn:ietf:params:xml:ns:caldav}calendar-data', + ]; + $calendarSearchReport->filters = [ + 'comps' => [ + 'VEVENT', + ], + 'props' => [ + 'SUMMARY', + ], + 'search-term' => 'foo' + ]; + $calendarSearchReport->limit = null; + $calendarSearchReport->offset = null; + + $this->assertEquals( + $calendarSearchReport, + $result['value'] + ); + } + + /** + * @expectedException \Sabre\DAV\Exception\BadRequest + * @expectedExceptionMessage {http://nextcloud.com/ns}prop-filter or {http://nextcloud.com/ns}param-filter given without any {http://nextcloud.com/ns}comp-filter + */ + public function testRequiresCompFilter() { + $xml = << + + + + + + + + + + + foo + + 10 + 5 + +XML; + + $this->parse($xml); + } + + /** + * @expectedException \Sabre\DAV\Exception\BadRequest + * @expectedExceptionMessage The {http://nextcloud.com/ns}filter element is required for this request + */ + public function testRequiresFilter() { + $xml = << + + + + + + +XML; + + $this->parse($xml); + } + + /** + * @expectedException \Sabre\DAV\Exception\BadRequest + * @expectedExceptionMessage {http://nextcloud.com/ns}search-term is required for this request + */ + public function testNoSearchTerm() { + $xml = << + + + + + + + + + + + + + + 10 + 5 + +XML; + + $result = $this->parse($xml); + } + + public function testCompOnly() { + $xml = << + + + + + + + + + foo + + +XML; + + $result = $this->parse($xml); + + $calendarSearchReport = new CalendarSearchReport(); + $calendarSearchReport->properties = [ + '{DAV:}getetag', + '{urn:ietf:params:xml:ns:caldav}calendar-data', + ]; + $calendarSearchReport->filters = [ + 'comps' => [ + 'VEVENT', + 'VTODO' + ], + 'search-term' => 'foo' + ]; + $calendarSearchReport->limit = null; + $calendarSearchReport->offset = null; + + $this->assertEquals( + $calendarSearchReport, + $result['value'] + ); + } + + public function testPropOnly() { + $xml = << + + + + + + + + + foo + + +XML; + + $result = $this->parse($xml); + + $calendarSearchReport = new CalendarSearchReport(); + $calendarSearchReport->properties = [ + '{DAV:}getetag', + '{urn:ietf:params:xml:ns:caldav}calendar-data', + ]; + $calendarSearchReport->filters = [ + 'comps' => [ + 'VEVENT', + ], + 'props' => [ + 'SUMMARY', + ], + 'search-term' => 'foo' + ]; + $calendarSearchReport->limit = null; + $calendarSearchReport->offset = null; + + $this->assertEquals( + $calendarSearchReport, + $result['value'] + ); + } + + public function testParamOnly() { + $xml = << + + + + + + + + + foo + + +XML; + + $result = $this->parse($xml); + + $calendarSearchReport = new CalendarSearchReport(); + $calendarSearchReport->properties = [ + '{DAV:}getetag', + '{urn:ietf:params:xml:ns:caldav}calendar-data', + ]; + $calendarSearchReport->filters = [ + 'comps' => [ + 'VEVENT', + ], + 'params' => [ + [ + 'property' => 'ATTENDEE', + 'parameter' => 'CN' + ] + ], + 'search-term' => 'foo' + ]; + $calendarSearchReport->limit = null; + $calendarSearchReport->offset = null; + + $this->assertEquals( + $calendarSearchReport, + $result['value'] + ); + } + + private function parse($xml, array $elementMap = []) { + $reader = new Reader(); + $reader->elementMap = array_merge($this->elementMap, $elementMap); + $reader->xml($xml); + return $reader->parse(); + } +} diff --git a/apps/dav/tests/unit/CalDAV/Search/SearchPluginTest.php b/apps/dav/tests/unit/CalDAV/Search/SearchPluginTest.php new file mode 100644 index 0000000000..fc647bb5da --- /dev/null +++ b/apps/dav/tests/unit/CalDAV/Search/SearchPluginTest.php @@ -0,0 +1,124 @@ + + * + * @copyright Copyright (c) 2017 Georg Ehrke + * @license GNU AGPL version 3 or any later version + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * 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, version 3, + * along with this program. If not, see + * + */ + +namespace OCA\DAV\Tests\unit\CalDAV\Search; + +use OCA\DAV\CalDAV\CalendarHome; +use OCA\DAV\CalDAV\Search\SearchPlugin; +use OCA\DAV\CalDAV\Search\Xml\Request\CalendarSearchReport; +use Test\TestCase; + +class SearchPluginTest extends TestCase { + + protected $server; + + /** @var \OCA\DAV\CalDAV\Search\SearchPlugin $plugin */ + protected $plugin; + + public function setUp() { + parent::setUp(); + + $this->server = $this->createMock(\Sabre\DAV\Server::class); + $this->server->tree = $this->createMock(\Sabre\DAV\Tree::class); + $this->server->httpResponse = $this->createMock(\Sabre\HTTP\Response::class); + + $this->plugin = new SearchPlugin(); + $this->plugin->initialize($this->server); + } + + public function testGetFeatures() { + $this->assertEquals(['nc-calendar-search'], $this->plugin->getFeatures()); + } + + public function testGetName() { + $this->assertEquals('nc-calendar-search', $this->plugin->getPluginName()); + } + + public function testInitialize() { + $server = $this->createMock(\Sabre\DAV\Server::class); + + $plugin = new SearchPlugin(); + + $server->expects($this->at(0)) + ->method('on') + ->with('report', [$plugin, 'report']); + + $plugin->initialize($server); + + $this->assertEquals( + $server->xml->elementMap['{http://nextcloud.com/ns}calendar-search'], + 'OCA\\DAV\\CalDAV\\Search\\Xml\\Request\\CalendarSearchReport' + ); + } + + public function testReportUnknown() { + $result = $this->plugin->report('{urn:ietf:params:xml:ns:caldav}calendar-query', 'REPORT', null); + $this->assertEquals($result, null); + $this->assertNotEquals($this->server->transactionType, 'report-nc-calendar-search'); + } + + public function testReport() { + $report = $this->createMock(CalendarSearchReport::class); + $report->filters = []; + $calendarHome = $this->createMock(CalendarHome::class); + $this->server->expects($this->at(0)) + ->method('getRequestUri') + ->with() + ->will($this->returnValue('/re/quest/u/r/i')); + $this->server->tree->expects($this->at(0)) + ->method('getNodeForPath') + ->with('/re/quest/u/r/i') + ->will($this->returnValue($calendarHome)); + $this->server->expects($this->at(1)) + ->method('getHTTPDepth') + ->with(2) + ->will($this->returnValue(2)); + $calendarHome->expects($this->at(0)) + ->method('calendarSearch') + ->will($this->returnValue([])); + + $this->plugin->report('{http://nextcloud.com/ns}calendar-search', $report, ''); + } + + public function testSupportedReportSetNoCalendarHome() { + $this->server->tree->expects($this->once()) + ->method('getNodeForPath') + ->with('/foo/bar') + ->will($this->returnValue(null)); + + $reports = $this->plugin->getSupportedReportSet('/foo/bar'); + $this->assertEquals([], $reports); + } + + public function testSupportedReportSet() { + $calendarHome = $this->createMock(CalendarHome::class); + + $this->server->tree->expects($this->once()) + ->method('getNodeForPath') + ->with('/bar/foo') + ->will($this->returnValue($calendarHome)); + + $reports = $this->plugin->getSupportedReportSet('/bar/foo'); + $this->assertEquals([ + '{http://nextcloud.com/ns}calendar-search' + ], $reports); + } +}