File indexing completed on 2025-01-19 04:51:22

0001 /*
0002    SPDX-FileCopyrightText: 2015-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 #include "knotesgrantleeprinttest.h"
0007 #include "knotegrantleeprint.h"
0008 #include "print/knoteprintobject.h"
0009 
0010 #include <Akonadi/Item>
0011 #include <Akonadi/NoteUtils>
0012 
0013 #include <KMime/Message>
0014 
0015 #include <QTest>
0016 
0017 KNotesGrantleePrintTest::KNotesGrantleePrintTest(QObject *parent)
0018     : QObject(parent)
0019 {
0020 }
0021 
0022 KNotesGrantleePrintTest::~KNotesGrantleePrintTest() = default;
0023 
0024 void KNotesGrantleePrintTest::shouldReturnEmptyStringWhenNotContentAndNoNotes()
0025 {
0026     KNoteGrantleePrint grantleePrint;
0027     QList<KNotePrintObject *> lst;
0028     const QString result = grantleePrint.notesToHtml(lst);
0029     QVERIFY(result.isEmpty());
0030 }
0031 
0032 void KNotesGrantleePrintTest::shouldReturnEmptyStringWhenAddContentWithoutNotes()
0033 {
0034     KNoteGrantleePrint grantleePrint;
0035     grantleePrint.setTemplateContent(QStringLiteral("foo"));
0036     QList<KNotePrintObject *> lst;
0037     const QString result = grantleePrint.notesToHtml(lst);
0038     QVERIFY(result.isEmpty());
0039 }
0040 
0041 void KNotesGrantleePrintTest::shouldDisplayNoteInfo_data()
0042 {
0043     QTest::addColumn<QString>("variable");
0044     QTest::addColumn<QString>("result");
0045     QTest::newRow("name") << QStringLiteral("name") << QStringLiteral("Test Note");
0046     QTest::newRow("description") << QStringLiteral("description") << QStringLiteral("notes test");
0047 }
0048 
0049 void KNotesGrantleePrintTest::shouldDisplayNoteInfo()
0050 {
0051     QFETCH(QString, variable);
0052     QFETCH(QString, result);
0053     Akonadi::Item note(42);
0054 
0055     KNoteGrantleePrint grantleePrint;
0056     grantleePrint.setTemplateContent(QStringLiteral("{% if notes %}{% for note in notes %}{{ note.%1 }}{% endfor %}{% endif %}").arg(variable));
0057 
0058     KMime::Message::Ptr msg(new KMime::Message);
0059     note.setMimeType(Akonadi::NoteUtils::noteMimeType());
0060     QString subject = QStringLiteral("Test Note");
0061     msg->subject(true)->fromUnicodeString(subject, "us-ascii");
0062     msg->contentType(true)->setMimeType("text/plain");
0063     msg->contentType()->setCharset("utf-8");
0064     msg->contentTransferEncoding(true)->setEncoding(KMime::Headers::CEquPr);
0065     msg->date(true)->setDateTime(QDateTime::currentDateTime());
0066     msg->mainBodyPart()->fromUnicodeString(QStringLiteral("notes test"));
0067     note.setPayload(msg);
0068     msg->assemble();
0069 
0070     QList<KNotePrintObject *> lst;
0071     auto obj = new KNotePrintObject(note);
0072     lst << obj;
0073     const QString html = grantleePrint.notesToHtml(lst);
0074     QCOMPARE(html, result);
0075     qDeleteAll(lst);
0076 }
0077 
0078 QTEST_GUILESS_MAIN(KNotesGrantleePrintTest)
0079 
0080 #include "moc_knotesgrantleeprinttest.cpp"