ICalendarTest.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. <?php
  2. namespace Sabre\VObject\Splitter;
  3. use Sabre\VObject;
  4. class ICalendarTest extends \PHPUnit_Framework_TestCase {
  5. protected $version;
  6. function setUp() {
  7. $this->version = VObject\Version::VERSION;
  8. }
  9. function createStream($data) {
  10. $stream = fopen('php://memory','r+');
  11. fwrite($stream, $data);
  12. rewind($stream);
  13. return $stream;
  14. }
  15. function testICalendarImportValidEvent() {
  16. $data = <<<EOT
  17. BEGIN:VCALENDAR
  18. BEGIN:VEVENT
  19. UID:foo
  20. DTSTAMP:20140122T233226Z
  21. DTSTART:20140101T070000Z
  22. END:VEVENT
  23. END:VCALENDAR
  24. EOT;
  25. $tempFile = $this->createStream($data);
  26. $objects = new ICalendar($tempFile);
  27. $return = "";
  28. while($object=$objects->getNext()) {
  29. $return .= $object->serialize();
  30. }
  31. $this->assertEquals(array(), VObject\Reader::read($return)->validate());
  32. }
  33. /**
  34. * @expectedException Sabre\VObject\ParseException
  35. */
  36. function testICalendarImportWrongType() {
  37. $data = <<<EOT
  38. BEGIN:VCARD
  39. UID:foo1
  40. END:VCARD
  41. BEGIN:VCARD
  42. UID:foo2
  43. END:VCARD
  44. EOT;
  45. $tempFile = $this->createStream($data);
  46. $objects = new ICalendar($tempFile);
  47. }
  48. function testICalendarImportEndOfData() {
  49. $data = <<<EOT
  50. BEGIN:VCALENDAR
  51. BEGIN:VEVENT
  52. UID:foo
  53. DTSTAMP:20140122T233226Z
  54. END:VEVENT
  55. END:VCALENDAR
  56. EOT;
  57. $tempFile = $this->createStream($data);
  58. $objects = new ICalendar($tempFile);
  59. $return = "";
  60. while($object=$objects->getNext()) {
  61. $return .= $object->serialize();
  62. }
  63. $this->assertNull($object=$objects->getNext());
  64. }
  65. /**
  66. * @expectedException Sabre\VObject\ParseException
  67. */
  68. function testICalendarImportInvalidEvent() {
  69. $data = <<<EOT
  70. EOT;
  71. $tempFile = $this->createStream($data);
  72. $objects = new ICalendar($tempFile);
  73. }
  74. function testICalendarImportMultipleValidEvents() {
  75. $event[] = <<<EOT
  76. BEGIN:VEVENT
  77. UID:foo1
  78. DTSTAMP:20140122T233226Z
  79. DTSTART:20140101T050000Z
  80. END:VEVENT
  81. EOT;
  82. $event[] = <<<EOT
  83. BEGIN:VEVENT
  84. UID:foo2
  85. DTSTAMP:20140122T233226Z
  86. DTSTART:20140101T060000Z
  87. END:VEVENT
  88. EOT;
  89. $data = <<<EOT
  90. BEGIN:VCALENDAR
  91. $event[0]
  92. $event[1]
  93. END:VCALENDAR
  94. EOT;
  95. $tempFile = $this->createStream($data);
  96. $objects = new ICalendar($tempFile);
  97. $return = "";
  98. $i = 0;
  99. while($object=$objects->getNext()) {
  100. $expected = <<<EOT
  101. BEGIN:VCALENDAR
  102. VERSION:2.0
  103. PRODID:-//Sabre//Sabre VObject $this->version//EN
  104. CALSCALE:GREGORIAN
  105. $event[$i]
  106. END:VCALENDAR
  107. EOT;
  108. $return .= $object->serialize();
  109. $expected = str_replace("\n", "\r\n", $expected);
  110. $this->assertEquals($expected, $object->serialize());
  111. $i++;
  112. }
  113. $this->assertEquals(array(), VObject\Reader::read($return)->validate());
  114. }
  115. function testICalendarImportEventWithoutUID() {
  116. $data = <<<EOT
  117. BEGIN:VCALENDAR
  118. VERSION:2.0
  119. PRODID:-//Sabre//Sabre VObject $this->version//EN
  120. CALSCALE:GREGORIAN
  121. BEGIN:VEVENT
  122. DTSTART:20140101T040000Z
  123. DTSTAMP:20140122T233226Z
  124. END:VEVENT
  125. END:VCALENDAR
  126. EOT;
  127. $tempFile = $this->createStream($data);
  128. $objects = new ICalendar($tempFile);
  129. $return = "";
  130. while($object=$objects->getNext()) {
  131. $return .= $object->serialize();
  132. }
  133. $messages = VObject\Reader::read($return)->validate();
  134. if ($messages) {
  135. $messages = array_map(
  136. function($item) { return $item['message']; },
  137. $messages
  138. );
  139. $this->fail('Validation errors: ' . implode("\n", $messages));
  140. } else {
  141. $this->assertEquals(array(), $messages);
  142. }
  143. }
  144. function testICalendarImportMultipleVTIMEZONESAndMultipleValidEvents() {
  145. $timezones = <<<EOT
  146. BEGIN:VTIMEZONE
  147. TZID:Europe/Berlin
  148. BEGIN:DAYLIGHT
  149. TZOFFSETFROM:+0100
  150. RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU
  151. DTSTART:19810329T020000
  152. TZNAME:MESZ
  153. TZOFFSETTO:+0200
  154. END:DAYLIGHT
  155. BEGIN:STANDARD
  156. TZOFFSETFROM:+0200
  157. RRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU
  158. DTSTART:19961027T030000
  159. TZNAME:MEZ
  160. TZOFFSETTO:+0100
  161. END:STANDARD
  162. END:VTIMEZONE
  163. BEGIN:VTIMEZONE
  164. TZID:Europe/London
  165. BEGIN:DAYLIGHT
  166. TZOFFSETFROM:+0000
  167. RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU
  168. DTSTART:19810329T010000
  169. TZNAME:GMT+01:00
  170. TZOFFSETTO:+0100
  171. END:DAYLIGHT
  172. BEGIN:STANDARD
  173. TZOFFSETFROM:+0100
  174. RRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU
  175. DTSTART:19961027T020000
  176. TZNAME:GMT
  177. TZOFFSETTO:+0000
  178. END:STANDARD
  179. END:VTIMEZONE
  180. EOT;
  181. $event[] = <<<EOT
  182. BEGIN:VEVENT
  183. UID:foo1
  184. DTSTAMP:20140122T232710Z
  185. DTSTART:20140101T010000Z
  186. END:VEVENT
  187. EOT;
  188. $event[] = <<<EOT
  189. BEGIN:VEVENT
  190. UID:foo2
  191. DTSTAMP:20140122T232710Z
  192. DTSTART:20140101T020000Z
  193. END:VEVENT
  194. EOT;
  195. $event[] = <<<EOT
  196. BEGIN:VEVENT
  197. UID:foo3
  198. DTSTAMP:20140122T232710Z
  199. DTSTART:20140101T030000Z
  200. END:VEVENT
  201. EOT;
  202. $data = <<<EOT
  203. BEGIN:VCALENDAR
  204. $timezones
  205. $event[0]
  206. $event[1]
  207. $event[2]
  208. END:VCALENDAR
  209. EOT;
  210. $tempFile = $this->createStream($data);
  211. $objects = new ICalendar($tempFile);
  212. $return = "";
  213. $i = 0;
  214. while($object=$objects->getNext()) {
  215. $expected = <<<EOT
  216. BEGIN:VCALENDAR
  217. VERSION:2.0
  218. PRODID:-//Sabre//Sabre VObject $this->version//EN
  219. CALSCALE:GREGORIAN
  220. $timezones
  221. $event[$i]
  222. END:VCALENDAR
  223. EOT;
  224. $expected = str_replace("\n", "\r\n", $expected);
  225. $this->assertEquals($expected, $object->serialize());
  226. $return .= $object->serialize();
  227. $i++;
  228. }
  229. $this->assertEquals(array(), VObject\Reader::read($return)->validate());
  230. }
  231. function testICalendarImportWithOutVTIMEZONES() {
  232. $data = <<<EOT
  233. BEGIN:VCALENDAR
  234. VERSION:2.0
  235. PRODID:-//Apple Inc.//Mac OS X 10.8//EN
  236. CALSCALE:GREGORIAN
  237. BEGIN:VEVENT
  238. CREATED:20120605T072109Z
  239. UID:D6716295-C10F-4B20-82F9-E1A3026C7DCF
  240. DTEND;VALUE=DATE:20120717
  241. TRANSP:TRANSPARENT
  242. SUMMARY:Start Vorbereitung
  243. DTSTART;VALUE=DATE:20120716
  244. DTSTAMP:20120605T072115Z
  245. SEQUENCE:2
  246. BEGIN:VALARM
  247. X-WR-ALARMUID:A99EDA6A-35EB-4446-B8BC-CDA3C60C627D
  248. UID:A99EDA6A-35EB-4446-B8BC-CDA3C60C627D
  249. TRIGGER:-PT15H
  250. X-APPLE-DEFAULT-ALARM:TRUE
  251. ATTACH;VALUE=URI:Basso
  252. ACTION:AUDIO
  253. END:VALARM
  254. END:VEVENT
  255. END:VCALENDAR
  256. EOT;
  257. $tempFile = $this->createStream($data);
  258. $objects = new ICalendar($tempFile);
  259. $return = "";
  260. while($object=$objects->getNext()) {
  261. $return .= $object->serialize();
  262. }
  263. $messages = VObject\Reader::read($return)->validate();
  264. $this->assertEquals(array(), $messages);
  265. }
  266. }