Mercurial > hg > rc1
comparison vendor/sabre/vobject/tests/VObject/VCardConverterTest.php @ 7:430dbd5346f7
vendor sabre as distributed
| author | Charlie Root |
|---|---|
| date | Sat, 13 Jan 2018 09:06:10 -0500 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 6:cec75ba50afc | 7:430dbd5346f7 |
|---|---|
| 1 <?php | |
| 2 | |
| 3 namespace Sabre\VObject; | |
| 4 | |
| 5 class VCardConverterTest extends TestCase { | |
| 6 | |
| 7 function testConvert30to40() { | |
| 8 | |
| 9 $input = <<<IN | |
| 10 BEGIN:VCARD | |
| 11 VERSION:3.0 | |
| 12 PRODID:foo | |
| 13 FN;CHARSET=UTF-8:Steve | |
| 14 TEL;TYPE=PREF,HOME:+1 555 666 777 | |
| 15 ITEM1.TEL:+1 444 555 666 | |
| 16 ITEM1.X-ABLABEL:CustomLabel | |
| 17 PHOTO;ENCODING=b;TYPE=JPEG,HOME:Zm9v | |
| 18 PHOTO;ENCODING=b;TYPE=GIF:Zm9v | |
| 19 PHOTO;X-PARAM=FOO;ENCODING=b;TYPE=PNG:Zm9v | |
| 20 PHOTO;VALUE=URI:http://example.org/foo.png | |
| 21 X-ABShowAs:COMPANY | |
| 22 END:VCARD | |
| 23 IN; | |
| 24 | |
| 25 $output = <<<OUT | |
| 26 BEGIN:VCARD | |
| 27 VERSION:4.0 | |
| 28 FN:Steve | |
| 29 TEL;PREF=1;TYPE=HOME:+1 555 666 777 | |
| 30 ITEM1.TEL:+1 444 555 666 | |
| 31 ITEM1.X-ABLABEL:CustomLabel | |
| 32 PHOTO;TYPE=HOME:data:image/jpeg;base64,Zm9v | |
| 33 PHOTO:data:image/gif;base64,Zm9v | |
| 34 PHOTO;X-PARAM=FOO:data:image/png;base64,Zm9v | |
| 35 PHOTO:http://example.org/foo.png | |
| 36 KIND:ORG | |
| 37 END:VCARD | |
| 38 OUT; | |
| 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 | |
| 53 BEGIN:VCARD | |
| 54 VERSION:4.0 | |
| 55 FN:Steve | |
| 56 TEL;PREF=1;TYPE=HOME:+1 555 666 777 | |
| 57 PHOTO:data:image/jpeg;base64,Zm9v | |
| 58 PHOTO:data:image/gif;base64,Zm9v | |
| 59 PHOTO;X-PARAM=FOO:data:image/png;base64,Zm9v | |
| 60 PHOTO:http://example.org/foo.png | |
| 61 END:VCARD | |
| 62 | |
| 63 IN; | |
| 64 | |
| 65 $output = <<<OUT | |
| 66 BEGIN:VCARD | |
| 67 VERSION:4.0 | |
| 68 FN:Steve | |
| 69 TEL;PREF=1;TYPE=HOME:+1 555 666 777 | |
| 70 PHOTO:data:image/jpeg;base64,Zm9v | |
| 71 PHOTO:data:image/gif;base64,Zm9v | |
| 72 PHOTO;X-PARAM=FOO:data:image/png;base64,Zm9v | |
| 73 PHOTO:http://example.org/foo.png | |
| 74 END:VCARD | |
| 75 | |
| 76 OUT; | |
| 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 | |
| 91 BEGIN:VCARD | |
| 92 VERSION:2.1 | |
| 93 N:Family;Johnson | |
| 94 FN:Johnson Family | |
| 95 TEL;HOME;VOICE:555-12345-345 | |
| 96 ADR;HOME:;;100 Street Lane;Saubel Beach;ON;H0H0H0 | |
| 97 LABEL;HOME;ENCODING=QUOTED-PRINTABLE:100 Street Lane=0D=0ASaubel Beach, | |
| 98 ON H0H0H0 | |
| 99 REV:20110731T040251Z | |
| 100 UID:12345678 | |
| 101 END:VCARD | |
| 102 IN; | |
| 103 | |
| 104 $output = <<<OUT | |
| 105 BEGIN:VCARD | |
| 106 VERSION:4.0 | |
| 107 N:Family;Johnson;;; | |
| 108 FN:Johnson Family | |
| 109 TEL;TYPE=HOME,VOICE:555-12345-345 | |
| 110 ADR;TYPE=HOME:;;100 Street Lane;Saubel Beach;ON;H0H0H0; | |
| 111 REV:20110731T040251Z | |
| 112 UID:12345678 | |
| 113 END:VCARD | |
| 114 | |
| 115 OUT; | |
| 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 | |
| 130 BEGIN:VCARD | |
| 131 VERSION:3.0 | |
| 132 PRODID:foo | |
| 133 FN;CHARSET=UTF-8:Steve | |
| 134 TEL;TYPE=PREF,HOME:+1 555 666 777 | |
| 135 PHOTO;ENCODING=b;TYPE=JPEG:Zm9v | |
| 136 PHOTO;ENCODING=b;TYPE=GIF:Zm9v | |
| 137 PHOTO;X-PARAM=FOO;ENCODING=b;TYPE=PNG:Zm9v | |
| 138 PHOTO;VALUE=URI:http://example.org/foo.png | |
| 139 END:VCARD | |
| 140 | |
| 141 IN; | |
| 142 | |
| 143 $output = <<<OUT | |
| 144 BEGIN:VCARD | |
| 145 VERSION:3.0 | |
| 146 PRODID:foo | |
| 147 FN;CHARSET=UTF-8:Steve | |
| 148 TEL;TYPE=PREF,HOME:+1 555 666 777 | |
| 149 PHOTO;ENCODING=b;TYPE=JPEG:Zm9v | |
| 150 PHOTO;ENCODING=b;TYPE=GIF:Zm9v | |
| 151 PHOTO;X-PARAM=FOO;ENCODING=b;TYPE=PNG:Zm9v | |
| 152 PHOTO;VALUE=URI:http://example.org/foo.png | |
| 153 END:VCARD | |
| 154 | |
| 155 OUT; | |
| 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 | |
| 170 BEGIN:VCARD | |
| 171 VERSION:4.0 | |
| 172 PRODID:foo | |
| 173 FN:Steve | |
| 174 TEL;PREF=1;TYPE=HOME:+1 555 666 777 | |
| 175 PHOTO:data:image/jpeg;base64,Zm9v | |
| 176 PHOTO:data:image/gif,foo | |
| 177 PHOTO;X-PARAM=FOO:data:image/png;base64,Zm9v | |
| 178 PHOTO:http://example.org/foo.png | |
| 179 KIND:ORG | |
| 180 END:VCARD | |
| 181 | |
| 182 IN; | |
| 183 | |
| 184 $output = <<<OUT | |
| 185 BEGIN:VCARD | |
| 186 VERSION:3.0 | |
| 187 FN:Steve | |
| 188 TEL;TYPE=PREF,HOME:+1 555 666 777 | |
| 189 PHOTO;ENCODING=b;TYPE=JPEG:Zm9v | |
| 190 PHOTO;ENCODING=b;TYPE=GIF:Zm9v | |
| 191 PHOTO;ENCODING=b;TYPE=PNG;X-PARAM=FOO:Zm9v | |
| 192 PHOTO;VALUE=URI:http://example.org/foo.png | |
| 193 X-ABSHOWAS:COMPANY | |
| 194 END:VCARD | |
| 195 | |
| 196 OUT; | |
| 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 | |
| 211 BEGIN:VCARD | |
| 212 VERSION:3.0 | |
| 213 PRODID:foo | |
| 214 X-ADDRESSBOOKSERVER-KIND:GROUP | |
| 215 END:VCARD | |
| 216 | |
| 217 IN; | |
| 218 | |
| 219 $output = <<<OUT | |
| 220 BEGIN:VCARD | |
| 221 VERSION:4.0 | |
| 222 KIND:GROUP | |
| 223 END:VCARD | |
| 224 | |
| 225 OUT; | |
| 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 | |
| 237 BEGIN:VCARD | |
| 238 VERSION:3.0 | |
| 239 X-ADDRESSBOOKSERVER-KIND:GROUP | |
| 240 END:VCARD | |
| 241 | |
| 242 OUT; | |
| 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 | |
| 257 BEGIN:VCARD | |
| 258 VERSION:3.0 | |
| 259 PRODID:foo | |
| 260 BDAY;X-APPLE-OMIT-YEAR=1604:1604-04-16 | |
| 261 END:VCARD | |
| 262 | |
| 263 IN; | |
| 264 | |
| 265 $output = <<<OUT | |
| 266 BEGIN:VCARD | |
| 267 VERSION:4.0 | |
| 268 BDAY:--04-16 | |
| 269 END:VCARD | |
| 270 | |
| 271 OUT; | |
| 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 | |
| 283 BEGIN:VCARD | |
| 284 VERSION:3.0 | |
| 285 BDAY;X-APPLE-OMIT-YEAR=1604:1604-04-16 | |
| 286 END:VCARD | |
| 287 | |
| 288 OUT; | |
| 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 | |
| 306 BEGIN:VCARD | |
| 307 VERSION:4.2 | |
| 308 PRODID:foo | |
| 309 FN;CHARSET=UTF-8:Steve | |
| 310 TEL;TYPE=PREF,HOME:+1 555 666 777 | |
| 311 ITEM1.TEL:+1 444 555 666 | |
| 312 ITEM1.X-ABLABEL:CustomLabel | |
| 313 PHOTO;ENCODING=b;TYPE=JPEG,HOME:Zm9v | |
| 314 PHOTO;ENCODING=b;TYPE=GIF:Zm9v | |
| 315 PHOTO;X-PARAM=FOO;ENCODING=b;TYPE=PNG:Zm9v | |
| 316 PHOTO;VALUE=URI:http://example.org/foo.png | |
| 317 X-ABShowAs:COMPANY | |
| 318 END:VCARD | |
| 319 | |
| 320 IN; | |
| 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 | |
| 333 BEGIN:VCARD | |
| 334 VERSION:3.0 | |
| 335 PRODID:foo | |
| 336 END:VCARD | |
| 337 | |
| 338 IN; | |
| 339 | |
| 340 $vcard = Reader::read($input); | |
| 341 $vcard->convert(Document::VCARD21); | |
| 342 | |
| 343 } | |
| 344 | |
| 345 function testConvertIndividualCard() { | |
| 346 | |
| 347 $input = <<<IN | |
| 348 BEGIN:VCARD | |
| 349 VERSION:4.0 | |
| 350 PRODID:foo | |
| 351 KIND:INDIVIDUAL | |
| 352 END:VCARD | |
| 353 | |
| 354 IN; | |
| 355 | |
| 356 $output = <<<OUT | |
| 357 BEGIN:VCARD | |
| 358 VERSION:3.0 | |
| 359 END:VCARD | |
| 360 | |
| 361 OUT; | |
| 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 | |
| 373 BEGIN:VCARD | |
| 374 VERSION:4.0 | |
| 375 END:VCARD | |
| 376 | |
| 377 OUT; | |
| 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 | |
| 392 BEGIN:VCARD | |
| 393 VERSION:4.0 | |
| 394 ITEM1.ANNIVERSARY:20081210 | |
| 395 END:VCARD | |
| 396 | |
| 397 IN; | |
| 398 | |
| 399 $output = <<<'OUT' | |
| 400 BEGIN:VCARD | |
| 401 VERSION:3.0 | |
| 402 ITEM1.X-ABDATE;VALUE=DATE-AND-OR-TIME:20081210 | |
| 403 ITEM1.X-ABLABEL:_$!<Anniversary>!$_ | |
| 404 ITEM1.X-ANNIVERSARY;VALUE=DATE-AND-OR-TIME:20081210 | |
| 405 END:VCARD | |
| 406 | |
| 407 OUT; | |
| 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 | |
| 439 BEGIN:VCARD | |
| 440 VERSION:4.0 | |
| 441 ITEM1.ANNIVERSARY:20081210 | |
| 442 ITEM2.ANNIVERSARY:20091210 | |
| 443 ITEM3.ANNIVERSARY:20101210 | |
| 444 END:VCARD | |
| 445 | |
| 446 IN; | |
| 447 | |
| 448 $output = <<<'OUT' | |
| 449 BEGIN:VCARD | |
| 450 VERSION:3.0 | |
| 451 ITEM1.X-ABDATE;VALUE=DATE-AND-OR-TIME:20081210 | |
| 452 ITEM1.X-ABLABEL:_$!<Anniversary>!$_ | |
| 453 ITEM1.X-ANNIVERSARY;VALUE=DATE-AND-OR-TIME:20081210 | |
| 454 ITEM2.X-ABDATE;VALUE=DATE-AND-OR-TIME:20091210 | |
| 455 ITEM2.X-ABLABEL:_$!<Anniversary>!$_ | |
| 456 ITEM2.X-ANNIVERSARY;VALUE=DATE-AND-OR-TIME:20091210 | |
| 457 ITEM3.X-ABDATE;VALUE=DATE-AND-OR-TIME:20101210 | |
| 458 ITEM3.X-ABLABEL:_$!<Anniversary>!$_ | |
| 459 ITEM3.X-ANNIVERSARY;VALUE=DATE-AND-OR-TIME:20101210 | |
| 460 END:VCARD | |
| 461 | |
| 462 OUT; | |
| 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 | |
| 494 BEGIN:VCARD | |
| 495 VERSION:3.0 | |
| 496 UID:foo | |
| 497 N:Doe;John;;; | |
| 498 FN:John Doe | |
| 499 item1.X-ABDATE;type=pref:2008-12-11 | |
| 500 END:VCARD | |
| 501 | |
| 502 VCF; | |
| 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 | |
| 516 BEGIN:VCARD | |
| 517 VERSION:4.0 | |
| 518 PRODID:-//Sabre//Sabre VObject $version//EN | |
| 519 UID:foo | |
| 520 N:Doe;John;;; | |
| 521 FN:John Doe | |
| 522 ITEM1.X-ABDATE;PREF=1:2008-12-11 | |
| 523 END:VCARD | |
| 524 | |
| 525 VCF; | |
| 526 | |
| 527 $this->assertEquals($expected, str_replace("\r","", $vcard)); | |
| 528 | |
| 529 } | |
| 530 | |
| 531 } |
