File indexing completed on 2024-12-22 04:57:38

0001 /*  This file is part of the KDE project
0002     SPDX-FileCopyrightText: 2010 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.net>
0003     SPDX-FileContributor: Kevin Krammer <krake@kdab.com>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include "mixedmaildirstore.h"
0009 
0010 #include "collectioncreatejob.h"
0011 #include "collectiondeletejob.h"
0012 #include "collectionfetchjob.h"
0013 #include "collectionmodifyjob.h"
0014 #include "collectionmovejob.h"
0015 #include "itemcreatejob.h"
0016 #include "itemdeletejob.h"
0017 #include "itemfetchjob.h"
0018 #include "itemmodifyjob.h"
0019 #include "itemmovejob.h"
0020 #include "sessionimpls_p.h"
0021 #include "storecompactjob.h"
0022 
0023 #include <Akonadi/MessageParts>
0024 
0025 #include <Akonadi/CachePolicy>
0026 
0027 #include <KMime/Message>
0028 
0029 #include <KRandom>
0030 #include <QRandomGenerator>
0031 #include <QTemporaryDir>
0032 
0033 #include <QTest>
0034 
0035 using namespace Akonadi;
0036 
0037 class TestStore : public MixedMaildirStore
0038 {
0039     Q_OBJECT
0040 
0041 public:
0042     TestStore()
0043         : mLastCheckedJob(nullptr)
0044         , mErrorCode(0)
0045     {
0046     }
0047 
0048 public:
0049     Collection mTopLevelCollection;
0050 
0051     mutable FileStore::Job *mLastCheckedJob = nullptr;
0052     mutable int mErrorCode;
0053     mutable QString mErrorText;
0054 
0055 protected:
0056     void setTopLevelCollection(const Collection &collection) override
0057     {
0058         MixedMaildirStore::setTopLevelCollection(collection);
0059     }
0060 
0061     void checkCollectionMove(FileStore::CollectionMoveJob *job, int &errorCode, QString &errorText) const override
0062     {
0063         MixedMaildirStore::checkCollectionMove(job, errorCode, errorText);
0064 
0065         mLastCheckedJob = job;
0066         mErrorCode = errorCode;
0067         mErrorText = errorText;
0068     }
0069 
0070     void checkItemCreate(FileStore::ItemCreateJob *job, int &errorCode, QString &errorText) const override
0071     {
0072         MixedMaildirStore::checkItemCreate(job, errorCode, errorText);
0073 
0074         mLastCheckedJob = job;
0075         mErrorCode = errorCode;
0076         mErrorText = errorText;
0077     }
0078 };
0079 
0080 class TemplateMethodsTest : public QObject
0081 {
0082     Q_OBJECT
0083 
0084 public:
0085     TemplateMethodsTest()
0086         : QObject()
0087         , mStore(nullptr)
0088     {
0089     }
0090 
0091     ~TemplateMethodsTest() override
0092     {
0093         delete mStore;
0094     }
0095 
0096 private:
0097     QTemporaryDir mDir;
0098     TestStore *mStore = nullptr;
0099 
0100 private Q_SLOTS:
0101     void init();
0102     void testSetTopLevelCollection();
0103     void testMoveCollection();
0104     void testCreateItem();
0105 };
0106 
0107 void TemplateMethodsTest::init()
0108 {
0109     delete mStore;
0110     mStore = new TestStore;
0111     QVERIFY(mDir.isValid());
0112     QVERIFY(QDir(mDir.path()).exists());
0113 }
0114 
0115 void TemplateMethodsTest::testSetTopLevelCollection()
0116 {
0117     const QString file = KRandom::randomString(10);
0118     const QString path = mDir.path() + file;
0119 
0120     mStore->setPath(path);
0121 
0122     // check the adjustments on the top level collection
0123     const Collection collection = mStore->topLevelCollection();
0124     QCOMPARE(collection.contentMimeTypes(), QStringList() << Collection::mimeType());
0125     QCOMPARE(collection.rights(), Collection::CanCreateCollection | Collection::CanChangeCollection | Collection::CanDeleteCollection);
0126     const CachePolicy cachePolicy = collection.cachePolicy();
0127     QVERIFY(!cachePolicy.inheritFromParent());
0128     QCOMPARE(cachePolicy.localParts(), QStringList() << QLatin1StringView(MessagePart::Envelope));
0129     QVERIFY(cachePolicy.syncOnDemand());
0130 }
0131 
0132 void TemplateMethodsTest::testMoveCollection()
0133 {
0134     mStore->setPath(mDir.path());
0135 
0136     FileStore::CollectionMoveJob *job = nullptr;
0137 
0138     // test moving into itself
0139     Collection collection(QRandomGenerator::global()->generate());
0140     collection.setParentCollection(mStore->topLevelCollection());
0141     collection.setRemoteId(QStringLiteral("collection"));
0142     job = mStore->moveCollection(collection, collection);
0143     QVERIFY(job != nullptr);
0144     QCOMPARE(job->error(), (int)FileStore::Job::InvalidJobContext);
0145     QVERIFY(!job->errorText().isEmpty());
0146     QCOMPARE(mStore->mLastCheckedJob, job);
0147     QCOMPARE(mStore->mErrorCode, job->error());
0148     QCOMPARE(mStore->mErrorText, job->errorText());
0149 
0150     QVERIFY(!job->exec());
0151 
0152     // test moving into child
0153     Collection childCollection(collection.id() + 1);
0154     childCollection.setParentCollection(collection);
0155     childCollection.setRemoteId(QStringLiteral("child"));
0156     job = mStore->moveCollection(collection, childCollection);
0157     QVERIFY(job != nullptr);
0158     QCOMPARE(job->error(), (int)FileStore::Job::InvalidJobContext);
0159     QVERIFY(!job->errorText().isEmpty());
0160     QCOMPARE(mStore->mLastCheckedJob, job);
0161     QCOMPARE(mStore->mErrorCode, job->error());
0162     QCOMPARE(mStore->mErrorText, job->errorText());
0163 
0164     QVERIFY(!job->exec());
0165 
0166     // test moving into grand child child
0167     Collection grandChildCollection(collection.id() + 2);
0168     grandChildCollection.setParentCollection(childCollection);
0169     grandChildCollection.setRemoteId(QStringLiteral("grandchild"));
0170     job = mStore->moveCollection(collection, grandChildCollection);
0171     QVERIFY(job != nullptr);
0172     QCOMPARE(job->error(), (int)FileStore::Job::InvalidJobContext);
0173     QVERIFY(!job->errorText().isEmpty());
0174     QCOMPARE(mStore->mLastCheckedJob, job);
0175     QCOMPARE(mStore->mErrorCode, job->error());
0176     QCOMPARE(mStore->mErrorText, job->errorText());
0177 
0178     QVERIFY(!job->exec());
0179 
0180     // test moving into unrelated collection
0181     Collection otherCollection(collection.id() + QRandomGenerator::global()->generate());
0182     otherCollection.setParentCollection(mStore->topLevelCollection());
0183     otherCollection.setRemoteId(QStringLiteral("other"));
0184     job = mStore->moveCollection(collection, otherCollection);
0185     QVERIFY(job != nullptr);
0186     QCOMPARE(job->error(), 0);
0187     QVERIFY(job->errorText().isEmpty());
0188     QCOMPARE(mStore->mLastCheckedJob, job);
0189 
0190     // collections don't exist in the store, so processing still fails
0191     QVERIFY(!job->exec());
0192 }
0193 
0194 void TemplateMethodsTest::testCreateItem()
0195 {
0196     mStore->setPath(mDir.path());
0197 
0198     Collection collection(QRandomGenerator::global()->generate());
0199     collection.setParentCollection(mStore->topLevelCollection());
0200     collection.setRemoteId(QStringLiteral("collection"));
0201 
0202     FileStore::ItemCreateJob *job = nullptr;
0203 
0204     // test item without payload
0205     Item item(KMime::Message::mimeType());
0206     job = mStore->createItem(item, collection);
0207     QVERIFY(job != nullptr);
0208     QCOMPARE(job->error(), (int)FileStore::Job::InvalidJobContext);
0209     QVERIFY(!job->errorText().isEmpty());
0210     QCOMPARE(mStore->mLastCheckedJob, job);
0211     QCOMPARE(mStore->mErrorCode, job->error());
0212     QCOMPARE(mStore->mErrorText, job->errorText());
0213 
0214     QVERIFY(!job->exec());
0215 
0216     // test item with payload
0217     item.setPayloadFromData("Subject: dummy payload\n\nwith some content");
0218     job = mStore->createItem(item, collection);
0219     QVERIFY(job != nullptr);
0220     QCOMPARE(job->error(), 0);
0221     QVERIFY(job->errorText().isEmpty());
0222     QCOMPARE(mStore->mLastCheckedJob, job);
0223 
0224     // collections don't exist in the store, so processing still fails
0225     QVERIFY(!job->exec());
0226 }
0227 
0228 QTEST_MAIN(TemplateMethodsTest)
0229 
0230 #include "templatemethodstest.moc"