/
VCardTest.php
302 lines (264 loc) · 8.03 KB
/
VCardTest.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
<?php
namespace Sabre\VObject\Component;
use PHPUnit\Framework\TestCase;
use Sabre\VObject;
class VCardTest extends TestCase
{
/**
* @dataProvider validateData
*/
public function testValidate($input, $expectedWarnings, $expectedRepairedOutput)
{
$vcard = VObject\Reader::read($input);
$warnings = $vcard->validate();
$warnMsg = [];
foreach ($warnings as $warning) {
$warnMsg[] = $warning['message'];
}
$this->assertEquals($expectedWarnings, $warnMsg);
$vcard->validate(VObject\Component::REPAIR);
$this->assertEquals(
$expectedRepairedOutput,
$vcard->serialize()
);
}
public function validateData()
{
$tests = [];
// Correct
$tests[] = [
"BEGIN:VCARD\r\nVERSION:4.0\r\nFN:John Doe\r\nUID:foo\r\nEND:VCARD\r\n",
[],
"BEGIN:VCARD\r\nVERSION:4.0\r\nFN:John Doe\r\nUID:foo\r\nEND:VCARD\r\n",
];
// No VERSION
$tests[] = [
"BEGIN:VCARD\r\nFN:John Doe\r\nUID:foo\r\nEND:VCARD\r\n",
[
'VERSION MUST appear exactly once in a VCARD component',
],
"BEGIN:VCARD\r\nVERSION:4.0\r\nFN:John Doe\r\nUID:foo\r\nEND:VCARD\r\n",
];
// Unknown version
$tests[] = [
"BEGIN:VCARD\r\nVERSION:2.2\r\nFN:John Doe\r\nUID:foo\r\nEND:VCARD\r\n",
[
'Only vcard version 4.0 (RFC6350), version 3.0 (RFC2426) or version 2.1 (icm-vcard-2.1) are supported.',
],
"BEGIN:VCARD\r\nVERSION:2.1\r\nFN:John Doe\r\nUID:foo\r\nEND:VCARD\r\n",
];
// No FN
$tests[] = [
"BEGIN:VCARD\r\nVERSION:4.0\r\nUID:foo\r\nEND:VCARD\r\n",
[
'The FN property must appear in the VCARD component exactly 1 time',
],
"BEGIN:VCARD\r\nVERSION:4.0\r\nUID:foo\r\nEND:VCARD\r\n",
];
// No FN, N fallback
$tests[] = [
"BEGIN:VCARD\r\nVERSION:4.0\r\nUID:foo\r\nN:Doe;John;;;;;\r\nEND:VCARD\r\n",
[
'The FN property must appear in the VCARD component exactly 1 time',
],
"BEGIN:VCARD\r\nVERSION:4.0\r\nUID:foo\r\nN:Doe;John;;;;;\r\nFN:John Doe\r\nEND:VCARD\r\n",
];
// No FN, N fallback, no first name
$tests[] = [
"BEGIN:VCARD\r\nVERSION:4.0\r\nUID:foo\r\nN:Doe;;;;;;\r\nEND:VCARD\r\n",
[
'The FN property must appear in the VCARD component exactly 1 time',
],
"BEGIN:VCARD\r\nVERSION:4.0\r\nUID:foo\r\nN:Doe;;;;;;\r\nFN:Doe\r\nEND:VCARD\r\n",
];
// No FN, ORG fallback
$tests[] = [
"BEGIN:VCARD\r\nVERSION:4.0\r\nUID:foo\r\nORG:Acme Co.\r\nEND:VCARD\r\n",
[
'The FN property must appear in the VCARD component exactly 1 time',
],
"BEGIN:VCARD\r\nVERSION:4.0\r\nUID:foo\r\nORG:Acme Co.\r\nFN:Acme Co.\r\nEND:VCARD\r\n",
];
// No FN, EMAIL fallback
$tests[] = [
"BEGIN:VCARD\r\nVERSION:4.0\r\nUID:foo\r\nEMAIL:1@example.org\r\nEND:VCARD\r\n",
[
'The FN property must appear in the VCARD component exactly 1 time',
],
"BEGIN:VCARD\r\nVERSION:4.0\r\nUID:foo\r\nEMAIL:1@example.org\r\nFN:1@example.org\r\nEND:VCARD\r\n",
];
return $tests;
}
public function testGetDocumentType()
{
$vcard = new VCard([], false);
$vcard->VERSION = '2.1';
$this->assertEquals(VCard::VCARD21, $vcard->getDocumentType());
$vcard = new VCard([], false);
$vcard->VERSION = '3.0';
$this->assertEquals(VCard::VCARD30, $vcard->getDocumentType());
$vcard = new VCard([], false);
$vcard->VERSION = '4.0';
$this->assertEquals(VCard::VCARD40, $vcard->getDocumentType());
$vcard = new VCard([], false);
$this->assertEquals(VCard::UNKNOWN, $vcard->getDocumentType());
}
public function testGetByType()
{
$vcard = <<<VCF
BEGIN:VCARD
VERSION:3.0
EMAIL;TYPE=home:1@example.org
EMAIL;TYPE=work:2@example.org
END:VCARD
VCF;
$vcard = VObject\Reader::read($vcard);
$this->assertEquals('1@example.org', $vcard->getByType('EMAIL', 'home')->getValue());
$this->assertEquals('2@example.org', $vcard->getByType('EMAIL', 'work')->getValue());
$this->assertNull($vcard->getByType('EMAIL', 'non-existant'));
$this->assertNull($vcard->getByType('ADR', 'non-existant'));
}
public function testPreferredNoPref()
{
$vcard = <<<VCF
BEGIN:VCARD
VERSION:3.0
EMAIL:1@example.org
EMAIL:2@example.org
END:VCARD
VCF;
$vcard = VObject\Reader::read($vcard);
$this->assertEquals('1@example.org', $vcard->preferred('EMAIL')->getValue());
}
public function testPreferredWithPref()
{
$vcard = <<<VCF
BEGIN:VCARD
VERSION:3.0
EMAIL:1@example.org
EMAIL;TYPE=PREF:2@example.org
END:VCARD
VCF;
$vcard = VObject\Reader::read($vcard);
$this->assertEquals('2@example.org', $vcard->preferred('EMAIL')->getValue());
}
public function testPreferredWith40Pref()
{
$vcard = <<<VCF
BEGIN:VCARD
VERSION:4.0
EMAIL:1@example.org
EMAIL;PREF=3:2@example.org
EMAIL;PREF=2:3@example.org
END:VCARD
VCF;
$vcard = VObject\Reader::read($vcard);
$this->assertEquals('3@example.org', $vcard->preferred('EMAIL')->getValue());
}
public function testPreferredNotFound()
{
$vcard = <<<VCF
BEGIN:VCARD
VERSION:4.0
END:VCARD
VCF;
$vcard = VObject\Reader::read($vcard);
$this->assertNull($vcard->preferred('EMAIL'));
}
public function testNoUIDCardDAV()
{
$vcard = <<<VCF
BEGIN:VCARD
VERSION:4.0
FN:John Doe
END:VCARD
VCF;
$this->assertValidate(
$vcard,
VCard::PROFILE_CARDDAV,
3,
'vCards on CardDAV servers MUST have a UID property.'
);
}
public function testNoUIDNoCardDAV()
{
$vcard = <<<VCF
BEGIN:VCARD
VERSION:4.0
FN:John Doe
END:VCARD
VCF;
$this->assertValidate(
$vcard,
0,
2,
'Adding a UID to a vCard property is recommended.'
);
}
public function testNoUIDNoCardDAVRepair()
{
$vcard = <<<VCF
BEGIN:VCARD
VERSION:4.0
FN:John Doe
END:VCARD
VCF;
$this->assertValidate(
$vcard,
VCard::REPAIR,
1,
'Adding a UID to a vCard property is recommended.'
);
}
public function testVCard21CardDAV()
{
$vcard = <<<VCF
BEGIN:VCARD
VERSION:2.1
FN:John Doe
UID:foo
END:VCARD
VCF;
$this->assertValidate(
$vcard,
VCard::PROFILE_CARDDAV,
3,
'CardDAV servers are not allowed to accept vCard 2.1.'
);
}
public function testVCard21NoCardDAV()
{
$vcard = <<<VCF
BEGIN:VCARD
VERSION:2.1
FN:John Doe
UID:foo
END:VCARD
VCF;
$this->assertValidate(
$vcard,
0,
0
);
}
public function assertValidate($vcf, $options, $expectedLevel, $expectedMessage = null)
{
$vcal = VObject\Reader::read($vcf);
$result = $vcal->validate($options);
$this->assertValidateResult($result, $expectedLevel, $expectedMessage);
}
public function assertValidateResult($input, $expectedLevel, $expectedMessage = null)
{
$messages = [];
foreach ($input as $warning) {
$messages[] = $warning['message'];
}
if (0 === $expectedLevel) {
$this->assertEquals(0, count($input), 'No validation messages were expected. We got: '.implode(', ', $messages));
} else {
$this->assertEquals(1, count($input), 'We expected exactly 1 validation message, We got: '.implode(', ', $messages));
$this->assertEquals($expectedMessage, $input[0]['message']);
$this->assertEquals($expectedLevel, $input[0]['level']);
}
}
}