PageRenderTime 38ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/Sabre/CalDAV/ValidateICalTest.php

https://github.com/KOLANICH/SabreDAV
PHP | 276 lines | 178 code | 85 blank | 13 comment | 0 complexity | c5e877ecc9995ff1158ede105fa45569 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. namespace Sabre\CalDAV;
  3. use Sabre\DAV;
  4. use Sabre\DAVACL;
  5. use Sabre\HTTP;
  6. require_once 'Sabre/HTTP/ResponseMock.php';
  7. class ValidateICalTest extends \PHPUnit_Framework_TestCase {
  8. /**
  9. * @var Sabre\DAV\Server
  10. */
  11. protected $server;
  12. /**
  13. * @var Sabre\CalDAV\Backend\Mock
  14. */
  15. protected $calBackend;
  16. function setUp() {
  17. $calendars = array(
  18. array(
  19. 'id' => 'calendar1',
  20. 'principaluri' => 'principals/admin',
  21. 'uri' => 'calendar1',
  22. '{urn:ietf:params:xml:ns:caldav}supported-calendar-component-set' => new Property\SupportedCalendarComponentSet( array('VEVENT','VTODO','VJOURNAL') ),
  23. ),
  24. array(
  25. 'id' => 'calendar2',
  26. 'principaluri' => 'principals/admin',
  27. 'uri' => 'calendar2',
  28. '{urn:ietf:params:xml:ns:caldav}supported-calendar-component-set' => new Property\SupportedCalendarComponentSet( array('VTODO','VJOURNAL') ),
  29. )
  30. );
  31. $this->calBackend = new Backend\Mock($calendars,array());
  32. $principalBackend = new DAVACL\PrincipalBackend\Mock();
  33. $tree = array(
  34. new CalendarRootNode($principalBackend, $this->calBackend),
  35. );
  36. $this->server = new DAV\Server($tree);
  37. $this->server->debugExceptions = true;
  38. $plugin = new Plugin();
  39. $this->server->addPlugin($plugin);
  40. $response = new HTTP\ResponseMock();
  41. $this->server->httpResponse = $response;
  42. }
  43. function request(HTTP\Request $request) {
  44. $this->server->httpRequest = $request;
  45. $this->server->exec();
  46. return $this->server->httpResponse;
  47. }
  48. function testCreateFile() {
  49. $request = new HTTP\Request(array(
  50. 'REQUEST_METHOD' => 'PUT',
  51. 'REQUEST_URI' => '/calendars/admin/calendar1/blabla.ics',
  52. ));
  53. $response = $this->request($request);
  54. $this->assertEquals('HTTP/1.1 415 Unsupported Media Type', $response->status);
  55. }
  56. function testCreateFileValid() {
  57. $request = new HTTP\Request(array(
  58. 'REQUEST_METHOD' => 'PUT',
  59. 'REQUEST_URI' => '/calendars/admin/calendar1/blabla.ics',
  60. ));
  61. $request->setBody("BEGIN:VCALENDAR\r\nBEGIN:VEVENT\r\nUID:foo\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n");
  62. $response = $this->request($request);
  63. $this->assertEquals('HTTP/1.1 201 Created', $response->status, 'Incorrect status returned! Full response body: ' . $response->body);
  64. $this->assertEquals(array(
  65. 'Content-Length' => '0',
  66. 'ETag' => '"' . md5("BEGIN:VCALENDAR\r\nBEGIN:VEVENT\r\nUID:foo\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n") . '"',
  67. ), $response->headers);
  68. $expected = array(
  69. 'uri' => 'blabla.ics',
  70. 'calendardata' => "BEGIN:VCALENDAR\r\nBEGIN:VEVENT\r\nUID:foo\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n",
  71. 'calendarid' => 'calendar1',
  72. );
  73. $this->assertEquals($expected, $this->calBackend->getCalendarObject('calendar1','blabla.ics'));
  74. }
  75. function testCreateFileNoComponents() {
  76. $request = new HTTP\Request(array(
  77. 'REQUEST_METHOD' => 'PUT',
  78. 'REQUEST_URI' => '/calendars/admin/calendar1/blabla.ics',
  79. ));
  80. $request->setBody("BEGIN:VCALENDAR\r\nEND:VCALENDAR\r\n");
  81. $response = $this->request($request);
  82. $this->assertEquals('HTTP/1.1 400 Bad request', $response->status, 'Incorrect status returned! Full response body: ' . $response->body);
  83. }
  84. function testCreateFileNoUID() {
  85. $request = new HTTP\Request(array(
  86. 'REQUEST_METHOD' => 'PUT',
  87. 'REQUEST_URI' => '/calendars/admin/calendar1/blabla.ics',
  88. ));
  89. $request->setBody("BEGIN:VCALENDAR\r\nBEGIN:VEVENT\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n");
  90. $response = $this->request($request);
  91. $this->assertEquals('HTTP/1.1 400 Bad request', $response->status, 'Incorrect status returned! Full response body: ' . $response->body);
  92. }
  93. function testCreateFileVCard() {
  94. $request = new HTTP\Request(array(
  95. 'REQUEST_METHOD' => 'PUT',
  96. 'REQUEST_URI' => '/calendars/admin/calendar1/blabla.ics',
  97. ));
  98. $request->setBody("BEGIN:VCARD\r\nEND:VCARD\r\n");
  99. $response = $this->request($request);
  100. $this->assertEquals('HTTP/1.1 415 Unsupported Media Type', $response->status, 'Incorrect status returned! Full response body: ' . $response->body);
  101. }
  102. function testCreateFile2Components() {
  103. $request = new HTTP\Request(array(
  104. 'REQUEST_METHOD' => 'PUT',
  105. 'REQUEST_URI' => '/calendars/admin/calendar1/blabla.ics',
  106. ));
  107. $request->setBody("BEGIN:VCALENDAR\r\nBEGIN:VEVENT\r\nUID:foo\r\nEND:VEVENT\r\nBEGIN:VJOURNAL\r\nUID:foo\r\nEND:VJOURNAL\r\nEND:VCALENDAR\r\n");
  108. $response = $this->request($request);
  109. $this->assertEquals('HTTP/1.1 400 Bad request', $response->status, 'Incorrect status returned! Full response body: ' . $response->body);
  110. }
  111. function testCreateFile2UIDS() {
  112. $request = new HTTP\Request(array(
  113. 'REQUEST_METHOD' => 'PUT',
  114. 'REQUEST_URI' => '/calendars/admin/calendar1/blabla.ics',
  115. ));
  116. $request->setBody("BEGIN:VCALENDAR\r\nBEGIN:VTIMEZONE\r\nEND:VTIMEZONE\r\nBEGIN:VEVENT\r\nUID:foo\r\nEND:VEVENT\r\nBEGIN:VEVENT\r\nUID:bar\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n");
  117. $response = $this->request($request);
  118. $this->assertEquals('HTTP/1.1 400 Bad request', $response->status, 'Incorrect status returned! Full response body: ' . $response->body);
  119. }
  120. function testCreateFileWrongComponent() {
  121. $request = new HTTP\Request(array(
  122. 'REQUEST_METHOD' => 'PUT',
  123. 'REQUEST_URI' => '/calendars/admin/calendar1/blabla.ics',
  124. ));
  125. $request->setBody("BEGIN:VCALENDAR\r\nBEGIN:VTIMEZONE\r\nEND:VTIMEZONE\r\nBEGIN:VFREEBUSY\r\nUID:foo\r\nEND:VFREEBUSY\r\nEND:VCALENDAR\r\n");
  126. $response = $this->request($request);
  127. $this->assertEquals('HTTP/1.1 400 Bad request', $response->status, 'Incorrect status returned! Full response body: ' . $response->body);
  128. }
  129. function testUpdateFile() {
  130. $this->calBackend->createCalendarObject('calendar1','blabla.ics','foo');
  131. $request = new HTTP\Request(array(
  132. 'REQUEST_METHOD' => 'PUT',
  133. 'REQUEST_URI' => '/calendars/admin/calendar1/blabla.ics',
  134. ));
  135. $response = $this->request($request);
  136. $this->assertEquals('HTTP/1.1 415 Unsupported Media Type', $response->status);
  137. }
  138. function testUpdateFileParsableBody() {
  139. $this->calBackend->createCalendarObject('calendar1','blabla.ics','foo');
  140. $request = new HTTP\Request(array(
  141. 'REQUEST_METHOD' => 'PUT',
  142. 'REQUEST_URI' => '/calendars/admin/calendar1/blabla.ics',
  143. ));
  144. $body = "BEGIN:VCALENDAR\r\nBEGIN:VEVENT\r\nUID:foo\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n";
  145. $request->setBody($body);
  146. $response = $this->request($request);
  147. $this->assertEquals('HTTP/1.1 204 No Content', $response->status);
  148. $expected = array(
  149. 'uri' => 'blabla.ics',
  150. 'calendardata' => $body,
  151. 'calendarid' => 'calendar1',
  152. );
  153. $this->assertEquals($expected, $this->calBackend->getCalendarObject('calendar1','blabla.ics'));
  154. }
  155. function testCreateFileInvalidComponent() {
  156. $request = new HTTP\Request(array(
  157. 'REQUEST_METHOD' => 'PUT',
  158. 'REQUEST_URI' => '/calendars/admin/calendar2/blabla.ics',
  159. ));
  160. $request->setBody("BEGIN:VCALENDAR\r\nBEGIN:VTIMEZONE\r\nEND:VTIMEZONE\r\nBEGIN:VEVENT\r\nUID:foo\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n");
  161. $response = $this->request($request);
  162. $this->assertEquals('HTTP/1.1 403 Forbidden', $response->status, 'Incorrect status returned! Full response body: ' . $response->body);
  163. }
  164. function testUpdateFileInvalidComponent() {
  165. $this->calBackend->createCalendarObject('calendar2','blabla.ics','foo');
  166. $request = new HTTP\Request(array(
  167. 'REQUEST_METHOD' => 'PUT',
  168. 'REQUEST_URI' => '/calendars/admin/calendar2/blabla.ics',
  169. ));
  170. $request->setBody("BEGIN:VCALENDAR\r\nBEGIN:VTIMEZONE\r\nEND:VTIMEZONE\r\nBEGIN:VEVENT\r\nUID:foo\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n");
  171. $response = $this->request($request);
  172. $this->assertEquals('HTTP/1.1 403 Forbidden', $response->status, 'Incorrect status returned! Full response body: ' . $response->body);
  173. }
  174. /**
  175. * What we are testing here, is if we send in a latin1 character, the
  176. * server should automatically transform this into UTF-8.
  177. *
  178. * More importantly. If any transformation happens, the etag must no longer
  179. * be returned by the server.
  180. */
  181. function testCreateFileModified() {
  182. $request = new HTTP\Request(array(
  183. 'REQUEST_METHOD' => 'PUT',
  184. 'REQUEST_URI' => '/calendars/admin/calendar1/blabla.ics',
  185. ));
  186. $request->setBody("BEGIN:VCALENDAR\r\nBEGIN:VEVENT\r\nUID:foo\r\nSUMMARY:Meeting in M\xfcnster\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n");
  187. $response = $this->request($request);
  188. $this->assertEquals('HTTP/1.1 201 Created', $response->status, 'Incorrect status returned! Full response body: ' . $response->body);
  189. $this->assertFalse(isset($response->headers['ETag']));
  190. }
  191. }