File indexing completed on 2024-11-10 04:40:09

0001 /*
0002     SPDX-FileCopyrightText: 2006, 2009 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "collection.h"
0008 #include "collectionfetchjob.h"
0009 #include "collectionmovejob.h"
0010 #include "item.h"
0011 #include "itemfetchjob.h"
0012 #include "itemfetchscope.h"
0013 #include "qtest_akonadi.h"
0014 
0015 #include <QHash>
0016 
0017 using namespace Akonadi;
0018 
0019 class CollectionMoveTest : public QObject
0020 {
0021     Q_OBJECT
0022 private Q_SLOTS:
0023     void initTestCase()
0024     {
0025         AkonadiTest::checkTestIsIsolated();
0026     }
0027     void testIllegalMove_data()
0028     {
0029         QTest::addColumn<Collection>("source");
0030         QTest::addColumn<Collection>("destination");
0031 
0032         const Collection res1(AkonadiTest::collectionIdFromPath(QStringLiteral("res1")));
0033         const Collection res1foo(AkonadiTest::collectionIdFromPath(QStringLiteral("res1/foo")));
0034         const Collection res1bla(AkonadiTest::collectionIdFromPath(QStringLiteral("res1/foo/bar/bla")));
0035 
0036         QTest::newRow("non-existing-target") << res1 << Collection(INT_MAX);
0037         QTest::newRow("root") << Collection::root() << res1;
0038         QTest::newRow("move-into-child") << res1 << res1foo;
0039         QTest::newRow("same-name-in-target") << res1bla << res1foo;
0040         QTest::newRow("non-existing-source") << Collection(INT_MAX) << res1;
0041     }
0042 
0043     void testIllegalMove()
0044     {
0045         QFETCH(Collection, source);
0046         QFETCH(Collection, destination);
0047         QVERIFY(source.isValid());
0048         QVERIFY(destination.isValid());
0049 
0050         auto mod = new CollectionMoveJob(source, destination, this);
0051         QVERIFY(!mod->exec());
0052     }
0053 
0054     void testMove_data()
0055     {
0056         QTest::addColumn<Collection>("source");
0057         QTest::addColumn<Collection>("destination");
0058         QTest::addColumn<bool>("crossResource");
0059 
0060         QTest::newRow("inter-resource") << Collection(AkonadiTest::collectionIdFromPath(QStringLiteral("res1")))
0061                                         << Collection(AkonadiTest::collectionIdFromPath(QStringLiteral("res2"))) << true;
0062         QTest::newRow("intra-resource") << Collection(AkonadiTest::collectionIdFromPath(QStringLiteral("res1/foo/bla")))
0063                                         << Collection(AkonadiTest::collectionIdFromPath(QStringLiteral("res1/foo/bar/bla"))) << false;
0064     }
0065 
0066     // TODO: test signals
0067     void testMove()
0068     {
0069         QFETCH(Collection, source);
0070         QFETCH(Collection, destination);
0071         QFETCH(bool, crossResource);
0072         QVERIFY(source.isValid());
0073         QVERIFY(destination.isValid());
0074 
0075         auto fetch = new CollectionFetchJob(source, CollectionFetchJob::Base, this);
0076         AKVERIFYEXEC(fetch);
0077         QCOMPARE(fetch->collections().count(), 1);
0078         source = fetch->collections().first();
0079 
0080         // obtain reference listing
0081         fetch = new CollectionFetchJob(source, CollectionFetchJob::Recursive);
0082         AKVERIFYEXEC(fetch);
0083         QHash<Collection, Item::List> referenceData;
0084         const Collection::List collections = fetch->collections();
0085         for (const Collection &c : collections) {
0086             auto job = new ItemFetchJob(c, this);
0087             AKVERIFYEXEC(job);
0088             referenceData.insert(c, job->items());
0089         }
0090 
0091         // move collection
0092         auto mod = new CollectionMoveJob(source, destination, this);
0093         AKVERIFYEXEC(mod);
0094 
0095         // check if source was modified correctly
0096         auto ljob = new CollectionFetchJob(source, CollectionFetchJob::Base);
0097         AKVERIFYEXEC(ljob);
0098         Collection::List list = ljob->collections();
0099 
0100         QCOMPARE(list.count(), 1);
0101         Collection col = list.first();
0102         QCOMPARE(col.name(), source.name());
0103         QCOMPARE(col.parentCollection(), destination);
0104 
0105         // list destination and check if everything is still there
0106         ljob = new CollectionFetchJob(destination, CollectionFetchJob::Recursive);
0107         AKVERIFYEXEC(ljob);
0108         list = ljob->collections();
0109 
0110         QVERIFY(list.count() >= referenceData.count());
0111         for (QHash<Collection, Item::List>::ConstIterator it = referenceData.constBegin(); it != referenceData.constEnd(); ++it) {
0112             QVERIFY(list.contains(it.key()));
0113             if (crossResource) {
0114                 QVERIFY(list[list.indexOf(it.key())].resource() != it.key().resource());
0115             } else {
0116                 QCOMPARE(list[list.indexOf(it.key())].resource(), it.key().resource());
0117             }
0118             auto job = new ItemFetchJob(it.key(), this);
0119             job->fetchScope().fetchFullPayload();
0120             AKVERIFYEXEC(job);
0121             QCOMPARE(job->items().count(), it.value().count());
0122             const Item::List items = job->items();
0123             for (const Item &item : items) {
0124                 QVERIFY(it.value().contains(item));
0125                 QVERIFY(item.hasPayload());
0126             }
0127         }
0128 
0129         // cleanup
0130         mod = new CollectionMoveJob(col, source.parentCollection(), this);
0131         AKVERIFYEXEC(mod);
0132     }
0133 };
0134 
0135 QTEST_AKONADIMAIN(CollectionMoveTest)
0136 
0137 #include "collectionmovetest.moc"