Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F1841143
VCardTest.php
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Size
7 KB
Subscribers
None
VCardTest.php
View Options
<?php
namespace
Sabre\VObject\Component
;
use
Sabre\VObject
;
class
VCardTest
extends
\PHPUnit_Framework_TestCase
{
/**
* @dataProvider validateData
*/
function
testValidate
(
$input
,
$expectedWarnings
,
$expectedRepairedOutput
)
{
$vcard
=
VObject\Reader
::
read
(
$input
);
$warnings
=
$vcard
->
validate
();
$warnMsg
=
array
();
foreach
(
$warnings
as
$warning
)
{
$warnMsg
[]
=
$warning
[
'message'
];
}
$this
->
assertEquals
(
$expectedWarnings
,
$warnMsg
);
$vcard
->
validate
(
VObject\Component
::
REPAIR
);
$this
->
assertEquals
(
$expectedRepairedOutput
,
$vcard
->
serialize
()
);
}
public
function
validateData
()
{
$tests
=
array
();
// Correct
$tests
[]
=
array
(
"BEGIN:VCARD
\r\n
VERSION:4.0
\r\n
FN:John Doe
\r\n
UID:foo
\r\n
END:VCARD
\r\n
"
,
array
(),
"BEGIN:VCARD
\r\n
VERSION:4.0
\r\n
FN:John Doe
\r\n
UID:foo
\r\n
END:VCARD
\r\n
"
,
);
// No VERSION
$tests
[]
=
array
(
"BEGIN:VCARD
\r\n
FN:John Doe
\r\n
UID:foo
\r\n
END:VCARD
\r\n
"
,
array
(
'VERSION MUST appear exactly once in a VCARD component'
,
),
"BEGIN:VCARD
\r\n
VERSION:3.0
\r\n
FN:John Doe
\r\n
UID:foo
\r\n
END:VCARD
\r\n
"
,
);
// Unknown version
$tests
[]
=
array
(
"BEGIN:VCARD
\r\n
VERSION:2.2
\r\n
FN:John Doe
\r\n
UID:foo
\r\n
END:VCARD
\r\n
"
,
array
(
'Only vcard version 4.0 (RFC6350), version 3.0 (RFC2426) or version 2.1 (icm-vcard-2.1) are supported.'
,
),
"BEGIN:VCARD
\r\n
VERSION:2.1
\r\n
FN:John Doe
\r\n
UID:foo
\r\n
END:VCARD
\r\n
"
,
);
// No FN
$tests
[]
=
array
(
"BEGIN:VCARD
\r\n
VERSION:4.0
\r\n
UID:foo
\r\n
END:VCARD
\r\n
"
,
array
(
'The FN property must appear in the VCARD component exactly 1 time'
,
),
"BEGIN:VCARD
\r\n
VERSION:4.0
\r\n
UID:foo
\r\n
END:VCARD
\r\n
"
,
);
// No FN, N fallback
$tests
[]
=
array
(
"BEGIN:VCARD
\r\n
VERSION:4.0
\r\n
UID:foo
\r\n
N:Doe;John;;;;;
\r\n
END:VCARD
\r\n
"
,
array
(
'The FN property must appear in the VCARD component exactly 1 time'
,
),
"BEGIN:VCARD
\r\n
VERSION:4.0
\r\n
UID:foo
\r\n
N:Doe;John;;;;;
\r\n
FN:John Doe
\r\n
END:VCARD
\r\n
"
,
);
// No FN, N fallback, no first name
$tests
[]
=
array
(
"BEGIN:VCARD
\r\n
VERSION:4.0
\r\n
UID:foo
\r\n
N:Doe;;;;;;
\r\n
END:VCARD
\r\n
"
,
array
(
'The FN property must appear in the VCARD component exactly 1 time'
,
),
"BEGIN:VCARD
\r\n
VERSION:4.0
\r\n
UID:foo
\r\n
N:Doe;;;;;;
\r\n
FN:Doe
\r\n
END:VCARD
\r\n
"
,
);
// No FN, ORG fallback
$tests
[]
=
array
(
"BEGIN:VCARD
\r\n
VERSION:4.0
\r\n
UID:foo
\r\n
ORG:Acme Co.
\r\n
END:VCARD
\r\n
"
,
array
(
'The FN property must appear in the VCARD component exactly 1 time'
,
),
"BEGIN:VCARD
\r\n
VERSION:4.0
\r\n
UID:foo
\r\n
ORG:Acme Co.
\r\n
FN:Acme Co.
\r\n
END:VCARD
\r\n
"
,
);
return
$tests
;
}
function
testGetDocumentType
()
{
$vcard
=
new
VCard
(
array
(),
false
);
$vcard
->
VERSION
=
'2.1'
;
$this
->
assertEquals
(
VCard
::
VCARD21
,
$vcard
->
getDocumentType
());
$vcard
=
new
VCard
(
array
(),
false
);
$vcard
->
VERSION
=
'3.0'
;
$this
->
assertEquals
(
VCard
::
VCARD30
,
$vcard
->
getDocumentType
());
$vcard
=
new
VCard
(
array
(),
false
);
$vcard
->
VERSION
=
'4.0'
;
$this
->
assertEquals
(
VCard
::
VCARD40
,
$vcard
->
getDocumentType
());
$vcard
=
new
VCard
(
array
(),
false
);
$this
->
assertEquals
(
VCard
::
UNKNOWN
,
$vcard
->
getDocumentType
());
}
function
testPreferredNoPref
()
{
$vcard
=
<<<VCF
BEGIN:VCARD
VERSION:3.0
EMAIL:1@example.org
EMAIL:2@example.org
END:VCARD
VCF;
$vcard
=
VObject\Reader
::
read
(
$vcard
);
$this
->
assertEquals
(
'1@example.org'
,
$vcard
->
preferred
(
'EMAIL'
)->
getValue
());
}
function
testPreferredWithPref
()
{
$vcard
=
<<<VCF
BEGIN:VCARD
VERSION:3.0
EMAIL:1@example.org
EMAIL;TYPE=PREF:2@example.org
END:VCARD
VCF;
$vcard
=
VObject\Reader
::
read
(
$vcard
);
$this
->
assertEquals
(
'2@example.org'
,
$vcard
->
preferred
(
'EMAIL'
)->
getValue
());
}
function
testPreferredWith40Pref
()
{
$vcard
=
<<<VCF
BEGIN:VCARD
VERSION:4.0
EMAIL:1@example.org
EMAIL;PREF=3:2@example.org
EMAIL;PREF=2:3@example.org
END:VCARD
VCF;
$vcard
=
VObject\Reader
::
read
(
$vcard
);
$this
->
assertEquals
(
'3@example.org'
,
$vcard
->
preferred
(
'EMAIL'
)->
getValue
());
}
function
testPreferredNotFound
()
{
$vcard
=
<<<VCF
BEGIN:VCARD
VERSION:4.0
END:VCARD
VCF;
$vcard
=
VObject\Reader
::
read
(
$vcard
);
$this
->
assertNull
(
$vcard
->
preferred
(
'EMAIL'
));
}
function
testNoUIDCardDAV
()
{
$vcard
=
<<<VCF
BEGIN:VCARD
VERSION:4.0
FN:John Doe
END:VCARD
VCF;
$this
->
assertValidate
(
$vcard
,
VCARD
::
PROFILE_CARDDAV
,
3
,
'vCards on CardDAV servers MUST have a UID property.'
);
}
function
testNoUIDNoCardDAV
()
{
$vcard
=
<<<VCF
BEGIN:VCARD
VERSION:4.0
FN:John Doe
END:VCARD
VCF;
$this
->
assertValidate
(
$vcard
,
0
,
2
,
'Adding a UID to a vCard property is recommended.'
);
}
function
testNoUIDNoCardDAVRepair
()
{
$vcard
=
<<<VCF
BEGIN:VCARD
VERSION:4.0
FN:John Doe
END:VCARD
VCF;
$this
->
assertValidate
(
$vcard
,
VCARD
::
REPAIR
,
1
,
'Adding a UID to a vCard property is recommended.'
);
}
function
testVCard21CardDAV
()
{
$vcard
=
<<<VCF
BEGIN:VCARD
VERSION:2.1
FN:John Doe
UID:foo
END:VCARD
VCF;
$this
->
assertValidate
(
$vcard
,
VCARD
::
PROFILE_CARDDAV
,
3
,
'CardDAV servers are not allowed to accept vCard 2.1.'
);
}
function
testVCard21NoCardDAV
()
{
$vcard
=
<<<VCF
BEGIN:VCARD
VERSION:2.1
FN:John Doe
UID:foo
END:VCARD
VCF;
$this
->
assertValidate
(
$vcard
,
0
,
0
);
}
function
assertValidate
(
$vcf
,
$options
,
$expectedLevel
,
$expectedMessage
=
null
)
{
$vcal
=
VObject\Reader
::
read
(
$vcf
);
$result
=
$vcal
->
validate
(
$options
);
$this
->
assertValidateResult
(
$result
,
$expectedLevel
,
$expectedMessage
);
}
function
assertValidateResult
(
$input
,
$expectedLevel
,
$expectedMessage
=
null
)
{
$messages
=
array
();
foreach
(
$input
as
$warning
)
{
$messages
[]
=
$warning
[
'message'
];
}
if
(
$expectedLevel
===
0
)
{
$this
->
assertEquals
(
0
,
count
(
$input
),
'No validation messages were expected. We got: '
.
implode
(
', '
,
$messages
));
}
else
{
$this
->
assertEquals
(
1
,
count
(
$input
),
'We expected exactly 1 validation message, We got: '
.
implode
(
', '
,
$messages
));
$this
->
assertEquals
(
$expectedMessage
,
$input
[
0
][
'message'
]);
$this
->
assertEquals
(
$expectedLevel
,
$input
[
0
][
'level'
]);
}
}
}
File Metadata
Details
Attached
Mime Type
text/x-php
Expires
Tue, Jan 7, 2:16 PM (3 d, 6 h ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
916434
Default Alt Text
VCardTest.php (7 KB)
Attached To
rDAVCAL DokuWiki DAVCal PlugIn
Event Timeline
Log In to Comment