123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697 |
- <?php
- namespace Sabre\VObject\Component;
- use DateTimeZone;
- use Sabre\VObject;
- class VCalendarTest extends \PHPUnit_Framework_TestCase {
- /**
- * @dataProvider expandData
- */
- public function testExpand($input, $output, $timeZone = 'UTC', $start = '2011-12-01', $end = '2011-12-31') {
- $vcal = VObject\Reader::read($input);
- $timeZone = new DateTimeZone($timeZone);
- $vcal->expand(
- new \DateTime($start),
- new \DateTime($end),
- $timeZone
- );
- // This will normalize the output
- $output = VObject\Reader::read($output)->serialize();
- $this->assertEquals($output, $vcal->serialize());
- }
- public function expandData() {
- $tests = array();
- // No data
- $input = 'BEGIN:VCALENDAR
- CALSCALE:GREGORIAN
- VERSION:2.0
- END:VCALENDAR
- ';
- $output = $input;
- $tests[] = array($input,$output);
- // Simple events
- $input = 'BEGIN:VCALENDAR
- CALSCALE:GREGORIAN
- VERSION:2.0
- BEGIN:VEVENT
- UID:bla
- SUMMARY:InExpand
- DTSTART;VALUE=DATE:20111202
- END:VEVENT
- BEGIN:VEVENT
- UID:bla2
- SUMMARY:NotInExpand
- DTSTART;VALUE=DATE:20120101
- END:VEVENT
- END:VCALENDAR
- ';
- $output = 'BEGIN:VCALENDAR
- CALSCALE:GREGORIAN
- VERSION:2.0
- BEGIN:VEVENT
- UID:bla
- SUMMARY:InExpand
- DTSTART;VALUE=DATE:20111202
- END:VEVENT
- END:VCALENDAR
- ';
- $tests[] = array($input, $output);
- // Removing timezone info
- $input = 'BEGIN:VCALENDAR
- CALSCALE:GREGORIAN
- VERSION:2.0
- BEGIN:VTIMEZONE
- TZID:Europe/Paris
- END:VTIMEZONE
- BEGIN:VEVENT
- UID:bla4
- SUMMARY:RemoveTZ info
- DTSTART;TZID=Europe/Paris:20111203T130102
- END:VEVENT
- END:VCALENDAR
- ';
- $output = 'BEGIN:VCALENDAR
- CALSCALE:GREGORIAN
- VERSION:2.0
- BEGIN:VEVENT
- UID:bla4
- SUMMARY:RemoveTZ info
- DTSTART:20111203T120102Z
- END:VEVENT
- END:VCALENDAR
- ';
- $tests[] = array($input, $output);
- // Recurrence rule
- $input = 'BEGIN:VCALENDAR
- CALSCALE:GREGORIAN
- VERSION:2.0
- BEGIN:VEVENT
- UID:bla6
- SUMMARY:Testing RRule
- DTSTART:20111125T120000Z
- DTEND:20111125T130000Z
- RRULE:FREQ=WEEKLY
- END:VEVENT
- END:VCALENDAR
- ';
- $output = 'BEGIN:VCALENDAR
- CALSCALE:GREGORIAN
- VERSION:2.0
- BEGIN:VEVENT
- UID:bla6
- SUMMARY:Testing RRule
- DTSTART:20111202T120000Z
- DTEND:20111202T130000Z
- RECURRENCE-ID:20111202T120000Z
- END:VEVENT
- BEGIN:VEVENT
- UID:bla6
- SUMMARY:Testing RRule
- DTSTART:20111209T120000Z
- DTEND:20111209T130000Z
- RECURRENCE-ID:20111209T120000Z
- END:VEVENT
- BEGIN:VEVENT
- UID:bla6
- SUMMARY:Testing RRule
- DTSTART:20111216T120000Z
- DTEND:20111216T130000Z
- RECURRENCE-ID:20111216T120000Z
- END:VEVENT
- BEGIN:VEVENT
- UID:bla6
- SUMMARY:Testing RRule
- DTSTART:20111223T120000Z
- DTEND:20111223T130000Z
- RECURRENCE-ID:20111223T120000Z
- END:VEVENT
- BEGIN:VEVENT
- UID:bla6
- SUMMARY:Testing RRule
- DTSTART:20111230T120000Z
- DTEND:20111230T130000Z
- RECURRENCE-ID:20111230T120000Z
- END:VEVENT
- END:VCALENDAR
- ';
- $tests[] = array($input, $output);
- // Recurrence rule + override
- $input = 'BEGIN:VCALENDAR
- CALSCALE:GREGORIAN
- VERSION:2.0
- BEGIN:VEVENT
- UID:bla6
- SUMMARY:Testing RRule2
- DTSTART:20111125T120000Z
- DTEND:20111125T130000Z
- RRULE:FREQ=WEEKLY
- END:VEVENT
- BEGIN:VEVENT
- UID:bla6
- RECURRENCE-ID:20111209T120000Z
- DTSTART:20111209T140000Z
- DTEND:20111209T150000Z
- SUMMARY:Override!
- END:VEVENT
- END:VCALENDAR
- ';
- $output = 'BEGIN:VCALENDAR
- CALSCALE:GREGORIAN
- VERSION:2.0
- BEGIN:VEVENT
- UID:bla6
- SUMMARY:Testing RRule2
- DTSTART:20111202T120000Z
- DTEND:20111202T130000Z
- RECURRENCE-ID:20111202T120000Z
- END:VEVENT
- BEGIN:VEVENT
- UID:bla6
- RECURRENCE-ID:20111209T120000Z
- DTSTART:20111209T140000Z
- DTEND:20111209T150000Z
- SUMMARY:Override!
- END:VEVENT
- BEGIN:VEVENT
- UID:bla6
- SUMMARY:Testing RRule2
- DTSTART:20111216T120000Z
- DTEND:20111216T130000Z
- RECURRENCE-ID:20111216T120000Z
- END:VEVENT
- BEGIN:VEVENT
- UID:bla6
- SUMMARY:Testing RRule2
- DTSTART:20111223T120000Z
- DTEND:20111223T130000Z
- RECURRENCE-ID:20111223T120000Z
- END:VEVENT
- BEGIN:VEVENT
- UID:bla6
- SUMMARY:Testing RRule2
- DTSTART:20111230T120000Z
- DTEND:20111230T130000Z
- RECURRENCE-ID:20111230T120000Z
- END:VEVENT
- END:VCALENDAR
- ';
- $tests[] = array($input, $output);
- // Floating dates and times.
- $input = <<<ICS
- BEGIN:VCALENDAR
- VERSION:2.0
- BEGIN:VEVENT
- UID:bla1
- DTSTART:20141112T195000
- END:VEVENT
- BEGIN:VEVENT
- UID:bla2
- DTSTART;VALUE=DATE:20141112
- END:VEVENT
- BEGIN:VEVENT
- UID:bla3
- DTSTART;VALUE=DATE:20141112
- RRULE:FREQ=DAILY;COUNT=2
- END:VEVENT
- END:VCALENDAR
- ICS;
- $output = <<<ICS
- BEGIN:VCALENDAR
- VERSION:2.0
- BEGIN:VEVENT
- UID:bla1
- DTSTART:20141112T225000Z
- END:VEVENT
- BEGIN:VEVENT
- UID:bla2
- DTSTART;VALUE=DATE:20141112
- END:VEVENT
- BEGIN:VEVENT
- UID:bla3
- DTSTART;VALUE=DATE:20141112
- RECURRENCE-ID;VALUE=DATE:20141112
- END:VEVENT
- BEGIN:VEVENT
- UID:bla3
- DTSTART;VALUE=DATE:20141113
- RECURRENCE-ID;VALUE=DATE:20141113
- END:VEVENT
- END:VCALENDAR
- ICS;
- $tests[] = array($input, $output, 'America/Argentina/Buenos_Aires', '2014-01-01', '2015-01-01');
- // Recurrence rule with no valid instances
- $input = 'BEGIN:VCALENDAR
- CALSCALE:GREGORIAN
- VERSION:2.0
- BEGIN:VEVENT
- UID:bla6
- SUMMARY:Testing RRule3
- DTSTART:20111125T120000Z
- DTEND:20111125T130000Z
- RRULE:FREQ=WEEKLY;COUNT=1
- EXDATE:20111125T120000Z
- END:VEVENT
- END:VCALENDAR
- ';
- $output = 'BEGIN:VCALENDAR
- CALSCALE:GREGORIAN
- VERSION:2.0
- END:VCALENDAR
- ';
- $tests[] = array($input, $output);
- return $tests;
- }
- /**
- * @expectedException LogicException
- */
- public function testBrokenEventExpand() {
- $input = 'BEGIN:VCALENDAR
- CALSCALE:GREGORIAN
- VERSION:2.0
- BEGIN:VEVENT
- RRULE:FREQ=WEEKLY
- DTSTART;VALUE=DATE:20111202
- END:VEVENT
- END:VCALENDAR
- ';
- $vcal = VObject\Reader::read($input);
- $vcal->expand(
- new \DateTime('2011-12-01'),
- new \DateTime('2011-12-31')
- );
- }
- function testGetDocumentType() {
- $vcard = new VCalendar();
- $vcard->VERSION = '2.0';
- $this->assertEquals(VCalendar::ICALENDAR20, $vcard->getDocumentType());
- }
- function testValidateCorrect() {
- $input = 'BEGIN:VCALENDAR
- CALSCALE:GREGORIAN
- VERSION:2.0
- PRODID:foo
- BEGIN:VEVENT
- DTSTART;VALUE=DATE:20111202
- DTSTAMP:20140122T233226Z
- UID:foo
- END:VEVENT
- END:VCALENDAR
- ';
- $vcal = VObject\Reader::read($input);
- $this->assertEquals(array(), $vcal->validate(), 'Got an error');
- }
- function testValidateNoVersion() {
- $input = 'BEGIN:VCALENDAR
- CALSCALE:GREGORIAN
- PRODID:foo
- BEGIN:VEVENT
- DTSTART;VALUE=DATE:20111202
- UID:foo
- DTSTAMP:20140122T234434Z
- END:VEVENT
- END:VCALENDAR
- ';
- $vcal = VObject\Reader::read($input);
- $this->assertEquals(1, count($vcal->validate()));
- }
- function testValidateWrongVersion() {
- $input = 'BEGIN:VCALENDAR
- CALSCALE:GREGORIAN
- VERSION:3.0
- PRODID:foo
- BEGIN:VEVENT
- DTSTART;VALUE=DATE:20111202
- UID:foo
- DTSTAMP:20140122T234434Z
- END:VEVENT
- END:VCALENDAR
- ';
- $vcal = VObject\Reader::read($input);
- $this->assertEquals(1, count($vcal->validate()));
- }
- function testValidateNoProdId() {
- $input = 'BEGIN:VCALENDAR
- CALSCALE:GREGORIAN
- VERSION:2.0
- BEGIN:VEVENT
- DTSTART;VALUE=DATE:20111202
- UID:foo
- DTSTAMP:20140122T234434Z
- END:VEVENT
- END:VCALENDAR
- ';
- $vcal = VObject\Reader::read($input);
- $this->assertEquals(1, count($vcal->validate()));
- }
- function testValidateDoubleCalScale() {
- $input = 'BEGIN:VCALENDAR
- VERSION:2.0
- PRODID:foo
- CALSCALE:GREGORIAN
- CALSCALE:GREGORIAN
- BEGIN:VEVENT
- DTSTART;VALUE=DATE:20111202
- UID:foo
- DTSTAMP:20140122T234434Z
- END:VEVENT
- END:VCALENDAR
- ';
- $vcal = VObject\Reader::read($input);
- $this->assertEquals(1, count($vcal->validate()));
- }
- function testValidateDoubleMethod() {
- $input = 'BEGIN:VCALENDAR
- VERSION:2.0
- PRODID:foo
- METHOD:REQUEST
- METHOD:REQUEST
- BEGIN:VEVENT
- DTSTART;VALUE=DATE:20111202
- UID:foo
- DTSTAMP:20140122T234434Z
- END:VEVENT
- END:VCALENDAR
- ';
- $vcal = VObject\Reader::read($input);
- $this->assertEquals(1, count($vcal->validate()));
- }
- function testValidateTwoMasterEvents() {
- $input = 'BEGIN:VCALENDAR
- VERSION:2.0
- PRODID:foo
- METHOD:REQUEST
- BEGIN:VEVENT
- DTSTART;VALUE=DATE:20111202
- UID:foo
- DTSTAMP:20140122T234434Z
- END:VEVENT
- BEGIN:VEVENT
- DTSTART;VALUE=DATE:20111202
- UID:foo
- DTSTAMP:20140122T234434Z
- END:VEVENT
- END:VCALENDAR
- ';
- $vcal = VObject\Reader::read($input);
- $this->assertEquals(1, count($vcal->validate()));
- }
- function testValidateOneMasterEvent() {
- $input = 'BEGIN:VCALENDAR
- VERSION:2.0
- PRODID:foo
- METHOD:REQUEST
- BEGIN:VEVENT
- DTSTART;VALUE=DATE:20111202
- UID:foo
- DTSTAMP:20140122T234434Z
- END:VEVENT
- BEGIN:VEVENT
- DTSTART;VALUE=DATE:20111202
- UID:foo
- DTSTAMP:20140122T234434Z
- RECURRENCE-ID;VALUE=DATE:20111202
- END:VEVENT
- END:VCALENDAR
- ';
- $vcal = VObject\Reader::read($input);
- $this->assertEquals(0, count($vcal->validate()));
- }
- function testGetBaseComponent() {
- $input = 'BEGIN:VCALENDAR
- VERSION:2.0
- PRODID:foo
- METHOD:REQUEST
- BEGIN:VEVENT
- SUMMARY:test
- DTSTART;VALUE=DATE:20111202
- UID:foo
- DTSTAMP:20140122T234434Z
- END:VEVENT
- BEGIN:VEVENT
- DTSTART;VALUE=DATE:20111202
- UID:foo
- DTSTAMP:20140122T234434Z
- RECURRENCE-ID;VALUE=DATE:20111202
- END:VEVENT
- END:VCALENDAR
- ';
- $vcal = VObject\Reader::read($input);
- $result = $vcal->getBaseComponent();
- $this->assertEquals('test', $result->SUMMARY->getValue());
- }
- function testGetBaseComponentNoResult() {
- $input = 'BEGIN:VCALENDAR
- VERSION:2.0
- PRODID:foo
- METHOD:REQUEST
- BEGIN:VEVENT
- SUMMARY:test
- RECURRENCE-ID;VALUE=DATE:20111202
- DTSTART;VALUE=DATE:20111202
- UID:foo
- DTSTAMP:20140122T234434Z
- END:VEVENT
- BEGIN:VEVENT
- DTSTART;VALUE=DATE:20111202
- UID:foo
- DTSTAMP:20140122T234434Z
- RECURRENCE-ID;VALUE=DATE:20111202
- END:VEVENT
- END:VCALENDAR
- ';
- $vcal = VObject\Reader::read($input);
- $result = $vcal->getBaseComponent();
- $this->assertNull($result);
- }
- function testNoComponents() {
- $input = <<<ICS
- BEGIN:VCALENDAR
- VERSION:2.0
- PRODID:vobject
- END:VCALENDAR
- ICS;
- $this->assertValidate(
- $input,
- 0,
- 3,
- "An iCalendar object must have at least 1 component."
- );
- }
- function testCalDAVNoComponents() {
- $input = <<<ICS
- BEGIN:VCALENDAR
- VERSION:2.0
- PRODID:vobject
- BEGIN:VTIMEZONE
- TZID:America/Toronto
- END:VTIMEZONE
- END:VCALENDAR
- ICS;
- $this->assertValidate(
- $input,
- VCalendar::PROFILE_CALDAV,
- 3,
- "A calendar object on a CalDAV server must have at least 1 component (VTODO, VEVENT, VJOURNAL)."
- );
- }
- function testCalDAVMultiUID() {
- $input = <<<ICS
- BEGIN:VCALENDAR
- VERSION:2.0
- PRODID:vobject
- BEGIN:VEVENT
- UID:foo
- DTSTAMP:20150109T184500Z
- DTSTART:20150109T184500Z
- END:VEVENT
- BEGIN:VEVENT
- UID:bar
- DTSTAMP:20150109T184500Z
- DTSTART:20150109T184500Z
- END:VEVENT
- END:VCALENDAR
- ICS;
- $this->assertValidate(
- $input,
- VCalendar::PROFILE_CALDAV,
- 3,
- "A calendar object on a CalDAV server may only have components with the same UID."
- );
- }
- function testCalDAVMultiComponent() {
- $input = <<<ICS
- BEGIN:VCALENDAR
- VERSION:2.0
- PRODID:vobject
- BEGIN:VEVENT
- UID:foo
- RECURRENCE-ID:20150109T185200Z
- DTSTAMP:20150109T184500Z
- DTSTART:20150109T184500Z
- END:VEVENT
- BEGIN:VTODO
- UID:foo
- DTSTAMP:20150109T184500Z
- DTSTART:20150109T184500Z
- END:VTODO
- END:VCALENDAR
- ICS;
- $this->assertValidate(
- $input,
- VCalendar::PROFILE_CALDAV,
- 3,
- "A calendar object on a CalDAV server may only have 1 type of component (VEVENT, VTODO or VJOURNAL)."
- );
- }
- function testCalDAVMETHOD() {
- $input = <<<ICS
- BEGIN:VCALENDAR
- VERSION:2.0
- METHOD:PUBLISH
- PRODID:vobject
- BEGIN:VEVENT
- UID:foo
- RECURRENCE-ID:20150109T185200Z
- DTSTAMP:20150109T184500Z
- DTSTART:20150109T184500Z
- END:VEVENT
- END:VCALENDAR
- ICS;
- $this->assertValidate(
- $input,
- VCalendar::PROFILE_CALDAV,
- 3,
- "A calendar object on a CalDAV server MUST NOT have a METHOD property."
- );
- }
- function assertValidate($ics, $options, $expectedLevel, $expectedMessage = null) {
- $vcal = VObject\Reader::read($ics);
- $result = $vcal->validate($options);
- $this->assertValidateResult($result, $expectedLevel, $expectedMessage);
- }
- function assertValidateResult($input, $expectedLevel, $expectedMessage = null) {
- $messages = array();
- foreach($input as $warning) {
- $messages[] = $warning['message'];
- }
- if ($expectedLevel === 0) {
- $this->assertEquals(0, count($input), 'No validation messages were expected. We got: ' . implode(', ', $messages));
- } else {
- $this->assertEquals(1, count($input), 'We expected exactly 1 validation message, We got: ' . implode(', ', $messages));
- $this->assertEquals($expectedMessage, $input[0]['message']);
- $this->assertEquals($expectedLevel, $input[0]['level']);
- }
- }
- }
|