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 "fakeserver/fakeserver.h"
0030 #include "mediawiki_iface.h"
0031 #include "mediawiki_querysiteinfogeneral.h"
0032 #include "mediawiki_generalinfo.h"
0033 
0034 using MediaWiki::Iface;
0035 using MediaWiki::QuerySiteInfoGeneral;
0036 using MediaWiki::Generalinfo;
0037 
0038 class Q_DECL_HIDDEN QuerySiteInfoGeneralTest : public QObject
0039 {
0040     Q_OBJECT
0041 
0042 public:
0043 
0044     explicit QuerySiteInfoGeneralTest(QObject* const parent = nullptr)
0045       : generalCount(0)
0046     {
0047         Q_UNUSED(parent);
0048     }
0049 
0050 public Q_SLOTS:
0051 
0052     void resultHandle(const Generalinfo& generalinfo)
0053     {
0054         ++generalCount;
0055         generalResult = generalinfo;
0056     }
0057 
0058 private Q_SLOTS:
0059 
0060     void initTestCase()
0061     {
0062         generalCount  = 0;
0063         this->request = QStringLiteral("/?format=xml&action=query&meta=siteinfo&siprop=general");
0064     }
0065 
0066     void QuerySiteInfoGeneralTestConnectTrue()
0067     {
0068         Iface mediaWiki(QUrl(QStringLiteral("http://127.0.0.1:12566")));        // krazy:exclude=insecurenet
0069         FakeServer server;
0070 
0071         generalCount = 0;
0072         QString senario(QStringLiteral("<api><query><general mainpage=\"Main Page\" base=\"https://en.wikipedia.org/wiki/Main_Page\" sitename=\"Wikipedia\" generator=\"Iface 1.16wmf4\" phpversion=\"5.2.4-2ubuntu5.12wm1\" phpsapi=\"apache2handler\" dbtype=\"mysql\" dbversion=\"5.1.46-facebook-r3489-log\" rev=\"75268\" case=\"first-letter\" rights=\"Creative Commons Attribution-Share Alike 3.0 Unported\" lang=\"en\" fallback8bitEncoding=\"windows-1252\" writeapi=\"\" timezone=\"UTC\" timeoffset=\"0\" articlepath=\"/wiki/$1\" scriptpath=\"/w\" script=\"/w/index.php\" variantarticlepath=\"\" server=\"https://en.wikipedia.org\" wikiid=\"enwiki\" time=\"2010-10-24T19:53:13Z\"/></query></api>"));
0073         server.addScenario(senario);
0074         server.startAndWait();
0075 
0076         QuerySiteInfoGeneral* const general = new QuerySiteInfoGeneral(mediaWiki);
0077 
0078         connect(general, SIGNAL(result(Generalinfo)),
0079                 this, SLOT(resultHandle(Generalinfo)));
0080 
0081         general->exec();
0082         FakeServer::Request serverrequest = server.getRequest()[0];
0083         QCOMPARE(this->generalCount, 1);
0084         QCOMPARE(serverrequest.type, QStringLiteral("GET"));
0085         QCOMPARE(serverrequest.value, this->request);
0086         QVERIFY(general->error() == QuerySiteInfoGeneral::NoError);
0087     }
0088 
0089     void QuerySiteInfoGeneralTestAttribute()
0090     {
0091         Iface mediaWiki(QUrl(QStringLiteral("http://127.0.0.1:12566")));        // krazy:exclude=insecurenet
0092         FakeServer server;
0093 
0094         generalCount = 0;
0095         QString senario(QStringLiteral("<api><query><general mainpage=\"Main Page\" base=\"https://en.wikipedia.org/wiki/Main_Page\" sitename=\"Wikipedia\" generator=\"Iface 1.16wmf4\" phpversion=\"5.2.4-2ubuntu5.12wm1\" phpsapi=\"apache2handler\" dbtype=\"mysql\" dbversion=\"5.1.46-facebook-r3489-log\" rev=\"75268\" case=\"first-letter\" rights=\"Creative Commons Attribution-Share Alike 3.0 Unported\" lang=\"en\" fallback8bitEncoding=\"windows-1252\" writeapi=\"\" timezone=\"UTC\" timeoffset=\"0\" articlepath=\"/wiki/$1\" scriptpath=\"/w\" script=\"/w/index.php\" variantarticlepath=\"\" server=\"https://en.wikipedia.org\" wikiid=\"enwiki\" time=\"2010-10-24T19:53:13Z\"/></query></api>"));
0096         server.addScenario(senario);
0097         server.startAndWait();
0098 
0099         QuerySiteInfoGeneral* const general = new QuerySiteInfoGeneral(mediaWiki);
0100 
0101         connect(general, SIGNAL(result(Generalinfo)),
0102                 this, SLOT(resultHandle(Generalinfo)));
0103 
0104         general->exec();
0105         FakeServer::Request serverrequest = server.getRequest()[0];
0106         QCOMPARE(this->generalCount, 1);
0107         QCOMPARE(serverrequest.type, QStringLiteral("GET"));
0108         QCOMPARE(serverrequest.value, this->request);
0109         QVERIFY(general->error() == QuerySiteInfoGeneral::NoError);
0110         Generalinfo resultExpected;
0111         resultExpected.setMainPage(QStringLiteral("Main Page"));
0112         resultExpected.setUrl(QUrl(QStringLiteral("https://en.wikipedia.org/wiki/Main_Page")));
0113         resultExpected.setSiteName(QStringLiteral("Wikipedia"));
0114         resultExpected.setGenerator(QStringLiteral("Iface 1.16wmf4"));
0115         resultExpected.setPhpVersion(QStringLiteral("5.2.4-2ubuntu5.12wm1"));
0116         resultExpected.setPhpApi(QStringLiteral("apache2handler"));
0117         resultExpected.setDataBaseType(QStringLiteral("mysql"));
0118         resultExpected.setDataBaseVersion(QStringLiteral("5.1.46-facebook-r3489-log"));
0119         resultExpected.setRev(QStringLiteral("75268"));
0120         resultExpected.setCas(QStringLiteral("first-letter"));
0121         resultExpected.setLicense(QStringLiteral("Creative Commons Attribution-Share Alike 3.0 Unported"));
0122         resultExpected.setLanguage(QStringLiteral("en"));
0123         resultExpected.setFallBack8bitEncoding(QStringLiteral("windows-1252"));
0124         resultExpected.setWriteApi(QLatin1String());
0125         resultExpected.setTimeZone(QStringLiteral("UTC"));
0126         resultExpected.setTimeOffset(QStringLiteral("0"));
0127         resultExpected.setArticlePath(QStringLiteral("/wiki/$1"));
0128         resultExpected.setScriptPath(QStringLiteral("/w"));
0129         resultExpected.setScript(QStringLiteral("/w/index.php"));
0130         resultExpected.setVariantArticlePath(QString());
0131         resultExpected.setServerUrl(QUrl(QStringLiteral("https://en.wikipedia.org")));
0132         resultExpected.setWikiId(QStringLiteral("enwiki"));
0133         resultExpected.setTime(QDateTime(QDate(2010, 10, 24), QTime(19, 53, 13)));
0134         QCOMPARE(generalResult, resultExpected);
0135     }
0136 
0137     void QuerySiteInfoGeneralTestConnectFalseXML()
0138     {
0139         Iface mediaWiki(QUrl(QStringLiteral("http://127.0.0.1:12566")));        // krazy:exclude=insecurenet
0140         FakeServer server;
0141 
0142         generalCount = 0;
0143         QString senario(QStringLiteral("<api><query<general mainpage=\"Main Page\" base=\"https://en.wikipedia.org/wiki/Main_Page\" sitename=\"Wikipedia\" generator=\"Iface 1.16wmf4\" phpversion=\"5.2.4-2ubuntu5.12wm1\" phpsapi=\"apache2handler\" dbtype=\"mysql\" dbversion=\"5.1.46-facebook-r3489-log\" rev=\"75268\" case=\"first-letter\" rights=\"Creative Commons Attribution-Share Alike 3.0 Unported\" lang=\"en\" fallback8bitEncoding=\"windows-1252\" writeapi=\"\" timezone=\"UTC\" timeoffset=\"0\" articlepath=\"/wiki/$1\" scriptpath=\"/w\" script=\"/w/index.php\" variantarticlepath=\"\" server=\"https://en.wikipedia.org\" wikiid=\"enwiki\" time=\"2010-10-24T19:53:13Z\"/>"));
0144         server.addScenario(senario);
0145         server.startAndWait();
0146 
0147         QuerySiteInfoGeneral* const general = new QuerySiteInfoGeneral(mediaWiki);
0148 
0149         connect(general, SIGNAL(result(Generalinfo)),
0150                 this, SLOT(resultHandle(Generalinfo)));
0151 
0152         general->exec();
0153         FakeServer::Request serverrequest = server.getRequest()[0];
0154         QCOMPARE(this->generalCount, 0);
0155         QCOMPARE(serverrequest.type, QStringLiteral("GET"));
0156         QCOMPARE(serverrequest.value, this->request);
0157         QVERIFY(general->error() == QuerySiteInfoGeneral::XmlError);
0158     }
0159 
0160     void QuerySiteInfoGeneralTestErrortIncludeAllDenied()
0161     {
0162         Iface mediaWiki(QUrl(QStringLiteral("http://127.0.0.1:12566")));        // krazy:exclude=insecurenet
0163         FakeServer server;
0164 
0165         generalCount = 0;
0166         QString senario(QStringLiteral("<api><error code=\"includeAllDenied\" info=\"\"/></api>"));
0167         server.addScenario(senario);
0168         server.startAndWait();
0169 
0170         QuerySiteInfoGeneral* const general = new QuerySiteInfoGeneral(mediaWiki);
0171 
0172         connect(general, SIGNAL(result(Generalinfo)),
0173                 this, SLOT(resultHandle(Generalinfo)));
0174 
0175         general->exec();
0176         FakeServer::Request serverrequest = server.getRequest()[0];
0177         QCOMPARE(this->generalCount, 0);
0178         QCOMPARE(serverrequest.type, QStringLiteral("GET"));
0179         QCOMPARE(serverrequest.value, this->request);
0180         QVERIFY(general->error() == QuerySiteInfoGeneral::IncludeAllDenied);
0181     }
0182 
0183 private:
0184 
0185     int         generalCount;
0186     Generalinfo generalResult;
0187     QString     request;
0188 };
0189 
0190 QTEST_MAIN(QuerySiteInfoGeneralTest)
0191 
0192 #include "querysiteinfogeneral_utest.moc"