parent
40eec1e63c
commit
dd424fcb7b
@ -0,0 +1,335 @@ |
||||
<?php |
||||
/** |
||||
* @author Georg Ehrke <oc.list@georgehrke.com> |
||||
* |
||||
* @copyright Copyright (c) 2017 Georg Ehrke <oc.list@georgehrke.com> |
||||
* @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 <http://www.gnu.org/licenses/> |
||||
* |
||||
*/ |
||||
|
||||
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 = <<<XML |
||||
<?xml version="1.0" encoding="UTF-8"?> |
||||
<nc:calendar-search xmlns:nc="http://nextcloud.com/ns" xmlns:c="urn:ietf:params:xml:ns:caldav" xmlns:d="DAV:"> |
||||
<d:prop> |
||||
<d:getetag /> |
||||
<c:calendar-data /> |
||||
</d:prop> |
||||
<nc:filter> |
||||
<nc:comp-filter name="VEVENT" /> |
||||
<nc:comp-filter name="VTODO" /> |
||||
<nc:prop-filter name="SUMMARY" /> |
||||
<nc:prop-filter name="LOCATION" /> |
||||
<nc:prop-filter name="ATTENDEE" /> |
||||
<nc:param-filter property="ATTENDEE" name="CN" /> |
||||
<nc:search-term>foo</nc:search-term> |
||||
</nc:filter> |
||||
<nc:limit>10</nc:limit> |
||||
<nc:offset>5</nc:offset> |
||||
</nc:calendar-search> |
||||
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 = <<<XML |
||||
<?xml version="1.0" encoding="UTF-8"?> |
||||
<nc:calendar-search xmlns:nc="http://nextcloud.com/ns" xmlns:c="urn:ietf:params:xml:ns:caldav" xmlns:d="DAV:"> |
||||
<d:prop> |
||||
<d:getetag /> |
||||
<c:calendar-data /> |
||||
</d:prop> |
||||
<nc:filter> |
||||
<nc:comp-filter name="VEVENT" /> |
||||
<nc:prop-filter name="SUMMARY" /> |
||||
<nc:search-term>foo</nc:search-term> |
||||
</nc:filter> |
||||
</nc:calendar-search> |
||||
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 = <<<XML |
||||
<?xml version="1.0" encoding="UTF-8"?> |
||||
<nc:calendar-search xmlns:nc="http://nextcloud.com/ns" xmlns:c="urn:ietf:params:xml:ns:caldav" xmlns:d="DAV:"> |
||||
<d:prop> |
||||
<d:getetag /> |
||||
<c:calendar-data /> |
||||
</d:prop> |
||||
<nc:filter> |
||||
<nc:prop-filter name="SUMMARY" /> |
||||
<nc:prop-filter name="LOCATION" /> |
||||
<nc:prop-filter name="ATTENDEE" /> |
||||
<nc:param-filter property="ATTENDEE" name="CN" /> |
||||
<nc:search-term>foo</nc:search-term> |
||||
</nc:filter> |
||||
<nc:limit>10</nc:limit> |
||||
<nc:offset>5</nc:offset> |
||||
</nc:calendar-search> |
||||
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 |
||||
<?xml version="1.0" encoding="UTF-8"?> |
||||
<nc:calendar-search xmlns:nc="http://nextcloud.com/ns" xmlns:c="urn:ietf:params:xml:ns:caldav" xmlns:d="DAV:"> |
||||
<d:prop> |
||||
<d:getetag /> |
||||
<c:calendar-data /> |
||||
</d:prop> |
||||
</nc:calendar-search> |
||||
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 = <<<XML |
||||
<?xml version="1.0" encoding="UTF-8"?> |
||||
<nc:calendar-search xmlns:nc="http://nextcloud.com/ns" xmlns:c="urn:ietf:params:xml:ns:caldav" xmlns:d="DAV:"> |
||||
<d:prop> |
||||
<d:getetag /> |
||||
<c:calendar-data /> |
||||
</d:prop> |
||||
<nc:filter> |
||||
<nc:comp-filter name="VEVENT" /> |
||||
<nc:comp-filter name="VTODO" /> |
||||
<nc:prop-filter name="SUMMARY" /> |
||||
<nc:prop-filter name="LOCATION" /> |
||||
<nc:prop-filter name="ATTENDEE" /> |
||||
<nc:param-filter property="ATTENDEE" name="CN" /> |
||||
</nc:filter> |
||||
<nc:limit>10</nc:limit> |
||||
<nc:offset>5</nc:offset> |
||||
</nc:calendar-search> |
||||
XML; |
||||
|
||||
$result = $this->parse($xml); |
||||
} |
||||
|
||||
public function testCompOnly() { |
||||
$xml = <<<XML |
||||
<?xml version="1.0" encoding="UTF-8"?> |
||||
<nc:calendar-search xmlns:nc="http://nextcloud.com/ns" xmlns:c="urn:ietf:params:xml:ns:caldav" xmlns:d="DAV:"> |
||||
<d:prop> |
||||
<d:getetag /> |
||||
<c:calendar-data /> |
||||
</d:prop> |
||||
<nc:filter> |
||||
<nc:comp-filter name="VEVENT" /> |
||||
<nc:comp-filter name="VTODO" /> |
||||
<nc:search-term>foo</nc:search-term> |
||||
</nc:filter> |
||||
</nc:calendar-search> |
||||
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 = <<<XML |
||||
<?xml version="1.0" encoding="UTF-8"?> |
||||
<nc:calendar-search xmlns:nc="http://nextcloud.com/ns" xmlns:c="urn:ietf:params:xml:ns:caldav" xmlns:d="DAV:"> |
||||
<d:prop> |
||||
<d:getetag /> |
||||
<c:calendar-data /> |
||||
</d:prop> |
||||
<nc:filter> |
||||
<nc:comp-filter name="VEVENT" /> |
||||
<nc:prop-filter name="SUMMARY" /> |
||||
<nc:search-term>foo</nc:search-term> |
||||
</nc:filter> |
||||
</nc:calendar-search> |
||||
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 = <<<XML |
||||
<?xml version="1.0" encoding="UTF-8"?> |
||||
<nc:calendar-search xmlns:nc="http://nextcloud.com/ns" xmlns:c="urn:ietf:params:xml:ns:caldav" xmlns:d="DAV:"> |
||||
<d:prop> |
||||
<d:getetag /> |
||||
<c:calendar-data /> |
||||
</d:prop> |
||||
<nc:filter> |
||||
<nc:comp-filter name="VEVENT" /> |
||||
<nc:param-filter property="ATTENDEE" name="CN" /> |
||||
<nc:search-term>foo</nc:search-term> |
||||
</nc:filter> |
||||
</nc:calendar-search> |
||||
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(); |
||||
} |
||||
} |
@ -0,0 +1,124 @@ |
||||
<?php |
||||
/** |
||||
* @author Georg Ehrke <oc.list@georgehrke.com> |
||||
* |
||||
* @copyright Copyright (c) 2017 Georg Ehrke <oc.list@georgehrke.com> |
||||
* @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 <http://www.gnu.org/licenses/> |
||||
* |
||||
*/ |
||||
|
||||
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); |
||||
} |
||||
} |
Loading…
Reference in new issue