MainTest.php 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427
  1. <?php
  2. namespace Sabre\VObject\Recur\EventIterator;
  3. use DateTime;
  4. use DateTimeZone;
  5. use Sabre\VObject\Recur\EventIterator;
  6. use Sabre\VObject\Component\VCalendar;
  7. class MainTest extends \PHPUnit_Framework_TestCase {
  8. function testValues() {
  9. $vcal = new VCalendar();
  10. $ev = $vcal->createComponent('VEVENT');
  11. $ev->UID = 'bla';
  12. $ev->RRULE = 'FREQ=DAILY;BYHOUR=10;BYMINUTE=5;BYSECOND=16;BYWEEKNO=32;BYYEARDAY=100,200';
  13. $dtStart = $vcal->createProperty('DTSTART');
  14. $dtStart->setDateTime(new DateTime('2011-10-07'));
  15. $ev->add($dtStart);
  16. $vcal->add($ev);
  17. $it = new EventIterator($vcal,(string)$ev->uid);
  18. $this->assertTrue($it->isInfinite());
  19. }
  20. /**
  21. * @expectedException InvalidArgumentException
  22. * @depends testValues
  23. */
  24. function testInvalidFreq() {
  25. $vcal = new VCalendar();
  26. $ev = $vcal->createComponent('VEVENT');
  27. $ev->RRULE = 'FREQ=SMONTHLY;INTERVAL=3;UNTIL=20111025T000000Z';
  28. $ev->UID = 'foo';
  29. $dtStart = $vcal->createProperty('DTSTART');
  30. $dtStart->setDateTime(new DateTime('2011-10-07', new DateTimeZone('UTC')));
  31. $ev->add($dtStart);
  32. $vcal->add($ev);
  33. $it = new EventIterator($vcal,(string)$ev->uid);
  34. }
  35. /**
  36. * @expectedException InvalidArgumentException
  37. */
  38. function testVCalendarNoUID() {
  39. $vcal = new VCalendar();
  40. $it = new EventIterator($vcal);
  41. }
  42. /**
  43. * @expectedException InvalidArgumentException
  44. */
  45. function testVCalendarInvalidUID() {
  46. $vcal = new VCalendar();
  47. $it = new EventIterator($vcal,'foo');
  48. }
  49. /**
  50. * @depends testValues
  51. */
  52. function testHourly() {
  53. $vcal = new VCalendar();
  54. $ev = $vcal->createComponent('VEVENT');
  55. $ev->UID = 'bla';
  56. $ev->RRULE = 'FREQ=HOURLY;INTERVAL=3;UNTIL=20111025T000000Z';
  57. $dtStart = $vcal->createProperty('DTSTART');
  58. $dtStart->setDateTime(new DateTime('2011-10-07 12:00:00', new DateTimeZone('UTC')));
  59. $ev->add($dtStart);
  60. $vcal->add($ev);
  61. $it = new EventIterator($vcal,$ev->uid);
  62. // Max is to prevent overflow
  63. $max = 12;
  64. $result = array();
  65. foreach($it as $item) {
  66. $result[] = $item;
  67. $max--;
  68. if (!$max) break;
  69. }
  70. $tz = new DateTimeZone('UTC');
  71. $this->assertEquals(
  72. array(
  73. new DateTime('2011-10-07 12:00:00', $tz),
  74. new DateTime('2011-10-07 15:00:00', $tz),
  75. new DateTime('2011-10-07 18:00:00', $tz),
  76. new DateTime('2011-10-07 21:00:00', $tz),
  77. new DateTime('2011-10-08 00:00:00', $tz),
  78. new DateTime('2011-10-08 03:00:00', $tz),
  79. new DateTime('2011-10-08 06:00:00', $tz),
  80. new DateTime('2011-10-08 09:00:00', $tz),
  81. new DateTime('2011-10-08 12:00:00', $tz),
  82. new DateTime('2011-10-08 15:00:00', $tz),
  83. new DateTime('2011-10-08 18:00:00', $tz),
  84. new DateTime('2011-10-08 21:00:00', $tz),
  85. ),
  86. $result
  87. );
  88. }
  89. /**
  90. * @depends testValues
  91. */
  92. function testDaily() {
  93. $vcal = new VCalendar();
  94. $ev = $vcal->createComponent('VEVENT');
  95. $ev->UID = 'bla';
  96. $ev->RRULE = 'FREQ=DAILY;INTERVAL=3;UNTIL=20111025T000000Z';
  97. $dtStart = $vcal->createProperty('DTSTART');
  98. $dtStart->setDateTime(new DateTime('2011-10-07', new DateTimeZone('UTC')));
  99. $ev->add($dtStart);
  100. $vcal->add($ev);
  101. $it = new EventIterator($vcal,$ev->uid);
  102. // Max is to prevent overflow
  103. $max = 12;
  104. $result = array();
  105. foreach($it as $item) {
  106. $result[] = $item;
  107. $max--;
  108. if (!$max) break;
  109. }
  110. $tz = new DateTimeZone('UTC');
  111. $this->assertEquals(
  112. array(
  113. new DateTime('2011-10-07', $tz),
  114. new DateTime('2011-10-10', $tz),
  115. new DateTime('2011-10-13', $tz),
  116. new DateTime('2011-10-16', $tz),
  117. new DateTime('2011-10-19', $tz),
  118. new DateTime('2011-10-22', $tz),
  119. new DateTime('2011-10-25', $tz),
  120. ),
  121. $result
  122. );
  123. }
  124. /**
  125. * @depends testValues
  126. */
  127. function testNoRRULE() {
  128. $vcal = new VCalendar();
  129. $ev = $vcal->createComponent('VEVENT');
  130. $ev->UID = 'bla';
  131. $dtStart = $vcal->createProperty('DTSTART');
  132. $dtStart->setDateTime(new DateTime('2011-10-07', new DateTimeZone('UTC')));
  133. $ev->add($dtStart);
  134. $vcal->add($ev);
  135. $it = new EventIterator($vcal,$ev->uid);
  136. // Max is to prevent overflow
  137. $max = 12;
  138. $result = array();
  139. foreach($it as $item) {
  140. $result[] = $item;
  141. $max--;
  142. if (!$max) break;
  143. }
  144. $tz = new DateTimeZone('UTC');
  145. $this->assertEquals(
  146. array(
  147. new DateTime('2011-10-07', $tz),
  148. ),
  149. $result
  150. );
  151. }
  152. /**
  153. * @depends testValues
  154. */
  155. function testDailyByDayByHour() {
  156. $vcal = new VCalendar();
  157. $ev = $vcal->createComponent('VEVENT');
  158. $ev->UID = 'bla';
  159. $ev->RRULE = 'FREQ=DAILY;BYDAY=SA,SU;BYHOUR=6,7';
  160. $dtStart = $vcal->createProperty('DTSTART');
  161. $dtStart->setDateTime(new DateTime('2011-10-08 06:00:00', new DateTimeZone('UTC')));
  162. $ev->add($dtStart);
  163. $vcal->add($ev);
  164. $it = new EventIterator($vcal,(string)$ev->uid);
  165. // Grabbing the next 12 items
  166. $max = 12;
  167. $result = array();
  168. foreach($it as $item) {
  169. $result[] = $item;
  170. $max--;
  171. if (!$max) break;
  172. }
  173. $tz = new DateTimeZone('UTC');
  174. $this->assertEquals(
  175. array(
  176. new datetime('2011-10-08 06:00:00', $tz),
  177. new datetime('2011-10-08 07:00:00', $tz),
  178. new datetime('2011-10-09 06:00:00', $tz),
  179. new datetime('2011-10-09 07:00:00', $tz),
  180. new datetime('2011-10-15 06:00:00', $tz),
  181. new datetime('2011-10-15 07:00:00', $tz),
  182. new datetime('2011-10-16 06:00:00', $tz),
  183. new datetime('2011-10-16 07:00:00', $tz),
  184. new datetime('2011-10-22 06:00:00', $tz),
  185. new datetime('2011-10-22 07:00:00', $tz),
  186. new datetime('2011-10-23 06:00:00', $tz),
  187. new datetime('2011-10-23 07:00:00', $tz),
  188. ),
  189. $result
  190. );
  191. }
  192. /**
  193. * @depends testValues
  194. */
  195. function testDailyByHour() {
  196. $vcal = new VCalendar();
  197. $ev = $vcal->createComponent('VEVENT');
  198. $ev->UID = 'bla';
  199. $ev->RRULE = 'FREQ=DAILY;INTERVAL=2;BYHOUR=10,11,12,13,14,15';
  200. $dtStart = $vcal->createProperty('DTSTART');
  201. $dtStart->setDateTime(new DateTime('2012-10-11 12:00:00', new DateTimeZone('UTC')));
  202. $ev->add($dtStart);
  203. $vcal->add($ev);
  204. $it = new EventIterator($vcal,(string)$ev->uid);
  205. // Grabbing the next 12 items
  206. $max = 12;
  207. $result = array();
  208. foreach($it as $item) {
  209. $result[] = $item;
  210. $max--;
  211. if (!$max) break;
  212. }
  213. $tz = new DateTimeZone('UTC');
  214. $this->assertEquals(
  215. array(
  216. new datetime('2012-10-11 12:00:00', $tz),
  217. new datetime('2012-10-11 13:00:00', $tz),
  218. new datetime('2012-10-11 14:00:00', $tz),
  219. new datetime('2012-10-11 15:00:00', $tz),
  220. new datetime('2012-10-13 10:00:00', $tz),
  221. new datetime('2012-10-13 11:00:00', $tz),
  222. new datetime('2012-10-13 12:00:00', $tz),
  223. new datetime('2012-10-13 13:00:00', $tz),
  224. new datetime('2012-10-13 14:00:00', $tz),
  225. new datetime('2012-10-13 15:00:00', $tz),
  226. new datetime('2012-10-15 10:00:00', $tz),
  227. new datetime('2012-10-15 11:00:00', $tz),
  228. ),
  229. $result
  230. );
  231. }
  232. /**
  233. * @depends testValues
  234. */
  235. function testDailyByDay() {
  236. $vcal = new VCalendar();
  237. $ev = $vcal->createComponent('VEVENT');
  238. $ev->UID = 'bla';
  239. $ev->RRULE = 'FREQ=DAILY;INTERVAL=2;BYDAY=TU,WE,FR';
  240. $dtStart = $vcal->createProperty('DTSTART');
  241. $dtStart->setDateTime(new DateTime('2011-10-07', new DateTimeZone('UTC')));
  242. $ev->add($dtStart);
  243. $vcal->add($ev);
  244. $it = new EventIterator($vcal,(string)$ev->uid);
  245. // Grabbing the next 12 items
  246. $max = 12;
  247. $result = array();
  248. foreach($it as $item) {
  249. $result[] = $item;
  250. $max--;
  251. if (!$max) break;
  252. }
  253. $tz = new DateTimeZone('UTC');
  254. $this->assertEquals(
  255. array(
  256. new DateTime('2011-10-07', $tz),
  257. new DateTime('2011-10-11', $tz),
  258. new DateTime('2011-10-19', $tz),
  259. new DateTime('2011-10-21', $tz),
  260. new DateTime('2011-10-25', $tz),
  261. new DateTime('2011-11-02', $tz),
  262. new DateTime('2011-11-04', $tz),
  263. new DateTime('2011-11-08', $tz),
  264. new DateTime('2011-11-16', $tz),
  265. new DateTime('2011-11-18', $tz),
  266. new DateTime('2011-11-22', $tz),
  267. new DateTime('2011-11-30', $tz),
  268. ),
  269. $result
  270. );
  271. }
  272. /**
  273. * @depends testValues
  274. */
  275. function testWeekly() {
  276. $vcal = new VCalendar();
  277. $ev = $vcal->createComponent('VEVENT');
  278. $ev->UID = 'bla';
  279. $ev->RRULE = 'FREQ=WEEKLY;INTERVAL=2;COUNT=10';
  280. $dtStart = $vcal->createProperty('DTSTART');
  281. $dtStart->setDateTime(new DateTime('2011-10-07', new DateTimeZone('UTC')));
  282. $ev->add($dtStart);
  283. $vcal->add($ev);
  284. $it = new EventIterator($vcal,(string)$ev->uid);
  285. // Max is to prevent overflow
  286. $max = 12;
  287. $result = array();
  288. foreach($it as $item) {
  289. $result[] = $item;
  290. $max--;
  291. if (!$max) break;
  292. }
  293. $tz = new DateTimeZone('UTC');
  294. $this->assertEquals(
  295. array(
  296. new DateTime('2011-10-07', $tz),
  297. new DateTime('2011-10-21', $tz),
  298. new DateTime('2011-11-04', $tz),
  299. new DateTime('2011-11-18', $tz),
  300. new DateTime('2011-12-02', $tz),
  301. new DateTime('2011-12-16', $tz),
  302. new DateTime('2011-12-30', $tz),
  303. new DateTime('2012-01-13', $tz),
  304. new DateTime('2012-01-27', $tz),
  305. new DateTime('2012-02-10', $tz),
  306. ),
  307. $result
  308. );
  309. }
  310. /**
  311. * @depends testValues
  312. */
  313. function testWeeklyByDayByHour() {
  314. $vcal = new VCalendar();
  315. $ev = $vcal->createComponent('VEVENT');
  316. $ev->UID = 'bla';
  317. $ev->RRULE = 'FREQ=WEEKLY;INTERVAL=2;BYDAY=TU,WE,FR;WKST=MO;BYHOUR=8,9,10';
  318. $dtStart = $vcal->createProperty('DTSTART');
  319. $dtStart->setDateTime(new DateTime('2011-10-07 08:00:00', new DateTimeZone('UTC')));
  320. $ev->add($dtStart);
  321. $vcal->add($ev);
  322. $it = new EventIterator($vcal,(string)$ev->uid);
  323. // Grabbing the next 12 items
  324. $max = 15;
  325. $result = array();
  326. foreach($it as $item) {
  327. $result[] = $item;
  328. $max--;
  329. if (!$max) break;
  330. }
  331. $tz = new DateTimeZone('UTC');
  332. $this->assertEquals(
  333. array(
  334. new DateTime('2011-10-07 08:00:00', $tz),
  335. new DateTime('2011-10-07 09:00:00', $tz),
  336. new DateTime('2011-10-07 10:00:00', $tz),
  337. new DateTime('2011-10-18 08:00:00', $tz),
  338. new DateTime('2011-10-18 09:00:00', $tz),
  339. new DateTime('2011-10-18 10:00:00', $tz),
  340. new DateTime('2011-10-19 08:00:00', $tz),
  341. new DateTime('2011-10-19 09:00:00', $tz),
  342. new DateTime('2011-10-19 10:00:00', $tz),
  343. new DateTime('2011-10-21 08:00:00', $tz),
  344. new DateTime('2011-10-21 09:00:00', $tz),
  345. new DateTime('2011-10-21 10:00:00', $tz),
  346. new DateTime('2011-11-01 08:00:00', $tz),
  347. new DateTime('2011-11-01 09:00:00', $tz),
  348. new DateTime('2011-11-01 10:00:00', $tz),
  349. ),
  350. $result
  351. );
  352. }
  353. /**
  354. * @depends testValues
  355. */
  356. function testWeeklyByDaySpecificHour() {
  357. $vcal = new VCalendar();
  358. $ev = $vcal->createComponent('VEVENT');
  359. $ev->UID = 'bla';
  360. $ev->RRULE = 'FREQ=WEEKLY;INTERVAL=2;BYDAY=TU,WE,FR;WKST=SU';
  361. $dtStart = $vcal->createProperty('DTSTART');
  362. $dtStart->setDateTime(new DateTime('2011-10-07 18:00:00', new DateTimeZone('UTC')));
  363. $ev->add($dtStart);
  364. $vcal->add($ev);
  365. $it = new EventIterator($vcal,(string)$ev->uid);
  366. // Grabbing the next 12 items
  367. $max = 12;
  368. $result = array();
  369. foreach($it as $item) {
  370. $result[] = $item;
  371. $max--;
  372. if (!$max) break;
  373. }
  374. $tz = new DateTimeZone('UTC');
  375. $this->assertEquals(
  376. array(
  377. new DateTime('2011-10-07 18:00:00', $tz),
  378. new DateTime('2011-10-18 18:00:00', $tz),
  379. new DateTime('2011-10-19 18:00:00', $tz),
  380. new DateTime('2011-10-21 18:00:00', $tz),
  381. new DateTime('2011-11-01 18:00:00', $tz),
  382. new DateTime('2011-11-02 18:00:00', $tz),
  383. new DateTime('2011-11-04 18:00:00', $tz),
  384. new DateTime('2011-11-15 18:00:00', $tz),
  385. new DateTime('2011-11-16 18:00:00', $tz),
  386. new DateTime('2011-11-18 18:00:00', $tz),
  387. new DateTime('2011-11-29 18:00:00', $tz),
  388. new DateTime('2011-11-30 18:00:00', $tz),
  389. ),
  390. $result
  391. );
  392. }
  393. /**
  394. * @depends testValues
  395. */
  396. function testWeeklyByDay() {
  397. $vcal = new VCalendar();
  398. $ev = $vcal->createComponent('VEVENT');
  399. $ev->UID = 'bla';
  400. $ev->RRULE = 'FREQ=WEEKLY;INTERVAL=2;BYDAY=TU,WE,FR;WKST=SU';
  401. $dtStart = $vcal->createProperty('DTSTART');
  402. $dtStart->setDateTime(new DateTime('2011-10-07', new DateTimeZone('UTC')));
  403. $ev->add($dtStart);
  404. $vcal->add($ev);
  405. $it = new EventIterator($vcal,(string)$ev->uid);
  406. // Grabbing the next 12 items
  407. $max = 12;
  408. $result = array();
  409. foreach($it as $item) {
  410. $result[] = $item;
  411. $max--;
  412. if (!$max) break;
  413. }
  414. $tz = new DateTimeZone('UTC');
  415. $this->assertEquals(
  416. array(
  417. new DateTime('2011-10-07', $tz),
  418. new DateTime('2011-10-18', $tz),
  419. new DateTime('2011-10-19', $tz),
  420. new DateTime('2011-10-21', $tz),
  421. new DateTime('2011-11-01', $tz),
  422. new DateTime('2011-11-02', $tz),
  423. new DateTime('2011-11-04', $tz),
  424. new DateTime('2011-11-15', $tz),
  425. new DateTime('2011-11-16', $tz),
  426. new DateTime('2011-11-18', $tz),
  427. new DateTime('2011-11-29', $tz),
  428. new DateTime('2011-11-30', $tz),
  429. ),
  430. $result
  431. );
  432. }
  433. /**
  434. * @depends testValues
  435. */
  436. function testMonthly() {
  437. $vcal = new VCalendar();
  438. $ev = $vcal->createComponent('VEVENT');
  439. $ev->UID = 'bla';
  440. $ev->RRULE = 'FREQ=MONTHLY;INTERVAL=3;COUNT=5';
  441. $dtStart = $vcal->createProperty('DTSTART');
  442. $dtStart->setDateTime(new DateTime('2011-12-05', new DateTimeZone('UTC')));
  443. $ev->add($dtStart);
  444. $vcal->add($ev);
  445. $it = new EventIterator($vcal,(string)$ev->uid);
  446. $max = 14;
  447. $result = array();
  448. foreach($it as $item) {
  449. $result[] = $item;
  450. $max--;
  451. if (!$max) break;
  452. }
  453. $tz = new DateTimeZone('UTC');
  454. $this->assertEquals(
  455. array(
  456. new DateTime('2011-12-05', $tz),
  457. new DateTime('2012-03-05', $tz),
  458. new DateTime('2012-06-05', $tz),
  459. new DateTime('2012-09-05', $tz),
  460. new DateTime('2012-12-05', $tz),
  461. ),
  462. $result
  463. );
  464. }
  465. /**
  466. * @depends testValues
  467. */
  468. function testMonthlyEndOfMonth() {
  469. $vcal = new VCalendar();
  470. $ev = $vcal->createComponent('VEVENT');
  471. $ev->UID = 'bla';
  472. $ev->RRULE = 'FREQ=MONTHLY;INTERVAL=2;COUNT=12';
  473. $dtStart = $vcal->createProperty('DTSTART');
  474. $dtStart->setDateTime(new DateTime('2011-12-31', new DateTimeZone('UTC')));
  475. $ev->add($dtStart);
  476. $vcal->add($ev);
  477. $it = new EventIterator($vcal,(string)$ev->uid);
  478. $max = 14;
  479. $result = array();
  480. foreach($it as $item) {
  481. $result[] = $item;
  482. $max--;
  483. if (!$max) break;
  484. }
  485. $tz = new DateTimeZone('UTC');
  486. $this->assertEquals(
  487. array(
  488. new DateTime('2011-12-31', $tz),
  489. new DateTime('2012-08-31', $tz),
  490. new DateTime('2012-10-31', $tz),
  491. new DateTime('2012-12-31', $tz),
  492. new DateTime('2013-08-31', $tz),
  493. new DateTime('2013-10-31', $tz),
  494. new DateTime('2013-12-31', $tz),
  495. new DateTime('2014-08-31', $tz),
  496. new DateTime('2014-10-31', $tz),
  497. new DateTime('2014-12-31', $tz),
  498. new DateTime('2015-08-31', $tz),
  499. new DateTime('2015-10-31', $tz),
  500. ),
  501. $result
  502. );
  503. }
  504. /**
  505. * @depends testValues
  506. */
  507. function testMonthlyByMonthDay() {
  508. $vcal = new VCalendar();
  509. $ev = $vcal->createComponent('VEVENT');
  510. $ev->UID = 'bla';
  511. $ev->RRULE = 'FREQ=MONTHLY;INTERVAL=5;COUNT=9;BYMONTHDAY=1,31,-7';
  512. $dtStart = $vcal->createProperty('DTSTART');
  513. $dtStart->setDateTime(new DateTime('2011-01-01', new DateTimeZone('UTC')));
  514. $ev->add($dtStart);
  515. $vcal->add($ev);
  516. $it = new EventIterator($vcal,(string)$ev->uid);
  517. $max = 14;
  518. $result = array();
  519. foreach($it as $item) {
  520. $result[] = $item;
  521. $max--;
  522. if (!$max) break;
  523. }
  524. $tz = new DateTimeZone('UTC');
  525. $this->assertEquals(
  526. array(
  527. new DateTime('2011-01-01', $tz),
  528. new DateTime('2011-01-25', $tz),
  529. new DateTime('2011-01-31', $tz),
  530. new DateTime('2011-06-01', $tz),
  531. new DateTime('2011-06-24', $tz),
  532. new DateTime('2011-11-01', $tz),
  533. new DateTime('2011-11-24', $tz),
  534. new DateTime('2012-04-01', $tz),
  535. new DateTime('2012-04-24', $tz),
  536. ),
  537. $result
  538. );
  539. }
  540. /**
  541. * A pretty slow test. Had to be marked as 'medium' for phpunit to not die
  542. * after 1 second. Would be good to optimize later.
  543. *
  544. * @depends testValues
  545. * @medium
  546. */
  547. function testMonthlyByDay() {
  548. $vcal = new VCalendar();
  549. $ev = $vcal->createComponent('VEVENT');
  550. $ev->UID = 'bla';
  551. $ev->RRULE = 'FREQ=MONTHLY;INTERVAL=2;COUNT=16;BYDAY=MO,-2TU,+1WE,3TH';
  552. $dtStart = $vcal->createProperty('DTSTART');
  553. $dtStart->setDateTime(new DateTime('2011-01-03', new DateTimeZone('UTC')));
  554. $ev->add($dtStart);
  555. $vcal->add($ev);
  556. $it = new EventIterator($vcal,(string)$ev->uid);
  557. $max = 20;
  558. $result = array();
  559. foreach($it as $k=>$item) {
  560. $result[] = $item;
  561. $max--;
  562. if (!$max) break;
  563. }
  564. $tz = new DateTimeZone('UTC');
  565. $this->assertEquals(
  566. array(
  567. new DateTime('2011-01-03', $tz),
  568. new DateTime('2011-01-05', $tz),
  569. new DateTime('2011-01-10', $tz),
  570. new DateTime('2011-01-17', $tz),
  571. new DateTime('2011-01-18', $tz),
  572. new DateTime('2011-01-20', $tz),
  573. new DateTime('2011-01-24', $tz),
  574. new DateTime('2011-01-31', $tz),
  575. new DateTime('2011-03-02', $tz),
  576. new DateTime('2011-03-07', $tz),
  577. new DateTime('2011-03-14', $tz),
  578. new DateTime('2011-03-17', $tz),
  579. new DateTime('2011-03-21', $tz),
  580. new DateTime('2011-03-22', $tz),
  581. new DateTime('2011-03-28', $tz),
  582. new DateTime('2011-05-02', $tz),
  583. ),
  584. $result
  585. );
  586. }
  587. /**
  588. * @depends testValues
  589. */
  590. function testMonthlyByDayByMonthDay() {
  591. $vcal = new VCalendar();
  592. $ev = $vcal->createComponent('VEVENT');
  593. $ev->UID = 'bla';
  594. $ev->RRULE = 'FREQ=MONTHLY;COUNT=10;BYDAY=MO;BYMONTHDAY=1';
  595. $dtStart = $vcal->createProperty('DTSTART');
  596. $dtStart->setDateTime(new DateTime('2011-08-01', new DateTimeZone('UTC')));
  597. $ev->add($dtStart);
  598. $vcal->add($ev);
  599. $it = new EventIterator($vcal,(string)$ev->uid);
  600. $max = 20;
  601. $result = array();
  602. foreach($it as $k=>$item) {
  603. $result[] = $item;
  604. $max--;
  605. if (!$max) break;
  606. }
  607. $tz = new DateTimeZone('UTC');
  608. $this->assertEquals(
  609. array(
  610. new DateTime('2011-08-01', $tz),
  611. new DateTime('2012-10-01', $tz),
  612. new DateTime('2013-04-01', $tz),
  613. new DateTime('2013-07-01', $tz),
  614. new DateTime('2014-09-01', $tz),
  615. new DateTime('2014-12-01', $tz),
  616. new DateTime('2015-06-01', $tz),
  617. new DateTime('2016-02-01', $tz),
  618. new DateTime('2016-08-01', $tz),
  619. new DateTime('2017-05-01', $tz),
  620. ),
  621. $result
  622. );
  623. }
  624. /**
  625. * @depends testValues
  626. */
  627. function testMonthlyByDayBySetPos() {
  628. $vcal = new VCalendar();
  629. $ev = $vcal->createComponent('VEVENT');
  630. $ev->UID = 'bla';
  631. $ev->RRULE = 'FREQ=MONTHLY;COUNT=10;BYDAY=MO,TU,WE,TH,FR;BYSETPOS=1,-1';
  632. $dtStart = $vcal->createProperty('DTSTART');
  633. $dtStart->setDateTime(new DateTime('2011-01-03', new DateTimeZone('UTC')));
  634. $ev->add($dtStart);
  635. $vcal->add($ev);
  636. $it = new EventIterator($vcal,(string)$ev->uid);
  637. $max = 20;
  638. $result = array();
  639. foreach($it as $k=>$item) {
  640. $result[] = $item;
  641. $max--;
  642. if (!$max) break;
  643. }
  644. $tz = new DateTimeZone('UTC');
  645. $this->assertEquals(
  646. array(
  647. new DateTime('2011-01-03', $tz),
  648. new DateTime('2011-01-31', $tz),
  649. new DateTime('2011-02-01', $tz),
  650. new DateTime('2011-02-28', $tz),
  651. new DateTime('2011-03-01', $tz),
  652. new DateTime('2011-03-31', $tz),
  653. new DateTime('2011-04-01', $tz),
  654. new DateTime('2011-04-29', $tz),
  655. new DateTime('2011-05-02', $tz),
  656. new DateTime('2011-05-31', $tz),
  657. ),
  658. $result
  659. );
  660. }
  661. /**
  662. * @depends testValues
  663. */
  664. function testYearly() {
  665. $vcal = new VCalendar();
  666. $ev = $vcal->createComponent('VEVENT');
  667. $ev->UID = 'bla';
  668. $ev->RRULE = 'FREQ=YEARLY;COUNT=10;INTERVAL=3';
  669. $dtStart = $vcal->createProperty('DTSTART');
  670. $dtStart->setDateTime(new DateTime('2011-01-01', new DateTimeZone('UTC')));
  671. $ev->add($dtStart);
  672. $vcal->add($ev);
  673. $it = new EventIterator($vcal,(string)$ev->uid);
  674. $max = 20;
  675. $result = array();
  676. foreach($it as $k=>$item) {
  677. $result[] = $item;
  678. $max--;
  679. if (!$max) break;
  680. }
  681. $tz = new DateTimeZone('UTC');
  682. $this->assertEquals(
  683. array(
  684. new DateTime('2011-01-01', $tz),
  685. new DateTime('2014-01-01', $tz),
  686. new DateTime('2017-01-01', $tz),
  687. new DateTime('2020-01-01', $tz),
  688. new DateTime('2023-01-01', $tz),
  689. new DateTime('2026-01-01', $tz),
  690. new DateTime('2029-01-01', $tz),
  691. new DateTime('2032-01-01', $tz),
  692. new DateTime('2035-01-01', $tz),
  693. new DateTime('2038-01-01', $tz),
  694. ),
  695. $result
  696. );
  697. }
  698. /**
  699. * @depends testValues
  700. */
  701. function testYearlyLeapYear() {
  702. $vcal = new VCalendar();
  703. $ev = $vcal->createComponent('VEVENT');
  704. $ev->UID = 'bla';
  705. $ev->RRULE = 'FREQ=YEARLY;COUNT=3';
  706. $dtStart = $vcal->createProperty('DTSTART');
  707. $dtStart->setDateTime(new DateTime('2012-02-29', new DateTimeZone('UTC')));
  708. $ev->add($dtStart);
  709. $vcal->add($ev);
  710. $it = new EventIterator($vcal,(string)$ev->uid);
  711. $max = 20;
  712. $result = array();
  713. foreach($it as $k=>$item) {
  714. $result[] = $item;
  715. $max--;
  716. if (!$max) break;
  717. }
  718. $tz = new DateTimeZone('UTC');
  719. $this->assertEquals(
  720. array(
  721. new DateTime('2012-02-29', $tz),
  722. new DateTime('2016-02-29', $tz),
  723. new DateTime('2020-02-29', $tz),
  724. ),
  725. $result
  726. );
  727. }
  728. /**
  729. * @depends testValues
  730. */
  731. function testYearlyByMonth() {
  732. $vcal = new VCalendar();
  733. $ev = $vcal->createComponent('VEVENT');
  734. $ev->UID = 'bla';
  735. $ev->RRULE = 'FREQ=YEARLY;COUNT=8;INTERVAL=4;BYMONTH=4,10';
  736. $dtStart = $vcal->createProperty('DTSTART');
  737. $dtStart->setDateTime(new DateTime('2011-04-07', new DateTimeZone('UTC')));
  738. $ev->add($dtStart);
  739. $vcal->add($ev);
  740. $it = new EventIterator($vcal,(string)$ev->uid);
  741. $max = 20;
  742. $result = array();
  743. foreach($it as $k=>$item) {
  744. $result[] = $item;
  745. $max--;
  746. if (!$max) break;
  747. }
  748. $tz = new DateTimeZone('UTC');
  749. $this->assertEquals(
  750. array(
  751. new DateTime('2011-04-07', $tz),
  752. new DateTime('2011-10-07', $tz),
  753. new DateTime('2015-04-07', $tz),
  754. new DateTime('2015-10-07', $tz),
  755. new DateTime('2019-04-07', $tz),
  756. new DateTime('2019-10-07', $tz),
  757. new DateTime('2023-04-07', $tz),
  758. new DateTime('2023-10-07', $tz),
  759. ),
  760. $result
  761. );
  762. }
  763. /**
  764. * @depends testValues
  765. */
  766. function testYearlyByMonthByDay() {
  767. $vcal = new VCalendar();
  768. $ev = $vcal->createComponent('VEVENT');
  769. $ev->UID = 'bla';
  770. $ev->RRULE = 'FREQ=YEARLY;COUNT=8;INTERVAL=5;BYMONTH=4,10;BYDAY=1MO,-1SU';
  771. $dtStart = $vcal->createProperty('DTSTART');
  772. $dtStart->setDateTime(new DateTime('2011-04-04', new DateTimeZone('UTC')));
  773. $ev->add($dtStart);
  774. $vcal->add($ev);
  775. $it = new EventIterator($vcal,(string)$ev->uid);
  776. $max = 20;
  777. $result = array();
  778. foreach($it as $k=>$item) {
  779. $result[] = $item;
  780. $max--;
  781. if (!$max) break;
  782. }
  783. $tz = new DateTimeZone('UTC');
  784. $this->assertEquals(
  785. array(
  786. new DateTime('2011-04-04', $tz),
  787. new DateTime('2011-04-24', $tz),
  788. new DateTime('2011-10-03', $tz),
  789. new DateTime('2011-10-30', $tz),
  790. new DateTime('2016-04-04', $tz),
  791. new DateTime('2016-04-24', $tz),
  792. new DateTime('2016-10-03', $tz),
  793. new DateTime('2016-10-30', $tz),
  794. ),
  795. $result
  796. );
  797. }
  798. /**
  799. * @depends testValues
  800. */
  801. function testFastForward() {
  802. $vcal = new VCalendar();
  803. $ev = $vcal->createComponent('VEVENT');
  804. $ev->UID = 'bla';
  805. $ev->RRULE = 'FREQ=YEARLY;COUNT=8;INTERVAL=5;BYMONTH=4,10;BYDAY=1MO,-1SU';
  806. $dtStart = $vcal->createProperty('DTSTART');
  807. $dtStart->setDateTime(new DateTime('2011-04-04', new DateTimeZone('UTC')));
  808. $ev->add($dtStart);
  809. $vcal->add($ev);
  810. $it = new EventIterator($vcal,(string)$ev->uid);
  811. // The idea is that we're fast-forwarding too far in the future, so
  812. // there will be no results left.
  813. $it->fastForward(new DateTime('2020-05-05', new DateTimeZone('UTC')));
  814. $max = 20;
  815. $result = array();
  816. while($item = $it->current()) {
  817. $result[] = $item;
  818. $max--;
  819. if (!$max) break;
  820. $it->next();
  821. }
  822. $tz = new DateTimeZone('UTC');
  823. $this->assertEquals(array(), $result);
  824. }
  825. /**
  826. * @depends testValues
  827. */
  828. function testComplexExclusions() {
  829. $vcal = new VCalendar();
  830. $ev = $vcal->createComponent('VEVENT');
  831. $ev->UID = 'bla';
  832. $ev->RRULE = 'FREQ=YEARLY;COUNT=10';
  833. $dtStart = $vcal->createProperty('DTSTART');
  834. $tz = new DateTimeZone('Canada/Eastern');
  835. $dtStart->setDateTime(new DateTime('2011-01-01 13:50:20', $tz));
  836. $exDate1 = $vcal->createProperty('EXDATE');
  837. $exDate1->setDateTimes(array(new DateTime('2012-01-01 13:50:20', $tz), new DateTime('2014-01-01 13:50:20', $tz)));
  838. $exDate2 = $vcal->createProperty('EXDATE');
  839. $exDate2->setDateTimes(array(new DateTime('2016-01-01 13:50:20', $tz)));
  840. $ev->add($dtStart);
  841. $ev->add($exDate1);
  842. $ev->add($exDate2);
  843. $vcal->add($ev);
  844. $it = new EventIterator($vcal,(string)$ev->uid);
  845. $max = 20;
  846. $result = array();
  847. foreach($it as $k=>$item) {
  848. $result[] = $item;
  849. $max--;
  850. if (!$max) break;
  851. }
  852. $this->assertEquals(
  853. array(
  854. new DateTime('2011-01-01 13:50:20', $tz),
  855. new DateTime('2013-01-01 13:50:20', $tz),
  856. new DateTime('2015-01-01 13:50:20', $tz),
  857. new DateTime('2017-01-01 13:50:20', $tz),
  858. new DateTime('2018-01-01 13:50:20', $tz),
  859. new DateTime('2019-01-01 13:50:20', $tz),
  860. new DateTime('2020-01-01 13:50:20', $tz),
  861. ),
  862. $result
  863. );
  864. }
  865. /**
  866. * @depends testValues
  867. */
  868. function testOverridenEvent() {
  869. $vcal = new VCalendar();
  870. $ev1 = $vcal->createComponent('VEVENT');
  871. $ev1->UID = 'overridden';
  872. $ev1->RRULE = 'FREQ=DAILY;COUNT=10';
  873. $ev1->DTSTART = '20120107T120000Z';
  874. $ev1->SUMMARY = 'baseEvent';
  875. $vcal->add($ev1);
  876. // ev2 overrides an event, and puts it on 2pm instead.
  877. $ev2 = $vcal->createComponent('VEVENT');
  878. $ev2->UID = 'overridden';
  879. $ev2->{'RECURRENCE-ID'} = '20120110T120000Z';
  880. $ev2->DTSTART = '20120110T140000Z';
  881. $ev2->SUMMARY = 'Event 2';
  882. $vcal->add($ev2);
  883. // ev3 overrides an event, and puts it 2 days and 2 hours later
  884. $ev3 = $vcal->createComponent('VEVENT');
  885. $ev3->UID = 'overridden';
  886. $ev3->{'RECURRENCE-ID'} = '20120113T120000Z';
  887. $ev3->DTSTART = '20120115T140000Z';
  888. $ev3->SUMMARY = 'Event 3';
  889. $vcal->add($ev3);
  890. $it = new EventIterator($vcal,'overridden');
  891. $dates = array();
  892. $summaries = array();
  893. while($it->valid()) {
  894. $dates[] = $it->getDTStart();
  895. $summaries[] = (string)$it->getEventObject()->SUMMARY;
  896. $it->next();
  897. }
  898. $tz = new DateTimeZone('UTC');
  899. $this->assertEquals(array(
  900. new DateTime('2012-01-07 12:00:00',$tz),
  901. new DateTime('2012-01-08 12:00:00',$tz),
  902. new DateTime('2012-01-09 12:00:00',$tz),
  903. new DateTime('2012-01-10 14:00:00',$tz),
  904. new DateTime('2012-01-11 12:00:00',$tz),
  905. new DateTime('2012-01-12 12:00:00',$tz),
  906. new DateTime('2012-01-14 12:00:00',$tz),
  907. new DateTime('2012-01-15 12:00:00',$tz),
  908. new DateTime('2012-01-15 14:00:00',$tz),
  909. new DateTime('2012-01-16 12:00:00',$tz),
  910. ), $dates);
  911. $this->assertEquals(array(
  912. 'baseEvent',
  913. 'baseEvent',
  914. 'baseEvent',
  915. 'Event 2',
  916. 'baseEvent',
  917. 'baseEvent',
  918. 'baseEvent',
  919. 'baseEvent',
  920. 'Event 3',
  921. 'baseEvent',
  922. ), $summaries);
  923. }
  924. /**
  925. * @depends testValues
  926. */
  927. function testOverridenEvent2() {
  928. $vcal = new VCalendar();
  929. $ev1 = $vcal->createComponent('VEVENT');
  930. $ev1->UID = 'overridden';
  931. $ev1->RRULE = 'FREQ=WEEKLY;COUNT=3';
  932. $ev1->DTSTART = '20120112T120000Z';
  933. $ev1->SUMMARY = 'baseEvent';
  934. $vcal->add($ev1);
  935. // ev2 overrides an event, and puts it 6 days earlier instead.
  936. $ev2 = $vcal->createComponent('VEVENT');
  937. $ev2->UID = 'overridden';
  938. $ev2->{'RECURRENCE-ID'} = '20120119T120000Z';
  939. $ev2->DTSTART = '20120113T120000Z';
  940. $ev2->SUMMARY = 'Override!';
  941. $vcal->add($ev2);
  942. $it = new EventIterator($vcal,'overridden');
  943. $dates = array();
  944. $summaries = array();
  945. while($it->valid()) {
  946. $dates[] = $it->getDTStart();
  947. $summaries[] = (string)$it->getEventObject()->SUMMARY;
  948. $it->next();
  949. }
  950. $tz = new DateTimeZone('UTC');
  951. $this->assertEquals(array(
  952. new DateTime('2012-01-12 12:00:00',$tz),
  953. new DateTime('2012-01-13 12:00:00',$tz),
  954. new DateTime('2012-01-26 12:00:00',$tz),
  955. ), $dates);
  956. $this->assertEquals(array(
  957. 'baseEvent',
  958. 'Override!',
  959. 'baseEvent',
  960. ), $summaries);
  961. }
  962. /**
  963. * @depends testValues
  964. */
  965. function testOverridenEventNoValuesExpected() {
  966. $vcal = new VCalendar();
  967. $ev1 = $vcal->createComponent('VEVENT');
  968. $ev1->UID = 'overridden';
  969. $ev1->RRULE = 'FREQ=WEEKLY;COUNT=3';
  970. $ev1->DTSTART = '20120124T120000Z';
  971. $ev1->SUMMARY = 'baseEvent';
  972. $vcal->add($ev1);
  973. // ev2 overrides an event, and puts it 6 days earlier instead.
  974. $ev2 = $vcal->createComponent('VEVENT');
  975. $ev2->UID = 'overridden';
  976. $ev2->{'RECURRENCE-ID'} = '20120131T120000Z';
  977. $ev2->DTSTART = '20120125T120000Z';
  978. $ev2->SUMMARY = 'Override!';
  979. $vcal->add($ev2);
  980. $it = new EventIterator($vcal,'overridden');
  981. $dates = array();
  982. $summaries = array();
  983. // The reported problem was specifically related to the VCALENDAR
  984. // expansion. In this parcitular case, we had to forward to the 28th of
  985. // january.
  986. $it->fastForward(new DateTime('2012-01-28 23:00:00'));
  987. // We stop the loop when it hits the 6th of februari. Normally this
  988. // iterator would hit 24, 25 (overriden from 31) and 7 feb but because
  989. // we 'filter' from the 28th till the 6th, we should get 0 results.
  990. while($it->valid() && $it->getDTSTart() < new DateTime('2012-02-06 23:00:00')) {
  991. $dates[] = $it->getDTStart();
  992. $summaries[] = (string)$it->getEventObject()->SUMMARY;
  993. $it->next();
  994. }
  995. $this->assertEquals(array(), $dates);
  996. $this->assertEquals(array(), $summaries);
  997. }
  998. /**
  999. * @depends testValues
  1000. */
  1001. function testRDATE() {
  1002. $vcal = new VCalendar();
  1003. $ev = $vcal->createComponent('VEVENT');
  1004. $ev->UID = 'bla';
  1005. $ev->RDATE = array(
  1006. new DateTime('2014-08-07', new DateTimeZone('UTC')),
  1007. new DateTime('2014-08-08', new DateTimeZone('UTC')),
  1008. );
  1009. $dtStart = $vcal->createProperty('DTSTART');
  1010. $dtStart->setDateTime(new DateTime('2011-10-07', new DateTimeZone('UTC')));
  1011. $ev->add($dtStart);
  1012. $vcal->add($ev);
  1013. $it = new EventIterator($vcal,$ev->uid);
  1014. // Max is to prevent overflow
  1015. $max = 12;
  1016. $result = array();
  1017. foreach($it as $item) {
  1018. $result[] = $item;
  1019. $max--;
  1020. if (!$max) break;
  1021. }
  1022. $tz = new DateTimeZone('UTC');
  1023. $this->assertEquals(
  1024. array(
  1025. new DateTime('2011-10-07', $tz),
  1026. new DateTime('2014-08-07', $tz),
  1027. new DateTime('2014-08-08', $tz),
  1028. ),
  1029. $result
  1030. );
  1031. }
  1032. /**
  1033. * @depends testValues
  1034. * @expectedException \InvalidArgumentException
  1035. */
  1036. function testNoMasterBadUID() {
  1037. $vcal = new VCalendar();
  1038. // ev2 overrides an event, and puts it on 2pm instead.
  1039. $ev2 = $vcal->createComponent('VEVENT');
  1040. $ev2->UID = 'overridden';
  1041. $ev2->{'RECURRENCE-ID'} = '20120110T120000Z';
  1042. $ev2->DTSTART = '20120110T140000Z';
  1043. $ev2->SUMMARY = 'Event 2';
  1044. $vcal->add($ev2);
  1045. // ev3 overrides an event, and puts it 2 days and 2 hours later
  1046. $ev3 = $vcal->createComponent('VEVENT');
  1047. $ev3->UID = 'overridden';
  1048. $ev3->{'RECURRENCE-ID'} = '20120113T120000Z';
  1049. $ev3->DTSTART = '20120115T140000Z';
  1050. $ev3->SUMMARY = 'Event 3';
  1051. $vcal->add($ev3);
  1052. $it = new EventIterator($vcal,'broken');
  1053. }
  1054. }