File indexing completed on 2024-03-24 03:56:12

0001 /*
0002     This file is part of the KContacts framework.
0003     SPDX-FileCopyrightText: 2015-2019 Laurent Montel <montel@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include "customidentifiertest.h"
0009 #include "addressee.h"
0010 #include "vcardtool_p.h"
0011 #include <QTest>
0012 
0013 CustomIdentifierTest::CustomIdentifierTest(QObject *parent)
0014     : QObject(parent)
0015 {
0016 }
0017 
0018 CustomIdentifierTest::~CustomIdentifierTest()
0019 {
0020 }
0021 
0022 void CustomIdentifierTest::shouldHaveEmptyCustoms()
0023 {
0024     KContacts::Addressee addresseeWithMail;
0025     addresseeWithMail.addEmail(QStringLiteral("foo@bar.org"));
0026     QVERIFY(!addresseeWithMail.isEmpty());
0027     QVERIFY(addresseeWithMail.customs().isEmpty());
0028 }
0029 
0030 void CustomIdentifierTest::shouldExportVcard3()
0031 {
0032     KContacts::AddresseeList lst;
0033     KContacts::Addressee addr;
0034     addr.setEmails(QStringList() << QStringLiteral("foo@kde.org") << QStringLiteral("bla@kde.org"));
0035     addr.setUid(QStringLiteral("testuid"));
0036     addr.insertCustom(QStringLiteral("KADDRESSBOOK"), QStringLiteral("X-SpousesName"), QStringLiteral("foo"));
0037     lst << addr;
0038     KContacts::VCardTool vcard;
0039     QByteArray ba = vcard.exportVCards(lst, KContacts::VCard::v3_0);
0040     QByteArray expected(
0041         "BEGIN:VCARD\r\n"
0042         "VERSION:3.0\r\n"
0043         "EMAIL:foo@kde.org\r\n"
0044         "EMAIL:bla@kde.org\r\n"
0045         "N:;;;;\r\n"
0046         "UID:testuid\r\n"
0047         "X-KADDRESSBOOK-X-SpousesName:foo\r\n"
0048         "END:VCARD\r\n\r\n");
0049 
0050     QCOMPARE(ba, expected);
0051 
0052     addr.insertCustom(QStringLiteral("KADDRESSBOOK"), QStringLiteral("X-ANNIVERSARY"), QStringLiteral("19960415"));
0053     lst.clear();
0054     lst << addr;
0055     ba = vcard.exportVCards(lst, KContacts::VCard::v3_0);
0056     expected = QByteArray(
0057         "BEGIN:VCARD\r\n"
0058         "VERSION:3.0\r\n"
0059         "EMAIL:foo@kde.org\r\n"
0060         "EMAIL:bla@kde.org\r\n"
0061         "N:;;;;\r\n"
0062         "UID:testuid\r\n"
0063         "X-KADDRESSBOOK-X-ANNIVERSARY:19960415\r\n"
0064         "X-KADDRESSBOOK-X-SpousesName:foo\r\n"
0065         "END:VCARD\r\n\r\n");
0066 
0067     QCOMPARE(ba, expected);
0068 }
0069 
0070 void CustomIdentifierTest::shouldExportVcard4()
0071 {
0072     // TODO don't work to export custom!
0073     KContacts::AddresseeList lst;
0074     KContacts::Addressee addr;
0075     addr.setEmails(QStringList() << QStringLiteral("foo@kde.org") << QStringLiteral("bla@kde.org"));
0076     addr.setUid(QStringLiteral("testuid"));
0077     addr.insertCustom(QStringLiteral("KADDRESSBOOK"), QStringLiteral("X-SpousesName"), QStringLiteral("foo"));
0078     lst << addr;
0079     KContacts::VCardTool vcard;
0080     QByteArray ba = vcard.exportVCards(lst, KContacts::VCard::v4_0);
0081     QByteArray expected(
0082         "BEGIN:VCARD\r\n"
0083         "VERSION:4.0\r\n"
0084         "EMAIL:foo@kde.org\r\n"
0085         "EMAIL:bla@kde.org\r\n"
0086         "N:;;;;\r\n"
0087         "RELATED;TYPE=spouse;VALUE=foo:;\r\n"
0088         "UID:testuid\r\n"
0089         "END:VCARD\r\n\r\n");
0090 
0091     QCOMPARE(ba, expected);
0092 
0093     QDate dt(12, 9, 3);
0094     addr.insertCustom(QStringLiteral("KADDRESSBOOK"), QStringLiteral("X-ANNIVERSARY"), dt.toString(Qt::ISODate));
0095     lst.clear();
0096     lst << addr;
0097     ba = vcard.exportVCards(lst, KContacts::VCard::v4_0);
0098     expected = QByteArray(
0099         "BEGIN:VCARD\r\n"
0100         "VERSION:4.0\r\n"
0101         "ANNIVERSARY:00120903\r\n"
0102         "EMAIL:foo@kde.org\r\n"
0103         "EMAIL:bla@kde.org\r\n"
0104         "N:;;;;\r\n"
0105         "RELATED;TYPE=spouse;VALUE=foo:;\r\n"
0106         "UID:testuid\r\n"
0107         "END:VCARD\r\n\r\n");
0108 
0109     QCOMPARE(ba, expected);
0110 }
0111 
0112 QTEST_MAIN(CustomIdentifierTest)
0113 
0114 #include "moc_customidentifiertest.cpp"