File indexing completed on 2025-02-09 06:21:07

0001 /*
0002    SPDX-FileCopyrightText: 2014-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "followupreminderconfigtest.h"
0008 #include "../followupreminderinfo.h"
0009 #include "../followupreminderutil.h"
0010 
0011 #include <QStandardPaths>
0012 #include <QTest>
0013 
0014 FollowUpReminderConfigTest::FollowUpReminderConfigTest(QObject *parent)
0015     : QObject(parent)
0016 {
0017     QStandardPaths::setTestModeEnabled(true);
0018 }
0019 
0020 FollowUpReminderConfigTest::~FollowUpReminderConfigTest() = default;
0021 
0022 void FollowUpReminderConfigTest::init()
0023 {
0024     mConfig = KSharedConfig::openConfig(QStringLiteral("test-followupreminder.rc"), KConfig::SimpleConfig);
0025     mFollowupRegExpFilter = QRegularExpression(QStringLiteral("FollowupReminderItem \\d+"));
0026     cleanup();
0027 }
0028 
0029 void FollowUpReminderConfigTest::cleanup()
0030 {
0031     const QStringList filterGroups = mConfig->groupList();
0032     for (const QString &group : filterGroups) {
0033         mConfig->deleteGroup(group);
0034     }
0035     mConfig->sync();
0036     mConfig->reparseConfiguration();
0037 }
0038 
0039 void FollowUpReminderConfigTest::cleanupTestCase()
0040 {
0041     // Make sure to clean config
0042     cleanup();
0043 }
0044 
0045 void FollowUpReminderConfigTest::shouldConfigBeEmpty()
0046 {
0047     const QStringList filterGroups = mConfig->groupList();
0048     QCOMPARE(filterGroups.isEmpty(), true);
0049 }
0050 
0051 void FollowUpReminderConfigTest::shouldAddAnItem()
0052 {
0053     FollowUpReminder::FollowUpReminderInfo info;
0054     info.setMessageId(QStringLiteral("foo"));
0055     const QDate date(2014, 1, 1);
0056     info.setFollowUpReminderDate(QDate(date));
0057     const QString to = QStringLiteral("kde.org");
0058     info.setTo(to);
0059     FollowUpReminder::FollowUpReminderUtil::writeFollowupReminderInfo(mConfig, &info, false);
0060     const QStringList itemList = mConfig->groupList().filter(mFollowupRegExpFilter);
0061 
0062     QCOMPARE(itemList.isEmpty(), false);
0063     QCOMPARE(itemList.count(), 1);
0064     QCOMPARE(mConfig->hasGroup(QStringLiteral("General")), true);
0065 }
0066 
0067 void FollowUpReminderConfigTest::shouldNotAddAnInvalidItem()
0068 {
0069     FollowUpReminder::FollowUpReminderInfo info;
0070     FollowUpReminder::FollowUpReminderUtil::writeFollowupReminderInfo(mConfig, &info, false);
0071     const QStringList itemList = mConfig->groupList().filter(mFollowupRegExpFilter);
0072     QCOMPARE(itemList.isEmpty(), true);
0073 }
0074 
0075 void FollowUpReminderConfigTest::shouldReplaceItem()
0076 {
0077     FollowUpReminder::FollowUpReminderInfo info;
0078     info.setMessageId(QStringLiteral("foo"));
0079     const QDate date(2014, 1, 1);
0080     info.setFollowUpReminderDate(QDate(date));
0081     const QString to = QStringLiteral("kde.org");
0082     info.setTo(to);
0083     qint32 uniq = 42;
0084     info.setUniqueIdentifier(uniq);
0085     FollowUpReminder::FollowUpReminderUtil::writeFollowupReminderInfo(mConfig, &info, false);
0086     QStringList itemList = mConfig->groupList().filter(mFollowupRegExpFilter);
0087 
0088     QCOMPARE(itemList.count(), 1);
0089 
0090     info.setTo(QStringLiteral("kmail.org"));
0091     FollowUpReminder::FollowUpReminderUtil::writeFollowupReminderInfo(mConfig, &info, false);
0092     itemList = mConfig->groupList().filter(mFollowupRegExpFilter);
0093     QCOMPARE(itemList.count(), 1);
0094 }
0095 
0096 void FollowUpReminderConfigTest::shouldAddSeveralItem()
0097 {
0098     FollowUpReminder::FollowUpReminderInfo info;
0099     info.setMessageId(QStringLiteral("foo"));
0100     const QDate date(2014, 1, 1);
0101     info.setFollowUpReminderDate(QDate(date));
0102     const QString to = QStringLiteral("kde.org");
0103     info.setTo(to);
0104     qint32 uniq = 42;
0105     info.setUniqueIdentifier(uniq);
0106     FollowUpReminder::FollowUpReminderUtil::writeFollowupReminderInfo(mConfig, &info, false);
0107     QStringList itemList = mConfig->groupList().filter(mFollowupRegExpFilter);
0108 
0109     QCOMPARE(itemList.count(), 1);
0110 
0111     info.setTo(QStringLiteral("kmail.org"));
0112     uniq = 43;
0113     info.setUniqueIdentifier(uniq);
0114     FollowUpReminder::FollowUpReminderUtil::writeFollowupReminderInfo(mConfig, &info, false);
0115     itemList = mConfig->groupList().filter(mFollowupRegExpFilter);
0116     QCOMPARE(itemList.count(), 2);
0117 
0118     uniq = 44;
0119     info.setUniqueIdentifier(uniq);
0120     FollowUpReminder::FollowUpReminderUtil::writeFollowupReminderInfo(mConfig, &info, false);
0121     itemList = mConfig->groupList().filter(mFollowupRegExpFilter);
0122     QCOMPARE(itemList.count(), 3);
0123 
0124     // Replace It
0125 
0126     info.setUniqueIdentifier(uniq);
0127     info.setTo(QStringLiteral("kontact.org"));
0128     FollowUpReminder::FollowUpReminderUtil::writeFollowupReminderInfo(mConfig, &info, false);
0129     itemList = mConfig->groupList().filter(mFollowupRegExpFilter);
0130     QCOMPARE(itemList.count(), 3);
0131 
0132     // Add item without uniqIdentifier
0133     FollowUpReminder::FollowUpReminderInfo infoNotHaveUniq;
0134     infoNotHaveUniq.setMessageId(QStringLiteral("foo"));
0135     infoNotHaveUniq.setFollowUpReminderDate(QDate(date));
0136     infoNotHaveUniq.setTo(to);
0137 
0138     FollowUpReminder::FollowUpReminderUtil::writeFollowupReminderInfo(mConfig, &infoNotHaveUniq, false);
0139     itemList = mConfig->groupList().filter(mFollowupRegExpFilter);
0140     QCOMPARE(itemList.count(), 4);
0141     QCOMPARE(infoNotHaveUniq.uniqueIdentifier(), 4);
0142 }
0143 
0144 void FollowUpReminderConfigTest::shouldRemoveItems()
0145 {
0146     FollowUpReminder::FollowUpReminderInfo info;
0147     info.setMessageId(QStringLiteral("foo"));
0148     const QDate date(2014, 1, 1);
0149     info.setFollowUpReminderDate(QDate(date));
0150     const QString to = QStringLiteral("kde.org");
0151     info.setTo(to);
0152     qint32 uniq = 42;
0153     info.setUniqueIdentifier(uniq);
0154     FollowUpReminder::FollowUpReminderUtil::writeFollowupReminderInfo(mConfig, &info, false);
0155     QStringList itemList = mConfig->groupList().filter(mFollowupRegExpFilter);
0156     QCOMPARE(itemList.count(), 1);
0157 
0158     info.setTo(QStringLiteral("kmail.org"));
0159     uniq = 43;
0160     info.setUniqueIdentifier(uniq);
0161     FollowUpReminder::FollowUpReminderUtil::writeFollowupReminderInfo(mConfig, &info, false);
0162     itemList = mConfig->groupList().filter(mFollowupRegExpFilter);
0163     QCOMPARE(itemList.count(), 2);
0164 
0165     uniq = 44;
0166     info.setUniqueIdentifier(uniq);
0167     FollowUpReminder::FollowUpReminderUtil::writeFollowupReminderInfo(mConfig, &info, false);
0168     itemList = mConfig->groupList().filter(mFollowupRegExpFilter);
0169     QCOMPARE(itemList.count(), 3);
0170 
0171     // Add item without uniqIdentifier
0172     FollowUpReminder::FollowUpReminderInfo infoNotHaveUniq;
0173     infoNotHaveUniq.setMessageId(QStringLiteral("foo"));
0174     infoNotHaveUniq.setFollowUpReminderDate(QDate(date));
0175     infoNotHaveUniq.setTo(to);
0176 
0177     FollowUpReminder::FollowUpReminderUtil::writeFollowupReminderInfo(mConfig, &infoNotHaveUniq, false);
0178     itemList = mConfig->groupList().filter(mFollowupRegExpFilter);
0179     QCOMPARE(itemList.count(), 4);
0180     QCOMPARE(infoNotHaveUniq.uniqueIdentifier(), 3);
0181 
0182     QList<qint32> listRemove;
0183     listRemove << 43 << 42;
0184 
0185     const bool elementRemoved = FollowUpReminder::FollowUpReminderUtil::removeFollowupReminderInfo(mConfig, listRemove);
0186     itemList = mConfig->groupList().filter(mFollowupRegExpFilter);
0187     QCOMPARE(itemList.count(), 2);
0188     QVERIFY(elementRemoved);
0189 }
0190 
0191 void FollowUpReminderConfigTest::shouldNotRemoveItemWhenListIsEmpty()
0192 {
0193     QList<qint32> listRemove;
0194     const bool elementRemoved = FollowUpReminder::FollowUpReminderUtil::removeFollowupReminderInfo(mConfig, listRemove);
0195     QVERIFY(!elementRemoved);
0196 }
0197 
0198 void FollowUpReminderConfigTest::shouldNotRemoveItemWhenItemDoesntExist()
0199 {
0200     FollowUpReminder::FollowUpReminderInfo info;
0201     info.setMessageId(QStringLiteral("foo"));
0202     const QDate date(2014, 1, 1);
0203     info.setFollowUpReminderDate(QDate(date));
0204     const QString to = QStringLiteral("kde.org");
0205     info.setTo(to);
0206     qint32 uniq = 42;
0207     info.setUniqueIdentifier(uniq);
0208     FollowUpReminder::FollowUpReminderUtil::writeFollowupReminderInfo(mConfig, &info, false);
0209     QStringList itemList = mConfig->groupList().filter(mFollowupRegExpFilter);
0210 
0211     info.setTo(QStringLiteral("kmail.org"));
0212     uniq = 43;
0213     info.setUniqueIdentifier(uniq);
0214     QCOMPARE(itemList.count(), 1);
0215     FollowUpReminder::FollowUpReminderUtil::writeFollowupReminderInfo(mConfig, &info, false);
0216     itemList = mConfig->groupList().filter(mFollowupRegExpFilter);
0217     QCOMPARE(itemList.count(), 2);
0218 
0219     uniq = 44;
0220     info.setUniqueIdentifier(uniq);
0221     FollowUpReminder::FollowUpReminderUtil::writeFollowupReminderInfo(mConfig, &info, false);
0222 
0223     QList<qint32> listRemove;
0224     listRemove << 55 << 75;
0225     const bool elementRemoved = FollowUpReminder::FollowUpReminderUtil::removeFollowupReminderInfo(mConfig, listRemove);
0226     QVERIFY(!elementRemoved);
0227 }
0228 
0229 QTEST_MAIN(FollowUpReminderConfigTest)
0230 
0231 #include "moc_followupreminderconfigtest.cpp"