1
/** ===========================================================
4
* This file is a part of KDE project
5
* <a href="https://projects.kde.org/projects/extragear/libs/libmediawiki">libmediawiki</a>
8
* @brief a MediaWiki C++ interface for KDE
10
* @author Copyright (C) 2010 by Alexandre Mendes
11
* <a href="mailto:alex dot mendes1988 at gmail dot com">alex dot mendes1988 at gmail dot com</a>
13
* This program is free software; you can redistribute it
14
* and/or modify it under the terms of the GNU General
15
* Public License as published by the Free Software Foundation;
16
* either version 2, or (at your option)
19
* This program is distributed in the hope that it will be useful,
20
* but WITHOUT ANY WARRANTY; without even the implied warranty of
21
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22
* GNU General Public License for more details.
24
* ============================================================ */
26
#ifndef TEST_QUERYINFO_H
27
#define TEST_QUERYINFO_H
29
#include <QtCore/QObject>
30
#include <QtTest/QtTest>
32
#include <KCoreAddons/KJob>
34
#include "mediawiki.h"
35
#include "queryinfo.h"
37
#include "protection.h"
38
#include "libmediawikitest/fakeserver.h"
40
using mediawiki::MediaWiki;
41
using mediawiki::QueryInfo;
42
using mediawiki::Page;
43
using mediawiki::Protection;
45
Q_DECLARE_METATYPE(Page)
46
Q_DECLARE_METATYPE(Protection)
47
Q_DECLARE_METATYPE(QueryInfo*)
48
Q_DECLARE_METATYPE(QVector <Protection>)
50
class QueryInfoTest : public QObject
56
void queryInfoHandlePages(const Page& page)
59
queryInfoResultsPage = page;
62
void queryInfoHandleProtection(const QVector<Protection>& protection)
65
queryInfoResultsProtections = protection;
73
this->m_mediaWiki = new MediaWiki(QUrl(QStringLiteral("http://127.0.0.1:12566")));
78
QFETCH(QString, request);
79
QFETCH(QueryInfo*, job);
82
FakeServer fakeserver;
83
fakeserver.startAndWait();
87
QList<FakeServer::Request> requests = fakeserver.getRequest();
88
QCOMPARE(requests.size(), 1);
90
FakeServer::Request requestServeur = requests[0];
91
QCOMPARE(requestServeur.agent, m_mediaWiki->userAgent());
92
QCOMPARE(requestServeur.type, QStringLiteral("GET"));
93
QCOMPARE(requestServeur.value, request);
96
void constructQuery_data()
98
QTest::addColumn<QString>("request");
99
QTest::addColumn<QueryInfo*>("job");
101
QueryInfo* const j1 = new QueryInfo(*m_mediaWiki);
102
j1->setPageName(QStringLiteral("API"));
104
QTest::newRow("Name pages")
105
<< QStringLiteral("/?format=xml&action=query&prop=info&inprop=protection%7Ctalkid%7Cwatched%7Csubjectid%7Curl%7Creadable%7Cpreload&titles=API")
108
QueryInfo* const j2 = new QueryInfo(*m_mediaWiki);
109
j2->setToken( QStringLiteral("cecded1f35005d22904a35cc7b736e18+\\") );
111
QTest::newRow("Token")
112
<< QStringLiteral("/?format=xml&action=query&prop=info&inprop=protection%7Ctalkid%7Cwatched%7Csubjectid%7Curl%7Creadable%7Cpreload&intoken=cecded1f35005d22904a35cc7b736e18+%5C")
115
QueryInfo* const j3 = new QueryInfo(*m_mediaWiki);
116
j3->setPageId(25255);
118
QTest::newRow("Page Id")
119
<< QStringLiteral("/?format=xml&action=query&prop=info&inprop=protection%7Ctalkid%7Cwatched%7Csubjectid%7Curl%7Creadable%7Cpreload&pageids=25255")
122
QueryInfo *j4 = new QueryInfo(*m_mediaWiki);
123
j4->setRevisionId(44545);
125
QTest::newRow("Revision Id")
126
<< QStringLiteral("/?format=xml&action=query&prop=info&inprop=protection%7Ctalkid%7Cwatched%7Csubjectid%7Curl%7Creadable%7Cpreload&revids=44545")
132
QFETCH(QString,scenario);
134
QFETCH(QVector<Protection> ,protections);
136
QueryInfo job(*m_mediaWiki);
138
FakeServer fakeserver;
139
fakeserver.addScenario(scenario);
140
fakeserver.startAndWait();
142
connect(&job, SIGNAL(page(Page)),
143
this,SLOT(queryInfoHandlePages(Page)));
145
connect(&job, SIGNAL(protection(QVector<Protection>)),
146
this,SLOT(queryInfoHandleProtection(QVector<Protection>)));
150
QList<FakeServer::Request> requests = fakeserver.getRequest();
151
QCOMPARE(requests.size(), 1);
153
QCOMPARE(queryInfoCount, 2);
154
QCOMPARE(queryInfoResultsPage, page);
155
QCOMPARE(queryInfoResultsProtections, protections);
156
QVERIFY(fakeserver.isAllScenarioDone());
159
void parseData_data()
161
QTest::addColumn<QString>("scenario");
162
QTest::addColumn< Page >("page");
163
QTest::addColumn< QVector<Protection> >("protections");
166
pr1.setType(QStringLiteral("edit"));
167
pr1.setLevel(QStringLiteral("sysop"));
168
pr1.setExpiry(QStringLiteral("infinity"));
169
pr1.setSource(QString());
172
pr2.setType(QStringLiteral("move"));
173
pr2.setLevel(QStringLiteral("sysop"));
174
pr2.setExpiry(QStringLiteral("infinity"));
175
pr2.setSource(QString());
178
page.setPageId(27697087);
179
page.setTitle(QStringLiteral("API"));
181
page.setTouched( QDateTime::fromString(QStringLiteral("2010-11-25T13:59:03Z"), QStringLiteral("yyyy'-'MM'-'dd'T'hh':'mm':'ss'Z'")) );
182
page.setLastRevId(367741756);
185
page.setStarttimestamp(QDateTime::fromString(QStringLiteral("2010-11-25T16:14:51Z"), QStringLiteral("yyyy'-'MM'-'dd'T'hh':'mm':'ss'Z'")));
186
page.setEditToken(QStringLiteral("+\\"));
187
page.setTalkid(5477418);
188
page.setFullurl(QUrl(QStringLiteral("http://en.wikipedia.org/wiki/API")));
189
page.setEditurl(QUrl(QStringLiteral("http://en.wikipedia.org/w/index.php?title=API&action=edit")));
190
page.setReadable(QString());
191
page.setPreload(QString());
193
QTest::newRow("No protection")
194
<< 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>")
196
<< QVector<Protection>();
198
QTest::newRow("One pages and one protection")
199
<< 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>")
201
<< (QVector<Protection>() << pr1);
203
QTest::newRow("One pages and two protection")
204
<< 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>")
206
<< (QVector<Protection>() << pr1 << pr2);
209
void cleanupTestCase()
211
delete this->m_mediaWiki;
217
Page queryInfoResultsPage;
218
QVector <Protection> queryInfoResultsProtections;
219
MediaWiki* m_mediaWiki;
222
QTEST_MAIN(QueryInfoTest)
224
#include "queryinfotest.moc"
226
#endif // TEST_QUERYINFO_H