VCardTest.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. <?php
  2. namespace Sabre\VObject\Component;
  3. use Sabre\VObject;
  4. class VCardTest extends \PHPUnit_Framework_TestCase {
  5. /**
  6. * @dataProvider validateData
  7. */
  8. function testValidate($input, $expectedWarnings, $expectedRepairedOutput) {
  9. $vcard = VObject\Reader::read($input);
  10. $warnings = $vcard->validate();
  11. $warnMsg = array();
  12. foreach($warnings as $warning) {
  13. $warnMsg[] = $warning['message'];
  14. }
  15. $this->assertEquals($expectedWarnings, $warnMsg);
  16. $vcard->validate(VObject\Component::REPAIR);
  17. $this->assertEquals(
  18. $expectedRepairedOutput,
  19. $vcard->serialize()
  20. );
  21. }
  22. public function validateData() {
  23. $tests = array();
  24. // Correct
  25. $tests[] = array(
  26. "BEGIN:VCARD\r\nVERSION:4.0\r\nFN:John Doe\r\nUID:foo\r\nEND:VCARD\r\n",
  27. array(),
  28. "BEGIN:VCARD\r\nVERSION:4.0\r\nFN:John Doe\r\nUID:foo\r\nEND:VCARD\r\n",
  29. );
  30. // No VERSION
  31. $tests[] = array(
  32. "BEGIN:VCARD\r\nFN:John Doe\r\nUID:foo\r\nEND:VCARD\r\n",
  33. array(
  34. 'VERSION MUST appear exactly once in a VCARD component',
  35. ),
  36. "BEGIN:VCARD\r\nVERSION:3.0\r\nFN:John Doe\r\nUID:foo\r\nEND:VCARD\r\n",
  37. );
  38. // Unknown version
  39. $tests[] = array(
  40. "BEGIN:VCARD\r\nVERSION:2.2\r\nFN:John Doe\r\nUID:foo\r\nEND:VCARD\r\n",
  41. array(
  42. 'Only vcard version 4.0 (RFC6350), version 3.0 (RFC2426) or version 2.1 (icm-vcard-2.1) are supported.',
  43. ),
  44. "BEGIN:VCARD\r\nVERSION:2.1\r\nFN:John Doe\r\nUID:foo\r\nEND:VCARD\r\n",
  45. );
  46. // No FN
  47. $tests[] = array(
  48. "BEGIN:VCARD\r\nVERSION:4.0\r\nUID:foo\r\nEND:VCARD\r\n",
  49. array(
  50. 'The FN property must appear in the VCARD component exactly 1 time',
  51. ),
  52. "BEGIN:VCARD\r\nVERSION:4.0\r\nUID:foo\r\nEND:VCARD\r\n",
  53. );
  54. // No FN, N fallback
  55. $tests[] = array(
  56. "BEGIN:VCARD\r\nVERSION:4.0\r\nUID:foo\r\nN:Doe;John;;;;;\r\nEND:VCARD\r\n",
  57. array(
  58. 'The FN property must appear in the VCARD component exactly 1 time',
  59. ),
  60. "BEGIN:VCARD\r\nVERSION:4.0\r\nUID:foo\r\nN:Doe;John;;;;;\r\nFN:John Doe\r\nEND:VCARD\r\n",
  61. );
  62. // No FN, N fallback, no first name
  63. $tests[] = array(
  64. "BEGIN:VCARD\r\nVERSION:4.0\r\nUID:foo\r\nN:Doe;;;;;;\r\nEND:VCARD\r\n",
  65. array(
  66. 'The FN property must appear in the VCARD component exactly 1 time',
  67. ),
  68. "BEGIN:VCARD\r\nVERSION:4.0\r\nUID:foo\r\nN:Doe;;;;;;\r\nFN:Doe\r\nEND:VCARD\r\n",
  69. );
  70. // No FN, ORG fallback
  71. $tests[] = array(
  72. "BEGIN:VCARD\r\nVERSION:4.0\r\nUID:foo\r\nORG:Acme Co.\r\nEND:VCARD\r\n",
  73. array(
  74. 'The FN property must appear in the VCARD component exactly 1 time',
  75. ),
  76. "BEGIN:VCARD\r\nVERSION:4.0\r\nUID:foo\r\nORG:Acme Co.\r\nFN:Acme Co.\r\nEND:VCARD\r\n",
  77. );
  78. return $tests;
  79. }
  80. function testGetDocumentType() {
  81. $vcard = new VCard(array(), false);
  82. $vcard->VERSION = '2.1';
  83. $this->assertEquals(VCard::VCARD21, $vcard->getDocumentType());
  84. $vcard = new VCard(array(), false);
  85. $vcard->VERSION = '3.0';
  86. $this->assertEquals(VCard::VCARD30, $vcard->getDocumentType());
  87. $vcard = new VCard(array(), false);
  88. $vcard->VERSION = '4.0';
  89. $this->assertEquals(VCard::VCARD40, $vcard->getDocumentType());
  90. $vcard = new VCard(array(), false);
  91. $this->assertEquals(VCard::UNKNOWN, $vcard->getDocumentType());
  92. }
  93. function testPreferredNoPref() {
  94. $vcard = <<<VCF
  95. BEGIN:VCARD
  96. VERSION:3.0
  97. EMAIL:1@example.org
  98. EMAIL:2@example.org
  99. END:VCARD
  100. VCF;
  101. $vcard = VObject\Reader::read($vcard);
  102. $this->assertEquals('1@example.org', $vcard->preferred('EMAIL')->getValue());
  103. }
  104. function testPreferredWithPref() {
  105. $vcard = <<<VCF
  106. BEGIN:VCARD
  107. VERSION:3.0
  108. EMAIL:1@example.org
  109. EMAIL;TYPE=PREF:2@example.org
  110. END:VCARD
  111. VCF;
  112. $vcard = VObject\Reader::read($vcard);
  113. $this->assertEquals('2@example.org', $vcard->preferred('EMAIL')->getValue());
  114. }
  115. function testPreferredWith40Pref() {
  116. $vcard = <<<VCF
  117. BEGIN:VCARD
  118. VERSION:4.0
  119. EMAIL:1@example.org
  120. EMAIL;PREF=3:2@example.org
  121. EMAIL;PREF=2:3@example.org
  122. END:VCARD
  123. VCF;
  124. $vcard = VObject\Reader::read($vcard);
  125. $this->assertEquals('3@example.org', $vcard->preferred('EMAIL')->getValue());
  126. }
  127. function testPreferredNotFound() {
  128. $vcard = <<<VCF
  129. BEGIN:VCARD
  130. VERSION:4.0
  131. END:VCARD
  132. VCF;
  133. $vcard = VObject\Reader::read($vcard);
  134. $this->assertNull($vcard->preferred('EMAIL'));
  135. }
  136. function testNoUIDCardDAV() {
  137. $vcard = <<<VCF
  138. BEGIN:VCARD
  139. VERSION:4.0
  140. FN:John Doe
  141. END:VCARD
  142. VCF;
  143. $this->assertValidate(
  144. $vcard,
  145. VCARD::PROFILE_CARDDAV,
  146. 3,
  147. 'vCards on CardDAV servers MUST have a UID property.'
  148. );
  149. }
  150. function testNoUIDNoCardDAV() {
  151. $vcard = <<<VCF
  152. BEGIN:VCARD
  153. VERSION:4.0
  154. FN:John Doe
  155. END:VCARD
  156. VCF;
  157. $this->assertValidate(
  158. $vcard,
  159. 0,
  160. 2,
  161. 'Adding a UID to a vCard property is recommended.'
  162. );
  163. }
  164. function testNoUIDNoCardDAVRepair() {
  165. $vcard = <<<VCF
  166. BEGIN:VCARD
  167. VERSION:4.0
  168. FN:John Doe
  169. END:VCARD
  170. VCF;
  171. $this->assertValidate(
  172. $vcard,
  173. VCARD::REPAIR,
  174. 1,
  175. 'Adding a UID to a vCard property is recommended.'
  176. );
  177. }
  178. function testVCard21CardDAV() {
  179. $vcard = <<<VCF
  180. BEGIN:VCARD
  181. VERSION:2.1
  182. FN:John Doe
  183. UID:foo
  184. END:VCARD
  185. VCF;
  186. $this->assertValidate(
  187. $vcard,
  188. VCARD::PROFILE_CARDDAV,
  189. 3,
  190. 'CardDAV servers are not allowed to accept vCard 2.1.'
  191. );
  192. }
  193. function testVCard21NoCardDAV() {
  194. $vcard = <<<VCF
  195. BEGIN:VCARD
  196. VERSION:2.1
  197. FN:John Doe
  198. UID:foo
  199. END:VCARD
  200. VCF;
  201. $this->assertValidate(
  202. $vcard,
  203. 0,
  204. 0
  205. );
  206. }
  207. function assertValidate($vcf, $options, $expectedLevel, $expectedMessage = null) {
  208. $vcal = VObject\Reader::read($vcf);
  209. $result = $vcal->validate($options);
  210. $this->assertValidateResult($result, $expectedLevel, $expectedMessage);
  211. }
  212. function assertValidateResult($input, $expectedLevel, $expectedMessage = null) {
  213. $messages = array();
  214. foreach($input as $warning) {
  215. $messages[] = $warning['message'];
  216. }
  217. if ($expectedLevel === 0) {
  218. $this->assertEquals(0, count($input), 'No validation messages were expected. We got: ' . implode(', ', $messages));
  219. } else {
  220. $this->assertEquals(1, count($input), 'We expected exactly 1 validation message, We got: ' . implode(', ', $messages));
  221. $this->assertEquals($expectedMessage, $input[0]['message']);
  222. $this->assertEquals($expectedLevel, $input[0]['level']);
  223. }
  224. }
  225. }