File indexing completed on 2024-04-14 03:51:20

0001 /*
0002     This file is part of the KContacts framework.
0003     SPDX-FileCopyrightText: 2016-2019 Laurent Montel <montel@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include "orgtest.h"
0009 #include "org.h"
0010 #include "parametermap_p.h"
0011 #include "vcardtool_p.h"
0012 #include <QTest>
0013 
0014 OrgTest::OrgTest(QObject *parent)
0015     : QObject(parent)
0016 {
0017 }
0018 
0019 OrgTest::~OrgTest()
0020 {
0021 }
0022 
0023 void OrgTest::shouldHaveDefaultValue()
0024 {
0025     KContacts::Org org;
0026     QVERIFY(!org.isValid());
0027     QVERIFY(org.organization().isEmpty());
0028     QVERIFY(org.params().empty());
0029 }
0030 
0031 void OrgTest::shouldAssignValue()
0032 {
0033     const QString organization(QStringLiteral("fr"));
0034     KContacts::ParameterMap params;
0035     params.push_back({QStringLiteral("Foo1"), {QStringLiteral("bla1"), QStringLiteral("blo1")}});
0036     params.push_back({QStringLiteral("Foo2"), {QStringLiteral("bla2"), QStringLiteral("blo2")}});
0037     KContacts::Org org(organization);
0038     org.setParams(params);
0039     QVERIFY(org.isValid());
0040     QVERIFY(!org.organization().isEmpty());
0041     QCOMPARE(org.organization(), organization);
0042     QVERIFY(!org.params().empty());
0043     QCOMPARE(org.params(), params);
0044 }
0045 
0046 void OrgTest::shouldAssignExternal()
0047 {
0048     KContacts::Org org;
0049     const QString organization(QStringLiteral("fr"));
0050     org.setOrganization(organization);
0051     QVERIFY(org.isValid());
0052     QCOMPARE(org.organization(), organization);
0053 }
0054 
0055 void OrgTest::shouldSerialized()
0056 {
0057     KContacts::Org org;
0058     KContacts::Org result;
0059     const QString organization(QStringLiteral("fr"));
0060     org.setOrganization(organization);
0061     KContacts::ParameterMap params;
0062     params.push_back({QStringLiteral("Foo1"), {QStringLiteral("bla1"), QStringLiteral("blo1")}});
0063     params.push_back({QStringLiteral("Foo2"), {QStringLiteral("bla2"), QStringLiteral("blo2")}});
0064     org.setParams(params);
0065 
0066     QByteArray data;
0067     QDataStream s(&data, QIODevice::WriteOnly);
0068     s << org;
0069 
0070     QDataStream t(&data, QIODevice::ReadOnly);
0071     t >> result;
0072 
0073     QVERIFY(org == result);
0074 }
0075 
0076 void OrgTest::shouldEqualOrg()
0077 {
0078     KContacts::Org org;
0079     KContacts::Org result;
0080     const QString organization(QStringLiteral("fr"));
0081     org.setOrganization(organization);
0082     KContacts::ParameterMap params;
0083     params.push_back({QStringLiteral("Foo1"), {QStringLiteral("bla1"), QStringLiteral("blo1")}});
0084     params.push_back({QStringLiteral("Foo2"), {QStringLiteral("bla2"), QStringLiteral("blo2")}});
0085     org.setParams(params);
0086 
0087     result = org;
0088     QVERIFY(org == result);
0089 }
0090 
0091 void OrgTest::shouldParseOrg()
0092 {
0093     QByteArray vcarddata(
0094         "BEGIN:VCARD\n"
0095         "VERSION:3.0\n"
0096         "N:LastName;FirstName;;;\n"
0097         "UID:c80cf296-0825-4eb0-ab16-1fac1d522a33@xxxxxx.xx\n"
0098         "Org:boo\n"
0099         "REV:2015-03-14T09:24:45+00:00\n"
0100         "FN:FirstName LastName\n"
0101         "END:VCARD\n");
0102 
0103     KContacts::VCardTool vcard;
0104     const KContacts::AddresseeList lst = vcard.parseVCards(vcarddata);
0105     QCOMPARE(lst.count(), 1);
0106     QCOMPARE(lst.at(0).extraOrganizationList().count(), 1);
0107     QCOMPARE(lst.at(0).organization(), QStringLiteral("boo"));
0108     QCOMPARE(lst.at(0).extraOrganizationList().at(0).organization(), QStringLiteral("boo"));
0109 }
0110 
0111 void OrgTest::shouldParseVcardWithTwoOrg()
0112 {
0113     QByteArray vcarddata(
0114         "BEGIN:VCARD\n"
0115         "VERSION:3.0\n"
0116         "N:LastName;FirstName;;;\n"
0117         "UID:c80cf296-0825-4eb0-ab16-1fac1d522a33@xxxxxx.xx\n"
0118         "Org:boo\n"
0119         "Org:bla\n"
0120         "REV:2015-03-14T09:24:45+00:00\n"
0121         "FN:FirstName LastName\n"
0122         "END:VCARD\n");
0123 
0124     KContacts::VCardTool vcard;
0125     const KContacts::AddresseeList lst = vcard.parseVCards(vcarddata);
0126     QCOMPARE(lst.count(), 1);
0127     QCOMPARE(lst.at(0).extraOrganizationList().count(), 2);
0128     QCOMPARE(lst.at(0).extraOrganizationList().at(0).organization(), QStringLiteral("boo"));
0129     QCOMPARE(lst.at(0).extraOrganizationList().at(1).organization(), QStringLiteral("bla"));
0130 }
0131 
0132 void OrgTest::shouldCreateVCardWithSemiColon()
0133 {
0134     KContacts::AddresseeList lst;
0135     KContacts::Addressee addr;
0136     addr.setEmails(QStringList() << QStringLiteral("foo@kde.org"));
0137     addr.setUid(QStringLiteral("testuid"));
0138     KContacts::Org::List lstOrg;
0139     KContacts::Org org(QStringLiteral("fr;bla"));
0140     lstOrg << org;
0141     addr.setExtraOrganizationList(lstOrg);
0142     lst << addr;
0143     KContacts::VCardTool vcard;
0144     const QByteArray ba = vcard.exportVCards(lst, KContacts::VCard::v4_0);
0145     QByteArray expected(
0146         "BEGIN:VCARD\r\n"
0147         "VERSION:4.0\r\n"
0148         "EMAIL:foo@kde.org\r\n"
0149         "N:;;;;\r\n"
0150         "ORG:fr\\\\;bla\r\n"
0151         "UID:testuid\r\n"
0152         "END:VCARD\r\n\r\n");
0153 
0154     QCOMPARE(ba, expected);
0155 }
0156 
0157 void OrgTest::shouldParseWithoutOrg()
0158 {
0159     QByteArray vcarddata(
0160         "BEGIN:VCARD\n"
0161         "VERSION:3.0\n"
0162         "N:LastName;FirstName;;;\n"
0163         "UID:c80cf296-0825-4eb0-ab16-1fac1d522a33@xxxxxx.xx\n"
0164         "REV:2015-03-14T09:24:45+00:00\n"
0165         "FN:FirstName LastName\n"
0166         "END:VCARD\n");
0167 
0168     KContacts::VCardTool vcard;
0169     const KContacts::AddresseeList lst = vcard.parseVCards(vcarddata);
0170     QCOMPARE(lst.count(), 1);
0171     QCOMPARE(lst.at(0).extraOrganizationList().count(), 0);
0172 }
0173 
0174 void OrgTest::shouldCreateVCard()
0175 {
0176     KContacts::AddresseeList lst;
0177     KContacts::Addressee addr;
0178     addr.setEmails(QStringList() << QStringLiteral("foo@kde.org"));
0179     addr.setUid(QStringLiteral("testuid"));
0180     KContacts::Org::List lstOrg;
0181     KContacts::Org org(QStringLiteral("fr"));
0182     lstOrg << org;
0183     addr.setExtraOrganizationList(lstOrg);
0184     lst << addr;
0185     KContacts::VCardTool vcard;
0186     const QByteArray ba = vcard.exportVCards(lst, KContacts::VCard::v4_0);
0187     QByteArray expected(
0188         "BEGIN:VCARD\r\n"
0189         "VERSION:4.0\r\n"
0190         "EMAIL:foo@kde.org\r\n"
0191         "N:;;;;\r\n"
0192         "ORG:fr\r\n"
0193         "UID:testuid\r\n"
0194         "END:VCARD\r\n\r\n");
0195 
0196     QCOMPARE(ba, expected);
0197 }
0198 
0199 void OrgTest::shouldCreateVCardWithTwoOrg()
0200 {
0201     KContacts::AddresseeList lst;
0202     KContacts::Addressee addr;
0203     addr.setEmails(QStringList() << QStringLiteral("foo@kde.org"));
0204     addr.setUid(QStringLiteral("testuid"));
0205     KContacts::Org::List lstOrg;
0206     KContacts::Org org(QStringLiteral("fr"));
0207     KContacts::Org org2(QStringLiteral("fr2"));
0208     lstOrg << org << org2;
0209     addr.setExtraOrganizationList(lstOrg);
0210     lst << addr;
0211     KContacts::VCardTool vcard;
0212     const QByteArray ba = vcard.exportVCards(lst, KContacts::VCard::v4_0);
0213     QByteArray expected(
0214         "BEGIN:VCARD\r\n"
0215         "VERSION:4.0\r\n"
0216         "EMAIL:foo@kde.org\r\n"
0217         "N:;;;;\r\n"
0218         "ORG:fr\r\n"
0219         "ORG:fr2\r\n"
0220         "UID:testuid\r\n"
0221         "END:VCARD\r\n\r\n");
0222 
0223     QCOMPARE(ba, expected);
0224 }
0225 
0226 void OrgTest::shouldCreateVCardWithParameters()
0227 {
0228     KContacts::AddresseeList lst;
0229     KContacts::Addressee addr;
0230     addr.setEmails(QStringList() << QStringLiteral("foo@kde.org"));
0231     addr.setUid(QStringLiteral("testuid"));
0232     KContacts::Org::List lstOrg;
0233     KContacts::Org org(QStringLiteral("fr"));
0234     KContacts::ParameterMap params;
0235     params.push_back({QStringLiteral("Foo1"), {QStringLiteral("bla1"), QStringLiteral("blo1")}});
0236     params.push_back({QStringLiteral("Foo2"), {QStringLiteral("bla2"), QStringLiteral("blo2")}});
0237     org.setParams(params);
0238     lstOrg << org;
0239     addr.setExtraOrganizationList(lstOrg);
0240     lst << addr;
0241     KContacts::VCardTool vcard;
0242     const QByteArray ba = vcard.exportVCards(lst, KContacts::VCard::v4_0);
0243     QByteArray expected(
0244         "BEGIN:VCARD\r\n"
0245         "VERSION:4.0\r\n"
0246         "EMAIL:foo@kde.org\r\n"
0247         "N:;;;;\r\n"
0248         "ORG;FOO1=bla1,blo1;FOO2=bla2,blo2:fr\r\n"
0249         "UID:testuid\r\n"
0250         "END:VCARD\r\n\r\n");
0251     QCOMPARE(ba, expected);
0252 }
0253 
0254 void OrgTest::shouldGenerateOrgForVCard3()
0255 {
0256     KContacts::AddresseeList lst;
0257     KContacts::Addressee addr;
0258     addr.setEmails(QStringList() << QStringLiteral("foo@kde.org"));
0259     addr.setUid(QStringLiteral("testuid"));
0260     KContacts::Org::List lstOrg;
0261     KContacts::Org org(QStringLiteral("fr"));
0262     KContacts::ParameterMap params;
0263     params.push_back({QStringLiteral("Foo1"), {QStringLiteral("bla1"), QStringLiteral("blo1")}});
0264     params.push_back({QStringLiteral("Foo2"), {QStringLiteral("bla2"), QStringLiteral("blo2")}});
0265     org.setParams(params);
0266     lstOrg << org;
0267     addr.setExtraOrganizationList(lstOrg);
0268     lst << addr;
0269     KContacts::VCardTool vcard;
0270     const QByteArray ba = vcard.exportVCards(lst, KContacts::VCard::v3_0);
0271     QByteArray expected(
0272         "BEGIN:VCARD\r\n"
0273         "VERSION:3.0\r\n"
0274         "EMAIL:foo@kde.org\r\n"
0275         "N:;;;;\r\n"
0276         "ORG;FOO1=bla1,blo1;FOO2=bla2,blo2:fr\r\n"
0277         "UID:testuid\r\n"
0278         "END:VCARD\r\n\r\n");
0279     QCOMPARE(ba, expected);
0280 }
0281 
0282 QTEST_MAIN(OrgTest)
0283 
0284 #include "moc_orgtest.cpp"