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