1<?php
2
3namespace Sabre\VObject;
4
5class VCardConverterTest extends TestCase {
6
7    function testConvert30to40() {
8
9        $input = <<<IN
10BEGIN:VCARD
11VERSION:3.0
12PRODID:foo
13FN;CHARSET=UTF-8:Steve
14TEL;TYPE=PREF,HOME:+1 555 666 777
15ITEM1.TEL:+1 444 555 666
16ITEM1.X-ABLABEL:CustomLabel
17PHOTO;ENCODING=b;TYPE=JPEG,HOME:Zm9v
18PHOTO;ENCODING=b;TYPE=GIF:Zm9v
19PHOTO;X-PARAM=FOO;ENCODING=b;TYPE=PNG:Zm9v
20PHOTO;VALUE=URI:http://example.org/foo.png
21X-ABShowAs:COMPANY
22END:VCARD
23IN;
24
25        $output = <<<OUT
26BEGIN:VCARD
27VERSION:4.0
28FN:Steve
29TEL;PREF=1;TYPE=HOME:+1 555 666 777
30ITEM1.TEL:+1 444 555 666
31ITEM1.X-ABLABEL:CustomLabel
32PHOTO;TYPE=HOME:data:image/jpeg;base64,Zm9v
33PHOTO:data:image/gif;base64,Zm9v
34PHOTO;X-PARAM=FOO:data:image/png;base64,Zm9v
35PHOTO:http://example.org/foo.png
36KIND:ORG
37END:VCARD
38OUT;
39
40        $vcard = Reader::read($input);
41        $vcard = $vcard->convert(Document::VCARD40);
42
43        $this->assertVObjEquals(
44            $output,
45            $vcard
46        );
47
48    }
49
50    function testConvert40to40() {
51
52        $input = <<<IN
53BEGIN:VCARD
54VERSION:4.0
55FN:Steve
56TEL;PREF=1;TYPE=HOME:+1 555 666 777
57PHOTO:data:image/jpeg;base64,Zm9v
58PHOTO:data:image/gif;base64,Zm9v
59PHOTO;X-PARAM=FOO:data:image/png;base64,Zm9v
60PHOTO:http://example.org/foo.png
61END:VCARD
62
63IN;
64
65        $output = <<<OUT
66BEGIN:VCARD
67VERSION:4.0
68FN:Steve
69TEL;PREF=1;TYPE=HOME:+1 555 666 777
70PHOTO:data:image/jpeg;base64,Zm9v
71PHOTO:data:image/gif;base64,Zm9v
72PHOTO;X-PARAM=FOO:data:image/png;base64,Zm9v
73PHOTO:http://example.org/foo.png
74END:VCARD
75
76OUT;
77
78        $vcard = Reader::read($input);
79        $vcard = $vcard->convert(Document::VCARD40);
80
81        $this->assertVObjEquals(
82            $output,
83            $vcard
84        );
85
86    }
87
88    function testConvert21to40() {
89
90        $input = <<<IN
91BEGIN:VCARD
92VERSION:2.1
93N:Family;Johnson
94FN:Johnson Family
95TEL;HOME;VOICE:555-12345-345
96ADR;HOME:;;100 Street Lane;Saubel Beach;ON;H0H0H0
97LABEL;HOME;ENCODING=QUOTED-PRINTABLE:100 Street Lane=0D=0ASaubel Beach,
98 ON H0H0H0
99REV:20110731T040251Z
100UID:12345678
101END:VCARD
102IN;
103
104        $output = <<<OUT
105BEGIN:VCARD
106VERSION:4.0
107N:Family;Johnson;;;
108FN:Johnson Family
109TEL;TYPE=HOME,VOICE:555-12345-345
110ADR;TYPE=HOME:;;100 Street Lane;Saubel Beach;ON;H0H0H0;
111REV:20110731T040251Z
112UID:12345678
113END:VCARD
114
115OUT;
116
117        $vcard = Reader::read($input);
118        $vcard = $vcard->convert(Document::VCARD40);
119
120        $this->assertVObjEquals(
121            $output,
122            $vcard
123        );
124
125    }
126
127    function testConvert30to30() {
128
129        $input = <<<IN
130BEGIN:VCARD
131VERSION:3.0
132PRODID:foo
133FN;CHARSET=UTF-8:Steve
134TEL;TYPE=PREF,HOME:+1 555 666 777
135PHOTO;ENCODING=b;TYPE=JPEG:Zm9v
136PHOTO;ENCODING=b;TYPE=GIF:Zm9v
137PHOTO;X-PARAM=FOO;ENCODING=b;TYPE=PNG:Zm9v
138PHOTO;VALUE=URI:http://example.org/foo.png
139END:VCARD
140
141IN;
142
143        $output = <<<OUT
144BEGIN:VCARD
145VERSION:3.0
146PRODID:foo
147FN;CHARSET=UTF-8:Steve
148TEL;TYPE=PREF,HOME:+1 555 666 777
149PHOTO;ENCODING=b;TYPE=JPEG:Zm9v
150PHOTO;ENCODING=b;TYPE=GIF:Zm9v
151PHOTO;X-PARAM=FOO;ENCODING=b;TYPE=PNG:Zm9v
152PHOTO;VALUE=URI:http://example.org/foo.png
153END:VCARD
154
155OUT;
156
157        $vcard = Reader::read($input);
158        $vcard = $vcard->convert(Document::VCARD30);
159
160        $this->assertVObjEquals(
161            $output,
162            $vcard
163        );
164
165    }
166
167    function testConvert40to30() {
168
169        $input = <<<IN
170BEGIN:VCARD
171VERSION:4.0
172PRODID:foo
173FN:Steve
174TEL;PREF=1;TYPE=HOME:+1 555 666 777
175PHOTO:data:image/jpeg;base64,Zm9v
176PHOTO:data:image/gif,foo
177PHOTO;X-PARAM=FOO:data:image/png;base64,Zm9v
178PHOTO:http://example.org/foo.png
179KIND:ORG
180END:VCARD
181
182IN;
183
184        $output = <<<OUT
185BEGIN:VCARD
186VERSION:3.0
187FN:Steve
188TEL;TYPE=PREF,HOME:+1 555 666 777
189PHOTO;ENCODING=b;TYPE=JPEG:Zm9v
190PHOTO;ENCODING=b;TYPE=GIF:Zm9v
191PHOTO;ENCODING=b;TYPE=PNG;X-PARAM=FOO:Zm9v
192PHOTO;VALUE=URI:http://example.org/foo.png
193X-ABSHOWAS:COMPANY
194END:VCARD
195
196OUT;
197
198        $vcard = Reader::read($input);
199        $vcard = $vcard->convert(Document::VCARD30);
200
201        $this->assertVObjEquals(
202            $output,
203            $vcard
204        );
205
206    }
207
208    function testConvertGroupCard() {
209
210        $input = <<<IN
211BEGIN:VCARD
212VERSION:3.0
213PRODID:foo
214X-ADDRESSBOOKSERVER-KIND:GROUP
215END:VCARD
216
217IN;
218
219        $output = <<<OUT
220BEGIN:VCARD
221VERSION:4.0
222KIND:GROUP
223END:VCARD
224
225OUT;
226
227        $vcard = Reader::read($input);
228        $vcard = $vcard->convert(Document::VCARD40);
229
230        $this->assertVObjEquals(
231            $output,
232            $vcard
233        );
234
235        $input = $output;
236        $output = <<<OUT
237BEGIN:VCARD
238VERSION:3.0
239X-ADDRESSBOOKSERVER-KIND:GROUP
240END:VCARD
241
242OUT;
243
244        $vcard = Reader::read($input);
245        $vcard = $vcard->convert(Document::VCARD30);
246
247        $this->assertVObjEquals(
248            $output,
249            $vcard
250        );
251
252    }
253
254    function testBDAYConversion() {
255
256        $input = <<<IN
257BEGIN:VCARD
258VERSION:3.0
259PRODID:foo
260BDAY;X-APPLE-OMIT-YEAR=1604:1604-04-16
261END:VCARD
262
263IN;
264
265        $output = <<<OUT
266BEGIN:VCARD
267VERSION:4.0
268BDAY:--04-16
269END:VCARD
270
271OUT;
272
273        $vcard = Reader::read($input);
274        $vcard = $vcard->convert(Document::VCARD40);
275
276        $this->assertVObjEquals(
277            $output,
278            $vcard
279        );
280
281        $input = $output;
282        $output = <<<OUT
283BEGIN:VCARD
284VERSION:3.0
285BDAY;X-APPLE-OMIT-YEAR=1604:1604-04-16
286END:VCARD
287
288OUT;
289
290        $vcard = Reader::read($input);
291        $vcard = $vcard->convert(Document::VCARD30);
292
293        $this->assertVObjEquals(
294            $output,
295            $vcard
296        );
297
298    }
299
300    /**
301     * @expectedException InvalidArgumentException
302     */
303    function testUnknownSourceVCardVersion() {
304
305        $input = <<<IN
306BEGIN:VCARD
307VERSION:4.2
308PRODID:foo
309FN;CHARSET=UTF-8:Steve
310TEL;TYPE=PREF,HOME:+1 555 666 777
311ITEM1.TEL:+1 444 555 666
312ITEM1.X-ABLABEL:CustomLabel
313PHOTO;ENCODING=b;TYPE=JPEG,HOME:Zm9v
314PHOTO;ENCODING=b;TYPE=GIF:Zm9v
315PHOTO;X-PARAM=FOO;ENCODING=b;TYPE=PNG:Zm9v
316PHOTO;VALUE=URI:http://example.org/foo.png
317X-ABShowAs:COMPANY
318END:VCARD
319
320IN;
321
322        $vcard = Reader::read($input);
323        $vcard->convert(Document::VCARD40);
324
325    }
326
327    /**
328     * @expectedException InvalidArgumentException
329     */
330    function testUnknownTargetVCardVersion() {
331
332        $input = <<<IN
333BEGIN:VCARD
334VERSION:3.0
335PRODID:foo
336END:VCARD
337
338IN;
339
340        $vcard = Reader::read($input);
341        $vcard->convert(Document::VCARD21);
342
343    }
344
345    function testConvertIndividualCard() {
346
347        $input = <<<IN
348BEGIN:VCARD
349VERSION:4.0
350PRODID:foo
351KIND:INDIVIDUAL
352END:VCARD
353
354IN;
355
356        $output = <<<OUT
357BEGIN:VCARD
358VERSION:3.0
359END:VCARD
360
361OUT;
362
363        $vcard = Reader::read($input);
364        $vcard = $vcard->convert(Document::VCARD30);
365
366        $this->assertVObjEquals(
367            $output,
368            $vcard
369        );
370
371        $input = $output;
372        $output = <<<OUT
373BEGIN:VCARD
374VERSION:4.0
375END:VCARD
376
377OUT;
378
379        $vcard = Reader::read($input);
380        $vcard = $vcard->convert(Document::VCARD40);
381
382        $this->assertVObjEquals(
383            $output,
384            $vcard
385        );
386
387    }
388
389    function testAnniversary() {
390
391        $input = <<<IN
392BEGIN:VCARD
393VERSION:4.0
394ITEM1.ANNIVERSARY:20081210
395END:VCARD
396
397IN;
398
399        $output = <<<'OUT'
400BEGIN:VCARD
401VERSION:3.0
402ITEM1.X-ABDATE;VALUE=DATE-AND-OR-TIME:20081210
403ITEM1.X-ABLABEL:_$!<Anniversary>!$_
404ITEM1.X-ANNIVERSARY;VALUE=DATE-AND-OR-TIME:20081210
405END:VCARD
406
407OUT;
408
409        $vcard = Reader::read($input);
410        $vcard = $vcard->convert(Document::VCARD30);
411
412        $this->assertVObjEquals(
413            $output,
414            $vcard
415        );
416
417        // Swapping input and output
418        list(
419            $input,
420            $output
421        ) = array(
422            $output,
423            $input
424        );
425
426        $vcard = Reader::read($input);
427        $vcard = $vcard->convert(Document::VCARD40);
428
429        $this->assertVObjEquals(
430            $output,
431            $vcard
432        );
433
434    }
435
436    function testMultipleAnniversaries() {
437
438        $input = <<<IN
439BEGIN:VCARD
440VERSION:4.0
441ITEM1.ANNIVERSARY:20081210
442ITEM2.ANNIVERSARY:20091210
443ITEM3.ANNIVERSARY:20101210
444END:VCARD
445
446IN;
447
448        $output = <<<'OUT'
449BEGIN:VCARD
450VERSION:3.0
451ITEM1.X-ABDATE;VALUE=DATE-AND-OR-TIME:20081210
452ITEM1.X-ABLABEL:_$!<Anniversary>!$_
453ITEM1.X-ANNIVERSARY;VALUE=DATE-AND-OR-TIME:20081210
454ITEM2.X-ABDATE;VALUE=DATE-AND-OR-TIME:20091210
455ITEM2.X-ABLABEL:_$!<Anniversary>!$_
456ITEM2.X-ANNIVERSARY;VALUE=DATE-AND-OR-TIME:20091210
457ITEM3.X-ABDATE;VALUE=DATE-AND-OR-TIME:20101210
458ITEM3.X-ABLABEL:_$!<Anniversary>!$_
459ITEM3.X-ANNIVERSARY;VALUE=DATE-AND-OR-TIME:20101210
460END:VCARD
461
462OUT;
463
464        $vcard = Reader::read($input);
465        $vcard = $vcard->convert(Document::VCARD30);
466
467        $this->assertVObjEquals(
468            $output,
469            $vcard
470        );
471
472        // Swapping input and output
473        list(
474            $input,
475            $output
476        ) = array(
477            $output,
478            $input
479        );
480
481        $vcard = Reader::read($input);
482        $vcard = $vcard->convert(Document::VCARD40);
483
484        $this->assertVObjEquals(
485            $output,
486            $vcard
487        );
488
489    }
490
491    function testNoLabel() {
492
493      $input = <<<VCF
494BEGIN:VCARD
495VERSION:3.0
496UID:foo
497N:Doe;John;;;
498FN:John Doe
499item1.X-ABDATE;type=pref:2008-12-11
500END:VCARD
501
502VCF;
503
504      $vcard = Reader::read($input);
505
506      $this->assertInstanceOf('Sabre\\VObject\\Component\\VCard', $vcard);
507      $vcard = $vcard->convert(Document::VCARD40);
508      $vcard = $vcard->serialize();
509
510      $converted = Reader::read($vcard);
511      $converted->validate();
512
513      $version = Version::VERSION;
514
515      $expected = <<<VCF
516BEGIN:VCARD
517VERSION:4.0
518PRODID:-//Sabre//Sabre VObject $version//EN
519UID:foo
520N:Doe;John;;;
521FN:John Doe
522ITEM1.X-ABDATE;PREF=1:2008-12-11
523END:VCARD
524
525VCF;
526
527      $this->assertEquals($expected, str_replace("\r","", $vcard));
528
529    }
530
531}
532