CalendarSearchReportTest.php 8.8 KB

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