VAlarmTest.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. namespace Sabre\VObject\Component;
  3. use Sabre\VObject\Component;
  4. use DateTime;
  5. use Sabre\VObject\Reader;
  6. class VAlarmTest extends \PHPUnit_Framework_TestCase {
  7. /**
  8. * @dataProvider timeRangeTestData
  9. */
  10. public function testInTimeRange(VAlarm $valarm,$start,$end,$outcome) {
  11. $this->assertEquals($outcome, $valarm->isInTimeRange($start, $end));
  12. }
  13. public function timeRangeTestData() {
  14. $tests = array();
  15. $calendar = new VCalendar();
  16. // Hard date and time
  17. $valarm1 = $calendar->createComponent('VALARM');
  18. $valarm1->add(
  19. $calendar->createProperty('TRIGGER', '20120312T130000Z', array('VALUE' => 'DATE-TIME'))
  20. );
  21. $tests[] = array($valarm1, new DateTime('2012-03-01 01:00:00'), new DateTime('2012-04-01 01:00:00'), true);
  22. $tests[] = array($valarm1, new DateTime('2012-03-01 01:00:00'), new DateTime('2012-03-10 01:00:00'), false);
  23. // Relation to start time of event
  24. $valarm2 = $calendar->createComponent('VALARM');
  25. $valarm2->add(
  26. $calendar->createProperty('TRIGGER', '-P1D', array('VALUE' => 'DURATION'))
  27. );
  28. $vevent2 = $calendar->createComponent('VEVENT');
  29. $vevent2->DTSTART = '20120313T130000Z';
  30. $vevent2->add($valarm2);
  31. $tests[] = array($valarm2, new DateTime('2012-03-01 01:00:00'), new DateTime('2012-04-01 01:00:00'), true);
  32. $tests[] = array($valarm2, new DateTime('2012-03-01 01:00:00'), new DateTime('2012-03-10 01:00:00'), false);
  33. // Relation to end time of event
  34. $valarm3 = $calendar->createComponent('VALARM');
  35. $valarm3->add( $calendar->createProperty('TRIGGER', '-P1D', array('VALUE'=>'DURATION', 'RELATED' => 'END')) );
  36. $vevent3 = $calendar->createComponent('VEVENT');
  37. $vevent3->DTSTART = '20120301T130000Z';
  38. $vevent3->DTEND = '20120401T130000Z';
  39. $vevent3->add($valarm3);
  40. $tests[] = array($valarm3, new DateTime('2012-02-25 01:00:00'), new DateTime('2012-03-05 01:00:00'), false);
  41. $tests[] = array($valarm3, new DateTime('2012-03-25 01:00:00'), new DateTime('2012-04-05 01:00:00'), true);
  42. // Relation to end time of todo
  43. $valarm4 = $calendar->createComponent('VALARM');
  44. $valarm4->TRIGGER = '-P1D';
  45. $valarm4->TRIGGER['VALUE'] = 'DURATION';
  46. $valarm4->TRIGGER['RELATED']= 'END';
  47. $vtodo4 = $calendar->createComponent('VTODO');
  48. $vtodo4->DTSTART = '20120301T130000Z';
  49. $vtodo4->DUE = '20120401T130000Z';
  50. $vtodo4->add($valarm4);
  51. $tests[] = array($valarm4, new DateTime('2012-02-25 01:00:00'), new DateTime('2012-03-05 01:00:00'), false);
  52. $tests[] = array($valarm4, new DateTime('2012-03-25 01:00:00'), new DateTime('2012-04-05 01:00:00'), true);
  53. // Relation to start time of event + repeat
  54. $valarm5 = $calendar->createComponent('VALARM');
  55. $valarm5->TRIGGER = '-P1D';
  56. $valarm5->TRIGGER['VALUE'] = 'DURATION';
  57. $valarm5->REPEAT = 10;
  58. $valarm5->DURATION = 'P1D';
  59. $vevent5 = $calendar->createComponent('VEVENT');
  60. $vevent5->DTSTART = '20120301T130000Z';
  61. $vevent5->add($valarm5);
  62. $tests[] = array($valarm5, new DateTime('2012-03-09 01:00:00'), new DateTime('2012-03-10 01:00:00'), true);
  63. // Relation to start time of event + duration, but no repeat
  64. $valarm6 = $calendar->createComponent('VALARM');
  65. $valarm6->TRIGGER = '-P1D';
  66. $valarm6->TRIGGER['VALUE'] = 'DURATION';
  67. $valarm6->DURATION = 'P1D';
  68. $vevent6 = $calendar->createComponent('VEVENT');
  69. $vevent6->DTSTART = '20120313T130000Z';
  70. $vevent6->add($valarm6);
  71. $tests[] = array($valarm6, new DateTime('2012-03-01 01:00:00'), new DateTime('2012-04-01 01:00:00'), true);
  72. $tests[] = array($valarm6, new DateTime('2012-03-01 01:00:00'), new DateTime('2012-03-10 01:00:00'), false);
  73. // Relation to end time of event (DURATION instead of DTEND)
  74. $valarm7 = $calendar->createComponent('VALARM');
  75. $valarm7->TRIGGER = '-P1D';
  76. $valarm7->TRIGGER['VALUE'] = 'DURATION';
  77. $valarm7->TRIGGER['RELATED']= 'END';
  78. $vevent7 = $calendar->createComponent('VEVENT');
  79. $vevent7->DTSTART = '20120301T130000Z';
  80. $vevent7->DURATION = 'P30D';
  81. $vevent7->add($valarm7);
  82. $tests[] = array($valarm7, new DateTime('2012-02-25 01:00:00'), new DateTime('2012-03-05 01:00:00'), false);
  83. $tests[] = array($valarm7, new DateTime('2012-03-25 01:00:00'), new DateTime('2012-04-05 01:00:00'), true);
  84. // Relation to end time of event (No DTEND or DURATION)
  85. $valarm7 = $calendar->createComponent('VALARM');
  86. $valarm7->TRIGGER = '-P1D';
  87. $valarm7->TRIGGER['VALUE'] = 'DURATION';
  88. $valarm7->TRIGGER['RELATED']= 'END';
  89. $vevent7 = $calendar->createComponent('VEVENT');
  90. $vevent7->DTSTART = '20120301T130000Z';
  91. $vevent7->add($valarm7);
  92. $tests[] = array($valarm7, new DateTime('2012-02-25 01:00:00'), new DateTime('2012-03-05 01:00:00'), true);
  93. $tests[] = array($valarm7, new DateTime('2012-03-25 01:00:00'), new DateTime('2012-04-05 01:00:00'), false);
  94. return $tests;
  95. }
  96. /**
  97. * @expectedException LogicException
  98. */
  99. public function testInTimeRangeInvalidComponent() {
  100. $calendar = new VCalendar();
  101. $valarm = $calendar->createComponent('VALARM');
  102. $valarm->TRIGGER = '-P1D';
  103. $valarm->TRIGGER['RELATED'] = 'END';
  104. $vjournal = $calendar->createComponent('VJOURNAL');
  105. $vjournal->add($valarm);
  106. $valarm->isInTimeRange(new DateTime('2012-02-25 01:00:00'), new DateTime('2012-03-05 01:00:00'));
  107. }
  108. /**
  109. * This bug was found and reported on the mailing list.
  110. */
  111. public function testInTimeRangeBuggy() {
  112. $input = <<<BLA
  113. BEGIN:VCALENDAR
  114. BEGIN:VTODO
  115. DTSTAMP:20121003T064931Z
  116. UID:b848cb9a7bb16e464a06c222ca1f8102@examle.com
  117. STATUS:NEEDS-ACTION
  118. DUE:20121005T000000Z
  119. SUMMARY:Task 1
  120. CATEGORIES:AlarmCategory
  121. BEGIN:VALARM
  122. TRIGGER:-PT10M
  123. ACTION:DISPLAY
  124. DESCRIPTION:Task 1
  125. END:VALARM
  126. END:VTODO
  127. END:VCALENDAR
  128. BLA;
  129. $vobj = Reader::read($input);
  130. $this->assertTrue($vobj->VTODO->VALARM->isInTimeRange(new \DateTime('2012-10-01 00:00:00'), new \DateTime('2012-11-01 00:00:00')));
  131. }
  132. }