[Pkg-owncloud-commits] [php-sabredav] 130/220: Validation/fixing mostly works for CalDAV now too.
David Prévot
taffit at moszumanska.debian.org
Thu May 12 01:21:17 UTC 2016
This is an automated email from the git hooks/post-receive script.
taffit pushed a commit to branch master
in repository php-sabredav.
commit 43fadc28a9020219c58942a1705c97862f43d338
Author: Evert Pot <me at evertpot.com>
Date: Wed Apr 6 01:37:49 2016 -0400
Validation/fixing mostly works for CalDAV now too.
---
lib/CalDAV/Plugin.php | 82 +++++----
lib/CardDAV/Plugin.php | 2 +-
tests/Sabre/CalDAV/JCalTransformTest.php | 23 ++-
.../Sabre/CalDAV/Schedule/ScheduleDeliverTest.php | 61 +++----
tests/Sabre/CalDAV/ValidateICalTest.php | 187 +++++++++++++++++----
5 files changed, 263 insertions(+), 92 deletions(-)
diff --git a/lib/CalDAV/Plugin.php b/lib/CalDAV/Plugin.php
index b268b0d..68dade5 100644
--- a/lib/CalDAV/Plugin.php
+++ b/lib/CalDAV/Plugin.php
@@ -822,11 +822,7 @@ class Plugin extends DAV\ServerPlugin {
$data = stream_get_contents($data);
}
- $before = md5($data);
- // Converting the data to unicode, if needed.
- $data = DAV\StringUtil::ensureUTF8($data);
-
- if ($before !== md5($data)) $modified = true;
+ $before = $data;
try {
@@ -866,7 +862,7 @@ class Plugin extends DAV\ServerPlugin {
}
$foundType = null;
- $foundUID = null;
+
foreach ($vobj->getComponents() as $component) {
switch ($component->name) {
case 'VTIMEZONE' :
@@ -874,31 +870,59 @@ class Plugin extends DAV\ServerPlugin {
case 'VEVENT' :
case 'VTODO' :
case 'VJOURNAL' :
- if (is_null($foundType)) {
- $foundType = $component->name;
- if (!in_array($foundType, $supportedComponents)) {
- throw new Exception\InvalidComponentType('This calendar only supports ' . implode(', ', $supportedComponents) . '. We found a ' . $foundType);
- }
- if (!isset($component->UID)) {
- throw new DAV\Exception\BadRequest('Every ' . $component->name . ' component must have an UID');
- }
- $foundUID = (string)$component->UID;
- } else {
- if ($foundType !== $component->name) {
- throw new DAV\Exception\BadRequest('A calendar object must only contain 1 component. We found a ' . $component->name . ' as well as a ' . $foundType);
- }
- if ($foundUID !== (string)$component->UID) {
- throw new DAV\Exception\BadRequest('Every ' . $component->name . ' in this object must have identical UIDs');
- }
- }
+ $foundType = $component->name;
+ break;
+ }
+
+ }
+
+ if (!$foundType || !in_array($foundType, $supportedComponents)) {
+ throw new Exception\InvalidComponentType('iCalendar objects must at least have a component of type ' . implode(', ', $supportedComponents));
+ }
+
+ $options = VObject\Node::PROFILE_CALDAV;
+ $prefer = $this->server->getHTTPPrefer();
+
+ if ($prefer['handling'] !== 'strict') {
+ $options |= VObject\Node::REPAIR;
+ }
+
+ $messages = $vobj->validate($options);
+
+ $highestLevel = 0;
+ $warningMessage = null;
+
+ // $messages contains a list of problems with the vcard, along with
+ // their severity.
+ foreach ($messages as $message) {
+
+ if ($message['level'] > $highestLevel) {
+ // Recording the highest reported error level.
+ $highestLevel = $message['level'];
+ $warningMessage = $message['message'];
+ }
+ switch ($message['level']) {
+
+ case 1 :
+ // Level 1 means that there was a problem, but it was repaired.
+ $modified = true;
+ break;
+ case 2 :
+ // Level 2 means a warning, but not critical
break;
- default :
- throw new DAV\Exception\BadRequest('You are not allowed to create components of type: ' . $component->name . ' here');
+ case 3 :
+ // Level 3 means a critical error
+ throw new DAV\Exception\UnsupportedMediaType('Validation error in iCalendar: ' . $message['message']);
}
+
+ }
+ if ($warningMessage) {
+ $response->setHeader(
+ 'X-Sabre-Ew-Gross',
+ 'iCalendar validation warning: ' . $warningMessage
+ );
}
- if (!$foundType)
- throw new DAV\Exception\BadRequest('iCalendar object must contain at least 1 of VEVENT, VTODO or VJOURNAL');
// We use an extra variable to allow event handles to tell us wether
// the object was modified or not.
@@ -918,12 +942,12 @@ class Plugin extends DAV\ServerPlugin {
]
);
- if ($subModified) {
+ if ($modified || $subModified) {
// An event handler told us that it modified the object.
$data = $vobj->serialize();
// Using md5 to figure out if there was an *actual* change.
- if (!$modified && $before !== md5($data)) {
+ if (!$modified && strcmp($data, $before) !== 0) {
$modified = true;
}
diff --git a/lib/CardDAV/Plugin.php b/lib/CardDAV/Plugin.php
index 2134127..dc1eae8 100644
--- a/lib/CardDAV/Plugin.php
+++ b/lib/CardDAV/Plugin.php
@@ -407,7 +407,7 @@ class Plugin extends DAV\ServerPlugin {
// Re-serializing object.
$data = $vobj->serialize();
- if (strcmp($data, $before) !== 0) {
+ if (!$modified && strcmp($data, $before) !== 0) {
// This ensures that the system does not send an ETag back.
$modified = true;
}
diff --git a/tests/Sabre/CalDAV/JCalTransformTest.php b/tests/Sabre/CalDAV/JCalTransformTest.php
index 6c99e49..f1eed17 100644
--- a/tests/Sabre/CalDAV/JCalTransformTest.php
+++ b/tests/Sabre/CalDAV/JCalTransformTest.php
@@ -3,9 +3,12 @@
namespace Sabre\CalDAV;
use Sabre\HTTP\Request;
+use Sabre\VObject;
class JCalTransformTest extends \Sabre\DAVServerTest {
+ use VObject\PHPUnitAssertions;
+
protected $setupCalDAV = true;
protected $caldavCalendars = [
[
@@ -222,7 +225,8 @@ XML;
[
'vevent',
[
- ['uid', (object)[], 'text', 'foo'],
+ ['uid', (object)[], 'text', 'foo'],
+ ['dtstart', (object)[], 'date', '2016-04-06'],
],
[],
],
@@ -236,7 +240,22 @@ XML;
$modified
);
- $this->assertEquals("BEGIN:VCALENDAR\r\nBEGIN:VEVENT\r\nUID:foo\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n", $input);
+
+ $expected = <<<ICS
+BEGIN:VCALENDAR
+VERSION:2.0
+BEGIN:VEVENT
+UID:foo
+DTSTART;VALUE=DATE:20160406
+DTSTAMP:**ANY**
+END:VEVENT
+END:VCALENDAR
+ICS;
+
+ $this->assertVObjectEqualsVObject(
+ $expected,
+ $input
+ );
}
diff --git a/tests/Sabre/CalDAV/Schedule/ScheduleDeliverTest.php b/tests/Sabre/CalDAV/Schedule/ScheduleDeliverTest.php
index 95512a8..8123c68 100644
--- a/tests/Sabre/CalDAV/Schedule/ScheduleDeliverTest.php
+++ b/tests/Sabre/CalDAV/Schedule/ScheduleDeliverTest.php
@@ -3,9 +3,12 @@
namespace Sabre\CalDAV\Schedule;
use Sabre\HTTP\Request;
+use Sabre\VObject;
class ScheduleDeliverTest extends \Sabre\DAVServerTest {
+ use VObject\PHPUnitAssertions;
+
public $setupCalDAV = true;
public $setupCalDAVScheduling = true;
public $setupACL = true;
@@ -48,16 +51,18 @@ ICS;
$expected = <<<ICS
BEGIN:VCALENDAR
+VERSION:2.0
BEGIN:VEVENT
UID:foo
DTSTART:20140811T230000Z
ORGANIZER:mailto:user1.sabredav at sabredav.org
ATTENDEE;SCHEDULE-STATUS=1.2:mailto:user2.sabredav at sabredav.org
+DTSTAMP:**ANY**
END:VEVENT
END:VCALENDAR
ICS;
- $this->assertVObjEquals(
+ $this->assertVObjectEqualsVObject(
$expected,
$newObject
);
@@ -68,6 +73,7 @@ ICS;
$newObject = <<<ICS
BEGIN:VCALENDAR
+VERSION:2.0
BEGIN:VEVENT
UID:foo
DTSTART:20140811T230000Z
@@ -114,6 +120,7 @@ END:VCALENDAR
ICS;
$oldObject = <<<ICS
BEGIN:VCALENDAR
+VERSION:2.0
BEGIN:VEVENT
UID:foo
DTSTART:20140811T230000Z
@@ -127,16 +134,18 @@ ICS;
$expected = <<<ICS
BEGIN:VCALENDAR
+VERSION:2.0
BEGIN:VEVENT
UID:foo
DTSTART:20140811T230000Z
ORGANIZER:mailto:user1.sabredav at sabredav.org
ATTENDEE;SCHEDULE-STATUS=1.2:mailto:user2.sabredav at sabredav.org
+DTSTAMP:**ANY**
END:VEVENT
END:VCALENDAR
ICS;
- $this->assertVObjEquals(
+ $this->assertVObjectEqualsVObject(
$expected,
$newObject
);
@@ -323,6 +332,7 @@ ICS;
$expected = <<<ICS
BEGIN:VCALENDAR
+VERSION:2.0
BEGIN:VEVENT
UID:foo
DTSTART:20140811T230000Z
@@ -330,11 +340,12 @@ ORGANIZER;SCHEDULE-STATUS=1.2:mailto:user2.sabredav at sabredav.org
ATTENDEE;PARTSTAT=ACCEPTED:mailto:user2.sabredav at sabredav.org
ATTENDEE;PARTSTAT=ACCEPTED:mailto:user1.sabredav at sabredav.org
ATTENDEE:mailto:user3.sabredav at sabredav.org
+DTSTAMP:**ANY**
END:VEVENT
END:VCALENDAR
ICS;
- $this->assertVObjEquals(
+ $this->assertVObjectEqualsVObject(
$expected,
$newObject
);
@@ -360,16 +371,18 @@ ICS;
$expected = <<<ICS
BEGIN:VCALENDAR
+VERSION:2.0
BEGIN:VEVENT
UID:foo
DTSTART:20140811T230000Z
ORGANIZER:mailto:user1.sabredav at sabredav.org
ATTENDEE;SCHEDULE-STATUS=3.7:mailto:user3.sabredav at sabredav.org
+DTSTAMP:**ANY**
END:VEVENT
END:VCALENDAR
ICS;
- $this->assertVObjEquals(
+ $this->assertVObjectEqualsVObject(
$expected,
$newObject
);
@@ -396,16 +409,18 @@ ICS;
$expected = <<<ICS
BEGIN:VCALENDAR
+VERSION:2.0
BEGIN:VEVENT
UID:foo
DTSTART:20140811T230000Z
ORGANIZER:mailto:user1.sabredav at sabredav.org
ATTENDEE;SCHEDULE-STATUS=5.2:mailto:user2.sabredav at sabredav.org
+DTSTAMP:**ANY**
END:VEVENT
END:VCALENDAR
ICS;
- $this->assertVObjEquals(
+ $this->assertVObjectEqualsVObject(
$expected,
$newObject
);
@@ -432,16 +447,18 @@ ICS;
$expected = <<<ICS
BEGIN:VCALENDAR
+VERSION:2.0
BEGIN:VEVENT
UID:foo
DTSTART:20140811T230000Z
ORGANIZER:mailto:user1.sabredav at sabredav.org
ATTENDEE;SCHEDULE-STATUS=5.2:mailto:user2.sabredav at sabredav.org
+DTSTAMP:**ANY**
END:VEVENT
END:VCALENDAR
ICS;
- $this->assertVObjEquals(
+ $this->assertVObjectEqualsVObject(
$expected,
$newObject
);
@@ -467,16 +484,18 @@ ICS;
$expected = <<<ICS
BEGIN:VCALENDAR
+VERSION:2.0
BEGIN:VEVENT
UID:foo
DTSTART:20140811T230000Z
ORGANIZER:mailto:user1.sabredav at sabredav.org
ATTENDEE;SCHEDULE-STATUS=5.2:mailto:user2.sabredav at sabredav.org
+DTSTAMP:**ANY**
END:VEVENT
END:VCALENDAR
ICS;
- $this->assertVObjEquals(
+ $this->assertVObjectEqualsVObject(
$expected,
$newObject
);
@@ -500,16 +519,18 @@ ICS;
$expected = <<<ICS
BEGIN:VCALENDAR
+VERSION:2.0
BEGIN:VEVENT
UID:foo
DTSTART:20140811T230000Z
ORGANIZER:mailto:user1.sabredav at sabredav.org
ATTENDEE;SCHEDULE-STATUS=5.2:mailto:user2.sabredav at sabredav.org
+DTSTAMP:**ANY**
END:VEVENT
END:VCALENDAR
ICS;
- $this->assertVObjEquals(
+ $this->assertVObjectEqualsVObject(
$expected,
$newObject
);
@@ -535,16 +556,18 @@ ICS;
$expected = <<<ICS
BEGIN:VCALENDAR
+VERSION:2.0
BEGIN:VEVENT
UID:foo
DTSTART:20140811T230000Z
ORGANIZER:mailto:user1.sabredav at sabredav.org
ATTENDEE;SCHEDULE-STATUS=5.2:mailto:user2.sabredav at sabredav.org
+DTSTAMP:**ANY**
END:VEVENT
END:VCALENDAR
ICS;
- $this->assertVObjEquals(
+ $this->assertVObjectEqualsVObject(
$expected,
$newObject
);
@@ -640,24 +663,4 @@ ICS;
}
- function assertVObjEquals($expected, $actual) {
-
- $format = function($data) {
-
- $data = trim($data, "\r\n");
- $data = str_replace("\r", "", $data);
- // Unfolding lines.
- $data = str_replace("\n ", "", $data);
-
- return $data;
-
- };
-
- $this->assertEquals(
- $format($expected),
- $format($actual)
- );
-
- }
-
}
diff --git a/tests/Sabre/CalDAV/ValidateICalTest.php b/tests/Sabre/CalDAV/ValidateICalTest.php
index 100f08e..629df90 100644
--- a/tests/Sabre/CalDAV/ValidateICalTest.php
+++ b/tests/Sabre/CalDAV/ValidateICalTest.php
@@ -79,11 +79,25 @@ class ValidateICalTest extends \PHPUnit_Framework_TestCase {
function testCreateFileValid() {
- $request = HTTP\Sapi::createFromServerArray([
- 'REQUEST_METHOD' => 'PUT',
- 'REQUEST_URI' => '/calendars/admin/calendar1/blabla.ics',
- ]);
- $request->setBody("BEGIN:VCALENDAR\r\nBEGIN:VEVENT\r\nUID:foo\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n");
+ $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);
$response = $this->request($request);
@@ -91,12 +105,12 @@ class ValidateICalTest extends \PHPUnit_Framework_TestCase {
$this->assertEquals([
'X-Sabre-Version' => [DAV\Version::VERSION],
'Content-Length' => ['0'],
- 'ETag' => ['"' . md5("BEGIN:VCALENDAR\r\nBEGIN:VEVENT\r\nUID:foo\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n") . '"'],
+ 'ETag' => ['"' . md5($ics) . '"'],
], $response->getHeaders());
$expected = [
'uri' => 'blabla.ics',
- 'calendardata' => "BEGIN:VCALENDAR\r\nBEGIN:VEVENT\r\nUID:foo\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n",
+ 'calendardata' => $ics,
'calendarid' => 'calendar1',
'lastmodified' => null,
];
@@ -105,17 +119,105 @@ class ValidateICalTest extends \PHPUnit_Framework_TestCase {
}
- function testCreateFileNoComponents() {
+ function testCreateFileNoVersion() {
- $request = HTTP\Sapi::createFromServerArray([
- 'REQUEST_METHOD' => 'PUT',
- 'REQUEST_URI' => '/calendars/admin/calendar1/blabla.ics',
- ]);
- $request->setBody("BEGIN:VCALENDAR\r\nEND:VCALENDAR\r\n");
+ $request = new HTTP\Request(
+ 'PUT',
+ '/calendars/admin/calendar1/blabla.ics',
+ ['Prefer' => 'handling=strict']
+ );
+
+ $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);
+
+ }
+
+ function testCreateFileNoVersionFixed() {
+
+ $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);
$response = $this->request($request);
- $this->assertEquals(400, $response->status, 'Incorrect status returned! Full response body: ' . $response->body);
+ $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);
}
@@ -129,7 +231,7 @@ class ValidateICalTest extends \PHPUnit_Framework_TestCase {
$response = $this->request($request);
- $this->assertEquals(400, $response->status, 'Incorrect status returned! Full response body: ' . $response->body);
+ $this->assertEquals(415, $response->status, 'Incorrect status returned! Full response body: ' . $response->body);
}
@@ -157,7 +259,7 @@ class ValidateICalTest extends \PHPUnit_Framework_TestCase {
$response = $this->request($request);
- $this->assertEquals(400, $response->status, 'Incorrect status returned! Full response body: ' . $response->body);
+ $this->assertEquals(415, $response->status, 'Incorrect status returned! Full response body: ' . $response->body);
}
@@ -171,7 +273,7 @@ class ValidateICalTest extends \PHPUnit_Framework_TestCase {
$response = $this->request($request);
- $this->assertEquals(400, $response->status, 'Incorrect status returned! Full response body: ' . $response->body);
+ $this->assertEquals(415, $response->status, 'Incorrect status returned! Full response body: ' . $response->body);
}
@@ -185,7 +287,7 @@ class ValidateICalTest extends \PHPUnit_Framework_TestCase {
$response = $this->request($request);
- $this->assertEquals(400, $response->status, 'Incorrect status returned! Full response body: ' . $response->body);
+ $this->assertEquals(403, $response->status, 'Incorrect status returned! Full response body: ' . $response->body);
}
@@ -206,20 +308,30 @@ class ValidateICalTest extends \PHPUnit_Framework_TestCase {
function testUpdateFileParsableBody() {
$this->calBackend->createCalendarObject('calendar1', 'blabla.ics', 'foo');
- $request = HTTP\Sapi::createFromServerArray([
- 'REQUEST_METHOD' => 'PUT',
- 'REQUEST_URI' => '/calendars/admin/calendar1/blabla.ics',
- ]);
- $body = "BEGIN:VCALENDAR\r\nBEGIN:VEVENT\r\nUID:foo\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n";
- $request->setBody($body);
-
+ $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);
$response = $this->request($request);
$this->assertEquals(204, $response->status);
$expected = [
'uri' => 'blabla.ics',
- 'calendardata' => $body,
+ 'calendardata' => $ics,
'calendarid' => 'calendar1',
'lastmodified' => null,
];
@@ -266,11 +378,24 @@ class ValidateICalTest extends \PHPUnit_Framework_TestCase {
*/
function testCreateFileModified() {
- $request = HTTP\Sapi::createFromServerArray([
- 'REQUEST_METHOD' => 'PUT',
- 'REQUEST_URI' => '/calendars/admin/calendar1/blabla.ics',
- ]);
- $request->setBody("BEGIN:VCALENDAR\r\nBEGIN:VEVENT\r\nUID:foo\r\nSUMMARY:Meeting in M\xfcnster\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n");
+ $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);
$response = $this->request($request);
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-php/php-sabredav.git
More information about the Pkg-owncloud-commits
mailing list