1<?php
2
3namespace Sabre\VObject;
4
5use
6    Sabre\VObject\Component\VCalendar,
7    Sabre\VObject\Component\VCard;
8
9class PropertyTest extends \PHPUnit_Framework_TestCase {
10
11    function testToString() {
12
13        $cal = new VCalendar();
14
15        $property = $cal->createProperty('propname','propvalue');
16        $this->assertEquals('PROPNAME', $property->name);
17        $this->assertEquals('propvalue', $property->__toString());
18        $this->assertEquals('propvalue', (string)$property);
19        $this->assertEquals('propvalue', $property->getValue());
20
21    }
22
23    function testCreate() {
24
25        $cal = new VCalendar();
26
27        $params = array(
28            'param1' => 'value1',
29            'param2' => 'value2',
30        );
31
32        $property = $cal->createProperty('propname','propvalue', $params);
33
34        $this->assertEquals('value1', $property['param1']->getValue());
35        $this->assertEquals('value2', $property['param2']->getValue());
36
37    }
38
39    function testSetValue() {
40
41        $cal = new VCalendar();
42
43        $property = $cal->createProperty('propname','propvalue');
44        $property->setValue('value2');
45
46        $this->assertEquals('PROPNAME', $property->name);
47        $this->assertEquals('value2', $property->__toString());
48
49    }
50
51    function testParameterExists() {
52
53        $cal = new VCalendar();
54        $property = $cal->createProperty('propname','propvalue');
55        $property['paramname'] = 'paramvalue';
56
57        $this->assertTrue(isset($property['PARAMNAME']));
58        $this->assertTrue(isset($property['paramname']));
59        $this->assertFalse(isset($property['foo']));
60
61    }
62
63    function testParameterGet() {
64
65        $cal = new VCalendar();
66        $property = $cal->createProperty('propname','propvalue');
67        $property['paramname'] = 'paramvalue';
68
69        $this->assertInstanceOf('Sabre\\VObject\\Parameter',$property['paramname']);
70
71    }
72
73    function testParameterNotExists() {
74
75        $cal = new VCalendar();
76        $property = $cal->createProperty('propname','propvalue');
77        $property['paramname'] = 'paramvalue';
78
79        $this->assertInternalType('null',$property['foo']);
80
81    }
82
83    function testParameterMultiple() {
84
85        $cal = new VCalendar();
86        $property = $cal->createProperty('propname','propvalue');
87        $property['paramname'] = 'paramvalue';
88        $property->add('paramname', 'paramvalue');
89
90        $this->assertInstanceOf('Sabre\\VObject\\Parameter',$property['paramname']);
91        $this->assertEquals(2,count($property['paramname']->getParts()));
92
93    }
94
95    function testSetParameterAsString() {
96
97        $cal = new VCalendar();
98        $property = $cal->createProperty('propname','propvalue');
99        $property['paramname'] = 'paramvalue';
100
101        $this->assertEquals(1,count($property->parameters()));
102        $this->assertInstanceOf('Sabre\\VObject\\Parameter', $property->parameters['PARAMNAME']);
103        $this->assertEquals('PARAMNAME',$property->parameters['PARAMNAME']->name);
104        $this->assertEquals('paramvalue',$property->parameters['PARAMNAME']->getValue());
105
106    }
107
108    function testUnsetParameter() {
109
110        $cal = new VCalendar();
111        $property = $cal->createProperty('propname','propvalue');
112        $property['paramname'] = 'paramvalue';
113
114        unset($property['PARAMNAME']);
115        $this->assertEquals(0,count($property->parameters()));
116
117    }
118
119    function testSerialize() {
120
121        $cal = new VCalendar();
122        $property = $cal->createProperty('propname','propvalue');
123
124        $this->assertEquals("PROPNAME:propvalue\r\n",$property->serialize());
125
126    }
127
128    function testSerializeParam() {
129
130        $cal = new VCalendar();
131        $property = $cal->createProperty('propname','propvalue', array(
132            'paramname' => 'paramvalue',
133            'paramname2' => 'paramvalue2',
134        ));
135
136        $this->assertEquals("PROPNAME;PARAMNAME=paramvalue;PARAMNAME2=paramvalue2:propvalue\r\n",$property->serialize());
137
138    }
139
140    function testSerializeNewLine() {
141
142        $cal = new VCalendar();
143        $property = $cal->createProperty('SUMMARY',"line1\nline2");
144
145        $this->assertEquals("SUMMARY:line1\\nline2\r\n",$property->serialize());
146
147    }
148
149    function testSerializeLongLine() {
150
151        $cal = new VCalendar();
152        $value = str_repeat('!',200);
153        $property = $cal->createProperty('propname',$value);
154
155        $expected = "PROPNAME:" . str_repeat('!',66) . "\r\n " . str_repeat('!',74) . "\r\n " . str_repeat('!',60) . "\r\n";
156
157        $this->assertEquals($expected,$property->serialize());
158
159    }
160
161    function testSerializeUTF8LineFold() {
162
163        $cal = new VCalendar();
164        $value = str_repeat('!',65) . "\xc3\xa4bla"; // inserted umlaut-a
165        $property = $cal->createProperty('propname', $value);
166        $expected = "PROPNAME:" . str_repeat('!',65) . "\r\n \xc3\xa4bla\r\n";
167        $this->assertEquals($expected, $property->serialize());
168
169    }
170
171    function testGetIterator() {
172
173        $cal = new VCalendar();
174        $it = new ElementList(array());
175        $property = $cal->createProperty('propname','propvalue');
176        $property->setIterator($it);
177        $this->assertEquals($it,$property->getIterator());
178
179    }
180
181
182    function testGetIteratorDefault() {
183
184        $cal = new VCalendar();
185        $property = $cal->createProperty('propname','propvalue');
186        $it = $property->getIterator();
187        $this->assertTrue($it instanceof ElementList);
188        $this->assertEquals(1,count($it));
189
190    }
191
192    function testAddScalar() {
193
194        $cal = new VCalendar();
195        $property = $cal->createProperty('EMAIL');
196
197        $property->add('myparam','value');
198
199        $this->assertEquals(1, count($property->parameters()));
200
201        $this->assertTrue($property->parameters['MYPARAM'] instanceof Parameter);
202        $this->assertEquals('MYPARAM',$property->parameters['MYPARAM']->name);
203        $this->assertEquals('value',$property->parameters['MYPARAM']->getValue());
204
205    }
206
207    function testAddParameter() {
208
209        $cal = new VCalendar();
210        $prop = $cal->createProperty('EMAIL');
211
212        $prop->add('MYPARAM','value');
213
214        $this->assertEquals(1, count($prop->parameters()));
215        $this->assertEquals('MYPARAM',$prop['myparam']->name);
216
217    }
218
219    function testAddParameterTwice() {
220
221        $cal = new VCalendar();
222        $prop = $cal->createProperty('EMAIL');
223
224        $prop->add('MYPARAM', 'value1');
225        $prop->add('MYPARAM', 'value2');
226
227        $this->assertEquals(1, count($prop->parameters));
228        $this->assertEquals(2, count($prop->parameters['MYPARAM']->getParts()));
229
230        $this->assertEquals('MYPARAM',$prop['MYPARAM']->name);
231
232    }
233
234
235    function testClone() {
236
237        $cal = new VCalendar();
238        $property = $cal->createProperty('EMAIL','value');
239        $property['FOO'] = 'BAR';
240
241        $property2 = clone $property;
242
243        $property['FOO'] = 'BAZ';
244        $this->assertEquals('BAR', (string)$property2['FOO']);
245
246    }
247
248    function testCreateParams() {
249
250        $cal = new VCalendar();
251        $property = $cal->createProperty('X-PROP','value', array(
252            'param1' => 'value1',
253            'param2' => array('value2', 'value3')
254        ));
255
256        $this->assertEquals(1, count($property['PARAM1']->getParts()));
257        $this->assertEquals(2, count($property['PARAM2']->getParts()));
258
259    }
260
261    function testValidateNonUTF8() {
262
263        $calendar = new VCalendar();
264        $property = $calendar->createProperty('X-PROP', "Bla\x00");
265        $result = $property->validate(Property::REPAIR);
266
267        $this->assertEquals('Property contained a control character (0x00)', $result[0]['message']);
268        $this->assertEquals('Bla', $property->getValue());
269
270    }
271
272    function testValidateControlChars() {
273
274        $s = "chars[";
275        foreach (array(
276            0x7F, 0x5E, 0x5C, 0x3B, 0x3A, 0x2C, 0x22, 0x20,
277            0x1F, 0x1E, 0x1D, 0x1C, 0x1B, 0x1A, 0x19, 0x18,
278            0x17, 0x16, 0x15, 0x14, 0x13, 0x12, 0x11, 0x10,
279            0x0F, 0x0E, 0x0D, 0x0C, 0x0B, 0x0A, 0x09, 0x08,
280            0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00,
281          ) as $c) {
282            $s .= sprintf('%02X(%c)', $c, $c);
283        }
284        $s .= "]end";
285
286        $calendar = new VCalendar();
287        $property = $calendar->createProperty('X-PROP', $s);
288        $result = $property->validate(Property::REPAIR);
289
290        $this->assertEquals('Property contained a control character (0x7f)', $result[0]['message']);
291        $this->assertEquals("chars[7F()5E(^)5C(\\\\)3B(\\;)3A(:)2C(\\,)22(\")20( )1F()1E()1D()1C()1B()1A()19()18()17()16()15()14()13()12()11()10()0F()0E()0D()0C()0B()0A(\\n)09(	)08()07()06()05()04()03()02()01()00()]end", $property->getRawMimeDirValue());
292
293    }
294
295    function testValidateBadPropertyName() {
296
297        $calendar = new VCalendar();
298        $property = $calendar->createProperty("X_*&PROP*", "Bla");
299        $result = $property->validate(Property::REPAIR);
300
301        $this->assertEquals($result[0]['message'], 'The propertyname: X_*&PROP* contains invalid characters. Only A-Z, 0-9 and - are allowed');
302        $this->assertEquals('X-PROP', $property->name);
303
304    }
305
306    function testGetValue() {
307
308        $calendar = new VCalendar();
309        $property = $calendar->createProperty("SUMMARY", null);
310        $this->assertEquals(array(), $property->getParts());
311        $this->assertNull($property->getValue());
312
313        $property->setValue(array());
314        $this->assertEquals(array(), $property->getParts());
315        $this->assertNull($property->getValue());
316
317        $property->setValue(array(1));
318        $this->assertEquals(array(1), $property->getParts());
319        $this->assertEquals(1, $property->getValue());
320
321        $property->setValue(array(1,2));
322        $this->assertEquals(array(1,2), $property->getParts());
323        $this->assertEquals('1,2', $property->getValue());
324
325        $property->setValue('str');
326        $this->assertEquals(array('str'), $property->getParts());
327        $this->assertEquals('str', $property->getValue());
328    }
329
330    /**
331     * ElementList should reject this.
332     *
333     * @expectedException \LogicException
334     */
335    function testArrayAccessSetInt() {
336
337        $calendar = new VCalendar();
338        $property = $calendar->createProperty("X-PROP", null);
339
340        $calendar->add($property);
341        $calendar->{'X-PROP'}[0] = 'Something!';
342
343    }
344
345    /**
346     * ElementList should reject this.
347     *
348     * @expectedException \LogicException
349     */
350    function testArrayAccessUnsetInt() {
351
352        $calendar = new VCalendar();
353        $property = $calendar->createProperty("X-PROP", null);
354
355        $calendar->add($property);
356        unset($calendar->{'X-PROP'}[0]);
357
358    }
359
360    function testValidateBadEncoding() {
361
362        $document = new VCalendar();
363        $property = $document->add('X-FOO','value');
364        $property['ENCODING'] = 'invalid';
365
366        $result = $property->validate();
367
368        $this->assertEquals('ENCODING=INVALID is not valid for this document type.', $result[0]['message']);
369        $this->assertEquals(1, $result[0]['level']);
370
371    }
372
373    function testValidateBadEncodingVCard4() {
374
375        $document = new VCard(array('VERSION' => '4.0'));
376        $property = $document->add('X-FOO','value');
377        $property['ENCODING'] = 'BASE64';
378
379        $result = $property->validate();
380
381        $this->assertEquals('ENCODING parameter is not valid in vCard 4.', $result[0]['message']);
382        $this->assertEquals(1, $result[0]['level']);
383
384    }
385
386    function testValidateBadEncodingVCard3() {
387
388        $document = new VCard(array('VERSION' => '3.0'));
389        $property = $document->add('X-FOO','value');
390        $property['ENCODING'] = 'BASE64';
391
392        $result = $property->validate();
393
394        $this->assertEquals('ENCODING=BASE64 is not valid for this document type.', $result[0]['message']);
395        $this->assertEquals(1, $result[0]['level']);
396
397    }
398
399    function testValidateBadEncodingVCard21() {
400
401        $document = new VCard(array('VERSION' => '2.1'));
402        $property = $document->add('X-FOO','value');
403        $property['ENCODING'] = 'B';
404
405        $result = $property->validate();
406
407        $this->assertEquals('ENCODING=B is not valid for this document type.', $result[0]['message']);
408        $this->assertEquals(1, $result[0]['level']);
409
410    }
411}
412