2017-04-25 10:37:13 -04:00
< ? php
2025-05-24 17:00:05 -04:00
declare ( strict_types = 1 );
2017-04-25 10:37:13 -04:00
/**
2024-05-28 06:34:11 -04:00
* SPDX - FileCopyrightText : 2017 Nextcloud GmbH and Nextcloud contributors
* SPDX - License - Identifier : AGPL - 3.0 - or - later
2017-04-25 10:37:13 -04:00
*/
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 {
2025-05-24 17:00:05 -04:00
private array $elementMap = [
2017-04-25 10:37:13 -04:00
'{http://nextcloud.com/ns}calendar-search'
=> 'OCA\\DAV\\CalDAV\\Search\\Xml\\Request\\CalendarSearchReport' ,
];
2023-01-20 02:38:43 -05:00
public function testFoo () : void {
2017-04-25 10:37:13 -04:00
$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' ]
);
}
2023-01-20 02:38:43 -05:00
public function testNoLimitOffset () : void {
2017-04-25 10:37:13 -04:00
$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' ]
);
}
2025-05-24 17:00:05 -04:00
2023-01-20 02:38:43 -05:00
public function testRequiresCompFilter () : void {
2019-11-27 09:27:18 -05:00
$this -> expectException ( \Sabre\DAV\Exception\BadRequest :: class );
$this -> expectExceptionMessage ( '{http://nextcloud.com/ns}prop-filter or {http://nextcloud.com/ns}param-filter given without any {http://nextcloud.com/ns}comp-filter' );
2017-04-25 10:37:13 -04:00
$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 );
}
2025-05-24 17:00:05 -04:00
2023-01-20 02:38:43 -05:00
public function testRequiresFilter () : void {
2019-11-27 09:27:18 -05:00
$this -> expectException ( \Sabre\DAV\Exception\BadRequest :: class );
$this -> expectExceptionMessage ( 'The {http://nextcloud.com/ns}filter element is required for this request' );
2017-04-25 10:37:13 -04:00
$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 );
}
2025-05-24 17:00:05 -04:00
2023-01-20 02:38:43 -05:00
public function testNoSearchTerm () : void {
2019-11-27 09:27:18 -05:00
$this -> expectException ( \Sabre\DAV\Exception\BadRequest :: class );
$this -> expectExceptionMessage ( '{http://nextcloud.com/ns}search-term is required for this request' );
2017-04-25 10:37:13 -04:00
$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 ;
2017-11-06 03:43:45 -05:00
$this -> parse ( $xml );
2017-04-25 10:37:13 -04:00
}
2025-05-24 17:00:05 -04:00
2023-01-20 02:38:43 -05:00
public function testCompOnly () : void {
2019-11-27 09:27:18 -05:00
$this -> expectException ( \Sabre\DAV\Exception\BadRequest :: class );
$this -> expectExceptionMessage ( 'At least one{http://nextcloud.com/ns}prop-filter or {http://nextcloud.com/ns}param-filter is required for this request' );
2017-04-25 10:37:13 -04:00
$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' ]
);
}
2023-01-20 02:38:43 -05:00
public function testPropOnly () : void {
2017-04-25 10:37:13 -04:00
$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' ]
);
}
2023-01-20 02:38:43 -05:00
public function testParamOnly () : void {
2017-04-25 10:37:13 -04:00
$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' ]
);
}
2025-05-24 17:00:05 -04:00
private function parse ( string $xml , array $elementMap = []) : array {
2017-04-25 10:37:13 -04:00
$reader = new Reader ();
$reader -> elementMap = array_merge ( $this -> elementMap , $elementMap );
$reader -> xml ( $xml );
return $reader -> parse ();
}
}