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