ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
VCardTest.php
Go to the documentation of this file.
1 <?php
2 
4 
6 use Sabre\VObject;
7 
8 class VCardTest extends TestCase {
9 
13  function testValidate($input, $expectedWarnings, $expectedRepairedOutput) {
14 
15  $vcard = VObject\Reader::read($input);
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 
108  function testGetDocumentType() {
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
128 BEGIN:VCARD
129 VERSION:3.0
130 EMAIL;TYPE=home:1@example.org
131 EMAIL;TYPE=work:2@example.org
132 END:VCARD
133 VCF;
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 
142  function testPreferredNoPref() {
143 
144  $vcard = <<<VCF
145 BEGIN:VCARD
146 VERSION:3.0
147 EMAIL:1@example.org
148 EMAIL:2@example.org
149 END:VCARD
150 VCF;
151 
152  $vcard = VObject\Reader::read($vcard);
153  $this->assertEquals('1@example.org', $vcard->preferred('EMAIL')->getValue());
154 
155  }
156 
158 
159  $vcard = <<<VCF
160 BEGIN:VCARD
161 VERSION:3.0
162 EMAIL:1@example.org
163 EMAIL;TYPE=PREF:2@example.org
164 END:VCARD
165 VCF;
166 
167  $vcard = VObject\Reader::read($vcard);
168  $this->assertEquals('2@example.org', $vcard->preferred('EMAIL')->getValue());
169 
170  }
171 
173 
174  $vcard = <<<VCF
175 BEGIN:VCARD
176 VERSION:4.0
177 EMAIL:1@example.org
178 EMAIL;PREF=3:2@example.org
179 EMAIL;PREF=2:3@example.org
180 END:VCARD
181 VCF;
182 
183  $vcard = VObject\Reader::read($vcard);
184  $this->assertEquals('3@example.org', $vcard->preferred('EMAIL')->getValue());
185 
186  }
187 
189 
190  $vcard = <<<VCF
191 BEGIN:VCARD
192 VERSION:4.0
193 END:VCARD
194 VCF;
195 
196  $vcard = VObject\Reader::read($vcard);
197  $this->assertNull($vcard->preferred('EMAIL'));
198 
199  }
200 
201  function testNoUIDCardDAV() {
202 
203  $vcard = <<<VCF
204 BEGIN:VCARD
205 VERSION:4.0
206 FN:John Doe
207 END:VCARD
208 VCF;
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 
218  function testNoUIDNoCardDAV() {
219 
220  $vcard = <<<VCF
221 BEGIN:VCARD
222 VERSION:4.0
223 FN:John Doe
224 END:VCARD
225 VCF;
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
237 BEGIN:VCARD
238 VERSION:4.0
239 FN:John Doe
240 END:VCARD
241 VCF;
242  $this->assertValidate(
243  $vcard,
244  VCARD::REPAIR,
245  1,
246  'Adding a UID to a vCard property is recommended.'
247  );
248 
249  }
250 
251  function testVCard21CardDAV() {
252 
253  $vcard = <<<VCF
254 BEGIN:VCARD
255 VERSION:2.1
256 FN:John Doe
257 UID:foo
258 END:VCARD
259 VCF;
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 
269  function testVCard21NoCardDAV() {
270 
271  $vcard = <<<VCF
272 BEGIN:VCARD
273 VERSION:2.1
274 FN:John Doe
275 UID:foo
276 END:VCARD
277 VCF;
278  $this->assertValidate(
279  $vcard,
280  0,
281  0
282  );
283 
284  }
285 
286  function assertValidate($vcf, $options, $expectedLevel, $expectedMessage = null) {
287 
288  $vcal = VObject\Reader::read($vcf);
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
if($argc< 3) $input
$messages
Definition: en.php:5
assertValidateResult($input, $expectedLevel, $expectedMessage=null)
Definition: VCardTest.php:295
$tests
Definition: bench.php:104
$warning
Definition: X509warning.php:13
const VCARD30
vCard 3.0.
Definition: Document.php:44
const UNKNOWN
Unknown document type.
Definition: Document.php:24
const REPAIR
The following constants are used by the validate() method.
Definition: Node.php:27
count()
Returns the number of elements.
Definition: Node.php:177
static read($data, $options=0, $charset='UTF-8')
Parses a vCard or iCalendar object, and returns the top component.
Definition: Reader.php:42
testValidate($input, $expectedWarnings, $expectedRepairedOutput)
validateData
Definition: VCardTest.php:13
assertValidate($vcf, $options, $expectedLevel, $expectedMessage=null)
Definition: VCardTest.php:286
const VCARD21
vCard 2.1.
Definition: Document.php:39
const VCARD40
vCard 4.0.
Definition: Document.php:49
The VCard component.
Definition: VCard.php:18