File indexing completed on 2024-05-12 05:17:31

0001 /*
0002     SPDX-FileCopyrightText: 2018 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include <KItinerary/ExtractorEngine>
0008 #include <KItinerary/ExtractorPostprocessor>
0009 #include <KItinerary/JsonLdDocument>
0010 #include <KItinerary/Organization>
0011 #include <KItinerary/Place>
0012 
0013 #include <KPkPass/BoardingPass>
0014 
0015 #include <QDebug>
0016 #include <QDir>
0017 #include <QFile>
0018 #include <QJsonArray>
0019 #include <QJsonDocument>
0020 #include <QProcess>
0021 #include <QObject>
0022 #include <QTest>
0023 
0024 using namespace KItinerary;
0025 
0026 class PkPassExtractorTest : public QObject
0027 {
0028     Q_OBJECT
0029 private Q_SLOTS:
0030     void initTestCase()
0031     {
0032         // use some exotic locale to ensure the date/time parsing doesn't just work by luck
0033         QLocale::setDefault(QLocale(QStringLiteral("fr_FR")));
0034         qputenv("TZ", "EST");
0035     }
0036 
0037     void testExtractText_data()
0038     {
0039         QTest::addColumn<QString>("inputFile");
0040         QTest::addColumn<QString>("refFile");
0041 
0042         QDir dir(QStringLiteral(SOURCE_DIR "/pkpassdata"));
0043         const auto lst = dir.entryList(QStringList(QStringLiteral("*.pkpass")), QDir::Files | QDir::Readable | QDir::NoSymLinks);
0044         for (const auto &file : lst) {
0045             const QString refFile = dir.path() + QLatin1Char('/') + file.left(file.size() - 7) + QStringLiteral(".json");
0046             if (!QFile::exists(refFile)) {
0047                 qDebug() << "reference file" << refFile << "does not exist, skipping test file" << file;
0048                 continue;
0049             }
0050             QTest::newRow(file.toLatin1().constData()) << QString(dir.path() + QLatin1Char('/') +  file) << refFile;
0051         }
0052     }
0053 
0054     void testExtractText()
0055     {
0056         QFETCH(QString, inputFile);
0057         QFETCH(QString, refFile);
0058 
0059         const auto pass = KPkPass::Pass::fromFile(inputFile, this);
0060         QVERIFY(pass);
0061 
0062         ExtractorEngine engine;
0063         engine.setContextDate(QDateTime(QDate(2017, 12, 29), QTime(18, 46, 2)));
0064         engine.setContent(QVariant::fromValue(pass), u"application/vnd.apple.pkpass");
0065         auto result = JsonLdDocument::fromJson(engine.extract());
0066 
0067         ExtractorPostprocessor postproc;
0068         postproc.setContextDate(QDateTime(QDate(2017, 12, 29), QTime(18, 46, 2)));
0069         postproc.process(result);
0070         result = postproc.result();
0071         QVERIFY(result.size() <= 1);
0072         const auto resJson = JsonLdDocument::toJson(result);
0073 
0074         QFile ref(refFile);
0075         QVERIFY(ref.open(QFile::ReadOnly));
0076         const auto doc = QJsonDocument::fromJson(ref.readAll());
0077         QVERIFY(doc.isArray());
0078         QCOMPARE(doc.array().size(), result.size());
0079 
0080         if (resJson != doc.array()) {
0081           QFile failFile(refFile + QLatin1StringView(".fail"));
0082           QVERIFY(failFile.open(QFile::WriteOnly));
0083           failFile.write(QJsonDocument(resJson).toJson());
0084           failFile.close();
0085 
0086           QProcess proc;
0087           proc.setProcessChannelMode(QProcess::ForwardedChannels);
0088           proc.start(QStringLiteral("diff"),
0089                      {QStringLiteral("-u"), refFile, failFile.fileName()});
0090           QVERIFY(proc.waitForFinished());
0091         }
0092 
0093         QCOMPARE(resJson, doc.array());
0094     }
0095 };
0096 
0097 QTEST_GUILESS_MAIN(PkPassExtractorTest)
0098 
0099 #include "pkpassextractortest.moc"