File indexing completed on 2025-01-19 03:57:47

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2011-03-22
0007  * Description : a MediaWiki C++ interface
0008  *
0009  * SPDX-FileCopyrightText: 2011-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
0010  * SPDX-FileCopyrightText: 2011      by Alexandre Mendes <alex dot mendes1988 at gmail dot com>
0011  * SPDX-FileCopyrightText: 2011      by Hormiere Guillaume <hormiere dot guillaume at gmail dot com>
0012  * SPDX-FileCopyrightText: 2011      by Manuel Campomanes <campomanes dot manuel at gmail dot com>
0013  *
0014  * SPDX-License-Identifier: GPL-2.0-or-later
0015  *
0016  * ============================================================ */
0017 
0018 // Qt includes
0019 
0020 #include <QObject>
0021 #include <QTest>
0022 
0023 // KDE includes
0024 
0025 #include <kjob.h>
0026 
0027 // Local includes
0028 
0029 #include "mediawiki_iface.h"
0030 #include "mediawiki_parse.h"
0031 #include "fakeserver/fakeserver.h"
0032 
0033 using MediaWiki::Iface;
0034 using MediaWiki::Parse;
0035 
0036 Q_DECLARE_METATYPE(FakeServer::Request)
0037 Q_DECLARE_METATYPE(QVariant)
0038 Q_DECLARE_METATYPE(Parse*)
0039 
0040 QString QStringFromFile(const QString& fileName)
0041 {
0042     QFile file(fileName);
0043 
0044     if (!file.open( QFile::ReadOnly ))
0045         return QString();
0046 
0047     QTextStream in(&file);
0048     QString scenario;
0049     // When loading from files we never have the authentication phase
0050     // force jumping directly to authenticated state.
0051 
0052     while ( !in.atEnd() )
0053     {
0054         scenario.append( in.readLine() );
0055     }
0056 
0057     file.close();
0058     return scenario;
0059 }
0060 
0061 class Q_DECL_HIDDEN ParseTest : public QObject
0062 {
0063     Q_OBJECT
0064 
0065 public:
0066 
0067     explicit ParseTest(QObject* const parent = nullptr)
0068     {
0069         Q_UNUSED(parent);
0070         parseCount  = 0;
0071         m_mediaWiki = nullptr;
0072     }
0073 
0074 public Q_SLOTS:
0075 
0076     void parseHandle(KJob* job)
0077     {
0078         Q_UNUSED(job)
0079         parseCount++;
0080     }
0081 
0082     void resultHandle(const QString& result)
0083     {
0084         parseResult = result;
0085     }
0086 
0087 private Q_SLOTS:
0088 
0089     void initTestCase()
0090     {
0091         parseCount        = 0;
0092         this->m_mediaWiki = new Iface(QUrl(QStringLiteral("http://127.0.0.1:12566")));      // krazy:exclude=insecurenet
0093     }
0094 
0095     void result()
0096     {
0097         QString scenario = QStringFromFile(QStringLiteral("./data/parsetest_result.rc"));
0098         QString result   = QStringFromFile(QStringLiteral("./data/parsetest_resulttrue.rc"));
0099 
0100         Parse* const job = new Parse(*m_mediaWiki, nullptr);
0101         parseCount       = 0;
0102         FakeServer fakeserver;
0103         fakeserver.setScenario(scenario);
0104         fakeserver.startAndWait();
0105 
0106         connect(job, SIGNAL(result(QString)),
0107                 this, SLOT(resultHandle(QString)));
0108 
0109         connect(job, SIGNAL(result(KJob*)),
0110                 this, SLOT(parseHandle(KJob*)));
0111 
0112         job->exec();
0113         QCOMPARE(parseResult, result);
0114         QCOMPARE(this->parseCount, 1);
0115     }
0116 
0117     void parseSetters()
0118     {
0119         QFETCH(QString, scenario);
0120         QFETCH(QString, request);
0121         QFETCH(Parse*, job);
0122 
0123         parseCount = 0;
0124         FakeServer fakeserver;
0125         fakeserver.setScenario(scenario);
0126         fakeserver.startAndWait();
0127 
0128         connect(job, SIGNAL(result(KJob*)),
0129                 this, SLOT(parseHandle(KJob*)));
0130 
0131         job->exec();
0132         FakeServer::Request serverrequest = fakeserver.getRequest()[0];
0133         QCOMPARE(serverrequest.type, QStringLiteral("GET"));
0134         QCOMPARE(serverrequest.value, request);
0135         QCOMPARE(this->parseCount, 1);
0136     }
0137 
0138     void parseSetters_data()
0139     {
0140         QTest::addColumn<QString>("scenario");
0141         QTest::addColumn<QString>("request");
0142         QTest::addColumn<Parse*>("job");
0143 
0144         Parse* const p1 = new Parse( *m_mediaWiki, nullptr);
0145         p1->setText(QStringLiteral("listedecharacteres"));
0146 
0147         QTest::newRow("Text")
0148                 << QStringFromFile(QStringLiteral("./data/parsetest.rc"))
0149                 << QStringLiteral("/?format=xml&action=parse&text=listedecharacteres")
0150                 << p1;
0151 
0152         Parse* const p2 = new Parse( *m_mediaWiki, nullptr);
0153         p2->setPageName(QStringLiteral("listedecharacteres"));
0154 
0155         QTest::newRow("Page Name")
0156                 << QStringFromFile(QStringLiteral("./data/parsetest.rc"))
0157                 << QStringLiteral("/?format=xml&action=parse&page=listedecharacteres")
0158                 << p2;
0159 
0160         Parse* const p3 = new Parse( *m_mediaWiki, nullptr);
0161         p3->setTitle(QStringLiteral("listedecharacteres"));
0162 
0163         QTest::newRow("Title")
0164                 << QStringFromFile(QStringLiteral("./data/parsetest.rc"))
0165                 << QStringLiteral("/?format=xml&action=parse&title=listedecharacteres")
0166                 << p3;
0167 
0168         Parse* const p4 = new Parse( *m_mediaWiki, nullptr);
0169         p4->setUseLang(QStringLiteral("fr"));
0170 
0171         QTest::newRow("User Langue")
0172                 << QStringFromFile(QStringLiteral("./data/parsetest.rc"))
0173                 << QStringLiteral("/?format=xml&action=parse&uselang=fr")
0174                 << p4;
0175     }
0176 
0177     void error()
0178     {
0179         QFETCH(QString, scenario);
0180         QFETCH(int, error);
0181         parseCount = 0;
0182         FakeServer fakeserver;
0183 
0184         if (scenario != QStringLiteral("error serveur"))
0185         {
0186             fakeserver.addScenario(scenario);
0187             fakeserver.startAndWait();
0188         }
0189 
0190         Parse* const job = new Parse(*m_mediaWiki);
0191         job->setTitle( QStringLiteral("title") );
0192 
0193         connect(job, SIGNAL(result(KJob*)),
0194                 this, SLOT(parseHandle(KJob*)));
0195 
0196         job->exec();
0197 
0198         if (scenario != QStringLiteral("error serveur"))
0199         {
0200             QList<FakeServer::Request> requests = fakeserver.getRequest();
0201             QCOMPARE(requests.size(), 1);
0202         }
0203 
0204         QCOMPARE(job->error(), error);
0205         QCOMPARE(parseCount, 1);
0206 
0207         if (scenario != QStringLiteral("error serveur"))
0208         {
0209             QVERIFY(fakeserver.isAllScenarioDone());
0210         }
0211     }
0212 
0213     void error_data()
0214     {
0215         QTest::addColumn<QString>("scenario");
0216         QTest::addColumn<int>("error");
0217 
0218         QTest::newRow("XML")
0219                 << QStringLiteral("<?xml version=\"1.0\" encoding=\"utf-8\"?><api><parse><text>")
0220                 << int(Parse::XmlError);
0221 
0222         QTest::newRow("Network")
0223                 << QStringLiteral("error serveur")
0224                 << int(Parse::NetworkError);
0225 
0226         QTest::newRow("Params")
0227                 << QStringLiteral("<api><error code=\"params\" info=\"\"/></api>")
0228                 << int(Parse::TooManyParams);
0229 
0230         QTest::newRow("Missing Title")
0231                 << QStringLiteral("<api><error code=\"missingtitle\" info=\"\"/></api>")
0232                 << int(Parse::MissingPage);
0233     }
0234 
0235     void cleanupTestCase()
0236     {
0237         delete this->m_mediaWiki;
0238     }
0239 
0240 private:
0241 
0242     int        parseCount;
0243     QString    request;
0244     QString    parseResult;
0245     Iface*     m_mediaWiki;
0246 };
0247 
0248 QTEST_MAIN(ParseTest)
0249 
0250 #include "parse_utest.moc"