ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
VCardTest.php
Go to the documentation of this file.
1<?php
2
4
5use PHPUnit\Framework\TestCase;
7
8class VCardTest extends TestCase {
9
13 function testValidate($input, $expectedWarnings, $expectedRepairedOutput) {
14
16
17 $warnings = $vcard->validate();
18
19 $warnMsg = [];
20 foreach ($warnings as $warning) {
21 $warnMsg[] = $warning['message'];
22 }
23
24 $this->assertEquals($expectedWarnings, $warnMsg);
25
26 $vcard->validate(VObject\Component::REPAIR);
27
28 $this->assertEquals(
29 $expectedRepairedOutput,
30 $vcard->serialize()
31 );
32
33 }
34
35 function validateData() {
36
37 $tests = [];
38
39 // Correct
40 $tests[] = [
41 "BEGIN:VCARD\r\nVERSION:4.0\r\nFN:John Doe\r\nUID:foo\r\nEND:VCARD\r\n",
42 [],
43 "BEGIN:VCARD\r\nVERSION:4.0\r\nFN:John Doe\r\nUID:foo\r\nEND:VCARD\r\n",
44 ];
45
46 // No VERSION
47 $tests[] = [
48 "BEGIN:VCARD\r\nFN:John Doe\r\nUID:foo\r\nEND:VCARD\r\n",
49 [
50 'VERSION MUST appear exactly once in a VCARD component',
51 ],
52 "BEGIN:VCARD\r\nVERSION:4.0\r\nFN:John Doe\r\nUID:foo\r\nEND:VCARD\r\n",
53 ];
54
55 // Unknown version
56 $tests[] = [
57 "BEGIN:VCARD\r\nVERSION:2.2\r\nFN:John Doe\r\nUID:foo\r\nEND:VCARD\r\n",
58 [
59 'Only vcard version 4.0 (RFC6350), version 3.0 (RFC2426) or version 2.1 (icm-vcard-2.1) are supported.',
60 ],
61 "BEGIN:VCARD\r\nVERSION:2.1\r\nFN:John Doe\r\nUID:foo\r\nEND:VCARD\r\n",
62 ];
63
64 // No FN
65 $tests[] = [
66 "BEGIN:VCARD\r\nVERSION:4.0\r\nUID:foo\r\nEND:VCARD\r\n",
67 [
68 'The FN property must appear in the VCARD component exactly 1 time',
69 ],
70 "BEGIN:VCARD\r\nVERSION:4.0\r\nUID:foo\r\nEND:VCARD\r\n",
71 ];
72 // No FN, N fallback
73 $tests[] = [
74 "BEGIN:VCARD\r\nVERSION:4.0\r\nUID:foo\r\nN:Doe;John;;;;;\r\nEND:VCARD\r\n",
75 [
76 'The FN property must appear in the VCARD component exactly 1 time',
77 ],
78 "BEGIN:VCARD\r\nVERSION:4.0\r\nUID:foo\r\nN:Doe;John;;;;;\r\nFN:John Doe\r\nEND:VCARD\r\n",
79 ];
80 // No FN, N fallback, no first name
81 $tests[] = [
82 "BEGIN:VCARD\r\nVERSION:4.0\r\nUID:foo\r\nN:Doe;;;;;;\r\nEND:VCARD\r\n",
83 [
84 'The FN property must appear in the VCARD component exactly 1 time',
85 ],
86 "BEGIN:VCARD\r\nVERSION:4.0\r\nUID:foo\r\nN:Doe;;;;;;\r\nFN:Doe\r\nEND:VCARD\r\n",
87 ];
88 // No FN, ORG fallback
89 $tests[] = [
90 "BEGIN:VCARD\r\nVERSION:4.0\r\nUID:foo\r\nORG:Acme Co.\r\nEND:VCARD\r\n",
91 [
92 'The FN property must appear in the VCARD component exactly 1 time',
93 ],
94 "BEGIN:VCARD\r\nVERSION:4.0\r\nUID:foo\r\nORG:Acme Co.\r\nFN:Acme Co.\r\nEND:VCARD\r\n",
95 ];
96 // No FN, EMAIL fallback
97 $tests[] = [
98 "BEGIN:VCARD\r\nVERSION:4.0\r\nUID:foo\r\nEMAIL:1@example.org\r\nEND:VCARD\r\n",
99 [
100 'The FN property must appear in the VCARD component exactly 1 time',
101 ],
102 "BEGIN:VCARD\r\nVERSION:4.0\r\nUID:foo\r\nEMAIL:1@example.org\r\nFN:1@example.org\r\nEND:VCARD\r\n",
103 ];
104 return $tests;
105
106 }
107
109
110 $vcard = new VCard([], false);
111 $vcard->VERSION = '2.1';
112 $this->assertEquals(VCard::VCARD21, $vcard->getDocumentType());
113
114 $vcard = new VCard([], false);
115 $vcard->VERSION = '3.0';
116 $this->assertEquals(VCard::VCARD30, $vcard->getDocumentType());
117
118 $vcard = new VCard([], false);
119 $vcard->VERSION = '4.0';
120 $this->assertEquals(VCard::VCARD40, $vcard->getDocumentType());
121
122 $vcard = new VCard([], false);
123 $this->assertEquals(VCard::UNKNOWN, $vcard->getDocumentType());
124 }
125
126 function testGetByType() {
127 $vcard = <<<VCF
128BEGIN:VCARD
129VERSION:3.0
130EMAIL;TYPE=home:1@example.org
131EMAIL;TYPE=work:2@example.org
132END:VCARD
133VCF;
134
135 $vcard = VObject\Reader::read($vcard);
136 $this->assertEquals('1@example.org', $vcard->getByType('EMAIL', 'home')->getValue());
137 $this->assertEquals('2@example.org', $vcard->getByType('EMAIL', 'work')->getValue());
138 $this->assertNull($vcard->getByType('EMAIL', 'non-existant'));
139 $this->assertNull($vcard->getByType('ADR', 'non-existant'));
140 }
141
143
144 $vcard = <<<VCF
145BEGIN:VCARD
146VERSION:3.0
147EMAIL:1@example.org
148EMAIL:2@example.org
149END:VCARD
150VCF;
151
152 $vcard = VObject\Reader::read($vcard);
153 $this->assertEquals('1@example.org', $vcard->preferred('EMAIL')->getValue());
154
155 }
156
158
159 $vcard = <<<VCF
160BEGIN:VCARD
161VERSION:3.0
162EMAIL:1@example.org
163EMAIL;TYPE=PREF:2@example.org
164END:VCARD
165VCF;
166
167 $vcard = VObject\Reader::read($vcard);
168 $this->assertEquals('2@example.org', $vcard->preferred('EMAIL')->getValue());
169
170 }
171
173
174 $vcard = <<<VCF
175BEGIN:VCARD
176VERSION:4.0
177EMAIL:1@example.org
178EMAIL;PREF=3:2@example.org
179EMAIL;PREF=2:3@example.org
180END:VCARD
181VCF;
182
183 $vcard = VObject\Reader::read($vcard);
184 $this->assertEquals('3@example.org', $vcard->preferred('EMAIL')->getValue());
185
186 }
187
189
190 $vcard = <<<VCF
191BEGIN:VCARD
192VERSION:4.0
193END:VCARD
194VCF;
195
196 $vcard = VObject\Reader::read($vcard);
197 $this->assertNull($vcard->preferred('EMAIL'));
198
199 }
200
201 function testNoUIDCardDAV() {
202
203 $vcard = <<<VCF
204BEGIN:VCARD
205VERSION:4.0
206FN:John Doe
207END:VCARD
208VCF;
209 $this->assertValidate(
210 $vcard,
211 VCARD::PROFILE_CARDDAV,
212 3,
213 'vCards on CardDAV servers MUST have a UID property.'
214 );
215
216 }
217
219
220 $vcard = <<<VCF
221BEGIN:VCARD
222VERSION:4.0
223FN:John Doe
224END:VCARD
225VCF;
226 $this->assertValidate(
227 $vcard,
228 0,
229 2,
230 'Adding a UID to a vCard property is recommended.'
231 );
232
233 }
235
236 $vcard = <<<VCF
237BEGIN:VCARD
238VERSION:4.0
239FN:John Doe
240END:VCARD
241VCF;
242 $this->assertValidate(
243 $vcard,
244 VCARD::REPAIR,
245 1,
246 'Adding a UID to a vCard property is recommended.'
247 );
248
249 }
250
252
253 $vcard = <<<VCF
254BEGIN:VCARD
255VERSION:2.1
256FN:John Doe
257UID:foo
258END:VCARD
259VCF;
260 $this->assertValidate(
261 $vcard,
262 VCARD::PROFILE_CARDDAV,
263 3,
264 'CardDAV servers are not allowed to accept vCard 2.1.'
265 );
266
267 }
268
270
271 $vcard = <<<VCF
272BEGIN:VCARD
273VERSION:2.1
274FN:John Doe
275UID:foo
276END:VCARD
277VCF;
278 $this->assertValidate(
279 $vcard,
280 0,
281 0
282 );
283
284 }
285
286 function assertValidate($vcf, $options, $expectedLevel, $expectedMessage = null) {
287
289 $result = $vcal->validate($options);
290
291 $this->assertValidateResult($result, $expectedLevel, $expectedMessage);
292
293 }
294
295 function assertValidateResult($input, $expectedLevel, $expectedMessage = null) {
296
297 $messages = [];
298 foreach ($input as $warning) {
299 $messages[] = $warning['message'];
300 }
301
302 if ($expectedLevel === 0) {
303 $this->assertEquals(0, count($input), 'No validation messages were expected. We got: ' . implode(', ', $messages));
304 } else {
305 $this->assertEquals(1, count($input), 'We expected exactly 1 validation message, We got: ' . implode(', ', $messages));
306
307 $this->assertEquals($expectedMessage, $input[0]['message']);
308 $this->assertEquals($expectedLevel, $input[0]['level']);
309 }
310
311 }
312}
$result
$warning
Definition: X509warning.php:13
An exception for terminatinating execution or to throw for unit testing.
assertValidate($vcf, $options, $expectedLevel, $expectedMessage=null)
Definition: VCardTest.php:286
assertValidateResult($input, $expectedLevel, $expectedMessage=null)
Definition: VCardTest.php:295
testValidate($input, $expectedWarnings, $expectedRepairedOutput)
@dataProvider validateData
Definition: VCardTest.php:13
The VCard component.
Definition: VCard.php:18
const VCARD30
vCard 3.0.
Definition: Document.php:44
const UNKNOWN
Unknown document type.
Definition: Document.php:24
const VCARD21
vCard 2.1.
Definition: Document.php:39
const VCARD40
vCard 4.0.
Definition: Document.php:49
count()
Returns the number of elements.
Definition: Node.php:177
const REPAIR
The following constants are used by the validate() method.
Definition: Node.php:27
static read($data, $options=0, $charset='UTF-8')
Parses a vCard or iCalendar object, and returns the top component.
Definition: Reader.php:42
$messages
Definition: en.php:5
$tests
Definition: bench.php:104
foreach($paths as $path) if($argc< 3) $input