ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
ComponentTest.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Sabre\VObject;
4 
8 
9 class ComponentTest extends TestCase {
10 
11  function testIterate() {
12 
13  $comp = new VCalendar([], false);
14 
15  $sub = $comp->createComponent('VEVENT');
16  $comp->add($sub);
17 
18  $sub = $comp->createComponent('VTODO');
19  $comp->add($sub);
20 
21  $count = 0;
22  foreach ($comp->children() as $key => $subcomponent) {
23 
24  $count++;
25  $this->assertInstanceOf('Sabre\\VObject\\Component', $subcomponent);
26 
27  }
28  $this->assertEquals(2, $count);
29  $this->assertEquals(1, $key);
30 
31  }
32 
33  function testMagicGet() {
34 
35  $comp = new VCalendar([], false);
36 
37  $sub = $comp->createComponent('VEVENT');
38  $comp->add($sub);
39 
40  $sub = $comp->createComponent('VTODO');
41  $comp->add($sub);
42 
43  $event = $comp->vevent;
44  $this->assertInstanceOf('Sabre\\VObject\\Component', $event);
45  $this->assertEquals('VEVENT', $event->name);
46 
47  $this->assertInternalType('null', $comp->vjournal);
48 
49  }
50 
51  function testMagicGetGroups() {
52 
53  $comp = new VCard();
54 
55  $sub = $comp->createProperty('GROUP1.EMAIL', '1@1.com');
56  $comp->add($sub);
57 
58  $sub = $comp->createProperty('GROUP2.EMAIL', '2@2.com');
59  $comp->add($sub);
60 
61  $sub = $comp->createProperty('EMAIL', '3@3.com');
62  $comp->add($sub);
63 
64  $emails = $comp->email;
65  $this->assertEquals(3, count($emails));
66 
67  $email1 = $comp->{"group1.email"};
68  $this->assertEquals('EMAIL', $email1[0]->name);
69  $this->assertEquals('GROUP1', $email1[0]->group);
70 
71  $email3 = $comp->{".email"};
72  $this->assertEquals('EMAIL', $email3[0]->name);
73  $this->assertEquals(null, $email3[0]->group);
74 
75  }
76 
77  function testMagicIsset() {
78 
79  $comp = new VCalendar();
80 
81  $sub = $comp->createComponent('VEVENT');
82  $comp->add($sub);
83 
84  $sub = $comp->createComponent('VTODO');
85  $comp->add($sub);
86 
87  $this->assertTrue(isset($comp->vevent));
88  $this->assertTrue(isset($comp->vtodo));
89  $this->assertFalse(isset($comp->vjournal));
90 
91  }
92 
93  function testMagicSetScalar() {
94 
95  $comp = new VCalendar();
96  $comp->myProp = 'myValue';
97 
98  $this->assertInstanceOf('Sabre\\VObject\\Property', $comp->MYPROP);
99  $this->assertEquals('myValue', (string)$comp->MYPROP);
100 
101 
102  }
103 
105 
106  $comp = new VCalendar([], false);
107  $comp->myProp = 'myValue';
108  $comp->myProp = 'myValue';
109 
110  $this->assertEquals(1, count($comp->children()));
111  $this->assertInstanceOf('Sabre\\VObject\\Property', $comp->MYPROP);
112  $this->assertEquals('myValue', (string)$comp->MYPROP);
113 
114  }
115 
116  function testMagicSetArray() {
117 
118  $comp = new VCalendar();
119  $comp->ORG = ['Acme Inc', 'Section 9'];
120 
121  $this->assertInstanceOf('Sabre\\VObject\\Property', $comp->ORG);
122  $this->assertEquals(['Acme Inc', 'Section 9'], $comp->ORG->getParts());
123 
124  }
125 
127 
128  $comp = new VCalendar();
129 
130  // Note that 'myProp' is ignored here.
131  $comp->myProp = $comp->createComponent('VEVENT');
132 
133  $this->assertEquals(1, count($comp));
134 
135  $this->assertEquals('VEVENT', $comp->VEVENT->name);
136 
137  }
138 
139  function testMagicSetTwice() {
140 
141  $comp = new VCalendar([], false);
142 
143  $comp->VEVENT = $comp->createComponent('VEVENT');
144  $comp->VEVENT = $comp->createComponent('VEVENT');
145 
146  $this->assertEquals(1, count($comp->children()));
147 
148  $this->assertEquals('VEVENT', $comp->VEVENT->name);
149 
150  }
151 
152  function testArrayAccessGet() {
153 
154  $comp = new VCalendar([], false);
155 
156  $event = $comp->createComponent('VEVENT');
157  $event->summary = 'Event 1';
158 
159  $comp->add($event);
160 
161  $event2 = clone $event;
162  $event2->summary = 'Event 2';
163 
164  $comp->add($event2);
165 
166  $this->assertEquals(2, count($comp->children()));
167  $this->assertTrue($comp->vevent[1] instanceof Component);
168  $this->assertEquals('Event 2', (string)$comp->vevent[1]->summary);
169 
170  }
171 
173 
174  $comp = new VCalendar();
175 
176  $event = $comp->createComponent('VEVENT');
177  $event->summary = 'Event 1';
178 
179  $comp->add($event);
180 
181  $event2 = clone $event;
182  $event2->summary = 'Event 2';
183 
184  $comp->add($event2);
185 
186  $this->assertTrue(isset($comp->vevent[0]));
187  $this->assertTrue(isset($comp->vevent[1]));
188 
189  }
190 
194  function testArrayAccessSet() {
195 
196  $comp = new VCalendar();
197  $comp['hey'] = 'hi there';
198 
199  }
203  function testArrayAccessUnset() {
204 
205  $comp = new VCalendar();
206  unset($comp[0]);
207 
208  }
209 
210  function testAddScalar() {
211 
212  $comp = new VCalendar([], false);
213 
214  $comp->add('myprop', 'value');
215 
216  $this->assertEquals(1, count($comp->children()));
217 
218  $bla = $comp->children()[0];
219 
220  $this->assertTrue($bla instanceof Property);
221  $this->assertEquals('MYPROP', $bla->name);
222  $this->assertEquals('value', (string)$bla);
223 
224  }
225 
226  function testAddScalarParams() {
227 
228  $comp = new VCalendar([], false);
229 
230  $comp->add('myprop', 'value', ['param1' => 'value1']);
231 
232  $this->assertEquals(1, count($comp->children()));
233 
234  $bla = $comp->children()[0];
235 
236  $this->assertInstanceOf('Sabre\\VObject\\Property', $bla);
237  $this->assertEquals('MYPROP', $bla->name);
238  $this->assertEquals('value', (string)$bla);
239 
240  $this->assertEquals(1, count($bla->parameters()));
241 
242  $this->assertEquals('PARAM1', $bla->parameters['PARAM1']->name);
243  $this->assertEquals('value1', $bla->parameters['PARAM1']->getValue());
244 
245  }
246 
247 
248  function testAddComponent() {
249 
250  $comp = new VCalendar([], false);
251 
252  $comp->add($comp->createComponent('VEVENT'));
253 
254  $this->assertEquals(1, count($comp->children()));
255 
256  $this->assertEquals('VEVENT', $comp->VEVENT->name);
257 
258  }
259 
261 
262  $comp = new VCalendar([], false);
263 
264  $comp->add($comp->createComponent('VEVENT'));
265  $comp->add($comp->createComponent('VEVENT'));
266 
267  $this->assertEquals(2, count($comp->children()));
268 
269  $this->assertEquals('VEVENT', $comp->VEVENT->name);
270 
271  }
272 
276  function testAddArgFail() {
277 
278  $comp = new VCalendar();
279  $comp->add($comp->createComponent('VEVENT'), 'hello');
280 
281  }
282 
286  function testAddArgFail2() {
287 
288  $comp = new VCalendar();
289  $comp->add([]);
290 
291  }
292 
293  function testMagicUnset() {
294 
295  $comp = new VCalendar([], false);
296  $comp->add($comp->createComponent('VEVENT'));
297 
298  unset($comp->vevent);
299 
300  $this->assertEquals(0, count($comp->children()));
301 
302  }
303 
304 
305  function testCount() {
306 
307  $comp = new VCalendar();
308  $this->assertEquals(1, $comp->count());
309 
310  }
311 
312  function testChildren() {
313 
314  $comp = new VCalendar([], false);
315 
316  // Note that 'myProp' is ignored here.
317  $comp->add($comp->createComponent('VEVENT'));
318  $comp->add($comp->createComponent('VTODO'));
319 
320  $r = $comp->children();
321  $this->assertInternalType('array', $r);
322  $this->assertEquals(2, count($r));
323  }
324 
325  function testGetComponents() {
326 
327  $comp = new VCalendar();
328 
329  $comp->add($comp->createProperty('FOO', 'BAR'));
330  $comp->add($comp->createComponent('VTODO'));
331 
332  $r = $comp->getComponents();
333  $this->assertInternalType('array', $r);
334  $this->assertEquals(1, count($r));
335  $this->assertEquals('VTODO', $r[0]->name);
336  }
337 
338  function testSerialize() {
339 
340  $comp = new VCalendar([], false);
341  $this->assertEquals("BEGIN:VCALENDAR\r\nEND:VCALENDAR\r\n", $comp->serialize());
342 
343  }
344 
346 
347  $comp = new VCalendar([], false);
348  $event = $comp->add($comp->createComponent('VEVENT'));
349  unset($event->DTSTAMP, $event->UID);
350  $todo = $comp->add($comp->createComponent('VTODO'));
351  unset($todo->DTSTAMP, $todo->UID);
352 
353  $str = $comp->serialize();
354 
355  $this->assertEquals("BEGIN:VCALENDAR\r\nBEGIN:VEVENT\r\nEND:VEVENT\r\nBEGIN:VTODO\r\nEND:VTODO\r\nEND:VCALENDAR\r\n", $str);
356 
357  }
358 
360 
361  $comp = new VCalendar([], false);
362  $comp->add($event = $comp->createComponent('VEVENT'));
363  $comp->add('PROP1', 'BLABLA');
364  $comp->add('VERSION', '2.0');
365  $comp->add($comp->createComponent('VTIMEZONE'));
366 
367  unset($event->DTSTAMP, $event->UID);
368  $str = $comp->serialize();
369 
370  $this->assertEquals("BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPROP1:BLABLA\r\nBEGIN:VTIMEZONE\r\nEND:VTIMEZONE\r\nBEGIN:VEVENT\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n", $str);
371 
372  }
373 
375 
376  $prop4s = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10'];
377 
378  $comp = new VCard([], false);
379 
380  $comp->__set('SOMEPROP', 'FOO');
381  $comp->__set('ANOTHERPROP', 'FOO');
382  $comp->__set('THIRDPROP', 'FOO');
383  foreach ($prop4s as $prop4) {
384  $comp->add('PROP4', 'FOO ' . $prop4);
385  }
386  $comp->__set('PROPNUMBERFIVE', 'FOO');
387  $comp->__set('PROPNUMBERSIX', 'FOO');
388  $comp->__set('PROPNUMBERSEVEN', 'FOO');
389  $comp->__set('PROPNUMBEREIGHT', 'FOO');
390  $comp->__set('PROPNUMBERNINE', 'FOO');
391  $comp->__set('PROPNUMBERTEN', 'FOO');
392  $comp->__set('VERSION', '2.0');
393  $comp->__set('UID', 'FOO');
394 
395  $str = $comp->serialize();
396 
397  $this->assertEquals("BEGIN:VCARD\r\nVERSION:2.0\r\nSOMEPROP:FOO\r\nANOTHERPROP:FOO\r\nTHIRDPROP:FOO\r\nPROP4:FOO 1\r\nPROP4:FOO 2\r\nPROP4:FOO 3\r\nPROP4:FOO 4\r\nPROP4:FOO 5\r\nPROP4:FOO 6\r\nPROP4:FOO 7\r\nPROP4:FOO 8\r\nPROP4:FOO 9\r\nPROP4:FOO 10\r\nPROPNUMBERFIVE:FOO\r\nPROPNUMBERSIX:FOO\r\nPROPNUMBERSEVEN:FOO\r\nPROPNUMBEREIGHT:FOO\r\nPROPNUMBERNINE:FOO\r\nPROPNUMBERTEN:FOO\r\nUID:FOO\r\nEND:VCARD\r\n", $str);
398 
399  }
400 
402 
403  $comp = new VCard([
404  'ORG' => ['Acme Inc.', 'Section 9'],
405  'FN' => 'Finn The Human',
406  ]);
407 
408  $this->assertEquals(['Acme Inc.', 'Section 9'], $comp->ORG->getParts());
409  $this->assertEquals('Finn The Human', $comp->FN->getValue());
410 
411  }
412 
414 
415  $comp = new VCalendar();
416  $event = $comp->createComponent('VEVENT', [
417  $comp->createProperty('UID', '12345'),
418  ]);
419  $comp->add($event);
420 
421  $this->assertEquals('12345', $comp->VEVENT->UID->getValue());
422 
423  }
424 
425  function testRemoveByName() {
426 
427  $comp = new VCalendar([], false);
428  $comp->add('prop1', 'val1');
429  $comp->add('prop2', 'val2');
430  $comp->add('prop2', 'val2');
431 
432  $comp->remove('prop2');
433  $this->assertFalse(isset($comp->prop2));
434  $this->assertTrue(isset($comp->prop1));
435 
436  }
437 
438  function testRemoveByObj() {
439 
440  $comp = new VCalendar([], false);
441  $comp->add('prop1', 'val1');
442  $prop = $comp->add('prop2', 'val2');
443 
444  $comp->remove($prop);
445  $this->assertFalse(isset($comp->prop2));
446  $this->assertTrue(isset($comp->prop1));
447 
448  }
449 
453  function testRemoveNotFound() {
454 
455  $comp = new VCalendar([], false);
456  $prop = $comp->createProperty('A', 'B');
457  $comp->remove($prop);
458 
459  }
460 
464  function testValidateRules($componentList, $errorCount) {
465 
466  $vcard = new Component\VCard();
467 
468  $component = new FakeComponent($vcard, 'Hi', [], $defaults = false);
469  foreach ($componentList as $v) {
470  $component->add($v, 'Hello.');
471  }
472 
473  $this->assertEquals($errorCount, count($component->validate()));
474 
475  }
476 
477  function testValidateRepair() {
478 
479  $vcard = new Component\VCard();
480 
481  $component = new FakeComponent($vcard, 'Hi', [], $defaults = false);
482  $component->validate(Component::REPAIR);
483  $this->assertEquals('yow', $component->BAR->getValue());
484 
485  }
486 
488  $vcard = new Component\VCard();
489 
490  $component = new FakeComponent($vcard, 'WithDuplicateGIR', []);
491  $component->add('BAZ', 'BAZ');
492  $component->add('GIR', 'VALUE1');
493  $component->add('GIR', 'VALUE2'); // Different values
494 
495  $messages = $component->validate(Component::REPAIR);
496 
497  $this->assertEquals(1, count($messages));
498  $this->assertEquals(3, $messages[0]['level']);
499  $this->assertEquals(2, count($component->GIR));
500  }
501 
503  $vcard = new Component\VCard();
504 
505  $component = new FakeComponent($vcard, 'WithDuplicateGIR', []);
506  $component->add('BAZ', 'BAZ');
507  $component->add('GIR', 'VALUE')->add('PARAM', '1');
508  $component->add('GIR', 'VALUE')->add('PARAM', '2'); // Same value but different parameters
509 
510  $messages = $component->validate(Component::REPAIR);
511 
512  $this->assertEquals(1, count($messages));
513  $this->assertEquals(3, $messages[0]['level']);
514  $this->assertEquals(2, count($component->GIR));
515  }
516 
518  $vcard = new Component\VCard();
519 
520  $component = new FakeComponent($vcard, 'WithDuplicateGIR', []);
521  $component->add('BAZ', 'BAZ');
522  $component->add('GIR', 'VALUE')->add('PARAM', 'P');
523  $component->add('GIR', 'VALUE')->add('PARAM', 'P');
524 
525  $messages = $component->validate(Component::REPAIR);
526 
527  $this->assertEquals(1, count($messages));
528  $this->assertEquals(1, $messages[0]['level']);
529  $this->assertEquals(1, count($component->GIR));
530  }
531 
533  $vcard = new Component\VCard();
534 
535  $component = new FakeComponent($vcard, 'WithDuplicateGIR', []);
536  $component->add('BAZ', 'BAZ');
537  $component->add('GIR', 'VALUE');
538  $component->add('GIR', 'VALUE');
539 
540  $messages = $component->validate(Component::REPAIR);
541 
542  $this->assertEquals(1, count($messages));
543  $this->assertEquals(1, $messages[0]['level']);
544  $this->assertEquals(1, count($component->GIR));
545  }
546 
547  function ruleData() {
548 
549  return [
550 
551  [[], 2],
552  [['FOO'], 3],
553  [['BAR'], 1],
554  [['BAZ'], 1],
555  [['BAR','BAZ'], 0],
556  [['BAR','BAZ','ZIM',], 0],
557  [['BAR','BAZ','ZIM','GIR'], 0],
558  [['BAR','BAZ','ZIM','GIR','GIR'], 1],
559 
560  ];
561 
562  }
563 
564 }
565 
566 class FakeComponent extends Component {
567 
568  function getValidationRules() {
569 
570  return [
571  'FOO' => '0',
572  'BAR' => '1',
573  'BAZ' => '+',
574  'ZIM' => '*',
575  'GIR' => '?',
576  ];
577 
578  }
579 
580  function getDefaults() {
581 
582  return [
583  'BAR' => 'yow',
584  ];
585 
586  }
587 
588 }
testValidateRepairShouldNotDeduplicatePropertiesWhenValuesDiffer()
testRemoveNotFound()
InvalidArgumentException
testValidateRepairShouldNotDeduplicatePropertiesWhenParametersDiffer()
testAddArgFail()
InvalidArgumentException
The VCalendar component.
Definition: VCalendar.php:23
$messages
Definition: en.php:5
testValidateRules($componentList, $errorCount)
ruleData
$r
Definition: example_031.php:79
testArrayAccessSet()
LogicException
testArrayAccessUnset()
LogicException
const REPAIR
The following constants are used by the validate() method.
Definition: Node.php:27
testAddArgFail2()
InvalidArgumentException
testValidateRepairShouldDeduplicatePropertiesWhenValuesAreEqual()
The VCard component.
Definition: VCard.php:18
testValidateRepairShouldDeduplicatePropertiesWhenValuesAndParametersAreEqual()
$key
Definition: croninfo.php:18