1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
|
/** ===========================================================
* @file
*
* This file is a part of KDE project
* <a href="https://commits.kde.org/libmediawiki">libmediawiki</a>
*
* @date 2011-03-22
* @brief a MediaWiki C++ interface for KDE
*
* @author Copyright (C) 2010 by Alexandre Mendes
* <a href="mailto:alex dot mendes1988 at gmail dot com">alex dot mendes1988 at gmail dot com</a>
*
* This program is free software; you can redistribute it
* and/or modify it under the terms of the GNU General
* Public License as published by the Free Software Foundation;
* either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* ============================================================ */
#ifndef TEST_QUERYINFO_H
#define TEST_QUERYINFO_H
#include <QObject>
#include <QtTest>
#include <KJob>
#include "mediawiki.h"
#include "queryinfo.h"
#include "page.h"
#include "protection.h"
#include "libmediawikitest/fakeserver.h"
using mediawiki::MediaWiki;
using mediawiki::QueryInfo;
using mediawiki::Page;
using mediawiki::Protection;
Q_DECLARE_METATYPE(Page)
Q_DECLARE_METATYPE(Protection)
Q_DECLARE_METATYPE(QueryInfo*)
Q_DECLARE_METATYPE(QVector <Protection>)
class QueryInfoTest : public QObject
{
Q_OBJECT
public Q_SLOTS:
void queryInfoHandlePages(const Page& page)
{
++queryInfoCount;
queryInfoResultsPage = page;
}
void queryInfoHandleProtection(const QVector<Protection>& protection)
{
++queryInfoCount;
queryInfoResultsProtections = protection;
}
private Q_SLOTS:
void initTestCase()
{
queryInfoCount = 0;
this->m_mediaWiki = new MediaWiki(QUrl(QStringLiteral("http://127.0.0.1:12566")));
}
void constructQuery()
{
QFETCH(QString, request);
QFETCH(QueryInfo*, job);
queryInfoCount = 0;
FakeServer fakeserver;
fakeserver.startAndWait();
job->exec();
QList<FakeServer::Request> requests = fakeserver.getRequest();
QCOMPARE(requests.size(), 1);
FakeServer::Request requestServeur = requests[0];
QCOMPARE(requestServeur.agent, m_mediaWiki->userAgent());
QCOMPARE(requestServeur.type, QStringLiteral("GET"));
QCOMPARE(requestServeur.value, request);
}
void constructQuery_data()
{
QTest::addColumn<QString>("request");
QTest::addColumn<QueryInfo*>("job");
QueryInfo* const j1 = new QueryInfo(*m_mediaWiki);
j1->setPageName(QStringLiteral("API"));
QTest::newRow("Name pages")
<< QStringLiteral("/?format=xml&action=query&prop=info&inprop=protection%7Ctalkid%7Cwatched%7Csubjectid%7Curl%7Creadable%7Cpreload&titles=API")
<< j1;
QueryInfo* const j2 = new QueryInfo(*m_mediaWiki);
j2->setToken( QStringLiteral("cecded1f35005d22904a35cc7b736e18+\\") );
QTest::newRow("Token")
<< QStringLiteral("/?format=xml&action=query&prop=info&inprop=protection%7Ctalkid%7Cwatched%7Csubjectid%7Curl%7Creadable%7Cpreload&intoken=cecded1f35005d22904a35cc7b736e18+%5C")
<< j2;
QueryInfo* const j3 = new QueryInfo(*m_mediaWiki);
j3->setPageId(25255);
QTest::newRow("Page Id")
<< QStringLiteral("/?format=xml&action=query&prop=info&inprop=protection%7Ctalkid%7Cwatched%7Csubjectid%7Curl%7Creadable%7Cpreload&pageids=25255")
<< j3;
QueryInfo *j4 = new QueryInfo(*m_mediaWiki);
j4->setRevisionId(44545);
QTest::newRow("Revision Id")
<< QStringLiteral("/?format=xml&action=query&prop=info&inprop=protection%7Ctalkid%7Cwatched%7Csubjectid%7Curl%7Creadable%7Cpreload&revids=44545")
<< j4;
}
void parseData()
{
QFETCH(QString,scenario);
QFETCH(Page ,page);
QFETCH(QVector<Protection> ,protections);
QueryInfo job(*m_mediaWiki);
queryInfoCount = 0;
FakeServer fakeserver;
fakeserver.addScenario(scenario);
fakeserver.startAndWait();
connect(&job, SIGNAL(page(Page)),
this,SLOT(queryInfoHandlePages(Page)));
connect(&job, SIGNAL(protection(QVector<Protection>)),
this,SLOT(queryInfoHandleProtection(QVector<Protection>)));
job.exec();
QList<FakeServer::Request> requests = fakeserver.getRequest();
QCOMPARE(requests.size(), 1);
QCOMPARE(queryInfoCount, 2);
QCOMPARE(queryInfoResultsPage, page);
QCOMPARE(queryInfoResultsProtections, protections);
QVERIFY(fakeserver.isAllScenarioDone());
}
void parseData_data()
{
QTest::addColumn<QString>("scenario");
QTest::addColumn< Page >("page");
QTest::addColumn< QVector<Protection> >("protections");
Protection pr1;
pr1.setType(QStringLiteral("edit"));
pr1.setLevel(QStringLiteral("sysop"));
pr1.setExpiry(QStringLiteral("infinity"));
pr1.setSource(QString());
Protection pr2;
pr2.setType(QStringLiteral("move"));
pr2.setLevel(QStringLiteral("sysop"));
pr2.setExpiry(QStringLiteral("infinity"));
pr2.setSource(QString());
Page page;
page.setPageId(27697087);
page.setTitle(QStringLiteral("API"));
page.setNs(0);
page.setTouched( QDateTime::fromString(QStringLiteral("2010-11-25T13:59:03Z"), QStringLiteral("yyyy'-'MM'-'dd'T'hh':'mm':'ss'Z'")) );
page.setLastRevId(367741756);
page.setCounter(0);
page.setLength(70);
page.setStarttimestamp(QDateTime::fromString(QStringLiteral("2010-11-25T16:14:51Z"), QStringLiteral("yyyy'-'MM'-'dd'T'hh':'mm':'ss'Z'")));
page.setEditToken(QStringLiteral("+\\"));
page.setTalkid(5477418);
page.setFullurl(QUrl(QStringLiteral("http://en.wikipedia.org/wiki/API")));
page.setEditurl(QUrl(QStringLiteral("http://en.wikipedia.org/w/index.php?title=API&action=edit")));
page.setReadable(QString());
page.setPreload(QString());
QTest::newRow("No protection")
<< 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>")
<< page
<< QVector<Protection>();
QTest::newRow("One pages and one protection")
<< 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>")
<< page
<< (QVector<Protection>() << pr1);
QTest::newRow("One pages and two protection")
<< 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>")
<< page
<< (QVector<Protection>() << pr1 << pr2);
}
void cleanupTestCase()
{
delete this->m_mediaWiki;
}
private:
int queryInfoCount;
Page queryInfoResultsPage;
QVector <Protection> queryInfoResultsProtections;
MediaWiki* m_mediaWiki;
};
QTEST_MAIN(QueryInfoTest)
#include "queryinfotest.moc"
#endif // TEST_QUERYINFO_H
|