ReaderTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. <?php
  2. namespace Sabre\VObject;
  3. class ReaderTest extends \PHPUnit_Framework_TestCase {
  4. function testReadComponent() {
  5. $data = "BEGIN:VCALENDAR\r\nEND:VCALENDAR";
  6. $result = Reader::read($data);
  7. $this->assertInstanceOf('Sabre\\VObject\\Component', $result);
  8. $this->assertEquals('VCALENDAR', $result->name);
  9. $this->assertEquals(0, count($result->children));
  10. }
  11. function testReadStream() {
  12. $data = "BEGIN:VCALENDAR\r\nEND:VCALENDAR";
  13. $stream = fopen('php://memory', 'r+');
  14. fwrite($stream, $data);
  15. rewind($stream);
  16. $result = Reader::read($stream);
  17. $this->assertInstanceOf('Sabre\\VObject\\Component', $result);
  18. $this->assertEquals('VCALENDAR', $result->name);
  19. $this->assertEquals(0, count($result->children));
  20. }
  21. function testReadComponentUnixNewLine() {
  22. $data = "BEGIN:VCALENDAR\nEND:VCALENDAR";
  23. $result = Reader::read($data);
  24. $this->assertInstanceOf('Sabre\\VObject\\Component', $result);
  25. $this->assertEquals('VCALENDAR', $result->name);
  26. $this->assertEquals(0, count($result->children));
  27. }
  28. function testReadComponentLineFold() {
  29. $data = "BEGIN:\r\n\tVCALENDAR\r\nE\r\n ND:VCALENDAR";
  30. $result = Reader::read($data);
  31. $this->assertInstanceOf('Sabre\\VObject\\Component', $result);
  32. $this->assertEquals('VCALENDAR', $result->name);
  33. $this->assertEquals(0, count($result->children));
  34. }
  35. /**
  36. * @expectedException Sabre\VObject\ParseException
  37. */
  38. function testReadCorruptComponent() {
  39. $data = "BEGIN:VCALENDAR\r\nEND:FOO";
  40. $result = Reader::read($data);
  41. }
  42. /**
  43. * @expectedException Sabre\VObject\ParseException
  44. */
  45. function testReadCorruptSubComponent() {
  46. $data = "BEGIN:VCALENDAR\r\nBEGIN:VEVENT\r\nEND:FOO\r\nEND:VCALENDAR";
  47. $result = Reader::read($data);
  48. }
  49. function testReadProperty() {
  50. $data = "BEGIN:VCALENDAR\r\nSUMMARY:propValue\r\nEND:VCALENDAR";
  51. $result = Reader::read($data);
  52. $result = $result->SUMMARY;
  53. $this->assertInstanceOf('Sabre\\VObject\\Property', $result);
  54. $this->assertEquals('SUMMARY', $result->name);
  55. $this->assertEquals('propValue', $result->getValue());
  56. }
  57. function testReadPropertyWithNewLine() {
  58. $data = "BEGIN:VCALENDAR\r\nSUMMARY:Line1\\nLine2\\NLine3\\\\Not the 4th line!\r\nEND:VCALENDAR";
  59. $result = Reader::read($data);
  60. $result = $result->SUMMARY;
  61. $this->assertInstanceOf('Sabre\\VObject\\Property', $result);
  62. $this->assertEquals('SUMMARY', $result->name);
  63. $this->assertEquals("Line1\nLine2\nLine3\\Not the 4th line!", $result->getValue());
  64. }
  65. function testReadMappedProperty() {
  66. $data = "BEGIN:VCALENDAR\r\nDTSTART:20110529\r\nEND:VCALENDAR";
  67. $result = Reader::read($data);
  68. $result = $result->DTSTART;
  69. $this->assertInstanceOf('Sabre\\VObject\\Property\\ICalendar\\DateTime', $result);
  70. $this->assertEquals('DTSTART', $result->name);
  71. $this->assertEquals('20110529', $result->getValue());
  72. }
  73. function testReadMappedPropertyGrouped() {
  74. $data = "BEGIN:VCALENDAR\r\nfoo.DTSTART:20110529\r\nEND:VCALENDAR";
  75. $result = Reader::read($data);
  76. $result = $result->DTSTART;
  77. $this->assertInstanceOf('Sabre\\VObject\\Property\\ICalendar\\DateTime', $result);
  78. $this->assertEquals('DTSTART', $result->name);
  79. $this->assertEquals('20110529', $result->getValue());
  80. }
  81. /**
  82. * @expectedException Sabre\VObject\ParseException
  83. */
  84. function testReadBrokenLine() {
  85. $data = "BEGIN:VCALENDAR\r\nPROPNAME;propValue";
  86. $result = Reader::read($data);
  87. }
  88. function testReadPropertyInComponent() {
  89. $data = array(
  90. "BEGIN:VCALENDAR",
  91. "PROPNAME:propValue",
  92. "END:VCALENDAR"
  93. );
  94. $result = Reader::read(implode("\r\n",$data));
  95. $this->assertInstanceOf('Sabre\\VObject\\Component', $result);
  96. $this->assertEquals('VCALENDAR', $result->name);
  97. $this->assertEquals(1, count($result->children()));
  98. $this->assertInstanceOf('Sabre\\VObject\\Property', $result->children[0]);
  99. $this->assertEquals('PROPNAME', $result->children[0]->name);
  100. $this->assertEquals('propValue', $result->children[0]->getValue());
  101. }
  102. function testReadNestedComponent() {
  103. $data = array(
  104. "BEGIN:VCALENDAR",
  105. "BEGIN:VTIMEZONE",
  106. "BEGIN:DAYLIGHT",
  107. "END:DAYLIGHT",
  108. "END:VTIMEZONE",
  109. "END:VCALENDAR"
  110. );
  111. $result = Reader::read(implode("\r\n",$data));
  112. $this->assertInstanceOf('Sabre\\VObject\\Component', $result);
  113. $this->assertEquals('VCALENDAR', $result->name);
  114. $this->assertEquals(1, count($result->children()));
  115. $this->assertInstanceOf('Sabre\\VObject\\Component', $result->children[0]);
  116. $this->assertEquals('VTIMEZONE', $result->children[0]->name);
  117. $this->assertEquals(1, count($result->children[0]->children()));
  118. $this->assertInstanceOf('Sabre\\VObject\\Component', $result->children[0]->children[0]);
  119. $this->assertEquals('DAYLIGHT', $result->children[0]->children[0]->name);
  120. }
  121. function testReadPropertyParameter() {
  122. $data = "BEGIN:VCALENDAR\r\nPROPNAME;PARAMNAME=paramvalue:propValue\r\nEND:VCALENDAR";
  123. $result = Reader::read($data);
  124. $result = $result->PROPNAME;
  125. $this->assertInstanceOf('Sabre\\VObject\\Property', $result);
  126. $this->assertEquals('PROPNAME', $result->name);
  127. $this->assertEquals('propValue', $result->getValue());
  128. $this->assertEquals(1, count($result->parameters()));
  129. $this->assertEquals('PARAMNAME', $result->parameters['PARAMNAME']->name);
  130. $this->assertEquals('paramvalue', $result->parameters['PARAMNAME']->getValue());
  131. }
  132. function testReadPropertyRepeatingParameter() {
  133. $data = "BEGIN:VCALENDAR\r\nPROPNAME;N=1;N=2;N=3,4;N=\"5\",6;N=\"7,8\";N=9,10;N=^'11^':propValue\r\nEND:VCALENDAR";
  134. $result = Reader::read($data);
  135. $result = $result->PROPNAME;
  136. $this->assertInstanceOf('Sabre\\VObject\\Property', $result);
  137. $this->assertEquals('PROPNAME', $result->name);
  138. $this->assertEquals('propValue', $result->getValue());
  139. $this->assertEquals(1, count($result->parameters()));
  140. $this->assertEquals('N', $result->parameters['N']->name);
  141. $this->assertEquals('1,2,3,4,5,6,7,8,9,10,"11"', $result->parameters['N']->getValue());
  142. $this->assertEquals(array(1,2,3,4,5,6,"7,8",9,10,'"11"'), $result->parameters['N']->getParts());
  143. }
  144. function testReadPropertyRepeatingNamelessGuessedParameter() {
  145. $data = "BEGIN:VCALENDAR\r\nPROPNAME;WORK;VOICE;PREF:propValue\r\nEND:VCALENDAR";
  146. $result = Reader::read($data);
  147. $result = $result->PROPNAME;
  148. $this->assertInstanceOf('Sabre\\VObject\\Property', $result);
  149. $this->assertEquals('PROPNAME', $result->name);
  150. $this->assertEquals('propValue', $result->getValue());
  151. $this->assertEquals(1, count($result->parameters()));
  152. $this->assertEquals('TYPE', $result->parameters['TYPE']->name);
  153. $this->assertEquals('WORK,VOICE,PREF', $result->parameters['TYPE']->getValue());
  154. $this->assertEquals(array('WORK', 'VOICE', 'PREF'), $result->parameters['TYPE']->getParts());
  155. }
  156. function testReadPropertyNoName() {
  157. $data = "BEGIN:VCALENDAR\r\nPROPNAME;PRODIGY:propValue\r\nEND:VCALENDAR";
  158. $result = Reader::read($data);
  159. $result = $result->PROPNAME;
  160. $this->assertInstanceOf('Sabre\\VObject\\Property', $result);
  161. $this->assertEquals('PROPNAME', $result->name);
  162. $this->assertEquals('propValue', $result->getValue());
  163. $this->assertEquals(1, count($result->parameters()));
  164. $this->assertEquals('TYPE', $result->parameters['TYPE']->name);
  165. $this->assertTrue($result->parameters['TYPE']->noName);
  166. $this->assertEquals('PRODIGY', $result->parameters['TYPE']);
  167. }
  168. function testReadPropertyParameterExtraColon() {
  169. $data = "BEGIN:VCALENDAR\r\nPROPNAME;PARAMNAME=paramvalue:propValue:anotherrandomstring\r\nEND:VCALENDAR";
  170. $result = Reader::read($data);
  171. $result = $result->PROPNAME;
  172. $this->assertInstanceOf('Sabre\\VObject\\Property', $result);
  173. $this->assertEquals('PROPNAME', $result->name);
  174. $this->assertEquals('propValue:anotherrandomstring', $result->getValue());
  175. $this->assertEquals(1, count($result->parameters()));
  176. $this->assertEquals('PARAMNAME', $result->parameters['PARAMNAME']->name);
  177. $this->assertEquals('paramvalue', $result->parameters['PARAMNAME']->getValue());
  178. }
  179. function testReadProperty2Parameters() {
  180. $data = "BEGIN:VCALENDAR\r\nPROPNAME;PARAMNAME=paramvalue;PARAMNAME2=paramvalue2:propValue\r\nEND:VCALENDAR";
  181. $result = Reader::read($data);
  182. $result = $result->PROPNAME;
  183. $this->assertInstanceOf('Sabre\\VObject\\Property', $result);
  184. $this->assertEquals('PROPNAME', $result->name);
  185. $this->assertEquals('propValue', $result->getValue());
  186. $this->assertEquals(2, count($result->parameters()));
  187. $this->assertEquals('PARAMNAME', $result->parameters['PARAMNAME']->name);
  188. $this->assertEquals('paramvalue', $result->parameters['PARAMNAME']->getValue());
  189. $this->assertEquals('PARAMNAME2', $result->parameters['PARAMNAME2']->name);
  190. $this->assertEquals('paramvalue2', $result->parameters['PARAMNAME2']->getValue());
  191. }
  192. function testReadPropertyParameterQuoted() {
  193. $data = "BEGIN:VCALENDAR\r\nPROPNAME;PARAMNAME=\"paramvalue\":propValue\r\nEND:VCALENDAR";
  194. $result = Reader::read($data);
  195. $result = $result->PROPNAME;
  196. $this->assertInstanceOf('Sabre\\VObject\\Property', $result);
  197. $this->assertEquals('PROPNAME', $result->name);
  198. $this->assertEquals('propValue', $result->getValue());
  199. $this->assertEquals(1, count($result->parameters()));
  200. $this->assertEquals('PARAMNAME', $result->parameters['PARAMNAME']->name);
  201. $this->assertEquals('paramvalue', $result->parameters['PARAMNAME']->getValue());
  202. }
  203. function testReadPropertyParameterNewLines() {
  204. $data = "BEGIN:VCALENDAR\r\nPROPNAME;PARAMNAME=paramvalue1^nvalue2^^nvalue3:propValue\r\nEND:VCALENDAR";
  205. $result = Reader::read($data);
  206. $result = $result->propname;
  207. $this->assertInstanceOf('Sabre\\VObject\\Property', $result);
  208. $this->assertEquals('PROPNAME', $result->name);
  209. $this->assertEquals('propValue', $result->getValue());
  210. $this->assertEquals(1, count($result->parameters()));
  211. $this->assertEquals('PARAMNAME', $result->parameters['PARAMNAME']->name);
  212. $this->assertEquals("paramvalue1\nvalue2^nvalue3", $result->parameters['PARAMNAME']->getValue());
  213. }
  214. function testReadPropertyParameterQuotedColon() {
  215. $data = "BEGIN:VCALENDAR\r\nPROPNAME;PARAMNAME=\"param:value\":propValue\r\nEND:VCALENDAR";
  216. $result = Reader::read($data);
  217. $result = $result->propname;
  218. $this->assertInstanceOf('Sabre\\VObject\\Property', $result);
  219. $this->assertEquals('PROPNAME', $result->name);
  220. $this->assertEquals('propValue', $result->getValue());
  221. $this->assertEquals(1, count($result->parameters()));
  222. $this->assertEquals('PARAMNAME', $result->parameters['PARAMNAME']->name);
  223. $this->assertEquals('param:value', $result->parameters['PARAMNAME']->getValue());
  224. }
  225. function testReadForgiving() {
  226. $data = array(
  227. "BEGIN:VCALENDAR",
  228. "X_PROP:propValue",
  229. "END:VCALENDAR"
  230. );
  231. $caught = false;
  232. try {
  233. $result = Reader::read(implode("\r\n",$data));
  234. } catch (ParseException $e) {
  235. $caught = true;
  236. }
  237. $this->assertEquals(true, $caught);
  238. $result = Reader::read(implode("\r\n",$data), Reader::OPTION_FORGIVING);
  239. $expected = implode("\r\n", array(
  240. "BEGIN:VCALENDAR",
  241. "X_PROP:propValue",
  242. "END:VCALENDAR",
  243. ""
  244. ));
  245. $this->assertEquals($expected, $result->serialize());
  246. }
  247. function testReadWithInvalidLine() {
  248. $data = array(
  249. "BEGIN:VCALENDAR",
  250. "DESCRIPTION:propValue",
  251. "Yes, we've actually seen a file with non-idented property values on multiple lines",
  252. "END:VCALENDAR"
  253. );
  254. $caught = false;
  255. try {
  256. $result = Reader::read(implode("\r\n",$data));
  257. } catch (ParseException $e) {
  258. $caught = true;
  259. }
  260. $this->assertEquals(true, $caught);
  261. $result = Reader::read(implode("\r\n",$data), Reader::OPTION_IGNORE_INVALID_LINES);
  262. $expected = implode("\r\n", array(
  263. "BEGIN:VCALENDAR",
  264. "DESCRIPTION:propValue",
  265. "END:VCALENDAR",
  266. ""
  267. ));
  268. $this->assertEquals($expected, $result->serialize());
  269. }
  270. /**
  271. * Reported as Issue 32.
  272. *
  273. * @expectedException \Sabre\VObject\ParseException
  274. */
  275. public function testReadIncompleteFile() {
  276. $input = <<<ICS
  277. BEGIN:VCALENDAR
  278. VERSION:1.0
  279. BEGIN:VEVENT
  280. X-FUNAMBOL-FOLDER:DEFAULT_FOLDER
  281. X-FUNAMBOL-ALLDAY:0
  282. DTSTART:20111017T110000Z
  283. DTEND:20111017T123000Z
  284. X-MICROSOFT-CDO-BUSYSTATUS:BUSY
  285. CATEGORIES:
  286. LOCATION;ENCODING=QUOTED-PRINTABLE;CHARSET=UTF-8:Netviewer Meeting
  287. PRIORITY:1
  288. STATUS:3
  289. X-MICROSOFT-CDO-REPLYTIME:20111017T064200Z
  290. SUMMARY;ENCODING=QUOTED-PRINTABLE;CHARSET=UTF-8:Kopieren: test
  291. CLASS:PUBLIC
  292. AALARM:
  293. RRULE:
  294. X-FUNAMBOL-BILLINGINFO:
  295. X-FUNAMBOL-COMPANIES:
  296. X-FUNAMBOL-MILEAGE:
  297. X-FUNAMBOL-NOAGING:0
  298. ATTENDEE;STATUS=NEEDS ACTION;ENCODING=QUOTED-PRINTABLE;CHARSET=UTF-8:'Heino' heino@test.com
  299. ATTENDEE;STATUS=NEEDS ACTION;ENCODING=QUOTED-PRINTABLE;CHARSET=UTF-8:'Markus' test@test.com
  300. ATTENDEE;STATUS=NEEDS AC
  301. ICS;
  302. Reader::read($input);
  303. }
  304. /**
  305. * @expectedException \InvalidArgumentException
  306. */
  307. public function testReadBrokenInput() {
  308. Reader::read(false);
  309. }
  310. public function testReadBOM() {
  311. $data = chr(0xef) . chr(0xbb) . chr(0xbf) . "BEGIN:VCALENDAR\r\nEND:VCALENDAR";
  312. $result = Reader::read($data);
  313. $this->assertInstanceOf('Sabre\\VObject\\Component', $result);
  314. $this->assertEquals('VCALENDAR', $result->name);
  315. $this->assertEquals(0, count($result->children));
  316. }
  317. }