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