2013-10-21 22:46:31 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Sabre\CalDAV;
|
|
|
|
|
|
|
|
use Sabre\DAV;
|
|
|
|
use Sabre\DAVACL;
|
|
|
|
use Sabre\HTTP;
|
|
|
|
|
|
|
|
require_once 'Sabre/HTTP/ResponseMock.php';
|
|
|
|
|
|
|
|
class ValidateICalTest extends \PHPUnit_Framework_TestCase {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var Sabre\DAV\Server
|
|
|
|
*/
|
|
|
|
protected $server;
|
|
|
|
/**
|
|
|
|
* @var Sabre\CalDAV\Backend\Mock
|
|
|
|
*/
|
|
|
|
protected $calBackend;
|
|
|
|
|
|
|
|
function setUp() {
|
|
|
|
|
2016-05-28 15:46:24 +00:00
|
|
|
$calendars = [
|
|
|
|
[
|
|
|
|
'id' => 'calendar1',
|
|
|
|
'principaluri' => 'principals/admin',
|
|
|
|
'uri' => 'calendar1',
|
|
|
|
'{urn:ietf:params:xml:ns:caldav}supported-calendar-component-set' => new Xml\Property\SupportedCalendarComponentSet(['VEVENT', 'VTODO', 'VJOURNAL']),
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'id' => 'calendar2',
|
|
|
|
'principaluri' => 'principals/admin',
|
|
|
|
'uri' => 'calendar2',
|
|
|
|
'{urn:ietf:params:xml:ns:caldav}supported-calendar-component-set' => new Xml\Property\SupportedCalendarComponentSet(['VTODO', 'VJOURNAL']),
|
|
|
|
]
|
|
|
|
];
|
2013-10-21 22:46:31 +00:00
|
|
|
|
2016-05-11 00:26:44 +00:00
|
|
|
$this->calBackend = new Backend\Mock($calendars, []);
|
2013-10-21 22:46:31 +00:00
|
|
|
$principalBackend = new DAVACL\PrincipalBackend\Mock();
|
|
|
|
|
2016-05-11 00:26:44 +00:00
|
|
|
$tree = [
|
|
|
|
new CalendarRoot($principalBackend, $this->calBackend),
|
|
|
|
];
|
2013-10-21 22:46:31 +00:00
|
|
|
|
|
|
|
$this->server = new DAV\Server($tree);
|
2016-05-11 00:26:44 +00:00
|
|
|
$this->server->sapi = new HTTP\SapiMock();
|
2013-10-21 22:46:31 +00:00
|
|
|
$this->server->debugExceptions = true;
|
|
|
|
|
|
|
|
$plugin = new Plugin();
|
|
|
|
$this->server->addPlugin($plugin);
|
|
|
|
|
|
|
|
$response = new HTTP\ResponseMock();
|
|
|
|
$this->server->httpResponse = $response;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function request(HTTP\Request $request) {
|
|
|
|
|
|
|
|
$this->server->httpRequest = $request;
|
|
|
|
$this->server->exec();
|
|
|
|
|
|
|
|
return $this->server->httpResponse;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function testCreateFile() {
|
|
|
|
|
2016-05-28 15:46:24 +00:00
|
|
|
$request = HTTP\Sapi::createFromServerArray([
|
2013-10-21 22:46:31 +00:00
|
|
|
'REQUEST_METHOD' => 'PUT',
|
2016-05-28 15:46:24 +00:00
|
|
|
'REQUEST_URI' => '/calendars/admin/calendar1/blabla.ics',
|
|
|
|
]);
|
2013-10-21 22:46:31 +00:00
|
|
|
|
|
|
|
$response = $this->request($request);
|
|
|
|
|
2016-05-11 00:26:44 +00:00
|
|
|
$this->assertEquals(415, $response->status);
|
2013-10-21 22:46:31 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function testCreateFileValid() {
|
|
|
|
|
2016-05-28 15:46:24 +00:00
|
|
|
$request = new HTTP\Request(
|
|
|
|
'PUT',
|
|
|
|
'/calendars/admin/calendar1/blabla.ics',
|
|
|
|
['Prefer' => 'handling=strict']
|
|
|
|
);
|
|
|
|
|
|
|
|
$ics = <<<ICS
|
|
|
|
BEGIN:VCALENDAR
|
|
|
|
VERSION:2.0
|
|
|
|
PRODID:foo
|
|
|
|
BEGIN:VEVENT
|
|
|
|
UID:foo
|
|
|
|
DTSTAMP:20160406T052348Z
|
|
|
|
DTSTART:20160706T140000Z
|
|
|
|
END:VEVENT
|
|
|
|
END:VCALENDAR
|
|
|
|
ICS;
|
|
|
|
|
|
|
|
$request->setBody($ics);
|
2013-10-21 22:46:31 +00:00
|
|
|
|
|
|
|
$response = $this->request($request);
|
|
|
|
|
2016-05-11 00:26:44 +00:00
|
|
|
$this->assertEquals(201, $response->status, 'Incorrect status returned! Full response body: ' . $response->body);
|
2016-05-28 15:46:24 +00:00
|
|
|
$this->assertEquals([
|
2016-05-11 00:26:44 +00:00
|
|
|
'X-Sabre-Version' => [DAV\Version::VERSION],
|
2016-05-28 15:46:24 +00:00
|
|
|
'Content-Length' => ['0'],
|
|
|
|
'ETag' => ['"' . md5($ics) . '"'],
|
|
|
|
], $response->getHeaders());
|
2016-05-11 00:26:44 +00:00
|
|
|
|
2016-05-28 15:46:24 +00:00
|
|
|
$expected = [
|
2013-10-21 22:46:31 +00:00
|
|
|
'uri' => 'blabla.ics',
|
2016-05-28 15:46:24 +00:00
|
|
|
'calendardata' => $ics,
|
2013-10-21 22:46:31 +00:00
|
|
|
'calendarid' => 'calendar1',
|
2016-05-11 00:26:44 +00:00
|
|
|
'lastmodified' => null,
|
2016-05-28 15:46:24 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
$this->assertEquals($expected, $this->calBackend->getCalendarObject('calendar1', 'blabla.ics'));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function testCreateFileNoVersion() {
|
|
|
|
|
|
|
|
$request = new HTTP\Request(
|
|
|
|
'PUT',
|
|
|
|
'/calendars/admin/calendar1/blabla.ics',
|
|
|
|
['Prefer' => 'handling=strict']
|
2013-10-21 22:46:31 +00:00
|
|
|
);
|
|
|
|
|
2016-05-28 15:46:24 +00:00
|
|
|
$ics = <<<ICS
|
|
|
|
BEGIN:VCALENDAR
|
|
|
|
PRODID:foo
|
|
|
|
BEGIN:VEVENT
|
|
|
|
UID:foo
|
|
|
|
DTSTAMP:20160406T052348Z
|
|
|
|
DTSTART:20160706T140000Z
|
|
|
|
END:VEVENT
|
|
|
|
END:VCALENDAR
|
|
|
|
ICS;
|
|
|
|
|
|
|
|
$request->setBody($ics);
|
|
|
|
|
|
|
|
$response = $this->request($request);
|
|
|
|
|
|
|
|
$this->assertEquals(415, $response->status, 'Incorrect status returned! Full response body: ' . $response->body);
|
2013-10-21 22:46:31 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-05-28 15:46:24 +00:00
|
|
|
function testCreateFileNoVersionFixed() {
|
2013-10-21 22:46:31 +00:00
|
|
|
|
2016-05-28 15:46:24 +00:00
|
|
|
$request = new HTTP\Request(
|
|
|
|
'PUT',
|
|
|
|
'/calendars/admin/calendar1/blabla.ics',
|
|
|
|
['Prefer' => 'handling=lenient']
|
|
|
|
);
|
|
|
|
|
|
|
|
$ics = <<<ICS
|
|
|
|
BEGIN:VCALENDAR
|
|
|
|
PRODID:foo
|
|
|
|
BEGIN:VEVENT
|
|
|
|
UID:foo
|
|
|
|
DTSTAMP:20160406T052348Z
|
|
|
|
DTSTART:20160706T140000Z
|
|
|
|
END:VEVENT
|
|
|
|
END:VCALENDAR
|
|
|
|
ICS;
|
|
|
|
|
|
|
|
$request->setBody($ics);
|
2013-10-21 22:46:31 +00:00
|
|
|
|
|
|
|
$response = $this->request($request);
|
|
|
|
|
2016-05-28 15:46:24 +00:00
|
|
|
$this->assertEquals(201, $response->status, 'Incorrect status returned! Full response body: ' . $response->body);
|
|
|
|
$this->assertEquals([
|
|
|
|
'X-Sabre-Version' => [DAV\Version::VERSION],
|
|
|
|
'Content-Length' => ['0'],
|
|
|
|
'X-Sabre-Ew-Gross' => ['iCalendar validation warning: VERSION MUST appear exactly once in a VCALENDAR component'],
|
|
|
|
], $response->getHeaders());
|
|
|
|
|
|
|
|
$ics = <<<ICS
|
|
|
|
BEGIN:VCALENDAR\r
|
|
|
|
VERSION:2.0\r
|
|
|
|
PRODID:foo\r
|
|
|
|
BEGIN:VEVENT\r
|
|
|
|
UID:foo\r
|
|
|
|
DTSTAMP:20160406T052348Z\r
|
|
|
|
DTSTART:20160706T140000Z\r
|
|
|
|
END:VEVENT\r
|
|
|
|
END:VCALENDAR\r
|
|
|
|
|
|
|
|
ICS;
|
|
|
|
|
|
|
|
$expected = [
|
|
|
|
'uri' => 'blabla.ics',
|
|
|
|
'calendardata' => $ics,
|
|
|
|
'calendarid' => 'calendar1',
|
|
|
|
'lastmodified' => null,
|
|
|
|
];
|
|
|
|
|
|
|
|
$this->assertEquals($expected, $this->calBackend->getCalendarObject('calendar1', 'blabla.ics'));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function testCreateFileNoComponents() {
|
|
|
|
|
|
|
|
$request = new HTTP\Request(
|
|
|
|
'PUT',
|
|
|
|
'/calendars/admin/calendar1/blabla.ics',
|
|
|
|
['Prefer' => 'handling=strict']
|
|
|
|
);
|
|
|
|
$ics = <<<ICS
|
|
|
|
BEGIN:VCALENDAR
|
|
|
|
VERSION:2.0
|
|
|
|
PRODID:foo
|
|
|
|
END:VCALENDAR
|
|
|
|
ICS;
|
|
|
|
|
|
|
|
$request->setBody($ics);
|
|
|
|
|
|
|
|
$response = $this->request($request);
|
|
|
|
$this->assertEquals(403, $response->status, 'Incorrect status returned! Full response body: ' . $response->body);
|
2013-10-21 22:46:31 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function testCreateFileNoUID() {
|
|
|
|
|
2016-05-28 15:46:24 +00:00
|
|
|
$request = HTTP\Sapi::createFromServerArray([
|
2013-10-21 22:46:31 +00:00
|
|
|
'REQUEST_METHOD' => 'PUT',
|
2016-05-28 15:46:24 +00:00
|
|
|
'REQUEST_URI' => '/calendars/admin/calendar1/blabla.ics',
|
|
|
|
]);
|
2013-10-21 22:46:31 +00:00
|
|
|
$request->setBody("BEGIN:VCALENDAR\r\nBEGIN:VEVENT\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n");
|
|
|
|
|
|
|
|
$response = $this->request($request);
|
|
|
|
|
2016-05-28 15:46:24 +00:00
|
|
|
$this->assertEquals(415, $response->status, 'Incorrect status returned! Full response body: ' . $response->body);
|
2013-10-21 22:46:31 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function testCreateFileVCard() {
|
|
|
|
|
2016-05-28 15:46:24 +00:00
|
|
|
$request = HTTP\Sapi::createFromServerArray([
|
2013-10-21 22:46:31 +00:00
|
|
|
'REQUEST_METHOD' => 'PUT',
|
2016-05-28 15:46:24 +00:00
|
|
|
'REQUEST_URI' => '/calendars/admin/calendar1/blabla.ics',
|
|
|
|
]);
|
2013-10-21 22:46:31 +00:00
|
|
|
$request->setBody("BEGIN:VCARD\r\nEND:VCARD\r\n");
|
|
|
|
|
|
|
|
$response = $this->request($request);
|
|
|
|
|
2016-05-11 00:26:44 +00:00
|
|
|
$this->assertEquals(415, $response->status, 'Incorrect status returned! Full response body: ' . $response->body);
|
2013-10-21 22:46:31 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function testCreateFile2Components() {
|
|
|
|
|
2016-05-28 15:46:24 +00:00
|
|
|
$request = HTTP\Sapi::createFromServerArray([
|
2013-10-21 22:46:31 +00:00
|
|
|
'REQUEST_METHOD' => 'PUT',
|
2016-05-28 15:46:24 +00:00
|
|
|
'REQUEST_URI' => '/calendars/admin/calendar1/blabla.ics',
|
|
|
|
]);
|
2013-10-21 22:46:31 +00:00
|
|
|
$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");
|
|
|
|
|
|
|
|
$response = $this->request($request);
|
|
|
|
|
2016-05-28 15:46:24 +00:00
|
|
|
$this->assertEquals(415, $response->status, 'Incorrect status returned! Full response body: ' . $response->body);
|
2013-10-21 22:46:31 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function testCreateFile2UIDS() {
|
|
|
|
|
2016-05-28 15:46:24 +00:00
|
|
|
$request = HTTP\Sapi::createFromServerArray([
|
2013-10-21 22:46:31 +00:00
|
|
|
'REQUEST_METHOD' => 'PUT',
|
2016-05-28 15:46:24 +00:00
|
|
|
'REQUEST_URI' => '/calendars/admin/calendar1/blabla.ics',
|
|
|
|
]);
|
2013-10-21 22:46:31 +00:00
|
|
|
$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");
|
|
|
|
|
|
|
|
$response = $this->request($request);
|
|
|
|
|
2016-05-28 15:46:24 +00:00
|
|
|
$this->assertEquals(415, $response->status, 'Incorrect status returned! Full response body: ' . $response->body);
|
2013-10-21 22:46:31 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function testCreateFileWrongComponent() {
|
|
|
|
|
2016-05-28 15:46:24 +00:00
|
|
|
$request = HTTP\Sapi::createFromServerArray([
|
2013-10-21 22:46:31 +00:00
|
|
|
'REQUEST_METHOD' => 'PUT',
|
2016-05-28 15:46:24 +00:00
|
|
|
'REQUEST_URI' => '/calendars/admin/calendar1/blabla.ics',
|
|
|
|
]);
|
2013-10-21 22:46:31 +00:00
|
|
|
$request->setBody("BEGIN:VCALENDAR\r\nBEGIN:VTIMEZONE\r\nEND:VTIMEZONE\r\nBEGIN:VFREEBUSY\r\nUID:foo\r\nEND:VFREEBUSY\r\nEND:VCALENDAR\r\n");
|
|
|
|
|
|
|
|
$response = $this->request($request);
|
|
|
|
|
2016-05-28 15:46:24 +00:00
|
|
|
$this->assertEquals(403, $response->status, 'Incorrect status returned! Full response body: ' . $response->body);
|
2013-10-21 22:46:31 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function testUpdateFile() {
|
|
|
|
|
2016-05-28 15:46:24 +00:00
|
|
|
$this->calBackend->createCalendarObject('calendar1', 'blabla.ics', 'foo');
|
|
|
|
$request = HTTP\Sapi::createFromServerArray([
|
2013-10-21 22:46:31 +00:00
|
|
|
'REQUEST_METHOD' => 'PUT',
|
2016-05-28 15:46:24 +00:00
|
|
|
'REQUEST_URI' => '/calendars/admin/calendar1/blabla.ics',
|
|
|
|
]);
|
2013-10-21 22:46:31 +00:00
|
|
|
|
|
|
|
$response = $this->request($request);
|
|
|
|
|
2016-05-11 00:26:44 +00:00
|
|
|
$this->assertEquals(415, $response->status);
|
2013-10-21 22:46:31 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function testUpdateFileParsableBody() {
|
|
|
|
|
2016-05-28 15:46:24 +00:00
|
|
|
$this->calBackend->createCalendarObject('calendar1', 'blabla.ics', 'foo');
|
|
|
|
$request = new HTTP\Request(
|
|
|
|
'PUT',
|
|
|
|
'/calendars/admin/calendar1/blabla.ics'
|
|
|
|
);
|
|
|
|
$ics = <<<ICS
|
|
|
|
BEGIN:VCALENDAR
|
|
|
|
VERSION:2.0
|
|
|
|
PRODID:foo
|
|
|
|
BEGIN:VEVENT
|
|
|
|
UID:foo
|
|
|
|
DTSTAMP:20160406T052348Z
|
|
|
|
DTSTART:20160706T140000Z
|
|
|
|
END:VEVENT
|
|
|
|
END:VCALENDAR
|
|
|
|
ICS;
|
|
|
|
|
|
|
|
$request->setBody($ics);
|
2013-10-21 22:46:31 +00:00
|
|
|
$response = $this->request($request);
|
|
|
|
|
2016-05-11 00:26:44 +00:00
|
|
|
$this->assertEquals(204, $response->status);
|
2013-10-21 22:46:31 +00:00
|
|
|
|
2016-05-28 15:46:24 +00:00
|
|
|
$expected = [
|
2013-10-21 22:46:31 +00:00
|
|
|
'uri' => 'blabla.ics',
|
2016-05-28 15:46:24 +00:00
|
|
|
'calendardata' => $ics,
|
2013-10-21 22:46:31 +00:00
|
|
|
'calendarid' => 'calendar1',
|
2016-05-11 00:26:44 +00:00
|
|
|
'lastmodified' => null,
|
2016-05-28 15:46:24 +00:00
|
|
|
];
|
2013-10-21 22:46:31 +00:00
|
|
|
|
2016-05-28 15:46:24 +00:00
|
|
|
$this->assertEquals($expected, $this->calBackend->getCalendarObject('calendar1', 'blabla.ics'));
|
2013-10-21 22:46:31 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function testCreateFileInvalidComponent() {
|
|
|
|
|
2016-05-28 15:46:24 +00:00
|
|
|
$request = HTTP\Sapi::createFromServerArray([
|
2013-10-21 22:46:31 +00:00
|
|
|
'REQUEST_METHOD' => 'PUT',
|
2016-05-28 15:46:24 +00:00
|
|
|
'REQUEST_URI' => '/calendars/admin/calendar2/blabla.ics',
|
|
|
|
]);
|
2013-10-21 22:46:31 +00:00
|
|
|
$request->setBody("BEGIN:VCALENDAR\r\nBEGIN:VTIMEZONE\r\nEND:VTIMEZONE\r\nBEGIN:VEVENT\r\nUID:foo\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n");
|
|
|
|
|
|
|
|
$response = $this->request($request);
|
|
|
|
|
2016-05-11 00:26:44 +00:00
|
|
|
$this->assertEquals(403, $response->status, 'Incorrect status returned! Full response body: ' . $response->body);
|
2013-10-21 22:46:31 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function testUpdateFileInvalidComponent() {
|
|
|
|
|
2016-05-28 15:46:24 +00:00
|
|
|
$this->calBackend->createCalendarObject('calendar2', 'blabla.ics', 'foo');
|
|
|
|
$request = HTTP\Sapi::createFromServerArray([
|
2013-10-21 22:46:31 +00:00
|
|
|
'REQUEST_METHOD' => 'PUT',
|
2016-05-28 15:46:24 +00:00
|
|
|
'REQUEST_URI' => '/calendars/admin/calendar2/blabla.ics',
|
|
|
|
]);
|
2013-10-21 22:46:31 +00:00
|
|
|
$request->setBody("BEGIN:VCALENDAR\r\nBEGIN:VTIMEZONE\r\nEND:VTIMEZONE\r\nBEGIN:VEVENT\r\nUID:foo\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n");
|
|
|
|
|
|
|
|
$response = $this->request($request);
|
|
|
|
|
2016-05-11 00:26:44 +00:00
|
|
|
$this->assertEquals(403, $response->status, 'Incorrect status returned! Full response body: ' . $response->body);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* What we are testing here, is if we send in a latin1 character, the
|
|
|
|
* server should automatically transform this into UTF-8.
|
|
|
|
*
|
|
|
|
* More importantly. If any transformation happens, the etag must no longer
|
|
|
|
* be returned by the server.
|
|
|
|
*/
|
|
|
|
function testCreateFileModified() {
|
|
|
|
|
2016-05-28 15:46:24 +00:00
|
|
|
$request = new HTTP\Request(
|
|
|
|
'PUT',
|
|
|
|
'/calendars/admin/calendar1/blabla.ics'
|
|
|
|
);
|
|
|
|
$ics = <<<ICS
|
|
|
|
BEGIN:VCALENDAR
|
|
|
|
VERSION:2.0
|
|
|
|
PRODID:foo
|
|
|
|
BEGIN:VEVENT
|
|
|
|
UID:foo
|
|
|
|
SUMMARY:Meeting in M\xfcnster
|
|
|
|
DTSTAMP:20160406T052348Z
|
|
|
|
DTSTART:20160706T140000Z
|
|
|
|
END:VEVENT
|
|
|
|
END:VCALENDAR
|
|
|
|
ICS;
|
|
|
|
|
|
|
|
$request->setBody($ics);
|
2016-05-11 00:26:44 +00:00
|
|
|
|
|
|
|
$response = $this->request($request);
|
|
|
|
|
|
|
|
$this->assertEquals(201, $response->status, 'Incorrect status returned! Full response body: ' . $response->body);
|
|
|
|
$this->assertNull($response->getHeader('ETag'));
|
2013-10-21 22:46:31 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|