File indexing completed on 2024-04-21 15:32:10

0001 /** ===========================================================
0002  * @file
0003  *
0004  * This file is a part of KDE project
0005  * <a href="https://commits.kde.org/libmediawiki">libmediawiki</a>
0006  *
0007  * @date   2011-03-22
0008  * @brief  a MediaWiki C++ interface for KDE
0009  *
0010  * @author Copyright (C) 2010 by Alexandre Mendes
0011  *         <a href="mailto:alex dot mendes1988 at gmail dot com">alex dot mendes1988 at gmail dot com</a>
0012  *
0013  * This program is free software; you can redistribute it
0014  * and/or modify it under the terms of the GNU General
0015  * Public License as published by the Free Software Foundation;
0016  * either version 2, or (at your option)
0017  * any later version.
0018  *
0019  * This program is distributed in the hope that it will be useful,
0020  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0021  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0022  * GNU General Public License for more details.
0023  *
0024  * ============================================================ */
0025 
0026 #ifndef TEST_QUERYINFO_H
0027 #define TEST_QUERYINFO_H
0028 
0029 #include <QObject>
0030 #include <QtTest>
0031 
0032 #include <KJob>
0033 
0034 #include "mediawiki.h"
0035 #include "queryinfo.h"
0036 #include "page.h"
0037 #include "protection.h"
0038 #include "libmediawikitest/fakeserver.h"
0039 
0040 using mediawiki::MediaWiki;
0041 using mediawiki::QueryInfo;
0042 using mediawiki::Page;
0043 using mediawiki::Protection;
0044 
0045 Q_DECLARE_METATYPE(Page)
0046 Q_DECLARE_METATYPE(Protection)
0047 Q_DECLARE_METATYPE(QueryInfo*)
0048 Q_DECLARE_METATYPE(QVector <Protection>)
0049 
0050 class QueryInfoTest : public QObject
0051 {
0052     Q_OBJECT
0053 
0054 public Q_SLOTS:
0055 
0056     void queryInfoHandlePages(const Page& page)
0057     {
0058         ++queryInfoCount;
0059         queryInfoResultsPage = page;
0060     }
0061 
0062     void queryInfoHandleProtection(const QVector<Protection>& protection)
0063     {
0064         ++queryInfoCount;
0065         queryInfoResultsProtections = protection;
0066     }
0067 
0068 private Q_SLOTS:
0069 
0070     void initTestCase()
0071     {
0072         queryInfoCount    = 0;
0073         this->m_mediaWiki = new MediaWiki(QUrl(QStringLiteral("http://127.0.0.1:12566")));
0074     }
0075 
0076     void constructQuery()
0077     {
0078         QFETCH(QString, request);
0079         QFETCH(QueryInfo*, job);
0080 
0081         queryInfoCount = 0;
0082         FakeServer fakeserver;
0083         fakeserver.startAndWait();
0084 
0085         job->exec();
0086 
0087         QList<FakeServer::Request> requests = fakeserver.getRequest();
0088         QCOMPARE(requests.size(), 1);
0089 
0090         FakeServer::Request requestServeur = requests[0];
0091         QCOMPARE(requestServeur.agent, m_mediaWiki->userAgent());
0092         QCOMPARE(requestServeur.type, QStringLiteral("GET"));
0093         QCOMPARE(requestServeur.value, request);
0094     }
0095 
0096     void constructQuery_data()
0097     {
0098         QTest::addColumn<QString>("request");
0099         QTest::addColumn<QueryInfo*>("job");
0100 
0101         QueryInfo* const j1 = new QueryInfo(*m_mediaWiki);
0102         j1->setPageName(QStringLiteral("API"));
0103 
0104         QTest::newRow("Name pages")
0105                 << QStringLiteral("/?format=xml&action=query&prop=info&inprop=protection%7Ctalkid%7Cwatched%7Csubjectid%7Curl%7Creadable%7Cpreload&titles=API")
0106                 << j1;
0107 
0108         QueryInfo* const j2 = new QueryInfo(*m_mediaWiki);
0109         j2->setToken( QStringLiteral("cecded1f35005d22904a35cc7b736e18+\\") );
0110 
0111         QTest::newRow("Token")
0112                 << QStringLiteral("/?format=xml&action=query&prop=info&inprop=protection%7Ctalkid%7Cwatched%7Csubjectid%7Curl%7Creadable%7Cpreload&intoken=cecded1f35005d22904a35cc7b736e18+%5C")
0113                 << j2;
0114 
0115         QueryInfo* const j3 = new QueryInfo(*m_mediaWiki);
0116         j3->setPageId(25255);
0117 
0118         QTest::newRow("Page Id")
0119                 << QStringLiteral("/?format=xml&action=query&prop=info&inprop=protection%7Ctalkid%7Cwatched%7Csubjectid%7Curl%7Creadable%7Cpreload&pageids=25255")
0120                 << j3;
0121 
0122         QueryInfo *j4 = new QueryInfo(*m_mediaWiki);
0123         j4->setRevisionId(44545);
0124 
0125         QTest::newRow("Revision Id")
0126                 << QStringLiteral("/?format=xml&action=query&prop=info&inprop=protection%7Ctalkid%7Cwatched%7Csubjectid%7Curl%7Creadable%7Cpreload&revids=44545")
0127                 << j4;
0128     }
0129 
0130     void parseData()
0131     {
0132         QFETCH(QString,scenario);
0133         QFETCH(Page ,page);
0134         QFETCH(QVector<Protection> ,protections);
0135 
0136         QueryInfo job(*m_mediaWiki);
0137         queryInfoCount = 0;
0138         FakeServer fakeserver;
0139         fakeserver.addScenario(scenario);
0140         fakeserver.startAndWait();
0141 
0142         connect(&job, SIGNAL(page(Page)),
0143                 this,SLOT(queryInfoHandlePages(Page)));
0144 
0145         connect(&job, SIGNAL(protection(QVector<Protection>)),
0146                 this,SLOT(queryInfoHandleProtection(QVector<Protection>)));
0147 
0148         job.exec();
0149 
0150         QList<FakeServer::Request> requests = fakeserver.getRequest();
0151         QCOMPARE(requests.size(), 1);
0152 
0153         QCOMPARE(queryInfoCount, 2);
0154         QCOMPARE(queryInfoResultsPage, page);
0155         QCOMPARE(queryInfoResultsProtections, protections);
0156         QVERIFY(fakeserver.isAllScenarioDone());
0157     }
0158 
0159     void parseData_data()
0160     {
0161         QTest::addColumn<QString>("scenario");
0162         QTest::addColumn< Page >("page");
0163         QTest::addColumn< QVector<Protection> >("protections");
0164 
0165         Protection pr1;
0166         pr1.setType(QStringLiteral("edit"));
0167         pr1.setLevel(QStringLiteral("sysop"));
0168         pr1.setExpiry(QStringLiteral("infinity"));
0169         pr1.setSource(QString());
0170 
0171         Protection pr2;
0172         pr2.setType(QStringLiteral("move"));
0173         pr2.setLevel(QStringLiteral("sysop"));
0174         pr2.setExpiry(QStringLiteral("infinity"));
0175         pr2.setSource(QString());
0176 
0177         Page page;
0178         page.setPageId(27697087);
0179         page.setTitle(QStringLiteral("API"));
0180         page.setNs(0);
0181         page.setTouched( QDateTime::fromString(QStringLiteral("2010-11-25T13:59:03Z"), QStringLiteral("yyyy'-'MM'-'dd'T'hh':'mm':'ss'Z'")) );
0182         page.setLastRevId(367741756);
0183         page.setCounter(0);
0184         page.setLength(70);
0185         page.setStarttimestamp(QDateTime::fromString(QStringLiteral("2010-11-25T16:14:51Z"), QStringLiteral("yyyy'-'MM'-'dd'T'hh':'mm':'ss'Z'")));
0186         page.setEditToken(QStringLiteral("+\\"));
0187         page.setTalkid(5477418);
0188         page.setFullurl(QUrl(QStringLiteral("http://en.wikipedia.org/wiki/API")));
0189         page.setEditurl(QUrl(QStringLiteral("http://en.wikipedia.org/w/index.php?title=API&action=edit")));
0190         page.setReadable(QString());
0191         page.setPreload(QString());
0192 
0193         QTest::newRow("No protection")
0194                 << QStringLiteral("<api><query><pages><page pageid=\"27697087\" ns=\"0\" title=\"API\" touched=\"2010-11-25T13:59:03Z\" lastrevid=\"367741756\" counter=\"0\" length=\"70\" redirect=\"\" starttimestamp=\"2010-11-25T16:14:51Z\" edittoken=\"+\\\" talkid=\"5477418\" fullurl=\"http://en.wikipedia.org/wiki/API\" editurl=\"http://en.wikipedia.org/w/index.php?title=API&action=edit\" ><protection /></page></pages></query></api>")
0195                 << page
0196                 << QVector<Protection>();
0197 
0198         QTest::newRow("One pages and one protection")
0199                 << QStringLiteral("<api><query><pages><page pageid=\"27697087\" ns=\"0\" title=\"API\" touched=\"2010-11-25T13:59:03Z\" lastrevid=\"367741756\" counter=\"0\" length=\"70\" redirect=\"\" starttimestamp=\"2010-11-25T16:14:51Z\" edittoken=\"+\\\" talkid=\"5477418\" fullurl=\"http://en.wikipedia.org/wiki/API\" editurl=\"http://en.wikipedia.org/w/index.php?title=API&action=edit\" ><protection><pr type=\"edit\" level=\"sysop\" expiry=\"infinity\"/></protection></page></pages></query></api>")
0200                 << page
0201                 << (QVector<Protection>() << pr1);
0202 
0203         QTest::newRow("One pages and two protection")
0204                 << QStringLiteral("<api><query><pages><page pageid=\"27697087\" ns=\"0\" title=\"API\" touched=\"2010-11-25T13:59:03Z\" lastrevid=\"367741756\" counter=\"0\" length=\"70\" redirect=\"\" starttimestamp=\"2010-11-25T16:14:51Z\" edittoken=\"+\\\" talkid=\"5477418\" fullurl=\"http://en.wikipedia.org/wiki/API\" editurl=\"http://en.wikipedia.org/w/index.php?title=API&action=edit\" ><protection><pr type=\"edit\" level=\"sysop\" expiry=\"infinity\"/><pr type=\"move\" level=\"sysop\" expiry=\"infinity\"/></protection></page></pages></query></api>")
0205                 << page
0206                 << (QVector<Protection>() << pr1 << pr2);
0207     }
0208 
0209     void cleanupTestCase()
0210     {
0211         delete this->m_mediaWiki;
0212     }
0213 
0214 private:
0215 
0216     int                  queryInfoCount;
0217     Page                 queryInfoResultsPage;
0218     QVector <Protection> queryInfoResultsProtections;
0219     MediaWiki*           m_mediaWiki;
0220 };
0221 
0222 QTEST_MAIN(QueryInfoTest)
0223 
0224 #include "queryinfotest.moc"
0225 
0226 #endif // TEST_QUERYINFO_H