File indexing completed on 2024-03-03 04:01:26

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 "relatedtest.h"
0009 
0010 #include "parametermap_p.h"
0011 #include "related.h"
0012 #include "vcardtool_p.h"
0013 
0014 #include <QTest>
0015 
0016 RelatedTest::RelatedTest(QObject *parent)
0017     : QObject(parent)
0018 {
0019 }
0020 
0021 RelatedTest::~RelatedTest()
0022 {
0023 }
0024 
0025 void RelatedTest::shouldHaveDefaultValue()
0026 {
0027     KContacts::Related related;
0028     QVERIFY(!related.isValid());
0029     QVERIFY(related.related().isEmpty());
0030     QVERIFY(related.params().empty());
0031 }
0032 
0033 void RelatedTest::shouldAssignValue()
0034 {
0035     const QString relatedTo(QStringLiteral("friend"));
0036     KContacts::ParameterMap params;
0037     params.push_back({QStringLiteral("Foo1"), {QStringLiteral("bla1"), QStringLiteral("blo1")}});
0038     params.push_back({QStringLiteral("Foo2"), {QStringLiteral("bla2"), QStringLiteral("blo2")}});
0039     KContacts::Related related(relatedTo);
0040     related.setParams(params);
0041     QVERIFY(related.isValid());
0042     QVERIFY(!related.related().isEmpty());
0043     QCOMPARE(related.related(), relatedTo);
0044     QVERIFY(!related.params().empty());
0045     QCOMPARE(related.params(), params);
0046 }
0047 
0048 void RelatedTest::shouldAssignExternal()
0049 {
0050     KContacts::Related related;
0051     const QString relatedTo(QStringLiteral("friend"));
0052     related.setRelated(relatedTo);
0053     QVERIFY(related.isValid());
0054     QCOMPARE(related.related(), relatedTo);
0055 }
0056 
0057 void RelatedTest::shouldSerialized()
0058 {
0059     KContacts::Related related;
0060     KContacts::Related result;
0061     const QString relatedTo(QStringLiteral("friend"));
0062     related.setRelated(relatedTo);
0063     KContacts::ParameterMap params;
0064     params.push_back({QStringLiteral("Foo1"), {QStringLiteral("bla1"), QStringLiteral("blo1")}});
0065     params.push_back({QStringLiteral("Foo2"), {QStringLiteral("bla2"), QStringLiteral("blo2")}});
0066     related.setParams(params);
0067 
0068     QByteArray data;
0069     QDataStream s(&data, QIODevice::WriteOnly);
0070     s << related;
0071 
0072     QDataStream t(&data, QIODevice::ReadOnly);
0073     t >> result;
0074 
0075     QVERIFY(related == result);
0076 }
0077 
0078 void RelatedTest::shouldEqualRelated()
0079 {
0080     KContacts::Related related;
0081     KContacts::Related result;
0082     const QString relatedTo(QStringLiteral("friend"));
0083     related.setRelated(relatedTo);
0084     KContacts::ParameterMap params;
0085     params.push_back({QStringLiteral("Foo1"), {QStringLiteral("bla1"), QStringLiteral("blo1")}});
0086     params.push_back({QStringLiteral("Foo2"), {QStringLiteral("bla2"), QStringLiteral("blo2")}});
0087     related.setParams(params);
0088 
0089     result = related;
0090     QVERIFY(related == result);
0091 }
0092 
0093 void RelatedTest::shouldParseRelated()
0094 {
0095     QByteArray vcarddata(
0096         "BEGIN:VCARD\n"
0097         "VERSION:4.0\n"
0098         "N:LastName;FirstName;;;\n"
0099         "UID:c80cf296-0825-4eb0-ab16-1fac1d522a33@xxxxxx.xx\n"
0100         "RELATED:friend\n"
0101         "REV:2015-03-14T09:24:45+00:00\n"
0102         "FN:FirstName LastName\n"
0103         "END:VCARD\n");
0104 
0105     KContacts::VCardTool vcard;
0106     const KContacts::AddresseeList lst = vcard.parseVCards(vcarddata);
0107     QCOMPARE(lst.count(), 1);
0108     QCOMPARE(lst.at(0).relationships().count(), 1);
0109     const KContacts::Related related = lst.at(0).relationships().at(0);
0110     QCOMPARE(related.related(), QStringLiteral("friend"));
0111 }
0112 
0113 void RelatedTest::shouldParseRelatedWithArgument()
0114 {
0115     QByteArray vcarddata(
0116         "BEGIN:VCARD\n"
0117         "VERSION:4.0\n"
0118         "N:LastName;FirstName;;;\n"
0119         "UID:c80cf296-0825-4eb0-ab16-1fac1d522a33@xxxxxx.xx\n"
0120         "RELATED;FOO1=bla1,blo1;FOO2=bla2,blo2:friend\n"
0121         "REV:2015-03-14T09:24:45+00:00\n"
0122         "FN:FirstName LastName\n"
0123         "END:VCARD\n");
0124 
0125     KContacts::VCardTool vcard;
0126     const KContacts::AddresseeList lst = vcard.parseVCards(vcarddata);
0127     QCOMPARE(lst.count(), 1);
0128     QCOMPARE(lst.at(0).relationships().count(), 1);
0129     const KContacts::Related related = lst.at(0).relationships().at(0);
0130     QCOMPARE(related.related(), QStringLiteral("friend"));
0131     QCOMPARE(related.params().size(), 2);
0132 }
0133 
0134 void RelatedTest::shouldParseWithoutRelated()
0135 {
0136     QByteArray vcarddata(
0137         "BEGIN:VCARD\n"
0138         "VERSION:3.0\n"
0139         "N:LastName;FirstName;;;\n"
0140         "UID:c80cf296-0825-4eb0-ab16-1fac1d522a33@xxxxxx.xx\n"
0141         "REV:2015-03-14T09:24:45+00:00\n"
0142         "FN:FirstName LastName\n"
0143         "END:VCARD\n");
0144 
0145     KContacts::VCardTool vcard;
0146     const KContacts::AddresseeList lst = vcard.parseVCards(vcarddata);
0147     QCOMPARE(lst.count(), 1);
0148     QCOMPARE(lst.at(0).relationships().count(), 0);
0149 }
0150 
0151 void RelatedTest::shouldCreateVCard4()
0152 {
0153     KContacts::AddresseeList lst;
0154     KContacts::Addressee addr;
0155     addr.setEmails(QStringList() << QStringLiteral("foo@kde.org"));
0156     addr.setUid(QStringLiteral("testuid"));
0157     KContacts::Related::List lstRelated;
0158     KContacts::Related related(QStringLiteral("friend"));
0159     lstRelated << related;
0160     addr.setRelationships(lstRelated);
0161     lst << addr;
0162     KContacts::VCardTool vcard;
0163     const QByteArray ba = vcard.exportVCards(lst, KContacts::VCard::v4_0);
0164     QByteArray expected(
0165         "BEGIN:VCARD\r\n"
0166         "VERSION:4.0\r\n"
0167         "EMAIL:foo@kde.org\r\n"
0168         "N:;;;;\r\n"
0169         "RELATED:friend\r\n"
0170         "UID:testuid\r\n"
0171         "END:VCARD\r\n\r\n");
0172 
0173     QCOMPARE(ba, expected);
0174 }
0175 
0176 void RelatedTest::shouldCreateVCardWithTwoRelated()
0177 {
0178     KContacts::AddresseeList lst;
0179     KContacts::Addressee addr;
0180     addr.setEmails(QStringList() << QStringLiteral("foo@kde.org"));
0181     addr.setUid(QStringLiteral("testuid"));
0182     KContacts::Related::List lstRelated;
0183     KContacts::Related related(QStringLiteral("friend"));
0184     KContacts::Related related2(QStringLiteral("kde"));
0185     lstRelated << related << related2;
0186     addr.setRelationships(lstRelated);
0187     lst << addr;
0188     KContacts::VCardTool vcard;
0189     const QByteArray ba = vcard.exportVCards(lst, KContacts::VCard::v4_0);
0190     QByteArray expected(
0191         "BEGIN:VCARD\r\n"
0192         "VERSION:4.0\r\n"
0193         "EMAIL:foo@kde.org\r\n"
0194         "N:;;;;\r\n"
0195         "RELATED:friend\r\n"
0196         "RELATED:kde\r\n"
0197         "UID:testuid\r\n"
0198         "END:VCARD\r\n\r\n");
0199 
0200     QCOMPARE(ba, expected);
0201 }
0202 
0203 void RelatedTest::shouldCreateVCardWithParameters()
0204 {
0205     KContacts::AddresseeList lst;
0206     KContacts::Addressee addr;
0207     addr.setEmails(QStringList() << QStringLiteral("foo@kde.org"));
0208     addr.setUid(QStringLiteral("testuid"));
0209     KContacts::Related::List lstRelated;
0210     KContacts::Related related(QStringLiteral("friend"));
0211     KContacts::ParameterMap params;
0212     params.push_back({QStringLiteral("Foo1"), {QStringLiteral("bla1"), QStringLiteral("blo1")}});
0213     params.push_back({QStringLiteral("Foo2"), {QStringLiteral("bla2"), QStringLiteral("blo2")}});
0214     related.setParams(params);
0215     lstRelated << related;
0216     addr.setRelationships(lstRelated);
0217     lst << addr;
0218     KContacts::VCardTool vcard;
0219     const QByteArray ba = vcard.exportVCards(lst, KContacts::VCard::v4_0);
0220     QByteArray expected(
0221         "BEGIN:VCARD\r\n"
0222         "VERSION:4.0\r\n"
0223         "EMAIL:foo@kde.org\r\n"
0224         "N:;;;;\r\n"
0225         "RELATED;FOO1=bla1,blo1;FOO2=bla2,blo2:friend\r\n"
0226         "UID:testuid\r\n"
0227         "END:VCARD\r\n\r\n");
0228     QCOMPARE(ba, expected);
0229 }
0230 
0231 void RelatedTest::shouldNotExportInVcard3()
0232 {
0233     KContacts::AddresseeList lst;
0234     KContacts::Addressee addr;
0235     addr.setEmails(QStringList() << QStringLiteral("foo@kde.org"));
0236     addr.setUid(QStringLiteral("testuid"));
0237     KContacts::Related::List lstRelated;
0238     KContacts::Related related(QStringLiteral("friend"));
0239     KContacts::ParameterMap params;
0240     params.push_back({QStringLiteral("Foo1"), {QStringLiteral("bla1"), QStringLiteral("blo1")}});
0241     params.push_back({QStringLiteral("Foo2"), {QStringLiteral("bla2"), QStringLiteral("blo2")}});
0242     related.setParams(params);
0243     lstRelated << related;
0244     addr.setRelationships(lstRelated);
0245     lst << addr;
0246     KContacts::VCardTool vcard;
0247     const QByteArray ba = vcard.exportVCards(lst, KContacts::VCard::v3_0);
0248     QByteArray expected(
0249         "BEGIN:VCARD\r\n"
0250         "VERSION:3.0\r\n"
0251         "EMAIL:foo@kde.org\r\n"
0252         "N:;;;;\r\n"
0253         "RELATED;FOO1=bla1,blo1;FOO2=bla2,blo2:friend\r\n"
0254         "UID:testuid\r\n"
0255         "END:VCARD\r\n\r\n");
0256     QCOMPARE(ba, expected);
0257 }
0258 
0259 QTEST_MAIN(RelatedTest)
0260 
0261 #include "moc_relatedtest.cpp"