File indexing completed on 2025-02-16 04:57:40

0001 /*
0002    SPDX-FileCopyrightText: 2017-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "templateparserjobtest.h"
0008 
0009 #include "templateparserjob.h"
0010 #include "templateparserjob_p.h"
0011 #include <KIdentityManagementCore/Identity>
0012 #include <KIdentityManagementCore/IdentityManager>
0013 #include <MimeTreeParser/ObjectTreeParser>
0014 
0015 #include <QDir>
0016 #include <QLocale>
0017 #include <QSignalSpy>
0018 #include <QStandardPaths>
0019 #include <QTest>
0020 
0021 using namespace MimeTreeParser;
0022 
0023 TemplateParserJobTest::TemplateParserJobTest(QObject *parent)
0024     : QObject(parent)
0025 {
0026     QStandardPaths::setTestModeEnabled(true);
0027 }
0028 
0029 TemplateParserJobTest::~TemplateParserJobTest()
0030 {
0031     // Workaround QTestLib not flushing deleteLater()s on exit, which leads to WebEngine asserts (view not deleted)
0032     QCoreApplication::sendPostedEvents(nullptr, QEvent::DeferredDelete);
0033 }
0034 
0035 void TemplateParserJobTest::test_convertedHtml_data()
0036 {
0037     QTest::addColumn<QString>("mailFileName");
0038     QTest::addColumn<QString>("referenceFileName");
0039 
0040     QDir dir(QStringLiteral(MAIL_DATA_DIR));
0041     const auto l = dir.entryList(QStringList(QStringLiteral("plain*.mbox")), QDir::Files | QDir::Readable | QDir::NoSymLinks);
0042     for (const QString &file : l) {
0043         QTest::newRow(file.toLatin1().constData()) << QString(dir.path() + QLatin1Char('/') + file)
0044                                                    << QString(dir.path() + QLatin1Char('/') + file + QLatin1StringView(".html"));
0045     }
0046 }
0047 
0048 void TemplateParserJobTest::test_convertedHtml()
0049 {
0050     QFETCH(QString, mailFileName);
0051     QFETCH(QString, referenceFileName);
0052 
0053     // load input mail
0054     QFile mailFile(mailFileName);
0055     QVERIFY(mailFile.open(QIODevice::ReadOnly));
0056     const QByteArray mailData = KMime::CRLFtoLF(mailFile.readAll());
0057     QVERIFY(!mailData.isEmpty());
0058     KMime::Message::Ptr msg(new KMime::Message);
0059     KMime::Message::Ptr origMsg(new KMime::Message);
0060     origMsg->setContent(mailData);
0061     origMsg->parse();
0062     QCOMPARE(origMsg->subject()->as7BitString(false).constData(), "Plain Message Test");
0063     QCOMPARE(origMsg->contents().size(), 0);
0064 
0065     // load expected result
0066     QFile referenceFile(referenceFileName);
0067     QVERIFY(referenceFile.open(QIODevice::ReadOnly));
0068     const QByteArray referenceRawData = KMime::CRLFtoLF(referenceFile.readAll());
0069     const QString referenceData = QString::fromLatin1(referenceRawData);
0070     QVERIFY(!referenceData.isEmpty());
0071 
0072     auto parser = new TemplateParser::TemplateParserJob(msg, TemplateParser::TemplateParserJob::NewMessage);
0073     auto identMan = new KIdentityManagementCore::IdentityManager;
0074     parser->setIdentityManager(identMan);
0075 
0076     parser->d->mOrigMsg = origMsg;
0077 
0078     QSignalSpy spy(parser, &TemplateParser::TemplateParserJob::parsingDone);
0079     parser->processWithTemplate(QString());
0080     QVERIFY(spy.wait());
0081     QVERIFY(parser->d->mOtp->htmlContent().isEmpty());
0082     QVERIFY(!parser->d->mOtp->plainTextContent().isEmpty());
0083 
0084     const QString convertedHtmlContent = parser->htmlMessageText(false, TemplateParser::TemplateParserJob::NoSelectionAllowed);
0085     QVERIFY(!convertedHtmlContent.isEmpty());
0086 
0087     QCOMPARE(convertedHtmlContent, referenceData);
0088     msg.clear();
0089     origMsg.clear();
0090     delete parser;
0091 }
0092 
0093 void TemplateParserJobTest::test_replyHtml_data()
0094 {
0095     QTest::addColumn<QString>("mailFileName");
0096     QTest::addColumn<QString>("referenceFileName");
0097 
0098     QDir dir(QStringLiteral(MAIL_DATA_DIR));
0099     const auto l = dir.entryList(QStringList(QStringLiteral("*.mbox")), QDir::Files | QDir::Readable | QDir::NoSymLinks);
0100     for (const QString &file : l) {
0101         const QString expectedFile = dir.path() + QLatin1Char('/') + file + QStringLiteral(".html.reply");
0102         if (!QFile::exists(expectedFile)) {
0103             continue;
0104         }
0105         QTest::newRow(file.toLatin1().constData()) << QString(dir.path() + QLatin1Char('/') + file) << expectedFile;
0106     }
0107 }
0108 
0109 void TemplateParserJobTest::test_replyHtml()
0110 {
0111     QFETCH(QString, mailFileName);
0112     QFETCH(QString, referenceFileName);
0113 
0114     // load input mail
0115     QFile mailFile(mailFileName);
0116     QVERIFY(mailFile.open(QIODevice::ReadOnly));
0117     const QByteArray mailData = KMime::CRLFtoLF(mailFile.readAll());
0118     QVERIFY(!mailData.isEmpty());
0119     KMime::Message::Ptr msg(new KMime::Message);
0120     KMime::Message::Ptr origMsg(new KMime::Message);
0121     origMsg->setContent(mailData);
0122     origMsg->parse();
0123 
0124     // load expected result
0125     QFile referenceFile(referenceFileName);
0126     QVERIFY(referenceFile.open(QIODevice::ReadOnly));
0127     const QByteArray referenceRawData = KMime::CRLFtoLF(referenceFile.readAll());
0128     const QString referenceData = QString::fromLatin1(referenceRawData);
0129     QVERIFY(!referenceData.isEmpty());
0130 
0131     auto parser = new TemplateParser::TemplateParserJob(msg, TemplateParser::TemplateParserJob::NewMessage);
0132     auto identMan = new KIdentityManagementCore::IdentityManager;
0133     parser->setIdentityManager(identMan);
0134 
0135     parser->d->mOrigMsg = origMsg;
0136 
0137     QSignalSpy spy(parser, &TemplateParser::TemplateParserJob::parsingDone);
0138     parser->processWithTemplate(QString());
0139     QVERIFY(spy.wait());
0140 
0141     QString convertedHtmlContent = parser->htmlMessageText(false, TemplateParser::TemplateParserJob::NoSelectionAllowed);
0142     QVERIFY(!convertedHtmlContent.isEmpty());
0143 
0144     // referenceData is read from a file and most text editors add a \n at the end of the last line
0145     if (!convertedHtmlContent.endsWith(QLatin1Char('\n'))) {
0146         convertedHtmlContent += QStringLiteral("\n");
0147     }
0148 
0149     QCOMPARE(convertedHtmlContent, referenceData);
0150     msg.clear();
0151     origMsg.clear();
0152     delete parser;
0153 }
0154 
0155 void TemplateParserJobTest::test_replyPlain_data()
0156 {
0157     QTest::addColumn<QString>("mailFileName");
0158     QTest::addColumn<QString>("referenceFileName");
0159 
0160     QDir dir(QStringLiteral(MAIL_DATA_DIR));
0161     const auto l = dir.entryList(QStringList(QStringLiteral("*.mbox")), QDir::Files | QDir::Readable | QDir::NoSymLinks);
0162     for (const QString &file : l) {
0163         const QString expectedFile = dir.path() + QLatin1Char('/') + file + QStringLiteral(".plain.reply");
0164         if (!QFile::exists(expectedFile)) {
0165             continue;
0166         }
0167         QTest::newRow(file.toLatin1().constData()) << QString(dir.path() + QLatin1Char('/') + file) << expectedFile;
0168     }
0169 }
0170 
0171 void TemplateParserJobTest::test_replyPlain()
0172 {
0173     QFETCH(QString, mailFileName);
0174     QFETCH(QString, referenceFileName);
0175 
0176     // load input mail
0177     QFile mailFile(mailFileName);
0178     QVERIFY(mailFile.open(QIODevice::ReadOnly));
0179     const QByteArray mailData = KMime::CRLFtoLF(mailFile.readAll());
0180     QVERIFY(!mailData.isEmpty());
0181     KMime::Message::Ptr msg(new KMime::Message);
0182     KMime::Message::Ptr origMsg(new KMime::Message);
0183     origMsg->setContent(mailData);
0184     origMsg->parse();
0185 
0186     // load expected result
0187     QFile referenceFile(referenceFileName);
0188     QVERIFY(referenceFile.open(QIODevice::ReadOnly));
0189     const QByteArray referenceRawData = KMime::CRLFtoLF(referenceFile.readAll());
0190     const QString referenceData = QString::fromLatin1(referenceRawData);
0191     QVERIFY(!referenceData.isEmpty());
0192 
0193     auto parser = new TemplateParser::TemplateParserJob(msg, TemplateParser::TemplateParserJob::Reply);
0194     parser->d->mOrigMsg = origMsg;
0195 
0196     QSignalSpy spy(parser, &TemplateParser::TemplateParserJob::parsingDone);
0197     parser->processWithTemplate(QString());
0198     QVERIFY(spy.wait());
0199 
0200     const QString convertedPlainTextContent = parser->plainMessageText(false, TemplateParser::TemplateParserJob::NoSelectionAllowed);
0201 
0202     QCOMPARE(convertedPlainTextContent, referenceData);
0203     msg.clear();
0204     origMsg.clear();
0205     delete parser;
0206 }
0207 
0208 void TemplateParserJobTest::test_forwardPlain_data()
0209 {
0210     QTest::addColumn<QString>("mailFileName");
0211     QTest::addColumn<QString>("referenceFileName");
0212 
0213     QDir dir(QStringLiteral(MAIL_DATA_DIR));
0214     const auto l = dir.entryList(QStringList(QStringLiteral("*.mbox")), QDir::Files | QDir::Readable | QDir::NoSymLinks);
0215     for (const QString &file : l) {
0216         const QString expectedFile = dir.path() + QLatin1Char('/') + file + QStringLiteral(".plain.reply");
0217         if (!QFile::exists(expectedFile)) {
0218             continue;
0219         }
0220         QTest::newRow(file.toLatin1().constData()) << QString(dir.path() + QLatin1Char('/') + file) << expectedFile;
0221     }
0222 }
0223 
0224 void TemplateParserJobTest::test_forwardPlain()
0225 {
0226     QFETCH(QString, mailFileName);
0227     QFETCH(QString, referenceFileName);
0228 
0229     // load input mail
0230     QFile mailFile(mailFileName);
0231     QVERIFY(mailFile.open(QIODevice::ReadOnly));
0232     const QByteArray mailData = KMime::CRLFtoLF(mailFile.readAll());
0233     QVERIFY(!mailData.isEmpty());
0234     KMime::Message::Ptr msg(new KMime::Message);
0235     KMime::Message::Ptr origMsg(new KMime::Message);
0236     origMsg->setContent(mailData);
0237     origMsg->parse();
0238 
0239     // load expected result
0240     QFile referenceFile(referenceFileName);
0241     QVERIFY(referenceFile.open(QIODevice::ReadOnly));
0242     const QByteArray referenceRawData = KMime::CRLFtoLF(referenceFile.readAll());
0243     const QString referenceData = QString::fromLatin1(referenceRawData);
0244     QVERIFY(!referenceData.isEmpty());
0245 
0246     auto parser = new TemplateParser::TemplateParserJob(msg, TemplateParser::TemplateParserJob::Forward);
0247     parser->d->mOrigMsg = origMsg;
0248 
0249     QSignalSpy spy(parser, &TemplateParser::TemplateParserJob::parsingDone);
0250     parser->processWithTemplate(QString());
0251     QVERIFY(spy.wait());
0252 
0253     const QString convertedPlainTextContent = parser->plainMessageText(false, TemplateParser::TemplateParserJob::NoSelectionAllowed);
0254 
0255     QCOMPARE(convertedPlainTextContent, referenceData);
0256     msg.clear();
0257     origMsg.clear();
0258     delete parser;
0259 }
0260 
0261 void TemplateParserJobTest::test_forwardHtml_data()
0262 {
0263     QTest::addColumn<QString>("mailFileName");
0264     QTest::addColumn<QString>("referenceFileName");
0265 
0266     QDir dir(QStringLiteral(MAIL_DATA_DIR));
0267     const auto l = dir.entryList(QStringList(QStringLiteral("*.mbox")), QDir::Files | QDir::Readable | QDir::NoSymLinks);
0268     for (const QString &file : l) {
0269         const QString expectedFile = dir.path() + QLatin1Char('/') + file + QStringLiteral(".html.reply");
0270         if (!QFile::exists(expectedFile)) {
0271             continue;
0272         }
0273         QTest::newRow(file.toLatin1().constData()) << QString(dir.path() + QLatin1Char('/') + file) << expectedFile;
0274     }
0275 }
0276 
0277 void TemplateParserJobTest::test_forwardHtml()
0278 {
0279     QFETCH(QString, mailFileName);
0280     QFETCH(QString, referenceFileName);
0281 
0282     // load input mail
0283     QFile mailFile(mailFileName);
0284     QVERIFY(mailFile.open(QIODevice::ReadOnly));
0285     const QByteArray mailData = KMime::CRLFtoLF(mailFile.readAll());
0286     QVERIFY(!mailData.isEmpty());
0287     KMime::Message::Ptr msg(new KMime::Message);
0288     KMime::Message::Ptr origMsg(new KMime::Message);
0289     origMsg->setContent(mailData);
0290     origMsg->parse();
0291 
0292     // load expected result
0293     QFile referenceFile(referenceFileName);
0294     QVERIFY(referenceFile.open(QIODevice::ReadOnly));
0295     const QByteArray referenceRawData = KMime::CRLFtoLF(referenceFile.readAll());
0296     const QString referenceData = QString::fromLatin1(referenceRawData);
0297     QVERIFY(!referenceData.isEmpty());
0298 
0299     auto parser = new TemplateParser::TemplateParserJob(msg, TemplateParser::TemplateParserJob::Forward);
0300     parser->d->mOrigMsg = origMsg;
0301 
0302     QSignalSpy spy(parser, &TemplateParser::TemplateParserJob::parsingDone);
0303     parser->processWithTemplate(QString());
0304     QVERIFY(spy.wait());
0305 
0306     QString convertedHtmlContent = parser->htmlMessageText(false, TemplateParser::TemplateParserJob::NoSelectionAllowed);
0307     // referenceData is read from a file and most text editors add a \n at the end of the last line
0308     if (!convertedHtmlContent.endsWith(QLatin1Char('\n'))) {
0309         convertedHtmlContent += QStringLiteral("\n");
0310     }
0311 
0312     QCOMPARE(convertedHtmlContent, referenceData);
0313     msg.clear();
0314     origMsg.clear();
0315     delete parser;
0316 }
0317 
0318 void TemplateParserJobTest::test_forwardedAttachments_data()
0319 {
0320     QTest::addColumn<QString>("mailFileName");
0321     QTest::addColumn<QString>("referenceFileName");
0322 
0323     QDir dir(QStringLiteral(MAIL_DATA_DIR));
0324     const auto l = dir.entryList(QStringList(QStringLiteral("*.mbox")), QDir::Files | QDir::Readable | QDir::NoSymLinks);
0325     for (const QString &file : l) {
0326         if (!QFile::exists(dir.path() + QLatin1Char('/') + file + QStringLiteral(".html.reply"))) {
0327             continue;
0328         }
0329         QString expectedFile = dir.path() + QLatin1Char('/') + file + QStringLiteral(".forwarded.mbox");
0330         QTest::newRow(file.toLatin1().constData()) << QString(dir.path() + QLatin1Char('/') + file) << expectedFile;
0331     }
0332 }
0333 
0334 void TemplateParserJobTest::test_forwardedAttachments()
0335 {
0336     QFETCH(QString, mailFileName);
0337     QFETCH(QString, referenceFileName);
0338 
0339     // load input mail
0340     QFile mailFile(mailFileName);
0341     QVERIFY(mailFile.open(QIODevice::ReadOnly));
0342     const QByteArray mailData = KMime::CRLFtoLF(mailFile.readAll());
0343     QVERIFY(!mailData.isEmpty());
0344     KMime::Message::Ptr msg(new KMime::Message);
0345     KMime::Message::Ptr origMsg(new KMime::Message);
0346     origMsg->setContent(mailData);
0347     origMsg->parse();
0348 
0349     bool referenceExists = QFile::exists(referenceFileName);
0350 
0351     auto parser = new TemplateParser::TemplateParserJob(msg, TemplateParser::TemplateParserJob::Forward);
0352     parser->d->mOrigMsg = origMsg;
0353 
0354     QSignalSpy spy(parser, &TemplateParser::TemplateParserJob::parsingDone);
0355     parser->processWithTemplate(QString());
0356     QVERIFY(spy.wait());
0357 
0358     if (referenceExists) {
0359         QFile referenceFile(referenceFileName);
0360         QVERIFY(referenceFile.open(QIODevice::ReadOnly));
0361         const QByteArray referenceRawData = KMime::CRLFtoLF(referenceFile.readAll());
0362         KMime::Message::Ptr referenceMsg(new KMime::Message);
0363         referenceMsg->setContent(referenceRawData);
0364         referenceMsg->parse();
0365 
0366         QCOMPARE(msg->contents().size(), referenceMsg->contents().size());
0367         for (int i = 1; i < msg->contents().size(); i++) {
0368             QCOMPARE(msg->contents()[i]->encodedContent(), referenceMsg->contents()[i]->encodedContent());
0369         }
0370         referenceMsg.clear();
0371     } else {
0372         QCOMPARE(msg->contents().size(), 0);
0373     }
0374     msg.clear();
0375     origMsg.clear();
0376     delete parser;
0377 }
0378 
0379 void TemplateParserJobTest::test_processWithTemplatesForBody_data()
0380 {
0381     QTest::addColumn<QString>("command");
0382     QTest::addColumn<QString>("text");
0383     QTest::addColumn<QString>("expected");
0384     QTest::addColumn<QString>("selection");
0385 
0386     QTest::newRow("%OTEXT-plain") << "%OTEXT"
0387                                   << "Original text.\nLine two."
0388                                   << "Original text.\nLine two."
0389                                   << "";
0390     QTest::newRow("%OTEXT-encrypted") << "%OTEXT"
0391                                       << "-----BEGIN PGP MESSAGE-----\nVersion: GnuPG v1.4.12 (GNU/Linux)\n"
0392                                          "\n"
0393                                          "hQEMAwzOQ1qnzNo7AQgA1345CrnOBTGf2eo4ABR6wkOdasI9SELRBKA1fNkFcq+Z\n"
0394                                          "Qg0gWB5RLapU+VFRc5hK1zPOZ1dY6j3+uPHO4RhjfUgfiZ8T7oaWav15yP+07u21\n"
0395                                          "EI9W9sk+eQU9GZSOayURucmZa/mbBz9hrsmePpORxD+C3uNTYa6ePTFlQP6wEZOI\n"
0396                                          "7E53DrtJnF0EzIsCBIVep6CyuYfuSSwQ5gMgyPzfBqiGHNw96w2i/eayErc6lquL\n"
0397                                          "JPFhIcMMq8w9Yo9+vXCAbkns6dtBAzlnAzuV86VFUZ/MnHTlCNk2yHyGLP6BS6hG\n"
0398                                          "kFEUmgdHrGRizdz1sjo1tSmOLu+Gyjlv1Ir/Sqr8etJQAeTq3heKslAfhtotAMMt\n"
0399                                          "R3tk228Su13Q3CAP/rktAyuGMDFtH8klW09zFdsZBDu8svE6d9e2nZ541NGspFVI\n"
0400                                          "6XTZHUMMdlgnTBcu3aPc0ow=\n"
0401                                          "=0xtc\n"
0402                                          "-----END PGP MESSAGE-----"
0403                                       << "Crypted line.\nCrypted line two."
0404                                       << "";
0405     QTest::newRow("%QUOTE") << "%QUOTE"
0406                             << "Quoted text.\nLine two."
0407                             << "> Quoted text.\n> Line two."
0408                             << "";
0409 }
0410 
0411 void TemplateParserJobTest::test_processWithTemplatesForBody()
0412 {
0413     QFETCH(QString, command);
0414     QFETCH(QString, text);
0415     QFETCH(QString, expected);
0416     QFETCH(QString, selection);
0417 
0418     KMime::Message::Ptr msg(new KMime::Message());
0419     KMime::Message::Ptr origMsg(new KMime::Message());
0420     origMsg->setBody(text.toLocal8Bit());
0421     origMsg->parse();
0422     auto parser = new TemplateParser::TemplateParserJob(msg, TemplateParser::TemplateParserJob::Reply);
0423     parser->setSelection(selection);
0424     auto identMan = new KIdentityManagementCore::IdentityManager;
0425     parser->setIdentityManager(identMan);
0426     parser->setAllowDecryption(true);
0427     parser->d->mOrigMsg = origMsg;
0428 
0429     QSignalSpy spy(parser, &TemplateParser::TemplateParserJob::parsingDone);
0430     parser->processWithTemplate(command);
0431     QVERIFY(spy.wait());
0432 
0433     identMan->deleteLater();
0434     QCOMPARE(QString::fromLatin1(msg->encodedBody()), expected);
0435     QCOMPARE(spy.count(), 1);
0436     delete parser;
0437 }
0438 
0439 void TemplateParserJobTest::test_processWithTemplatesForContent_data()
0440 {
0441     QTest::addColumn<QString>("command");
0442     QTest::addColumn<QString>("mailFileName");
0443     QTest::addColumn<QString>("expectedBody");
0444     QTest::addColumn<bool>("hasDictionary");
0445     qputenv("TZ", "Europe/Paris");
0446     QDir dir(QStringLiteral(MAIL_DATA_DIR));
0447     const QString file = QStringLiteral("plain-message.mbox");
0448     const QString fileName = QString(dir.path() + QLatin1Char('/') + file);
0449     QTest::newRow("%OTIME") << "%OTIME" << fileName << QLocale().toString(QTime(8, 0, 27), QLocale::ShortFormat) << false;
0450     QTest::newRow("%OTIMELONG") << "%OTIMELONG" << fileName << QLocale().toString(QTime(8, 0, 27), QLocale::LongFormat) << false;
0451     QTest::newRow("%OTIMELONGEN") << "%OTIMELONGEN" << fileName << QLocale(QLocale::C).toString(QTime(8, 0, 27), QLocale::LongFormat) << false;
0452     QTest::newRow("%ODATE") << "%ODATE" << fileName << QLocale().toString(QDate(2011, 8, 7), QLocale::LongFormat) << false;
0453     QTest::newRow("%ODATESHORT") << "%ODATESHORT" << fileName << QLocale().toString(QDate(2011, 8, 7), QLocale::ShortFormat) << false;
0454     QTest::newRow("%ODATEEN") << "%ODATEEN" << fileName << QLocale::c().toString(QDate(2011, 8, 7), QLocale::LongFormat) << false;
0455     QTest::newRow("%OFULLSUBJ") << "%OFULLSUBJ" << fileName << "Plain Message Test" << false;
0456     QTest::newRow("%OFULLSUBJECT") << "%OFULLSUBJECT" << fileName << "Plain Message Test" << false;
0457     QTest::newRow("%OFROMFNAME") << "%OFROMFNAME" << fileName << "Sudhendu" << false;
0458     QTest::newRow("%OFROMLNAME") << "%OFROMLNAME" << fileName << "Kumar" << false;
0459     QTest::newRow("%OFROMNAME") << "%OFROMNAME" << fileName << "Sudhendu Kumar" << false;
0460     QTest::newRow("%OFROMADDR") << "%OFROMADDR" << fileName << "Sudhendu Kumar <dontspamme@yoohoo.com>" << false;
0461     QTest::newRow("%OTOADDR") << "%OTOADDR" << fileName << "kde <foo@yoohoo.org>" << false;
0462     QTest::newRow("%OTOFNAME") << "%OTOFNAME" << fileName << "kde" << false;
0463     QTest::newRow("%OTONAME") << "%OTONAME" << fileName << "kde" << false;
0464     QTest::newRow("%OTOLNAME") << "%OTOLNAME" << fileName << "kde" << false;
0465     QTest::newRow("%OTOLIST") << "%OTOLIST" << fileName << "kde <foo@yoohoo.org>" << false;
0466     QTest::newRow("%ODOW") << "%ODOW" << fileName << QLocale().dayName(7, QLocale::LongFormat) << false;
0467     QTest::newRow("%BLANK") << "%BLANK" << fileName << "" << false;
0468     QTest::newRow("%NOP") << "%NOP" << fileName << "" << false;
0469     QTest::newRow("%DICTIONARYLANGUAGE=\"en\"") << "%DICTIONARYLANGUAGE=\"en\"" << fileName << "" << true;
0470     QTest::newRow("%DICTIONARYLANGUAGE=\"\"") << "%DICTIONARYLANGUAGE=\"\"" << fileName << "" << false;
0471     QTest::newRow("%OTIMELONG %OFULLSUBJECT") << "%OTIMELONG %OFULLSUBJECT" << fileName
0472                                               << QLocale().toString(QTime(8, 0, 27), QLocale::LongFormat) + QStringLiteral(" Plain Message Test") << false;
0473     QTest::newRow("%OTIMELONG\n%OFULLSUBJECT") << "%OTIMELONG\n%OFULLSUBJECT" << fileName
0474                                                << QLocale().toString(QTime(8, 0, 27), QLocale::LongFormat) + QStringLiteral("\nPlain Message Test") << false;
0475     QTest::newRow("%REM=\"sdfsfsdsdfsdf\"") << "%REM=\"sdfsfsdsdfsdf\"" << fileName << "" << false;
0476     QTest::newRow("%CLEAR") << "%CLEAR" << fileName << "" << false;
0477     QTest::newRow("FOO foo") << "FOO foo" << fileName << "FOO foo" << false;
0478     const QString insertFileName = QString(dir.path() + QLatin1Char('/') + QLatin1StringView("insert-file.txt"));
0479     QString insertFileNameCommand = QStringLiteral("%INSERT=\"%1\"").arg(insertFileName);
0480     QTest::newRow("%INSERT") << insertFileNameCommand << fileName << "test insert file!\n" << false;
0481     insertFileNameCommand = QStringLiteral("%PUT=\"%1\"").arg(insertFileName);
0482     QTest::newRow("%PUT") << insertFileNameCommand << fileName << "test insert file!\n" << false;
0483     QTest::newRow("%OMSGID") << "%OMSGID" << fileName << "<20150@foo.kde.org>" << false;
0484     QTest::newRow("%SYSTEM") << "%SYSTEM=\"echo foo\"" << fileName << "foo\n" << false;
0485     QTest::newRow("%DEBUG") << "%DEBUG" << fileName << "" << false;
0486     QTest::newRow("%DEBUGOFF") << "%DEBUGOFF" << fileName << "" << false;
0487     QTest::newRow("%HEADER=\"Reply-To\"") << "%HEADER=\"Reply-To\"" << fileName << "bla@yoohoo.org" << false;
0488     // Header doesn't exist => don't add value
0489     QTest::newRow("%OHEADER=\"SSS\"") << "%HEADER=\"SSS\"" << fileName << "" << false;
0490     QTest::newRow("%OHEADER=\"To\"") << "%OHEADER=\"To\"" << fileName << "kde <foo@yoohoo.org>" << false;
0491     QTest::newRow("%HEADER( To )") << "%HEADER( To )" << fileName << "kde <foo@yoohoo.org>" << false;
0492     QTest::newRow("%HEADER( To ) second test ") << "foo %HEADER( To )" << fileName << "foo kde <foo@yoohoo.org>" << false;
0493     // Unknown command
0494     QTest::newRow("unknown command") << "%GGGGG" << fileName << "%GGGGG" << false;
0495 
0496     // Test bug 308444
0497     const QString file2 = QStringLiteral("plain-message-timezone.mbox");
0498     const QString fileName2 = QString(dir.path() + QLatin1Char('/') + file2);
0499     QTest::newRow("bug308444-%OTIMELONG") << "%OTIMELONG" << fileName2 << QLocale::system().toString(QTime(20, 31, 25), QLocale::LongFormat) << false;
0500 }
0501 
0502 void TemplateParserJobTest::test_processWithTemplatesForContent()
0503 {
0504     QFETCH(QString, command);
0505     QFETCH(QString, mailFileName);
0506     QFETCH(QString, expectedBody);
0507     QFETCH(bool, hasDictionary);
0508 
0509     QFile mailFile(mailFileName);
0510     QVERIFY(mailFile.open(QIODevice::ReadOnly));
0511     const QByteArray mailData = KMime::CRLFtoLF(mailFile.readAll());
0512     QVERIFY(!mailData.isEmpty());
0513     KMime::Message::Ptr msg(new KMime::Message);
0514     KMime::Message::Ptr origMsg(new KMime::Message);
0515     origMsg->setContent(mailData);
0516     origMsg->parse();
0517 
0518     auto parser = new TemplateParser::TemplateParserJob(msg, TemplateParser::TemplateParserJob::Reply);
0519     auto identMan = new KIdentityManagementCore::IdentityManager;
0520     parser->setIdentityManager(identMan);
0521     parser->setAllowDecryption(false);
0522     parser->d->mOrigMsg = origMsg;
0523     QSignalSpy spy(parser, &TemplateParser::TemplateParserJob::parsingDone);
0524     parser->processWithTemplate(command);
0525     QVERIFY(spy.wait());
0526     QCOMPARE(msg->hasHeader("X-KMail-Dictionary"), hasDictionary);
0527 
0528     identMan->deleteLater();
0529     QCOMPARE(QString::fromUtf8(msg->encodedBody()), expectedBody);
0530     QCOMPARE(spy.count(), 1);
0531 }
0532 
0533 void TemplateParserJobTest::test_processWithTemplatesForContentOtherTimeZone_data()
0534 {
0535     QTest::addColumn<QString>("command");
0536     QTest::addColumn<QString>("mailFileName");
0537     QTest::addColumn<QString>("expectedBody");
0538     QTest::addColumn<bool>("hasDictionary");
0539     qputenv("TZ", "America/New_York");
0540     QDir dir(QStringLiteral(MAIL_DATA_DIR));
0541     // Test bug 308444
0542     const QString file2 = QStringLiteral("plain-message-timezone.mbox");
0543     const QString fileName2 = QString(dir.path() + QLatin1Char('/') + file2);
0544     QTest::newRow("bug308444-%OTIMELONG") << "%OTIMELONG" << fileName2 << QLocale::system().toString(QTime(14, 31, 25), QLocale::LongFormat) << false;
0545 }
0546 
0547 void TemplateParserJobTest::test_processWithTemplatesForContentOtherTimeZone()
0548 {
0549     QFETCH(QString, command);
0550     QFETCH(QString, mailFileName);
0551     QFETCH(QString, expectedBody);
0552     QFETCH(bool, hasDictionary);
0553 
0554     QFile mailFile(mailFileName);
0555     QVERIFY(mailFile.open(QIODevice::ReadOnly));
0556     const QByteArray mailData = KMime::CRLFtoLF(mailFile.readAll());
0557     QVERIFY(!mailData.isEmpty());
0558     KMime::Message::Ptr msg(new KMime::Message);
0559     KMime::Message::Ptr origMsg(new KMime::Message);
0560     origMsg->setContent(mailData);
0561     origMsg->parse();
0562 
0563     auto parser = new TemplateParser::TemplateParserJob(msg, TemplateParser::TemplateParserJob::Reply);
0564     auto identMan = new KIdentityManagementCore::IdentityManager;
0565     parser->setIdentityManager(identMan);
0566     parser->setAllowDecryption(false);
0567     parser->d->mOrigMsg = origMsg;
0568     QSignalSpy spy(parser, &TemplateParser::TemplateParserJob::parsingDone);
0569     parser->processWithTemplate(command);
0570     QVERIFY(spy.wait());
0571     QCOMPARE(msg->hasHeader("X-KMail-Dictionary"), hasDictionary);
0572 
0573     identMan->deleteLater();
0574     QCOMPARE(QString::fromUtf8(msg->encodedBody()), expectedBody);
0575     QCOMPARE(spy.count(), 1);
0576 }
0577 
0578 void TemplateParserJobTest::test_makeValidHtml_data()
0579 {
0580     QTest::addColumn<QString>("message");
0581     QTest::addColumn<QString>("expected");
0582 
0583     QTest::newRow("plain text") << QStringLiteral("Some text\n-- \nSignature")
0584                                 << QStringLiteral(
0585                                        "<html><head></head><body>Some text\n"
0586                                        "-- \nSignature<br/></body></html>");
0587 
0588     QTest::newRow("existing HTML tag") << QStringLiteral("<html><body>Some text\n-- \nSignature</body></html>")
0589                                        << QStringLiteral("<html><body>Some text\n-- \nSignature</body></html>");
0590 
0591     QTest::newRow("existing body tag, no html") << QStringLiteral("<body>Some text\n-- \nSignature</body>")
0592                                                 << QStringLiteral(
0593                                                        "<html><head></head><body>Some text\n"
0594                                                        "-- \nSignature</body></html>");
0595 }
0596 
0597 void TemplateParserJobTest::test_makeValidHtml()
0598 {
0599     QFETCH(QString, message);
0600     QFETCH(QString, expected);
0601 
0602     KMime::Message::Ptr msg(new KMime::Message);
0603 
0604     auto parser = new TemplateParser::TemplateParserJob(msg, TemplateParser::TemplateParserJob::Reply);
0605     QString result = message;
0606     parser->makeValidHtml(result);
0607 
0608     QCOMPARE(result, expected);
0609 }
0610 
0611 QTEST_MAIN(TemplateParserJobTest)
0612 
0613 #include "moc_templateparserjobtest.cpp"