File indexing completed on 2024-12-22 04:57:34
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 "filestore/collectioncreatejob.h" 0011 0012 #include "libmaildir/maildir.h" 0013 0014 #include <KMime/Message> 0015 0016 #include <QTemporaryDir> 0017 0018 #include <QDir> 0019 #include <QFileInfo> 0020 #include <QTest> 0021 0022 using namespace Akonadi; 0023 0024 class CollectionCreateTest : public QObject 0025 { 0026 Q_OBJECT 0027 0028 public: 0029 CollectionCreateTest() 0030 : QObject() 0031 , mStore(nullptr) 0032 , mDir(nullptr) 0033 { 0034 } 0035 0036 ~CollectionCreateTest() override 0037 { 0038 delete mStore; 0039 delete mDir; 0040 } 0041 0042 private: 0043 MixedMaildirStore *mStore = nullptr; 0044 QTemporaryDir *mDir = nullptr; 0045 0046 private Q_SLOTS: 0047 void init(); 0048 void cleanup(); 0049 void testCollectionProperties(); 0050 void testEmptyDir(); 0051 void testMaildirTree(); 0052 void testMixedTree(); 0053 }; 0054 0055 void CollectionCreateTest::init() 0056 { 0057 mStore = new MixedMaildirStore; 0058 0059 mDir = new QTemporaryDir; 0060 QVERIFY(mDir->isValid()); 0061 QVERIFY(QDir(mDir->path()).exists()); 0062 } 0063 0064 void CollectionCreateTest::cleanup() 0065 { 0066 delete mStore; 0067 mStore = nullptr; 0068 delete mDir; 0069 mDir = nullptr; 0070 } 0071 0072 void CollectionCreateTest::testCollectionProperties() 0073 { 0074 mStore->setPath(mDir->path()); 0075 0076 FileStore::CollectionCreateJob *job = nullptr; 0077 0078 Collection collection1; 0079 collection1.setName(QStringLiteral("collection1")); 0080 job = mStore->createCollection(collection1, mStore->topLevelCollection()); 0081 QVERIFY(job != nullptr); 0082 0083 QVERIFY(job->exec()); 0084 QCOMPARE(job->error(), 0); 0085 0086 collection1 = job->collection(); 0087 QCOMPARE(collection1.remoteId(), collection1.name()); 0088 0089 QCOMPARE(collection1.contentMimeTypes(), QStringList() << Collection::mimeType() << KMime::Message::mimeType()); 0090 0091 QCOMPARE(collection1.rights(), 0092 Collection::CanCreateItem | Collection::CanChangeItem | Collection::CanDeleteItem | Collection::CanCreateCollection 0093 | Collection::CanChangeCollection | Collection::CanDeleteCollection); 0094 } 0095 0096 void CollectionCreateTest::testEmptyDir() 0097 { 0098 mStore->setPath(mDir->path()); 0099 0100 KPIM::Maildir topLevelMd(mStore->path(), true); 0101 0102 FileStore::CollectionCreateJob *job = nullptr; 0103 0104 // test creating first level collections 0105 Collection collection1; 0106 collection1.setName(QStringLiteral("collection1")); 0107 job = mStore->createCollection(collection1, mStore->topLevelCollection()); 0108 QVERIFY(job != nullptr); 0109 0110 QVERIFY(job->exec()); 0111 QCOMPARE(job->error(), 0); 0112 0113 collection1 = job->collection(); 0114 QVERIFY(!collection1.remoteId().isEmpty()); 0115 QVERIFY(collection1.parentCollection() == mStore->topLevelCollection()); 0116 0117 QCOMPARE(topLevelMd.subFolderList(), QStringList() << QStringLiteral("collection1")); 0118 KPIM::Maildir md1 = topLevelMd.subFolder(collection1.remoteId()); 0119 QVERIFY(md1.isValid()); 0120 0121 Collection collection2; 0122 collection2.setName(QStringLiteral("collection2")); 0123 job = mStore->createCollection(collection2, mStore->topLevelCollection()); 0124 QVERIFY(job != nullptr); 0125 0126 QVERIFY(job->exec()); 0127 QCOMPARE(job->error(), 0); 0128 0129 collection2 = job->collection(); 0130 QVERIFY(!collection2.remoteId().isEmpty()); 0131 QVERIFY(collection2.parentCollection() == mStore->topLevelCollection()); 0132 0133 QCOMPARE(topLevelMd.subFolderList(), QStringList() << QStringLiteral("collection1") << QStringLiteral("collection2")); 0134 KPIM::Maildir md2 = topLevelMd.subFolder(collection2.remoteId()); 0135 QVERIFY(md2.isValid()); 0136 0137 // test creating second level collections 0138 Collection collection1_1; 0139 collection1_1.setName(QStringLiteral("collection1_1")); 0140 job = mStore->createCollection(collection1_1, collection1); 0141 QVERIFY(job != nullptr); 0142 0143 QVERIFY(job->exec()); 0144 QCOMPARE(job->error(), 0); 0145 0146 collection1_1 = job->collection(); 0147 QVERIFY(!collection1_1.remoteId().isEmpty()); 0148 QVERIFY(collection1_1.parentCollection() == collection1); 0149 0150 QCOMPARE(md1.subFolderList(), QStringList() << QStringLiteral("collection1_1")); 0151 KPIM::Maildir md1_1 = md1.subFolder(collection1_1.remoteId()); 0152 QVERIFY(md1_1.isValid()); 0153 0154 Collection collection1_2; 0155 collection1_2.setName(QStringLiteral("collection1_2")); 0156 job = mStore->createCollection(collection1_2, collection1); 0157 QVERIFY(job != nullptr); 0158 0159 QVERIFY(job->exec()); 0160 QCOMPARE(job->error(), 0); 0161 0162 collection1_2 = job->collection(); 0163 QVERIFY(!collection1_2.remoteId().isEmpty()); 0164 QVERIFY(collection1_2.parentCollection() == collection1); 0165 0166 QCOMPARE(md1.subFolderList(), QStringList() << QStringLiteral("collection1_1") << QStringLiteral("collection1_2")); 0167 KPIM::Maildir md1_2 = md1.subFolder(collection1_2.remoteId()); 0168 QVERIFY(md1_2.isValid()); 0169 0170 QCOMPARE(md2.subFolderList(), QStringList()); 0171 } 0172 0173 void CollectionCreateTest::testMaildirTree() 0174 { 0175 KPIM::Maildir topLevelMd(mDir->path(), true); 0176 QVERIFY(topLevelMd.isValid()); 0177 0178 KPIM::Maildir md1(topLevelMd.addSubFolder(QStringLiteral("collection1")), false); 0179 0180 KPIM::Maildir md1_2(md1.addSubFolder(QStringLiteral("collection1_2")), false); 0181 0182 mStore->setPath(mDir->path()); 0183 0184 FileStore::CollectionCreateJob *job = nullptr; 0185 0186 // test creating first level collections 0187 Collection collection1; 0188 collection1.setName(QStringLiteral("collection1")); 0189 job = mStore->createCollection(collection1, mStore->topLevelCollection()); 0190 QVERIFY(job != nullptr); 0191 0192 QVERIFY(job->exec()); // works because it already exists 0193 QCOMPARE(job->error(), 0); 0194 0195 collection1 = job->collection(); 0196 QVERIFY(!collection1.remoteId().isEmpty()); 0197 QVERIFY(collection1.parentCollection() == mStore->topLevelCollection()); 0198 0199 Collection collection2; 0200 collection2.setName(QStringLiteral("collection2")); 0201 job = mStore->createCollection(collection2, mStore->topLevelCollection()); 0202 QVERIFY(job != nullptr); 0203 0204 QVERIFY(job->exec()); 0205 QCOMPARE(job->error(), 0); 0206 0207 collection2 = job->collection(); 0208 QVERIFY(!collection2.remoteId().isEmpty()); 0209 QVERIFY(collection2.parentCollection() == mStore->topLevelCollection()); 0210 0211 QCOMPARE(topLevelMd.subFolderList(), QStringList() << QStringLiteral("collection1") << QStringLiteral("collection2")); 0212 KPIM::Maildir md2 = topLevelMd.subFolder(collection2.remoteId()); 0213 QVERIFY(md2.isValid()); 0214 0215 // test creating second level collections 0216 Collection collection1_1; 0217 collection1_1.setName(QStringLiteral("collection1_1")); 0218 job = mStore->createCollection(collection1_1, collection1); 0219 QVERIFY(job != nullptr); 0220 0221 QVERIFY(job->exec()); 0222 QCOMPARE(job->error(), 0); 0223 0224 collection1_1 = job->collection(); 0225 QVERIFY(!collection1_1.remoteId().isEmpty()); 0226 QCOMPARE(collection1_1.parentCollection().remoteId(), QStringLiteral("collection1")); 0227 0228 QCOMPARE(md1.subFolderList(), QStringList() << QStringLiteral("collection1_1") << QStringLiteral("collection1_2")); 0229 KPIM::Maildir md1_1 = md1.subFolder(collection1_1.remoteId()); 0230 QVERIFY(md1_1.isValid()); 0231 0232 Collection collection1_2; 0233 collection1_2.setName(QStringLiteral("collection1_2")); 0234 job = mStore->createCollection(collection1_2, collection1); 0235 QVERIFY(job != nullptr); 0236 0237 QVERIFY(job->exec()); // works because it already exists 0238 QCOMPARE(job->error(), 0); 0239 0240 collection1_2 = job->collection(); 0241 QVERIFY(!collection1_2.remoteId().isEmpty()); 0242 QCOMPARE(collection1_2.parentCollection().remoteId(), QStringLiteral("collection1")); 0243 0244 QCOMPARE(md2.subFolderList(), QStringList()); 0245 } 0246 0247 void CollectionCreateTest::testMixedTree() 0248 { 0249 KPIM::Maildir topLevelMd(mDir->path(), true); 0250 QVERIFY(topLevelMd.isValid()); 0251 0252 // simulate a first level MBox 0253 QFileInfo fileInfo1(mDir->path(), QStringLiteral("collection1")); 0254 QFile file1(fileInfo1.absoluteFilePath()); 0255 file1.open(QIODevice::WriteOnly); 0256 file1.close(); 0257 QVERIFY(fileInfo1.exists()); 0258 0259 mStore->setPath(mDir->path()); 0260 0261 FileStore::CollectionCreateJob *job = nullptr; 0262 0263 // test creating first level collections 0264 Collection collection1; 0265 collection1.setName(QStringLiteral("collection1")); 0266 job = mStore->createCollection(collection1, mStore->topLevelCollection()); 0267 QVERIFY(job != nullptr); 0268 0269 QVERIFY(!job->exec()); // fails, there is an MBox with that name 0270 QCOMPARE(job->error(), (int)FileStore::Job::InvalidJobContext); 0271 0272 collection1 = job->collection(); 0273 QVERIFY(collection1.remoteId().isEmpty()); 0274 0275 collection1.setRemoteId(QStringLiteral("collection1")); 0276 collection1.setParentCollection(mStore->topLevelCollection()); 0277 0278 Collection collection2; 0279 collection2.setName(QStringLiteral("collection2")); 0280 job = mStore->createCollection(collection2, mStore->topLevelCollection()); 0281 QVERIFY(job != nullptr); 0282 0283 QVERIFY(job->exec()); 0284 QCOMPARE(job->error(), 0); 0285 0286 collection2 = job->collection(); 0287 QVERIFY(!collection2.remoteId().isEmpty()); 0288 QVERIFY(collection2.parentCollection() == mStore->topLevelCollection()); 0289 0290 // mbox does not show up as a maildir subfolder 0291 QCOMPARE(topLevelMd.subFolderList(), QStringList() << QStringLiteral("collection2")); 0292 KPIM::Maildir md2 = topLevelMd.subFolder(collection2.remoteId()); 0293 QVERIFY(md2.isValid()); 0294 0295 // test creating second level collections inside mbox 0296 Collection collection1_1; 0297 collection1_1.setName(QStringLiteral("collection1_1")); 0298 job = mStore->createCollection(collection1_1, collection1); 0299 QVERIFY(job != nullptr); 0300 0301 QVERIFY(job->exec()); 0302 QCOMPARE(job->error(), 0); 0303 0304 collection1_1 = job->collection(); 0305 QVERIFY(!collection1_1.remoteId().isEmpty()); 0306 QCOMPARE(collection1_1.parentCollection().remoteId(), QStringLiteral("collection1")); 0307 0308 // treat the MBox subdir path like a top level maildir 0309 KPIM::Maildir md1(KPIM::Maildir::subDirPathForFolderPath(fileInfo1.absoluteFilePath()), true); 0310 KPIM::Maildir md1_1 = md1.subFolder(collection1_1.remoteId()); 0311 QVERIFY(md1_1.isValid()); 0312 0313 QCOMPARE(md1.subFolderList(), QStringList() << QStringLiteral("collection1_1")); 0314 } 0315 0316 QTEST_MAIN(CollectionCreateTest) 0317 0318 #include "collectioncreatetest.moc"