File indexing completed on 2024-11-24 04:44:10

0001 /*
0002  * SPDX-FileCopyrightText: 2012 Christian Mollekopf <mollekopf@kolabsys.com>
0003  *
0004  * SPDX-License-Identifier: LGPL-3.0-or-later
0005  */
0006 
0007 #include "upgradetest.h"
0008 
0009 #include "kolabformat/errorhandler.h"
0010 #include <QTest>
0011 #include <kolabcontainers.h>
0012 #include <kolabformat.h>
0013 
0014 #include "conversion/commonconversion.h"
0015 #include "kolabformat/kolabobject.h"
0016 #include "testutils.h"
0017 #include <KCalendarCore/ICalFormat>
0018 #include <KContacts/VCardConverter>
0019 
0020 void UpgradeTest::testIncidence_data()
0021 {
0022     QTest::addColumn<Kolab::ObjectType>("type");
0023     QTest::addColumn<QString>("filename");
0024     QTest::addColumn<bool>("forceType");
0025 
0026     //     QTest::newRow( "v2contactSimple" ) << Kolab::KolabV2 << Kolab::ContactObject << TESTFILEDIR+QString::fromLatin1("v2/contacts/simple.vcf") <<
0027     //     TESTFILEDIR+QString::fromLatin1("v2/contacts/simple.vcf.mime");
0028     QTest::newRow("v2eventSimple") << Kolab::EventObject << TESTFILEDIR + QLatin1StringView("v2/event/simple.ics.mime") << false;
0029     QTest::newRow("v2eventComplex") << Kolab::EventObject << TESTFILEDIR + QLatin1StringView("v2/event/complex.ics.mime") << false;
0030     QTest::newRow("v2eventAllday") << Kolab::EventObject << TESTFILEDIR + QLatin1StringView("v2/event/allday.ics.mime") << false;
0031     QTest::newRow("v2eventAttachment") << Kolab::EventObject << TESTFILEDIR + QLatin1StringView("v2/event/attachment.ics.mime") << false;
0032     QTest::newRow("v2eventHorde") << Kolab::EventObject << TESTFILEDIR + QLatin1StringView("v2/event/horde.ics.mime") << false;
0033 
0034     // Still broken, although it seems that rather the event comparison is implemented wrong (not using equals)
0035     QTest::newRow("v2taskSimple") << Kolab::TodoObject << TESTFILEDIR + QLatin1StringView("v2/task/simple.ics.mime") << false;
0036     QTest::newRow("v2taskComplex") << Kolab::TodoObject << TESTFILEDIR + QLatin1StringView("v2/task/complex.ics.mime") << false;
0037     QTest::newRow("v2taskPrio1") << Kolab::TodoObject << TESTFILEDIR + QLatin1StringView("v2/task/prioritytest1.ics.mime") << false;
0038     QTest::newRow("v2taskPrio2") << Kolab::TodoObject << TESTFILEDIR + QLatin1StringView("v2/task/prioritytest2.ics.mime") << false;
0039 
0040     QTest::newRow("v2journalSimple") << Kolab::JournalObject << TESTFILEDIR + QLatin1StringView("v2/journal/simple.ics.mime") << false;
0041     QTest::newRow("v2journalComplex") << Kolab::JournalObject << TESTFILEDIR + QLatin1StringView("v2/journal/complex.ics.mime") << false;
0042 
0043     QTest::newRow("v2eventSimple_missingTypeHeader") << Kolab::EventObject << TESTFILEDIR + QLatin1StringView("v2/event/simple_missingTypeHeader.ics.mime")
0044                                                      << true;
0045     QTest::newRow("v2eventis8859-1") << Kolab::EventObject << TESTFILEDIR + QLatin1StringView("v2/event/iso8859-1.mime") << false;
0046 }
0047 
0048 void UpgradeTest::testIncidence()
0049 {
0050     QFETCH(Kolab::ObjectType, type);
0051     QFETCH(QString, filename); // To compare
0052     QFETCH(bool, forceType);
0053 
0054     bool ok = false;
0055     const KMime::Message::Ptr &msg = readMimeFile(filename, ok);
0056     QVERIFY(ok);
0057     Kolab::KolabObjectReader reader;
0058     if (forceType) {
0059         reader.setObjectType(type);
0060     }
0061     Kolab::ObjectType t = reader.parseMimeMessage(msg);
0062     QCOMPARE(t, type);
0063     QCOMPARE(reader.getVersion(), Kolab::KolabV2);
0064     QCOMPARE(Kolab::ErrorHandler::instance().error(), Kolab::ErrorHandler::Debug);
0065 
0066     KCalendarCore::Incidence::Ptr v2result = reader.getIncidence();
0067     QVERIFY(!v2result.isNull());
0068 
0069     // write KCalendarCore V3
0070     KMime::Message::Ptr v3message = Kolab::KolabObjectWriter::writeIncidence(v2result, Kolab::KolabV3);
0071     QVERIFY(Kolab::error() == Kolab::NoError);
0072     //     qDebug() << v3message->encodedContent();
0073     // load KCalendarCore V3
0074     Kolab::KolabObjectReader reader2;
0075     QCOMPARE(reader2.parseMimeMessage(v3message), type);
0076     KCalendarCore::Incidence::Ptr v3result = reader2.getIncidence();
0077     QVERIFY(!v3result.isNull());
0078     normalizeIncidence(v2result);
0079     // We have to override the last modified time with a specific value, so we normalize the input to the same
0080     normalizeIncidence(v3result);
0081     qDebug() << "--------------------------------------------------------";
0082     KCalendarCore::ICalFormat format;
0083     const QString v2 = format.toString(v2result);
0084     const QString v3 = format.toString(v3result);
0085     if (*v2result != *v3result) {
0086         showDiff(v2, v3);
0087         //         qDebug() << "V2: " << v2;
0088         //         qDebug() << "V3: " << v3;
0089     }
0090     QVERIFY(!v2.isEmpty());
0091     QVERIFY(!v3.isEmpty());
0092     QCOMPARE(v2, v3);
0093     qDebug() << "--------------------------------------------------------";
0094 
0095     //     if (v2result->type() == KCalendarCore::IncidenceBase::TypeTodo) {
0096     //         KCalendarCore::Todo::Ptr t1 = v2result.dynamicCast<KCalendarCore::Todo>();
0097     //         KCalendarCore::Todo::Ptr t2 = v3result.dynamicCast<KCalendarCore::Todo>();
0098     //         QCOMPARE(t1->percentComplete(), t2->percentComplete());
0099     //         QCOMPARE(t1->priority(), t2->priority());
0100     //         QCOMPARE(t1->uid(), t2->uid());
0101     //         QCOMPARE(t1->dtStart(), t2->dtStart());
0102     //         QCOMPARE(t1->dtDue(), t2->dtDue());
0103     //         QCOMPARE(*t1->organizer().data(), *t2->organizer().data());
0104     //         QCOMPARE(t1->uid(), t2->uid());
0105     //         QCOMPARE(t1->allDay(), t2->allDay());
0106     //         QCOMPARE(t1->duration(), t2->duration());
0107     //         QCOMPARE(t1->hasDuration(), t2->hasDuration());
0108     //         qDebug() << "V2: " << v2;
0109     //     }
0110     // Test comparator
0111     QCOMPARE(*v2result, *v3result);
0112     QCOMPARE(Kolab::ErrorHandler::instance().error(), Kolab::ErrorHandler::Debug);
0113 }
0114 
0115 void UpgradeTest::testContact_data()
0116 {
0117     QTest::addColumn<Kolab::ObjectType>("type");
0118     QTest::addColumn<QString>("filename");
0119 
0120     QTest::newRow("v2contactSimple") << Kolab::ContactObject << TESTFILEDIR + QLatin1StringView("v2/contacts/simple.vcf.mime");
0121     QTest::newRow("v2contactComplex") << Kolab::ContactObject << TESTFILEDIR + QLatin1StringView("v2/contacts/complex.vcf.mime");
0122     QTest::newRow("v2contactAddress") << Kolab::ContactObject << TESTFILEDIR + QLatin1StringView("v2/contacts/address.vcf.mime");
0123     QTest::newRow("v2contactBug238996") << Kolab::ContactObject << TESTFILEDIR + QLatin1StringView("v2/contacts/bug238996.vcf.mime");
0124     QTest::newRow("v2contactDisplayname") << Kolab::ContactObject << TESTFILEDIR + QLatin1StringView("v2/contacts/displayname.vcf.mime");
0125     QTest::newRow("v2contactEmails") << Kolab::ContactObject << TESTFILEDIR + QLatin1StringView("v2/contacts/emails.vcf.mime");
0126     QTest::newRow("v2contactPhonenumber") << Kolab::ContactObject << TESTFILEDIR + QLatin1StringView("v2/contacts/phonenumbers.vcf.mime");
0127     QTest::newRow("v2contactPicture") << Kolab::ContactObject << TESTFILEDIR + QLatin1StringView("v2/contacts/picture.vcf.mime");
0128     QTest::newRow("v2iso8859-1") << Kolab::ContactObject << TESTFILEDIR + QLatin1StringView("v2/contacts/iso8859-1.mime");
0129 }
0130 
0131 void UpgradeTest::testContact()
0132 {
0133     QFETCH(Kolab::ObjectType, type);
0134     QFETCH(QString, filename);
0135 
0136     // Parse mime message
0137     bool ok = false;
0138     const KMime::Message::Ptr &msg = readMimeFile(filename, ok);
0139     QVERIFY(ok);
0140     Kolab::KolabObjectReader reader;
0141     Kolab::ObjectType t = reader.parseMimeMessage(msg);
0142     QCOMPARE(t, type);
0143     QCOMPARE(reader.getVersion(), Kolab::KolabV2);
0144     QCOMPARE(Kolab::ErrorHandler::instance().error(), Kolab::ErrorHandler::Debug);
0145 
0146     KContacts::Addressee v2Addressee = reader.getContact();
0147     QVERIFY(!v2Addressee.isEmpty());
0148 
0149     // Write
0150     const KMime::Message::Ptr &v3message = Kolab::KolabObjectWriter::writeContact(v2Addressee, Kolab::KolabV3);
0151 
0152     // load V3
0153     Kolab::KolabObjectReader reader2;
0154     QCOMPARE(reader2.parseMimeMessage(v3message), type);
0155     KContacts::Addressee v3result = reader2.getContact();
0156     QVERIFY(!v3result.isEmpty());
0157 
0158     normalizeContact(v2Addressee);
0159     QVERIFY(normalizePhoneNumbers(v3result, v2Addressee)); // phone number ids are random
0160     QVERIFY(normalizeAddresses(v3result, v2Addressee)); // same here
0161 
0162     KContacts::VCardConverter converter;
0163     if (v2Addressee != v3result) {
0164         showDiff(QString::fromUtf8(converter.createVCard(v2Addressee)), QString::fromUtf8(converter.createVCard(v3result)));
0165         QVERIFY(false);
0166     }
0167     QCOMPARE(Kolab::ErrorHandler::instance().error(), Kolab::ErrorHandler::Debug);
0168 }
0169 
0170 QTEST_MAIN(UpgradeTest)
0171 
0172 #include "moc_upgradetest.cpp"