CalendarSearchReportTest.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\DAV\Tests\unit\CalDAV\Search\Xml\Request;
  7. use OCA\DAV\CalDAV\Search\Xml\Request\CalendarSearchReport;
  8. use Sabre\Xml\Reader;
  9. use Test\TestCase;
  10. class CalendarSearchReportTest extends TestCase {
  11. private $elementMap = [
  12. '{http://nextcloud.com/ns}calendar-search' =>
  13. 'OCA\\DAV\\CalDAV\\Search\\Xml\\Request\\CalendarSearchReport',
  14. ];
  15. public function testFoo(): void {
  16. $xml = <<<XML
  17. <?xml version="1.0" encoding="UTF-8"?>
  18. <nc:calendar-search xmlns:nc="http://nextcloud.com/ns" xmlns:c="urn:ietf:params:xml:ns:caldav" xmlns:d="DAV:">
  19. <d:prop>
  20. <d:getetag />
  21. <c:calendar-data />
  22. </d:prop>
  23. <nc:filter>
  24. <nc:comp-filter name="VEVENT" />
  25. <nc:comp-filter name="VTODO" />
  26. <nc:prop-filter name="SUMMARY" />
  27. <nc:prop-filter name="LOCATION" />
  28. <nc:prop-filter name="ATTENDEE" />
  29. <nc:param-filter property="ATTENDEE" name="CN" />
  30. <nc:search-term>foo</nc:search-term>
  31. </nc:filter>
  32. <nc:limit>10</nc:limit>
  33. <nc:offset>5</nc:offset>
  34. </nc:calendar-search>
  35. XML;
  36. $result = $this->parse($xml);
  37. $calendarSearchReport = new CalendarSearchReport();
  38. $calendarSearchReport->properties = [
  39. '{DAV:}getetag',
  40. '{urn:ietf:params:xml:ns:caldav}calendar-data',
  41. ];
  42. $calendarSearchReport->filters = [
  43. 'comps' => [
  44. 'VEVENT',
  45. 'VTODO'
  46. ],
  47. 'props' => [
  48. 'SUMMARY',
  49. 'LOCATION',
  50. 'ATTENDEE'
  51. ],
  52. 'params' => [
  53. [
  54. 'property' => 'ATTENDEE',
  55. 'parameter' => 'CN'
  56. ]
  57. ],
  58. 'search-term' => 'foo'
  59. ];
  60. $calendarSearchReport->limit = 10;
  61. $calendarSearchReport->offset = 5;
  62. $this->assertEquals(
  63. $calendarSearchReport,
  64. $result['value']
  65. );
  66. }
  67. public function testNoLimitOffset(): void {
  68. $xml = <<<XML
  69. <?xml version="1.0" encoding="UTF-8"?>
  70. <nc:calendar-search xmlns:nc="http://nextcloud.com/ns" xmlns:c="urn:ietf:params:xml:ns:caldav" xmlns:d="DAV:">
  71. <d:prop>
  72. <d:getetag />
  73. <c:calendar-data />
  74. </d:prop>
  75. <nc:filter>
  76. <nc:comp-filter name="VEVENT" />
  77. <nc:prop-filter name="SUMMARY" />
  78. <nc:search-term>foo</nc:search-term>
  79. </nc:filter>
  80. </nc:calendar-search>
  81. XML;
  82. $result = $this->parse($xml);
  83. $calendarSearchReport = new CalendarSearchReport();
  84. $calendarSearchReport->properties = [
  85. '{DAV:}getetag',
  86. '{urn:ietf:params:xml:ns:caldav}calendar-data',
  87. ];
  88. $calendarSearchReport->filters = [
  89. 'comps' => [
  90. 'VEVENT',
  91. ],
  92. 'props' => [
  93. 'SUMMARY',
  94. ],
  95. 'search-term' => 'foo'
  96. ];
  97. $calendarSearchReport->limit = null;
  98. $calendarSearchReport->offset = null;
  99. $this->assertEquals(
  100. $calendarSearchReport,
  101. $result['value']
  102. );
  103. }
  104. public function testRequiresCompFilter(): void {
  105. $this->expectException(\Sabre\DAV\Exception\BadRequest::class);
  106. $this->expectExceptionMessage('{http://nextcloud.com/ns}prop-filter or {http://nextcloud.com/ns}param-filter given without any {http://nextcloud.com/ns}comp-filter');
  107. $xml = <<<XML
  108. <?xml version="1.0" encoding="UTF-8"?>
  109. <nc:calendar-search xmlns:nc="http://nextcloud.com/ns" xmlns:c="urn:ietf:params:xml:ns:caldav" xmlns:d="DAV:">
  110. <d:prop>
  111. <d:getetag />
  112. <c:calendar-data />
  113. </d:prop>
  114. <nc:filter>
  115. <nc:prop-filter name="SUMMARY" />
  116. <nc:prop-filter name="LOCATION" />
  117. <nc:prop-filter name="ATTENDEE" />
  118. <nc:param-filter property="ATTENDEE" name="CN" />
  119. <nc:search-term>foo</nc:search-term>
  120. </nc:filter>
  121. <nc:limit>10</nc:limit>
  122. <nc:offset>5</nc:offset>
  123. </nc:calendar-search>
  124. XML;
  125. $this->parse($xml);
  126. }
  127. public function testRequiresFilter(): void {
  128. $this->expectException(\Sabre\DAV\Exception\BadRequest::class);
  129. $this->expectExceptionMessage('The {http://nextcloud.com/ns}filter element is required for this request');
  130. $xml = <<<XML
  131. <?xml version="1.0" encoding="UTF-8"?>
  132. <nc:calendar-search xmlns:nc="http://nextcloud.com/ns" xmlns:c="urn:ietf:params:xml:ns:caldav" xmlns:d="DAV:">
  133. <d:prop>
  134. <d:getetag />
  135. <c:calendar-data />
  136. </d:prop>
  137. </nc:calendar-search>
  138. XML;
  139. $this->parse($xml);
  140. }
  141. public function testNoSearchTerm(): void {
  142. $this->expectException(\Sabre\DAV\Exception\BadRequest::class);
  143. $this->expectExceptionMessage('{http://nextcloud.com/ns}search-term is required for this request');
  144. $xml = <<<XML
  145. <?xml version="1.0" encoding="UTF-8"?>
  146. <nc:calendar-search xmlns:nc="http://nextcloud.com/ns" xmlns:c="urn:ietf:params:xml:ns:caldav" xmlns:d="DAV:">
  147. <d:prop>
  148. <d:getetag />
  149. <c:calendar-data />
  150. </d:prop>
  151. <nc:filter>
  152. <nc:comp-filter name="VEVENT" />
  153. <nc:comp-filter name="VTODO" />
  154. <nc:prop-filter name="SUMMARY" />
  155. <nc:prop-filter name="LOCATION" />
  156. <nc:prop-filter name="ATTENDEE" />
  157. <nc:param-filter property="ATTENDEE" name="CN" />
  158. </nc:filter>
  159. <nc:limit>10</nc:limit>
  160. <nc:offset>5</nc:offset>
  161. </nc:calendar-search>
  162. XML;
  163. $this->parse($xml);
  164. }
  165. public function testCompOnly(): void {
  166. $this->expectException(\Sabre\DAV\Exception\BadRequest::class);
  167. $this->expectExceptionMessage('At least one{http://nextcloud.com/ns}prop-filter or {http://nextcloud.com/ns}param-filter is required for this request');
  168. $xml = <<<XML
  169. <?xml version="1.0" encoding="UTF-8"?>
  170. <nc:calendar-search xmlns:nc="http://nextcloud.com/ns" xmlns:c="urn:ietf:params:xml:ns:caldav" xmlns:d="DAV:">
  171. <d:prop>
  172. <d:getetag />
  173. <c:calendar-data />
  174. </d:prop>
  175. <nc:filter>
  176. <nc:comp-filter name="VEVENT" />
  177. <nc:comp-filter name="VTODO" />
  178. <nc:search-term>foo</nc:search-term>
  179. </nc:filter>
  180. </nc:calendar-search>
  181. XML;
  182. $result = $this->parse($xml);
  183. $calendarSearchReport = new CalendarSearchReport();
  184. $calendarSearchReport->properties = [
  185. '{DAV:}getetag',
  186. '{urn:ietf:params:xml:ns:caldav}calendar-data',
  187. ];
  188. $calendarSearchReport->filters = [
  189. 'comps' => [
  190. 'VEVENT',
  191. 'VTODO'
  192. ],
  193. 'search-term' => 'foo'
  194. ];
  195. $calendarSearchReport->limit = null;
  196. $calendarSearchReport->offset = null;
  197. $this->assertEquals(
  198. $calendarSearchReport,
  199. $result['value']
  200. );
  201. }
  202. public function testPropOnly(): void {
  203. $xml = <<<XML
  204. <?xml version="1.0" encoding="UTF-8"?>
  205. <nc:calendar-search xmlns:nc="http://nextcloud.com/ns" xmlns:c="urn:ietf:params:xml:ns:caldav" xmlns:d="DAV:">
  206. <d:prop>
  207. <d:getetag />
  208. <c:calendar-data />
  209. </d:prop>
  210. <nc:filter>
  211. <nc:comp-filter name="VEVENT" />
  212. <nc:prop-filter name="SUMMARY" />
  213. <nc:search-term>foo</nc:search-term>
  214. </nc:filter>
  215. </nc:calendar-search>
  216. XML;
  217. $result = $this->parse($xml);
  218. $calendarSearchReport = new CalendarSearchReport();
  219. $calendarSearchReport->properties = [
  220. '{DAV:}getetag',
  221. '{urn:ietf:params:xml:ns:caldav}calendar-data',
  222. ];
  223. $calendarSearchReport->filters = [
  224. 'comps' => [
  225. 'VEVENT',
  226. ],
  227. 'props' => [
  228. 'SUMMARY',
  229. ],
  230. 'search-term' => 'foo'
  231. ];
  232. $calendarSearchReport->limit = null;
  233. $calendarSearchReport->offset = null;
  234. $this->assertEquals(
  235. $calendarSearchReport,
  236. $result['value']
  237. );
  238. }
  239. public function testParamOnly(): void {
  240. $xml = <<<XML
  241. <?xml version="1.0" encoding="UTF-8"?>
  242. <nc:calendar-search xmlns:nc="http://nextcloud.com/ns" xmlns:c="urn:ietf:params:xml:ns:caldav" xmlns:d="DAV:">
  243. <d:prop>
  244. <d:getetag />
  245. <c:calendar-data />
  246. </d:prop>
  247. <nc:filter>
  248. <nc:comp-filter name="VEVENT" />
  249. <nc:param-filter property="ATTENDEE" name="CN" />
  250. <nc:search-term>foo</nc:search-term>
  251. </nc:filter>
  252. </nc:calendar-search>
  253. XML;
  254. $result = $this->parse($xml);
  255. $calendarSearchReport = new CalendarSearchReport();
  256. $calendarSearchReport->properties = [
  257. '{DAV:}getetag',
  258. '{urn:ietf:params:xml:ns:caldav}calendar-data',
  259. ];
  260. $calendarSearchReport->filters = [
  261. 'comps' => [
  262. 'VEVENT',
  263. ],
  264. 'params' => [
  265. [
  266. 'property' => 'ATTENDEE',
  267. 'parameter' => 'CN'
  268. ]
  269. ],
  270. 'search-term' => 'foo'
  271. ];
  272. $calendarSearchReport->limit = null;
  273. $calendarSearchReport->offset = null;
  274. $this->assertEquals(
  275. $calendarSearchReport,
  276. $result['value']
  277. );
  278. }
  279. private function parse($xml, array $elementMap = []) {
  280. $reader = new Reader();
  281. $reader->elementMap = array_merge($this->elementMap, $elementMap);
  282. $reader->xml($xml);
  283. return $reader->parse();
  284. }
  285. }