DateTimeTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. <?php
  2. namespace Sabre\VObject\Property\ICalendar;
  3. use Sabre\VObject\Component;
  4. use Sabre\VObject\Component\VCalendar;
  5. class DateTimeTest extends \PHPUnit_Framework_TestCase {
  6. protected $vcal;
  7. function setUp() {
  8. $this->vcal = new VCalendar();
  9. }
  10. function testSetDateTime() {
  11. $tz = new \DateTimeZone('Europe/Amsterdam');
  12. $dt = new \DateTime('1985-07-04 01:30:00', $tz);
  13. $dt->setTimeZone($tz);
  14. $elem = $this->vcal->createProperty('DTSTART');
  15. $elem->setDateTime($dt);
  16. $this->assertEquals('19850704T013000', (string)$elem);
  17. $this->assertEquals('Europe/Amsterdam', (string)$elem['TZID']);
  18. $this->assertNull($elem['VALUE']);
  19. $this->assertTrue($elem->hasTime());
  20. }
  21. function testSetDateTimeLOCAL() {
  22. $tz = new \DateTimeZone('Europe/Amsterdam');
  23. $dt = new \DateTime('1985-07-04 01:30:00', $tz);
  24. $dt->setTimeZone($tz);
  25. $elem = $this->vcal->createProperty('DTSTART');
  26. $elem->setDateTime($dt, $isFloating = true);
  27. $this->assertEquals('19850704T013000', (string)$elem);
  28. $this->assertNull($elem['TZID']);
  29. $this->assertTrue($elem->hasTime());
  30. }
  31. function testSetDateTimeUTC() {
  32. $tz = new \DateTimeZone('GMT');
  33. $dt = new \DateTime('1985-07-04 01:30:00', $tz);
  34. $dt->setTimeZone($tz);
  35. $elem = $this->vcal->createProperty('DTSTART');
  36. $elem->setDateTime($dt);
  37. $this->assertEquals('19850704T013000Z', (string)$elem);
  38. $this->assertNull($elem['TZID']);
  39. $this->assertTrue($elem->hasTime());
  40. }
  41. function testSetDateTimeLOCALTZ() {
  42. $tz = new \DateTimeZone('Europe/Amsterdam');
  43. $dt = new \DateTime('1985-07-04 01:30:00', $tz);
  44. $dt->setTimeZone($tz);
  45. $elem = $this->vcal->createProperty('DTSTART');
  46. $elem->setDateTime($dt);
  47. $this->assertEquals('19850704T013000', (string)$elem);
  48. $this->assertEquals('Europe/Amsterdam', (string)$elem['TZID']);
  49. $this->assertTrue($elem->hasTime());
  50. }
  51. function testSetDateTimeDATE() {
  52. $tz = new \DateTimeZone('Europe/Amsterdam');
  53. $dt = new \DateTime('1985-07-04 01:30:00', $tz);
  54. $dt->setTimeZone($tz);
  55. $elem = $this->vcal->createProperty('DTSTART');
  56. $elem['VALUE'] = 'DATE';
  57. $elem->setDateTime($dt);
  58. $this->assertEquals('19850704', (string)$elem);
  59. $this->assertNull($elem['TZID']);
  60. $this->assertEquals('DATE', (string)$elem['VALUE']);
  61. $this->assertFalse($elem->hasTime());
  62. }
  63. function testSetValue() {
  64. $tz = new \DateTimeZone('Europe/Amsterdam');
  65. $dt = new \DateTime('1985-07-04 01:30:00', $tz);
  66. $dt->setTimeZone($tz);
  67. $elem = $this->vcal->createProperty('DTSTART');
  68. $elem->setValue($dt);
  69. $this->assertEquals('19850704T013000', (string)$elem);
  70. $this->assertEquals('Europe/Amsterdam', (string)$elem['TZID']);
  71. $this->assertNull($elem['VALUE']);
  72. $this->assertTrue($elem->hasTime());
  73. }
  74. function testSetValueArray() {
  75. $tz = new \DateTimeZone('Europe/Amsterdam');
  76. $dt1 = new \DateTime('1985-07-04 01:30:00', $tz);
  77. $dt2 = new \DateTime('1985-07-04 02:30:00', $tz);
  78. $dt1->setTimeZone($tz);
  79. $dt2->setTimeZone($tz);
  80. $elem = $this->vcal->createProperty('DTSTART');
  81. $elem->setValue(array($dt1, $dt2));
  82. $this->assertEquals('19850704T013000,19850704T023000', (string)$elem);
  83. $this->assertEquals('Europe/Amsterdam', (string)$elem['TZID']);
  84. $this->assertNull($elem['VALUE']);
  85. $this->assertTrue($elem->hasTime());
  86. }
  87. function testSetParts() {
  88. $tz = new \DateTimeZone('Europe/Amsterdam');
  89. $dt1 = new \DateTime('1985-07-04 01:30:00', $tz);
  90. $dt2 = new \DateTime('1985-07-04 02:30:00', $tz);
  91. $dt1->setTimeZone($tz);
  92. $dt2->setTimeZone($tz);
  93. $elem = $this->vcal->createProperty('DTSTART');
  94. $elem->setParts(array($dt1, $dt2));
  95. $this->assertEquals('19850704T013000,19850704T023000', (string)$elem);
  96. $this->assertEquals('Europe/Amsterdam', (string)$elem['TZID']);
  97. $this->assertNull($elem['VALUE']);
  98. $this->assertTrue($elem->hasTime());
  99. }
  100. function testSetPartsStrings() {
  101. $dt1 = '19850704T013000Z';
  102. $dt2 = '19850704T023000Z';
  103. $elem = $this->vcal->createProperty('DTSTART');
  104. $elem->setParts(array($dt1, $dt2));
  105. $this->assertEquals('19850704T013000Z,19850704T023000Z', (string)$elem);
  106. $this->assertNull($elem['VALUE']);
  107. $this->assertTrue($elem->hasTime());
  108. }
  109. function testGetDateTimeCached() {
  110. $tz = new \DateTimeZone('Europe/Amsterdam');
  111. $dt = new \DateTime('1985-07-04 01:30:00', $tz);
  112. $dt->setTimeZone($tz);
  113. $elem = $this->vcal->createProperty('DTSTART');
  114. $elem->setDateTime($dt);
  115. $this->assertEquals($elem->getDateTime(), $dt);
  116. }
  117. function testGetDateTimeDateNULL() {
  118. $elem = $this->vcal->createProperty('DTSTART');
  119. $dt = $elem->getDateTime();
  120. $this->assertNull($dt);
  121. }
  122. function testGetDateTimeDateDATE() {
  123. $elem = $this->vcal->createProperty('DTSTART','19850704');
  124. $dt = $elem->getDateTime();
  125. $this->assertInstanceOf('DateTime', $dt);
  126. $this->assertEquals('1985-07-04 00:00:00', $dt->format('Y-m-d H:i:s'));
  127. }
  128. function testGetDateTimeDateDATEReferenceTimeZone() {
  129. $elem = $this->vcal->createProperty('DTSTART','19850704');
  130. $tz = new \DateTimeZone('America/Toronto');
  131. $dt = $elem->getDateTime($tz);
  132. $dt->setTimeZone(new \DateTimeZone('UTC'));
  133. $this->assertInstanceOf('DateTime', $dt);
  134. $this->assertEquals('1985-07-04 04:00:00', $dt->format('Y-m-d H:i:s'));
  135. }
  136. function testGetDateTimeDateFloating() {
  137. $elem = $this->vcal->createProperty('DTSTART','19850704T013000');
  138. $dt = $elem->getDateTime();
  139. $this->assertInstanceOf('DateTime', $dt);
  140. $this->assertEquals('1985-07-04 01:30:00', $dt->format('Y-m-d H:i:s'));
  141. }
  142. function testGetDateTimeDateFloatingReferenceTimeZone() {
  143. $elem = $this->vcal->createProperty('DTSTART','19850704T013000');
  144. $tz = new \DateTimeZone('America/Toronto');
  145. $dt = $elem->getDateTime($tz);
  146. $dt->setTimeZone(new \DateTimeZone('UTC'));
  147. $this->assertInstanceOf('DateTime', $dt);
  148. $this->assertEquals('1985-07-04 05:30:00', $dt->format('Y-m-d H:i:s'));
  149. }
  150. function testGetDateTimeDateUTC() {
  151. $elem = $this->vcal->createProperty('DTSTART','19850704T013000Z');
  152. $dt = $elem->getDateTime();
  153. $this->assertInstanceOf('DateTime', $dt);
  154. $this->assertEquals('1985-07-04 01:30:00', $dt->format('Y-m-d H:i:s'));
  155. $this->assertEquals('UTC', $dt->getTimeZone()->getName());
  156. }
  157. function testGetDateTimeDateLOCALTZ() {
  158. $elem = $this->vcal->createProperty('DTSTART','19850704T013000');
  159. $elem['TZID'] = 'Europe/Amsterdam';
  160. $dt = $elem->getDateTime();
  161. $this->assertInstanceOf('DateTime', $dt);
  162. $this->assertEquals('1985-07-04 01:30:00', $dt->format('Y-m-d H:i:s'));
  163. $this->assertEquals('Europe/Amsterdam', $dt->getTimeZone()->getName());
  164. }
  165. /**
  166. * @expectedException LogicException
  167. */
  168. function testGetDateTimeDateInvalid() {
  169. $elem = $this->vcal->createProperty('DTSTART','bla');
  170. $dt = $elem->getDateTime();
  171. }
  172. function testGetDateTimeWeirdTZ() {
  173. $elem = $this->vcal->createProperty('DTSTART','19850704T013000');
  174. $elem['TZID'] = '/freeassociation.sourceforge.net/Tzfile/Europe/Amsterdam';
  175. $event = $this->vcal->createComponent('VEVENT');
  176. $event->add($elem);
  177. $timezone = $this->vcal->createComponent('VTIMEZONE');
  178. $timezone->TZID = '/freeassociation.sourceforge.net/Tzfile/Europe/Amsterdam';
  179. $timezone->{'X-LIC-LOCATION'} = 'Europe/Amsterdam';
  180. $this->vcal->add($event);
  181. $this->vcal->add($timezone);
  182. $dt = $elem->getDateTime();
  183. $this->assertInstanceOf('DateTime', $dt);
  184. $this->assertEquals('1985-07-04 01:30:00', $dt->format('Y-m-d H:i:s'));
  185. $this->assertEquals('Europe/Amsterdam', $dt->getTimeZone()->getName());
  186. }
  187. function testGetDateTimeBadTimeZone() {
  188. $default = date_default_timezone_get();
  189. date_default_timezone_set('Canada/Eastern');
  190. $elem = $this->vcal->createProperty('DTSTART','19850704T013000');
  191. $elem['TZID'] = 'Moon';
  192. $event = $this->vcal->createComponent('VEVENT');
  193. $event->add($elem);
  194. $timezone = $this->vcal->createComponent('VTIMEZONE');
  195. $timezone->TZID = 'Moon';
  196. $timezone->{'X-LIC-LOCATION'} = 'Moon';
  197. $this->vcal->add($event);
  198. $this->vcal->add($timezone);
  199. $dt = $elem->getDateTime();
  200. $this->assertInstanceOf('DateTime', $dt);
  201. $this->assertEquals('1985-07-04 01:30:00', $dt->format('Y-m-d H:i:s'));
  202. $this->assertEquals('Canada/Eastern', $dt->getTimeZone()->getName());
  203. date_default_timezone_set($default);
  204. }
  205. function testUpdateValueParameter() {
  206. $dtStart = $this->vcal->createProperty('DTSTART', new \DateTime('2013-06-07 15:05:00'));
  207. $dtStart['VALUE'] = 'DATE';
  208. $this->assertEquals("DTSTART;VALUE=DATE:20130607\r\n", $dtStart->serialize());
  209. }
  210. function testValidate() {
  211. $exDate = $this->vcal->createProperty('EXDATE', '-00011130T143000Z');
  212. $messages = $exDate->validate();
  213. $this->assertEquals(1, count($messages));
  214. $this->assertEquals(3, $messages[0]['level']);
  215. }
  216. /**
  217. * This issue was discovered on the sabredav mailing list.
  218. */
  219. function testCreateDatePropertyThroughAdd() {
  220. $vcal = new VCalendar();
  221. $vevent = $vcal->add('VEVENT');
  222. $dtstart = $vevent->add(
  223. 'DTSTART',
  224. new \DateTime('2014-03-07'),
  225. array('VALUE' => 'DATE')
  226. );
  227. $this->assertEquals("DTSTART;VALUE=DATE:20140307\r\n", $dtstart->serialize());
  228. }
  229. }