ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
DateAndOrTimeTest.php
Go to the documentation of this file.
1 <?php
2 
4 
6 use Sabre\VObject;
8 
9 class DateAndOrTimeTest extends TestCase {
10 
15 
16  $vcard = new VObject\Component\VCard();
17  $prop = $vcard->createProperty('BDAY', $input);
18 
19  $this->assertEquals([$output], $prop->getJsonValue());
20 
21  }
22 
23  function dates() {
24 
25  return [
26  [
27  "19961022T140000",
28  "1996-10-22T14:00:00",
29  ],
30  [
31  "--1022T1400",
32  "--10-22T14:00",
33  ],
34  [
35  "---22T14",
36  "---22T14",
37  ],
38  [
39  "19850412",
40  "1985-04-12",
41  ],
42  [
43  "1985-04",
44  "1985-04",
45  ],
46  [
47  "1985",
48  "1985",
49  ],
50  [
51  "--0412",
52  "--04-12",
53  ],
54  [
55  "T102200",
56  "T10:22:00",
57  ],
58  [
59  "T1022",
60  "T10:22",
61  ],
62  [
63  "T10",
64  "T10",
65  ],
66  [
67  "T-2200",
68  "T-22:00",
69  ],
70  [
71  "T102200Z",
72  "T10:22:00Z",
73  ],
74  [
75  "T102200-0800",
76  "T10:22:00-0800",
77  ],
78  [
79  "T--00",
80  "T--00",
81  ],
82  ];
83 
84  }
85 
86  function testSetParts() {
87 
88  $vcard = new VObject\Component\VCard();
89 
90  $prop = $vcard->createProperty('BDAY');
91  $prop->setParts([
92  new \DateTime('2014-04-02 18:37:00')
93  ]);
94 
95  $this->assertEquals('20140402T183700Z', $prop->getValue());
96 
97  }
98 
100 
101  $vcard = new VObject\Component\VCard();
102 
103  $prop = $vcard->createProperty('BDAY');
104  $prop->setParts([
105  new \DateTimeImmutable('2014-04-02 18:37:00')
106  ]);
107 
108  $this->assertEquals('20140402T183700Z', $prop->getValue());
109 
110  }
111 
115  function testSetPartsTooMany() {
116 
117  $vcard = new VObject\Component\VCard();
118 
119  $prop = $vcard->createProperty('BDAY');
120  $prop->setParts([
121  1,
122  2
123  ]);
124 
125  }
126 
127  function testSetPartsString() {
128 
129  $vcard = new VObject\Component\VCard();
130 
131  $prop = $vcard->createProperty('BDAY');
132  $prop->setParts([
133  "20140402T183700Z"
134  ]);
135 
136  $this->assertEquals('20140402T183700Z', $prop->getValue());
137 
138  }
139 
140  function testSetValueDateTime() {
141 
142  $vcard = new VObject\Component\VCard();
143 
144  $prop = $vcard->createProperty('BDAY');
145  $prop->setValue(
146  new \DateTime('2014-04-02 18:37:00')
147  );
148 
149  $this->assertEquals('20140402T183700Z', $prop->getValue());
150 
151  }
152 
154 
155  $vcard = new VObject\Component\VCard();
156 
157  $prop = $vcard->createProperty('BDAY');
158  $prop->setValue(
159  new \DateTimeImmutable('2014-04-02 18:37:00')
160  );
161 
162  $this->assertEquals('20140402T183700Z', $prop->getValue());
163 
164  }
165 
167 
168  $vcard = new VObject\Component\VCard();
169 
170  $prop = $vcard->createProperty('BDAY');
171  $prop->setValue(
172  new \DateTime('2014-04-02 18:37:00', new \DateTimeZone('America/Toronto'))
173  );
174 
175  $this->assertEquals('20140402T183700-0400', $prop->getValue());
176 
177  }
178 
179  function testGetDateTime() {
180 
181  $datetime = new \DateTime('2014-04-02 18:37:00', new \DateTimeZone('America/Toronto'));
182 
183  $vcard = new VObject\Component\VCard();
184  $prop = $vcard->createProperty('BDAY', $datetime);
185 
186  $dt = $prop->getDateTime();
187  $this->assertEquals('2014-04-02T18:37:00-04:00', $dt->format('c'), "For some reason this one failed. Current default timezone is: " . date_default_timezone_get());
188 
189  }
190 
191  function testGetDate() {
192 
193  $datetime = new \DateTime('2014-04-02');
194 
195  $vcard = new VObject\Component\VCard();
196  $prop = $vcard->createProperty('BDAY', $datetime, null, 'DATE');
197 
198  $this->assertEquals('DATE', $prop->getValueType());
199  $this->assertEquals('BDAY:20140402', rtrim($prop->serialize()));
200 
201  }
202 
204 
205  $datetime = '--0407';
206 
207  $vcard = new VObject\Component\VCard();
208  $prop = $vcard->add('BDAY', $datetime);
209 
210  $dt = $prop->getDateTime();
211  // Note: if the year changes between the last line and the next line of
212  // code, this test may fail.
213  //
214  // If that happens, head outside and have a drink.
215  $current = new \DateTime('now');
216  $year = $current->format('Y');
217 
218  $this->assertEquals($year . '0407', $dt->format('Ymd'));
219 
220  }
221 
223 
224  $vcard = <<<VCF
225 BEGIN:VCARD
226 VERSION:4.0
227 BDAY:--0407
228 END:VCARD
229 VCF;
230  $vcard = Reader::read($vcard);
231  $prop = $vcard->BDAY;
232 
233  $dt = $prop->getDateTime();
234  // Note: if the year changes between the last line and the next line of
235  // code, this test may fail.
236  //
237  // If that happens, head outside and have a drink.
238  $current = new \DateTime('now');
239  $year = $current->format('Y');
240 
241  $this->assertEquals($year . '0407', $dt->format('Ymd'));
242 
243  }
244 
245  function testValidate() {
246 
247  $datetime = '--0407';
248 
249  $vcard = new VObject\Component\VCard();
250  $prop = $vcard->add('BDAY', $datetime);
251 
252  $this->assertEquals([], $prop->validate());
253 
254  }
255 
256  function testValidateBroken() {
257 
258  $datetime = '123';
259 
260  $vcard = new VObject\Component\VCard();
261  $prop = $vcard->add('BDAY', $datetime);
262 
263  $this->assertEquals([[
264  'level' => 3,
265  'message' => 'The supplied value (123) is not a correct DATE-AND-OR-TIME property',
266  'node' => $prop,
267  ]], $prop->validate());
268 
269  }
270 }
if($argc< 3) $input
static read($data, $options=0, $charset='UTF-8')
Parses a vCard or iCalendar object, and returns the top component.
Definition: Reader.php:42
The VCard component.
Definition: VCard.php:18