File indexing completed on 2025-03-09 04:57:00

0001 /*
0002  * SPDX-FileCopyrightText: 2014 Kevin Ottens <ervin@kde.org>
0003    SPDX-FileCopyrightText: 2014 David Faure <faure@kde.org>
0004  * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005  */
0006 
0007 
0008 #include "zanshin021migrator.h"
0009 
0010 #include <testlib/qtest_zanshin.h>
0011 #include <QSignalSpy>
0012 #include <testlib/testsafety.h>
0013 #include <testlib/akonadidebug.h>
0014 
0015 #include <KCalendarCore/Todo>
0016 #include <KCalendarCore/ICalFormat>
0017 
0018 #include <Akonadi/Collection>
0019 #include <Akonadi/TransactionSequence>
0020 #include "akonadi/akonadicollectionfetchjobinterface.h"
0021 #include "akonadi/akonadiitemfetchjobinterface.h"
0022 #include "akonadi/akonadistorage.h"
0023 
0024 #include <Akonadi/CollectionFetchJob>
0025 #include <Akonadi/ItemFetchJob>
0026 #include <Akonadi/ItemFetchScope>
0027 
0028 #include <QProcess>
0029 
0030 class Zanshin021MigrationTest : public QObject
0031 {
0032     Q_OBJECT
0033 public:
0034     explicit Zanshin021MigrationTest(QObject *parent = nullptr)
0035         : QObject(parent)
0036     {
0037         qRegisterMetaType<Akonadi::Collection>();
0038         qRegisterMetaType<Akonadi::Item>();
0039     }
0040 
0041 private slots:
0042     void initTestCase()
0043     {
0044         QVERIFY(TestLib::TestSafety::checkTestIsIsolated());
0045         auto storage = Akonadi::StorageInterface::Ptr(new Akonadi::Storage);
0046         TestLib::AkonadiDebug::dumpTree(storage);
0047         //QProcess proc;
0048         //proc.execute("akonadiconsole");
0049         //qApp->exec();
0050     }
0051 
0052     void cleanupTestCase()
0053     {
0054         // Give a chance for jobs still waiting for an event loop
0055         // run to be deleted through deleteLater()
0056         QTest::qWait(10);
0057     }
0058 
0059     void shouldFetchAllItems()
0060     {
0061         // GIVEN
0062         Zanshin021Migrator migrator;
0063 
0064         // WHEN
0065         Zanshin021Migrator::SeenItemHash hash = migrator.fetchAllItems();
0066 
0067         // THEN
0068         // the migrator gathered all items and the initial state of "is a project" for each one is correct
0069         m_expectedUids.insert(QStringLiteral("child-of-project"), false);
0070         m_expectedUids.insert(QStringLiteral("new-project-with-property"), true);
0071         m_expectedUids.insert(QStringLiteral("old-project-with-comment"), false); // not yet
0072         m_expectedUids.insert(QStringLiteral("project-with-comment-and-property"), true);
0073         m_expectedUids.insert(QStringLiteral("project-with-children"), false); // not yet
0074         m_expectedUids.insert(QStringLiteral("standalone-task"), false);
0075 
0076         checkExpectedIsProject(hash, m_expectedUids);
0077     }
0078 
0079     void shouldMigrateCommentToProperty()
0080     {
0081         // GIVEN
0082         Zanshin021Migrator migrator;
0083         Zanshin021Migrator::SeenItemHash hash = migrator.fetchAllItems();
0084 
0085         // WHEN
0086         Akonadi::TransactionSequence *sequence = new Akonadi::TransactionSequence();
0087         migrator.migrateProjectComments(hash, sequence);
0088 
0089         // THEN
0090         // the project with an old-style comment was modified to have the property
0091         SeenItem item = hash.value(QStringLiteral("old-project-with-comment"));
0092         QVERIFY(item.isDirty());
0093 
0094         m_expectedUids[QStringLiteral("old-project-with-comment")] = true; // migrated!
0095         checkExpectedIsProject(hash, m_expectedUids);
0096         m_expectedUids[QStringLiteral("old-project-with-comment")] = false; // revert for now
0097 
0098         sequence->rollback();
0099         sequence->exec();
0100     }
0101 
0102     void shouldMigrateTaskWithChildrenToProject()
0103     {
0104         // GIVEN
0105         Zanshin021Migrator migrator;
0106         Zanshin021Migrator::SeenItemHash hash = migrator.fetchAllItems();
0107 
0108         // WHEN
0109         Akonadi::TransactionSequence *sequence = new Akonadi::TransactionSequence();
0110         migrator.migrateProjectWithChildren(hash, sequence);
0111 
0112         // THEN
0113         // the project with children was modified to have the property
0114         SeenItem item = hash.value(QStringLiteral("project-with-children"));
0115         QVERIFY(item.isDirty());
0116 
0117         m_expectedUids[QStringLiteral("project-with-children")] = true; // migrated!
0118         checkExpectedIsProject(hash, m_expectedUids);
0119         m_expectedUids[QStringLiteral("project-with-children")] = false; // revert for now
0120 
0121         sequence->rollback();
0122         sequence->exec();
0123     }
0124 
0125     void shouldMigrateProjects()
0126     {
0127         // GIVEN
0128         Zanshin021Migrator migrator;
0129 
0130         // WHEN
0131         const bool ret = migrator.migrateProjects();
0132 
0133         // THEN
0134         QVERIFY(ret); // success
0135         m_expectedUids[QStringLiteral("old-project-with-comment")] = true; // migrated!
0136         m_expectedUids[QStringLiteral("project-with-children")] = true; // migrated!
0137         Zanshin021Migrator::SeenItemHash hash = migrator.fetchAllItems();
0138         checkExpectedIsProject(hash, m_expectedUids);
0139     }
0140 
0141 private:
0142 
0143     void checkExpectedIsProject(const Zanshin021Migrator::SeenItemHash &hash, const QMap<QString /*uid*/, bool /*isProject*/> &expectedItems)
0144     {
0145         QStringList uids = hash.keys();
0146         uids.sort();
0147         if (uids.count() != expectedItems.count()) // QCOMPARE for QStringList isn't verbose enough
0148             qWarning() << "Got" << uids << "expected" << expectedItems.keys();
0149         QCOMPARE(uids, QStringList(expectedItems.keys()));
0150 
0151         for (auto it = expectedItems.constBegin(); it != expectedItems.constEnd(); ++it) {
0152             //qDebug() << it.key();
0153             QCOMPARE(Zanshin021Migrator::isProject(hash.value(it.key()).item()), it.value());
0154         }
0155     }
0156 
0157     QMap<QString /*uid*/, bool /*isProject*/> m_expectedUids;
0158 };
0159 
0160 
0161 ZANSHIN_TEST_MAIN(Zanshin021MigrationTest)
0162 
0163 #include "zanshin021migrationtest.moc"
0164 
0165 
0166