1<?php
2
3namespace Sabre\VObject\Component;
4
5use Sabre\VObject\Component;
6use Sabre\VObject\Reader;
7
8class VTodoTest extends \PHPUnit_Framework_TestCase {
9
10    /**
11     * @dataProvider timeRangeTestData
12     */
13    function testInTimeRange(VTodo $vtodo, $start, $end, $outcome) {
14
15        $this->assertEquals($outcome, $vtodo->isInTimeRange($start, $end));
16
17    }
18
19    function timeRangeTestData() {
20
21        $tests = [];
22
23        $calendar = new VCalendar();
24
25        $vtodo = $calendar->createComponent('VTODO');
26        $vtodo->DTSTART = '20111223T120000Z';
27        $tests[] = [$vtodo, new \DateTime('2011-01-01'), new \DateTime('2012-01-01'), true];
28        $tests[] = [$vtodo, new \DateTime('2011-01-01'), new \DateTime('2011-11-01'), false];
29
30        $vtodo2 = clone $vtodo;
31        $vtodo2->DURATION = 'P1D';
32        $tests[] = [$vtodo2, new \DateTime('2011-01-01'), new \DateTime('2012-01-01'), true];
33        $tests[] = [$vtodo2, new \DateTime('2011-01-01'), new \DateTime('2011-11-01'), false];
34
35        $vtodo3 = clone $vtodo;
36        $vtodo3->DUE = '20111225';
37        $tests[] = [$vtodo3, new \DateTime('2011-01-01'), new \DateTime('2012-01-01'), true];
38        $tests[] = [$vtodo3, new \DateTime('2011-01-01'), new \DateTime('2011-11-01'), false];
39
40        $vtodo4 = $calendar->createComponent('VTODO');
41        $vtodo4->DUE = '20111225';
42        $tests[] = [$vtodo4, new \DateTime('2011-01-01'), new \DateTime('2012-01-01'), true];
43        $tests[] = [$vtodo4, new \DateTime('2011-01-01'), new \DateTime('2011-11-01'), false];
44
45        $vtodo5 = $calendar->createComponent('VTODO');
46        $vtodo5->COMPLETED = '20111225';
47        $tests[] = [$vtodo5, new \DateTime('2011-01-01'), new \DateTime('2012-01-01'), true];
48        $tests[] = [$vtodo5, new \DateTime('2011-01-01'), new \DateTime('2011-11-01'), false];
49
50        $vtodo6 = $calendar->createComponent('VTODO');
51        $vtodo6->CREATED = '20111225';
52        $tests[] = [$vtodo6, new \DateTime('2011-01-01'), new \DateTime('2012-01-01'), true];
53        $tests[] = [$vtodo6, new \DateTime('2011-01-01'), new \DateTime('2011-11-01'), false];
54
55        $vtodo7 = $calendar->createComponent('VTODO');
56        $vtodo7->CREATED = '20111225';
57        $vtodo7->COMPLETED = '20111226';
58        $tests[] = [$vtodo7, new \DateTime('2011-01-01'), new \DateTime('2012-01-01'), true];
59        $tests[] = [$vtodo7, new \DateTime('2011-01-01'), new \DateTime('2011-11-01'), false];
60
61        $vtodo7 = $calendar->createComponent('VTODO');
62        $tests[] = [$vtodo7, new \DateTime('2011-01-01'), new \DateTime('2012-01-01'), true];
63        $tests[] = [$vtodo7, new \DateTime('2011-01-01'), new \DateTime('2011-11-01'), true];
64
65        return $tests;
66
67    }
68
69    function testValidate() {
70
71        $input = <<<HI
72BEGIN:VCALENDAR
73VERSION:2.0
74PRODID:YoYo
75BEGIN:VTODO
76UID:1234-21355-123156
77DTSTAMP:20140402T183400Z
78END:VTODO
79END:VCALENDAR
80HI;
81
82        $obj = Reader::read($input);
83
84        $warnings = $obj->validate();
85        $messages = [];
86        foreach ($warnings as $warning) {
87            $messages[] = $warning['message'];
88        }
89
90        $this->assertEquals([], $messages);
91
92    }
93
94    function testValidateInvalid() {
95
96        $input = <<<HI
97BEGIN:VCALENDAR
98VERSION:2.0
99PRODID:YoYo
100BEGIN:VTODO
101END:VTODO
102END:VCALENDAR
103HI;
104
105        $obj = Reader::read($input);
106
107        $warnings = $obj->validate();
108        $messages = [];
109        foreach ($warnings as $warning) {
110            $messages[] = $warning['message'];
111        }
112
113        $this->assertEquals([
114            "UID MUST appear exactly once in a VTODO component",
115            "DTSTAMP MUST appear exactly once in a VTODO component",
116        ], $messages);
117
118    }
119
120    function testValidateDUEDTSTARTMisMatch() {
121
122        $input = <<<HI
123BEGIN:VCALENDAR
124VERSION:2.0
125PRODID:YoYo
126BEGIN:VTODO
127UID:FOO
128DTSTART;VALUE=DATE-TIME:20140520T131600Z
129DUE;VALUE=DATE:20140520
130DTSTAMP;VALUE=DATE-TIME:20140520T131600Z
131END:VTODO
132END:VCALENDAR
133HI;
134
135        $obj = Reader::read($input);
136
137        $warnings = $obj->validate();
138        $messages = [];
139        foreach ($warnings as $warning) {
140            $messages[] = $warning['message'];
141        }
142
143        $this->assertEquals([
144            "The value type (DATE or DATE-TIME) must be identical for DUE and DTSTART",
145        ], $messages);
146
147    }
148
149    function testValidateDUEbeforeDTSTART() {
150
151        $input = <<<HI
152BEGIN:VCALENDAR
153VERSION:2.0
154PRODID:YoYo
155BEGIN:VTODO
156UID:FOO
157DTSTART;VALUE=DATE:20140520
158DUE;VALUE=DATE:20140518
159DTSTAMP;VALUE=DATE-TIME:20140520T131600Z
160END:VTODO
161END:VCALENDAR
162HI;
163
164        $obj = Reader::read($input);
165
166        $warnings = $obj->validate();
167        $messages = [];
168        foreach ($warnings as $warning) {
169            $messages[] = $warning['message'];
170        }
171
172        $this->assertEquals([
173            "DUE must occur after DTSTART",
174        ], $messages);
175
176    }
177
178}
179