JCalTransformTest.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php
  2. namespace Sabre\CalDAV;
  3. use Sabre\HTTP\Request;
  4. class JCalTransformTest extends \Sabre\DAVServerTest {
  5. protected $setupCalDAV = true;
  6. protected $caldavCalendars = [
  7. [
  8. 'id' => 1,
  9. 'principaluri' => 'principals/user1',
  10. 'uri' => 'foo',
  11. ]
  12. ];
  13. protected $caldavCalendarObjects = [
  14. 1 => [
  15. 'bar.ics' => [
  16. 'uri' => 'bar.ics',
  17. 'calendarid' => 1,
  18. 'calendardata' => "BEGIN:VCALENDAR\r\nBEGIN:VEVENT\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n",
  19. 'lastmodified' => null
  20. ]
  21. ],
  22. ];
  23. function testGet() {
  24. $headers = [
  25. 'Accept' => 'application/calendar+json',
  26. ];
  27. $request = new Request('GET', '/calendars/user1/foo/bar.ics', $headers);
  28. $response = $this->request($request);
  29. $body = $response->getBodyAsString();
  30. $this->assertEquals(200, $response->getStatus(), "Incorrect status code: " . $body);
  31. $response = json_decode($body,true);
  32. if (json_last_error() !== JSON_ERROR_NONE) {
  33. $this->fail('Json decoding error: ' . json_last_error_msg());
  34. }
  35. $this->assertEquals(
  36. [
  37. 'vcalendar',
  38. [],
  39. [
  40. [
  41. 'vevent',
  42. [],
  43. [],
  44. ],
  45. ],
  46. ],
  47. $response
  48. );
  49. }
  50. function testMultiGet() {
  51. $xml = <<<XML
  52. <?xml version="1.0"?>
  53. <c:calendar-multiget xmlns:c="urn:ietf:params:xml:ns:caldav" xmlns:d="DAV:">
  54. <d:prop>
  55. <c:calendar-data content-type="application/calendar+json" />
  56. </d:prop>
  57. <d:href>/calendars/user1/foo/bar.ics</d:href>
  58. </c:calendar-multiget>
  59. XML;
  60. $headers = [];
  61. $request = new Request('REPORT', '/calendars/user1/foo', $headers, $xml);
  62. $response = $this->request($request);
  63. $this->assertEquals(207, $response->getStatus(), 'Full rsponse: ' . $response->getBodyAsString());
  64. $multiStatus = $this->server->xml->parse(
  65. $response->getBodyAsString()
  66. );
  67. $responses = $multiStatus->getResponses();
  68. $this->assertEquals(1, count($responses));
  69. $response = $responses[0]->getResponseProperties()[200]["{urn:ietf:params:xml:ns:caldav}calendar-data"];
  70. $jresponse = json_decode($response,true);
  71. if (json_last_error()) {
  72. $this->fail('Json decoding error: ' . json_last_error_msg() . '. Full response: ' . $response);
  73. }
  74. $this->assertEquals(
  75. [
  76. 'vcalendar',
  77. [],
  78. [
  79. [
  80. 'vevent',
  81. [],
  82. [],
  83. ],
  84. ],
  85. ],
  86. $jresponse
  87. );
  88. }
  89. function testCalendarQueryDepth1() {
  90. $xml = <<<XML
  91. <?xml version="1.0"?>
  92. <c:calendar-query xmlns:c="urn:ietf:params:xml:ns:caldav" xmlns:d="DAV:">
  93. <d:prop>
  94. <c:calendar-data content-type="application/calendar+json" />
  95. </d:prop>
  96. <c:filter>
  97. <c:comp-filter name="VCALENDAR" />
  98. </c:filter>
  99. </c:calendar-query>
  100. XML;
  101. $headers = [
  102. 'Depth' => '1',
  103. ];
  104. $request = new Request('REPORT', '/calendars/user1/foo', $headers, $xml);
  105. $response = $this->request($request);
  106. $this->assertEquals(207, $response->getStatus(), "Invalid response code. Full body: " . $response->getBodyAsString());
  107. $multiStatus = $this->server->xml->parse(
  108. $response->getBodyAsString()
  109. );
  110. $responses = $multiStatus->getResponses();
  111. $this->assertEquals(1, count($responses));
  112. $response = $responses[0]->getResponseProperties()[200]["{urn:ietf:params:xml:ns:caldav}calendar-data"];
  113. $response = json_decode($response,true);
  114. if (json_last_error()) {
  115. $this->fail('Json decoding error: ' . json_last_error_msg());
  116. }
  117. $this->assertEquals(
  118. [
  119. 'vcalendar',
  120. [],
  121. [
  122. [
  123. 'vevent',
  124. [],
  125. [],
  126. ],
  127. ],
  128. ],
  129. $response
  130. );
  131. }
  132. function testCalendarQueryDepth0() {
  133. $xml = <<<XML
  134. <?xml version="1.0"?>
  135. <c:calendar-query xmlns:c="urn:ietf:params:xml:ns:caldav" xmlns:d="DAV:">
  136. <d:prop>
  137. <c:calendar-data content-type="application/calendar+json" />
  138. </d:prop>
  139. <c:filter>
  140. <c:comp-filter name="VCALENDAR" />
  141. </c:filter>
  142. </c:calendar-query>
  143. XML;
  144. $headers = [
  145. 'Depth' => '0',
  146. ];
  147. $request = new Request('REPORT', '/calendars/user1/foo/bar.ics', $headers, $xml);
  148. $response = $this->request($request);
  149. $this->assertEquals(207, $response->getStatus(), "Invalid response code. Full body: " . $response->getBodyAsString());
  150. $multiStatus = $this->server->xml->parse(
  151. $response->getBodyAsString()
  152. );
  153. $responses = $multiStatus->getResponses();
  154. $this->assertEquals(1, count($responses));
  155. $response = $responses[0]->getResponseProperties()[200]["{urn:ietf:params:xml:ns:caldav}calendar-data"];
  156. $response = json_decode($response,true);
  157. if (json_last_error()) {
  158. $this->fail('Json decoding error: ' . json_last_error_msg());
  159. }
  160. $this->assertEquals(
  161. [
  162. 'vcalendar',
  163. [],
  164. [
  165. [
  166. 'vevent',
  167. [],
  168. [],
  169. ],
  170. ],
  171. ],
  172. $response
  173. );
  174. }
  175. function testValidateICalendar() {
  176. $input = [
  177. 'vcalendar',
  178. [],
  179. [
  180. [
  181. 'vevent',
  182. [
  183. ['uid', (object)[], 'text', 'foo'],
  184. ],
  185. [],
  186. ],
  187. ],
  188. ];
  189. $input = json_encode($input);
  190. $this->caldavPlugin->beforeWriteContent(
  191. 'calendars/user1/foo/bar.ics',
  192. $this->server->tree->getNodeForPath('calendars/user1/foo/bar.ics'),
  193. $input,
  194. $modified
  195. );
  196. $this->assertEquals("BEGIN:VCALENDAR\r\nBEGIN:VEVENT\r\nUID:foo\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n", $input);
  197. }
  198. }