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