File indexing completed on 2024-11-10 04:49:59
0001 /* 0002 SPDX-FileCopyrightText: 2015-2024 Laurent Montel <montel@kde.org> 0003 0004 SPDX-License-Identifier: GPL-2.0-only 0005 */ 0006 0007 #include "filteractionaddheadertest.h" 0008 #include "../filteractions/filteractionaddheader.h" 0009 #include "filter/itemcontext.h" 0010 0011 #include <KLineEdit> 0012 #include <QComboBox> 0013 #include <QLabel> 0014 #include <QTest> 0015 0016 FilterActionAddHeaderTest::FilterActionAddHeaderTest(QObject *parent) 0017 : QObject(parent) 0018 { 0019 } 0020 0021 FilterActionAddHeaderTest::~FilterActionAddHeaderTest() = default; 0022 0023 void FilterActionAddHeaderTest::shouldCreateWidget() 0024 { 0025 MailCommon::FilterActionAddHeader filter; 0026 QWidget *widget = filter.createParamWidget(nullptr); 0027 QVERIFY(widget); 0028 auto comboBox = widget->findChild<QComboBox *>(QStringLiteral("combo")); 0029 QVERIFY(comboBox); 0030 QVERIFY(comboBox->isEditable()); 0031 auto label = widget->findChild<QLabel *>(QStringLiteral("label_value")); 0032 QVERIFY(label); 0033 auto lineEdit = widget->findChild<KLineEdit *>(QStringLiteral("ledit")); 0034 QVERIFY(lineEdit); 0035 QVERIFY(lineEdit->text().isEmpty()); 0036 } 0037 0038 void FilterActionAddHeaderTest::shouldAddValue_data() 0039 { 0040 QTest::addColumn<QString>("argsinput"); 0041 QTest::addColumn<QString>("resultheader"); 0042 QTest::addColumn<QString>("resultvalue"); 0043 QTest::newRow("empty") << QString() << QString() << QString(); 0044 QString val = QLatin1StringView("bla") + QLatin1Char('\t') + QLatin1StringView("blo"); 0045 QTest::newRow("real value") << val << QStringLiteral("bla") << QStringLiteral("blo"); 0046 } 0047 0048 void FilterActionAddHeaderTest::shouldClearWidget() 0049 { 0050 MailCommon::FilterActionAddHeader filter; 0051 QWidget *widget = filter.createParamWidget(nullptr); 0052 auto comboBox = widget->findChild<QComboBox *>(QStringLiteral("combo")); 0053 auto lineEdit = widget->findChild<KLineEdit *>(QStringLiteral("ledit")); 0054 comboBox->lineEdit()->setText(QStringLiteral("test")); 0055 lineEdit->setText(QStringLiteral("blo")); 0056 filter.clearParamWidget(widget); 0057 QVERIFY(comboBox->lineEdit()->text().isEmpty()); 0058 QVERIFY(lineEdit->text().isEmpty()); 0059 } 0060 0061 void FilterActionAddHeaderTest::shouldReturnSieveCode() 0062 { 0063 MailCommon::FilterActionAddHeader filter; 0064 QCOMPARE(filter.sieveRequires().join(QLatin1Char(',')), QStringLiteral("editheader")); 0065 } 0066 0067 void FilterActionAddHeaderTest::shouldBeEmpty() 0068 { 0069 MailCommon::FilterActionAddHeader filter; 0070 QVERIFY(filter.isEmpty()); 0071 0072 filter.argsFromString(QString()); 0073 QVERIFY(filter.isEmpty()); 0074 0075 filter.argsFromString(QStringLiteral("foo\tbla")); 0076 QVERIFY(!filter.isEmpty()); 0077 } 0078 0079 void FilterActionAddHeaderTest::shouldNotExecuteActionWhenParameterIsEmpty() 0080 { 0081 MailCommon::FilterActionAddHeader filter(this); 0082 KMime::Message::Ptr msgPtr = KMime::Message::Ptr(new KMime::Message()); 0083 Akonadi::Item item; 0084 item.setPayload<KMime::Message::Ptr>(msgPtr); 0085 MailCommon::ItemContext context(item, true); 0086 0087 filter.argsFromString(QLatin1StringView("")); 0088 QCOMPARE(filter.process(context, false), MailCommon::FilterAction::ErrorButGoOn); 0089 QCOMPARE(context.needsPayloadStore(), false); 0090 QCOMPARE(context.deleteItem(), false); 0091 QCOMPARE(context.needsFlagStore(), false); 0092 } 0093 0094 void FilterActionAddHeaderTest::shouldNotExecuteActionWhenValueIsEmpty() 0095 { 0096 MailCommon::FilterActionAddHeader filter(this); 0097 KMime::Message::Ptr msgPtr = KMime::Message::Ptr(new KMime::Message()); 0098 Akonadi::Item item; 0099 item.setPayload<KMime::Message::Ptr>(msgPtr); 0100 MailCommon::ItemContext context(item, true); 0101 0102 filter.argsFromString(QStringLiteral("foo")); 0103 QCOMPARE(filter.process(context, false), MailCommon::FilterAction::ErrorButGoOn); 0104 QCOMPARE(context.needsPayloadStore(), false); 0105 QCOMPARE(context.deleteItem(), false); 0106 QCOMPARE(context.needsFlagStore(), false); 0107 } 0108 0109 void FilterActionAddHeaderTest::shouldAddNewHeaderWhenNotExistingHeader() 0110 { 0111 const QByteArray data = 0112 "From: foo@kde.org\n" 0113 "To: foo@kde.org\n" 0114 "Subject: test\n" 0115 "Date: Wed, 01 Apr 2015 09:33:01 +0200\n" 0116 "MIME-Version: 1.0\n" 0117 "Content-Type: text/plain; charset=\"us-ascii\"\n" 0118 "\n" 0119 "test"; 0120 const QByteArray output = 0121 "From: foo@kde.org\n" 0122 "To: foo@kde.org\n" 0123 "Subject: test\n" 0124 "Date: Wed, 01 Apr 2015 09:33:01 +0200\n" 0125 "MIME-Version: 1.0\n" 0126 "Content-Type: text/plain; charset=\"us-ascii\"\n" 0127 "testheader: foo\n" 0128 "\n" 0129 "test"; 0130 0131 MailCommon::FilterActionAddHeader filter(this); 0132 KMime::Message::Ptr msgPtr = KMime::Message::Ptr(new KMime::Message()); 0133 msgPtr->setContent(data); 0134 msgPtr->parse(); 0135 Akonadi::Item item; 0136 item.setPayload<KMime::Message::Ptr>(msgPtr); 0137 MailCommon::ItemContext context(item, true); 0138 0139 filter.argsFromString(QStringLiteral("testheader\tfoo")); 0140 QCOMPARE(filter.process(context, false), MailCommon::FilterAction::GoOn); 0141 QCOMPARE(context.needsPayloadStore(), true); 0142 QCOMPARE(msgPtr->encodedContent(), output); 0143 QCOMPARE(context.deleteItem(), false); 0144 QCOMPARE(context.needsFlagStore(), false); 0145 } 0146 0147 void FilterActionAddHeaderTest::shouldReplaceHeaderWhenExistingHeader() 0148 { 0149 const QByteArray data = 0150 "From: foo@kde.org\n" 0151 "To: foo@kde.org\n" 0152 "Subject: test\n" 0153 "Date: Wed, 01 Apr 2015 09:33:01 +0200\n" 0154 "MIME-Version: 1.0\n" 0155 "Content-Type: text/plain; charset=\"us-ascii\"\n" 0156 "testheader: bla\n" 0157 "\n" 0158 "test"; 0159 const QByteArray output = 0160 "From: foo@kde.org\n" 0161 "To: foo@kde.org\n" 0162 "Subject: test\n" 0163 "Date: Wed, 01 Apr 2015 09:33:01 +0200\n" 0164 "MIME-Version: 1.0\n" 0165 "Content-Type: text/plain; charset=\"us-ascii\"\n" 0166 "testheader: foo\n" 0167 "\n" 0168 "test"; 0169 0170 MailCommon::FilterActionAddHeader filter(this); 0171 KMime::Message::Ptr msgPtr = KMime::Message::Ptr(new KMime::Message()); 0172 msgPtr->setContent(data); 0173 msgPtr->parse(); 0174 Akonadi::Item item; 0175 item.setPayload<KMime::Message::Ptr>(msgPtr); 0176 MailCommon::ItemContext context(item, true); 0177 0178 filter.argsFromString(QStringLiteral("testheader\tfoo")); 0179 QCOMPARE(filter.process(context, false), MailCommon::FilterAction::GoOn); 0180 QCOMPARE(context.needsPayloadStore(), true); 0181 QCOMPARE(msgPtr->encodedContent(), output); 0182 QCOMPARE(context.deleteItem(), false); 0183 QCOMPARE(context.needsFlagStore(), false); 0184 } 0185 0186 void FilterActionAddHeaderTest::shouldAddValue() 0187 { 0188 QFETCH(QString, argsinput); 0189 QFETCH(QString, resultheader); 0190 QFETCH(QString, resultvalue); 0191 0192 MailCommon::FilterActionAddHeader filter; 0193 QWidget *widget = filter.createParamWidget(nullptr); 0194 filter.argsFromString(argsinput); 0195 filter.setParamWidgetValue(widget); 0196 auto comboBox = widget->findChild<QComboBox *>(QStringLiteral("combo")); 0197 auto lineEdit = widget->findChild<KLineEdit *>(QStringLiteral("ledit")); 0198 QCOMPARE(comboBox->lineEdit()->text(), resultheader); 0199 QCOMPARE(lineEdit->text(), resultvalue); 0200 } 0201 0202 void FilterActionAddHeaderTest::shouldRequiresSieve() 0203 { 0204 MailCommon::FilterActionAddHeader filter; 0205 QCOMPARE(filter.sieveRequires(), QStringList() << QStringLiteral("editheader")); 0206 } 0207 0208 QTEST_MAIN(FilterActionAddHeaderTest) 0209 0210 #include "moc_filteractionaddheadertest.cpp"