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