123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359 |
- <?php
- namespace Sabre\VObject\Property\ICalendar;
- use Sabre\VObject\Component;
- use Sabre\VObject\Component\VCalendar;
- class DateTimeTest extends \PHPUnit_Framework_TestCase {
- protected $vcal;
- function setUp() {
- $this->vcal = new VCalendar();
- }
- function testSetDateTime() {
- $tz = new \DateTimeZone('Europe/Amsterdam');
- $dt = new \DateTime('1985-07-04 01:30:00', $tz);
- $dt->setTimeZone($tz);
- $elem = $this->vcal->createProperty('DTSTART');
- $elem->setDateTime($dt);
- $this->assertEquals('19850704T013000', (string)$elem);
- $this->assertEquals('Europe/Amsterdam', (string)$elem['TZID']);
- $this->assertNull($elem['VALUE']);
- $this->assertTrue($elem->hasTime());
- }
- function testSetDateTimeLOCAL() {
- $tz = new \DateTimeZone('Europe/Amsterdam');
- $dt = new \DateTime('1985-07-04 01:30:00', $tz);
- $dt->setTimeZone($tz);
- $elem = $this->vcal->createProperty('DTSTART');
- $elem->setDateTime($dt, $isFloating = true);
- $this->assertEquals('19850704T013000', (string)$elem);
- $this->assertNull($elem['TZID']);
- $this->assertTrue($elem->hasTime());
- }
- function testSetDateTimeUTC() {
- $tz = new \DateTimeZone('GMT');
- $dt = new \DateTime('1985-07-04 01:30:00', $tz);
- $dt->setTimeZone($tz);
- $elem = $this->vcal->createProperty('DTSTART');
- $elem->setDateTime($dt);
- $this->assertEquals('19850704T013000Z', (string)$elem);
- $this->assertNull($elem['TZID']);
- $this->assertTrue($elem->hasTime());
- }
- function testSetDateTimeLOCALTZ() {
- $tz = new \DateTimeZone('Europe/Amsterdam');
- $dt = new \DateTime('1985-07-04 01:30:00', $tz);
- $dt->setTimeZone($tz);
- $elem = $this->vcal->createProperty('DTSTART');
- $elem->setDateTime($dt);
- $this->assertEquals('19850704T013000', (string)$elem);
- $this->assertEquals('Europe/Amsterdam', (string)$elem['TZID']);
- $this->assertTrue($elem->hasTime());
- }
- function testSetDateTimeDATE() {
- $tz = new \DateTimeZone('Europe/Amsterdam');
- $dt = new \DateTime('1985-07-04 01:30:00', $tz);
- $dt->setTimeZone($tz);
- $elem = $this->vcal->createProperty('DTSTART');
- $elem['VALUE'] = 'DATE';
- $elem->setDateTime($dt);
- $this->assertEquals('19850704', (string)$elem);
- $this->assertNull($elem['TZID']);
- $this->assertEquals('DATE', (string)$elem['VALUE']);
- $this->assertFalse($elem->hasTime());
- }
- function testSetValue() {
- $tz = new \DateTimeZone('Europe/Amsterdam');
- $dt = new \DateTime('1985-07-04 01:30:00', $tz);
- $dt->setTimeZone($tz);
- $elem = $this->vcal->createProperty('DTSTART');
- $elem->setValue($dt);
- $this->assertEquals('19850704T013000', (string)$elem);
- $this->assertEquals('Europe/Amsterdam', (string)$elem['TZID']);
- $this->assertNull($elem['VALUE']);
- $this->assertTrue($elem->hasTime());
- }
- function testSetValueArray() {
- $tz = new \DateTimeZone('Europe/Amsterdam');
- $dt1 = new \DateTime('1985-07-04 01:30:00', $tz);
- $dt2 = new \DateTime('1985-07-04 02:30:00', $tz);
- $dt1->setTimeZone($tz);
- $dt2->setTimeZone($tz);
- $elem = $this->vcal->createProperty('DTSTART');
- $elem->setValue(array($dt1, $dt2));
- $this->assertEquals('19850704T013000,19850704T023000', (string)$elem);
- $this->assertEquals('Europe/Amsterdam', (string)$elem['TZID']);
- $this->assertNull($elem['VALUE']);
- $this->assertTrue($elem->hasTime());
- }
- function testSetParts() {
- $tz = new \DateTimeZone('Europe/Amsterdam');
- $dt1 = new \DateTime('1985-07-04 01:30:00', $tz);
- $dt2 = new \DateTime('1985-07-04 02:30:00', $tz);
- $dt1->setTimeZone($tz);
- $dt2->setTimeZone($tz);
- $elem = $this->vcal->createProperty('DTSTART');
- $elem->setParts(array($dt1, $dt2));
- $this->assertEquals('19850704T013000,19850704T023000', (string)$elem);
- $this->assertEquals('Europe/Amsterdam', (string)$elem['TZID']);
- $this->assertNull($elem['VALUE']);
- $this->assertTrue($elem->hasTime());
- }
- function testSetPartsStrings() {
- $dt1 = '19850704T013000Z';
- $dt2 = '19850704T023000Z';
- $elem = $this->vcal->createProperty('DTSTART');
- $elem->setParts(array($dt1, $dt2));
- $this->assertEquals('19850704T013000Z,19850704T023000Z', (string)$elem);
- $this->assertNull($elem['VALUE']);
- $this->assertTrue($elem->hasTime());
- }
- function testGetDateTimeCached() {
- $tz = new \DateTimeZone('Europe/Amsterdam');
- $dt = new \DateTime('1985-07-04 01:30:00', $tz);
- $dt->setTimeZone($tz);
- $elem = $this->vcal->createProperty('DTSTART');
- $elem->setDateTime($dt);
- $this->assertEquals($elem->getDateTime(), $dt);
- }
- function testGetDateTimeDateNULL() {
- $elem = $this->vcal->createProperty('DTSTART');
- $dt = $elem->getDateTime();
- $this->assertNull($dt);
- }
- function testGetDateTimeDateDATE() {
- $elem = $this->vcal->createProperty('DTSTART','19850704');
- $dt = $elem->getDateTime();
- $this->assertInstanceOf('DateTime', $dt);
- $this->assertEquals('1985-07-04 00:00:00', $dt->format('Y-m-d H:i:s'));
- }
- function testGetDateTimeDateDATEReferenceTimeZone() {
- $elem = $this->vcal->createProperty('DTSTART','19850704');
- $tz = new \DateTimeZone('America/Toronto');
- $dt = $elem->getDateTime($tz);
- $dt->setTimeZone(new \DateTimeZone('UTC'));
- $this->assertInstanceOf('DateTime', $dt);
- $this->assertEquals('1985-07-04 04:00:00', $dt->format('Y-m-d H:i:s'));
- }
- function testGetDateTimeDateFloating() {
- $elem = $this->vcal->createProperty('DTSTART','19850704T013000');
- $dt = $elem->getDateTime();
- $this->assertInstanceOf('DateTime', $dt);
- $this->assertEquals('1985-07-04 01:30:00', $dt->format('Y-m-d H:i:s'));
- }
- function testGetDateTimeDateFloatingReferenceTimeZone() {
- $elem = $this->vcal->createProperty('DTSTART','19850704T013000');
- $tz = new \DateTimeZone('America/Toronto');
- $dt = $elem->getDateTime($tz);
- $dt->setTimeZone(new \DateTimeZone('UTC'));
- $this->assertInstanceOf('DateTime', $dt);
- $this->assertEquals('1985-07-04 05:30:00', $dt->format('Y-m-d H:i:s'));
- }
- function testGetDateTimeDateUTC() {
- $elem = $this->vcal->createProperty('DTSTART','19850704T013000Z');
- $dt = $elem->getDateTime();
- $this->assertInstanceOf('DateTime', $dt);
- $this->assertEquals('1985-07-04 01:30:00', $dt->format('Y-m-d H:i:s'));
- $this->assertEquals('UTC', $dt->getTimeZone()->getName());
- }
- function testGetDateTimeDateLOCALTZ() {
- $elem = $this->vcal->createProperty('DTSTART','19850704T013000');
- $elem['TZID'] = 'Europe/Amsterdam';
- $dt = $elem->getDateTime();
- $this->assertInstanceOf('DateTime', $dt);
- $this->assertEquals('1985-07-04 01:30:00', $dt->format('Y-m-d H:i:s'));
- $this->assertEquals('Europe/Amsterdam', $dt->getTimeZone()->getName());
- }
- /**
- * @expectedException LogicException
- */
- function testGetDateTimeDateInvalid() {
- $elem = $this->vcal->createProperty('DTSTART','bla');
- $dt = $elem->getDateTime();
- }
- function testGetDateTimeWeirdTZ() {
- $elem = $this->vcal->createProperty('DTSTART','19850704T013000');
- $elem['TZID'] = '/freeassociation.sourceforge.net/Tzfile/Europe/Amsterdam';
- $event = $this->vcal->createComponent('VEVENT');
- $event->add($elem);
- $timezone = $this->vcal->createComponent('VTIMEZONE');
- $timezone->TZID = '/freeassociation.sourceforge.net/Tzfile/Europe/Amsterdam';
- $timezone->{'X-LIC-LOCATION'} = 'Europe/Amsterdam';
- $this->vcal->add($event);
- $this->vcal->add($timezone);
- $dt = $elem->getDateTime();
- $this->assertInstanceOf('DateTime', $dt);
- $this->assertEquals('1985-07-04 01:30:00', $dt->format('Y-m-d H:i:s'));
- $this->assertEquals('Europe/Amsterdam', $dt->getTimeZone()->getName());
- }
- function testGetDateTimeBadTimeZone() {
- $default = date_default_timezone_get();
- date_default_timezone_set('Canada/Eastern');
- $elem = $this->vcal->createProperty('DTSTART','19850704T013000');
- $elem['TZID'] = 'Moon';
- $event = $this->vcal->createComponent('VEVENT');
- $event->add($elem);
- $timezone = $this->vcal->createComponent('VTIMEZONE');
- $timezone->TZID = 'Moon';
- $timezone->{'X-LIC-LOCATION'} = 'Moon';
- $this->vcal->add($event);
- $this->vcal->add($timezone);
- $dt = $elem->getDateTime();
- $this->assertInstanceOf('DateTime', $dt);
- $this->assertEquals('1985-07-04 01:30:00', $dt->format('Y-m-d H:i:s'));
- $this->assertEquals('Canada/Eastern', $dt->getTimeZone()->getName());
- date_default_timezone_set($default);
- }
- function testUpdateValueParameter() {
- $dtStart = $this->vcal->createProperty('DTSTART', new \DateTime('2013-06-07 15:05:00'));
- $dtStart['VALUE'] = 'DATE';
- $this->assertEquals("DTSTART;VALUE=DATE:20130607\r\n", $dtStart->serialize());
- }
- function testValidate() {
- $exDate = $this->vcal->createProperty('EXDATE', '-00011130T143000Z');
- $messages = $exDate->validate();
- $this->assertEquals(1, count($messages));
- $this->assertEquals(3, $messages[0]['level']);
- }
- /**
- * This issue was discovered on the sabredav mailing list.
- */
- function testCreateDatePropertyThroughAdd() {
- $vcal = new VCalendar();
- $vevent = $vcal->add('VEVENT');
- $dtstart = $vevent->add(
- 'DTSTART',
- new \DateTime('2014-03-07'),
- array('VALUE' => 'DATE')
- );
- $this->assertEquals("DTSTART;VALUE=DATE:20140307\r\n", $dtstart->serialize());
- }
- }
|