~ubuntu-branches/debian/sid/libmediawiki/sid

« back to all changes in this revision

Viewing changes to tests/queryinfotest.cpp

  • Committer: Package Import Robot
  • Author(s): Steve M. Robbins
  • Date: 2017-11-05 21:15:29 UTC
  • Revision ID: package-import@ubuntu.com-20171105211529-bgfi385yv79030cf
Tags: upstream-5.37.0
ImportĀ upstreamĀ versionĀ 5.37.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/** ===========================================================
 
2
 * @file
 
3
 *
 
4
 * This file is a part of KDE project
 
5
 * <a href="https://projects.kde.org/projects/extragear/libs/libmediawiki">libmediawiki</a>
 
6
 *
 
7
 * @date   2011-03-22
 
8
 * @brief  a MediaWiki C++ interface for KDE
 
9
 *
 
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>
 
12
 *
 
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)
 
17
 * any later version.
 
18
 *
 
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.
 
23
 *
 
24
 * ============================================================ */
 
25
 
 
26
#ifndef TEST_QUERYINFO_H
 
27
#define TEST_QUERYINFO_H
 
28
 
 
29
#include <QtCore/QObject>
 
30
#include <QtTest/QtTest>
 
31
 
 
32
#include <KCoreAddons/KJob>
 
33
 
 
34
#include "mediawiki.h"
 
35
#include "queryinfo.h"
 
36
#include "page.h"
 
37
#include "protection.h"
 
38
#include "libmediawikitest/fakeserver.h"
 
39
 
 
40
using mediawiki::MediaWiki;
 
41
using mediawiki::QueryInfo;
 
42
using mediawiki::Page;
 
43
using mediawiki::Protection;
 
44
 
 
45
Q_DECLARE_METATYPE(Page)
 
46
Q_DECLARE_METATYPE(Protection)
 
47
Q_DECLARE_METATYPE(QueryInfo*)
 
48
Q_DECLARE_METATYPE(QVector <Protection>)
 
49
 
 
50
class QueryInfoTest : public QObject
 
51
{
 
52
    Q_OBJECT
 
53
 
 
54
public Q_SLOTS:
 
55
 
 
56
    void queryInfoHandlePages(const Page& page)
 
57
    {
 
58
        ++queryInfoCount;
 
59
        queryInfoResultsPage = page;
 
60
    }
 
61
 
 
62
    void queryInfoHandleProtection(const QVector<Protection>& protection)
 
63
    {
 
64
        ++queryInfoCount;
 
65
        queryInfoResultsProtections = protection;
 
66
    }
 
67
 
 
68
private Q_SLOTS:
 
69
 
 
70
    void initTestCase()
 
71
    {
 
72
        queryInfoCount    = 0;
 
73
        this->m_mediaWiki = new MediaWiki(QUrl(QStringLiteral("http://127.0.0.1:12566")));
 
74
    }
 
75
 
 
76
    void constructQuery()
 
77
    {
 
78
        QFETCH(QString, request);
 
79
        QFETCH(QueryInfo*, job);
 
80
 
 
81
        queryInfoCount = 0;
 
82
        FakeServer fakeserver;
 
83
        fakeserver.startAndWait();
 
84
 
 
85
        job->exec();
 
86
 
 
87
        QList<FakeServer::Request> requests = fakeserver.getRequest();
 
88
        QCOMPARE(requests.size(), 1);
 
89
 
 
90
        FakeServer::Request requestServeur = requests[0];
 
91
        QCOMPARE(requestServeur.agent, m_mediaWiki->userAgent());
 
92
        QCOMPARE(requestServeur.type, QStringLiteral("GET"));
 
93
        QCOMPARE(requestServeur.value, request);
 
94
    }
 
95
 
 
96
    void constructQuery_data()
 
97
    {
 
98
        QTest::addColumn<QString>("request");
 
99
        QTest::addColumn<QueryInfo*>("job");
 
100
 
 
101
        QueryInfo* const j1 = new QueryInfo(*m_mediaWiki);
 
102
        j1->setPageName(QStringLiteral("API"));
 
103
 
 
104
        QTest::newRow("Name pages")
 
105
                << QStringLiteral("/?format=xml&action=query&prop=info&inprop=protection%7Ctalkid%7Cwatched%7Csubjectid%7Curl%7Creadable%7Cpreload&titles=API")
 
106
                << j1;
 
107
 
 
108
        QueryInfo* const j2 = new QueryInfo(*m_mediaWiki);
 
109
        j2->setToken( QStringLiteral("cecded1f35005d22904a35cc7b736e18+\\") );
 
110
 
 
111
        QTest::newRow("Token")
 
112
                << QStringLiteral("/?format=xml&action=query&prop=info&inprop=protection%7Ctalkid%7Cwatched%7Csubjectid%7Curl%7Creadable%7Cpreload&intoken=cecded1f35005d22904a35cc7b736e18+%5C")
 
113
                << j2;
 
114
 
 
115
        QueryInfo* const j3 = new QueryInfo(*m_mediaWiki);
 
116
        j3->setPageId(25255);
 
117
 
 
118
        QTest::newRow("Page Id")
 
119
                << QStringLiteral("/?format=xml&action=query&prop=info&inprop=protection%7Ctalkid%7Cwatched%7Csubjectid%7Curl%7Creadable%7Cpreload&pageids=25255")
 
120
                << j3;
 
121
 
 
122
        QueryInfo *j4 = new QueryInfo(*m_mediaWiki);
 
123
        j4->setRevisionId(44545);
 
124
 
 
125
        QTest::newRow("Revision Id")
 
126
                << QStringLiteral("/?format=xml&action=query&prop=info&inprop=protection%7Ctalkid%7Cwatched%7Csubjectid%7Curl%7Creadable%7Cpreload&revids=44545")
 
127
                << j4;
 
128
    }
 
129
 
 
130
    void parseData()
 
131
    {
 
132
        QFETCH(QString,scenario);
 
133
        QFETCH(Page ,page);
 
134
        QFETCH(QVector<Protection> ,protections);
 
135
 
 
136
        QueryInfo job(*m_mediaWiki);
 
137
        queryInfoCount = 0;
 
138
        FakeServer fakeserver;
 
139
        fakeserver.addScenario(scenario);
 
140
        fakeserver.startAndWait();
 
141
 
 
142
        connect(&job, SIGNAL(page(Page)),
 
143
                this,SLOT(queryInfoHandlePages(Page)));
 
144
 
 
145
        connect(&job, SIGNAL(protection(QVector<Protection>)),
 
146
                this,SLOT(queryInfoHandleProtection(QVector<Protection>)));
 
147
 
 
148
        job.exec();
 
149
 
 
150
        QList<FakeServer::Request> requests = fakeserver.getRequest();
 
151
        QCOMPARE(requests.size(), 1);
 
152
 
 
153
        QCOMPARE(queryInfoCount, 2);
 
154
        QCOMPARE(queryInfoResultsPage, page);
 
155
        QCOMPARE(queryInfoResultsProtections, protections);
 
156
        QVERIFY(fakeserver.isAllScenarioDone());
 
157
    }
 
158
 
 
159
    void parseData_data()
 
160
    {
 
161
        QTest::addColumn<QString>("scenario");
 
162
        QTest::addColumn< Page >("page");
 
163
        QTest::addColumn< QVector<Protection> >("protections");
 
164
 
 
165
        Protection pr1;
 
166
        pr1.setType(QStringLiteral("edit"));
 
167
        pr1.setLevel(QStringLiteral("sysop"));
 
168
        pr1.setExpiry(QStringLiteral("infinity"));
 
169
        pr1.setSource(QString());
 
170
 
 
171
        Protection pr2;
 
172
        pr2.setType(QStringLiteral("move"));
 
173
        pr2.setLevel(QStringLiteral("sysop"));
 
174
        pr2.setExpiry(QStringLiteral("infinity"));
 
175
        pr2.setSource(QString());
 
176
 
 
177
        Page page;
 
178
        page.setPageId(27697087);
 
179
        page.setTitle(QStringLiteral("API"));
 
180
        page.setNs(0);
 
181
        page.setTouched( QDateTime::fromString(QStringLiteral("2010-11-25T13:59:03Z"), QStringLiteral("yyyy'-'MM'-'dd'T'hh':'mm':'ss'Z'")) );
 
182
        page.setLastRevId(367741756);
 
183
        page.setCounter(0);
 
184
        page.setLength(70);
 
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());
 
192
 
 
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>")
 
195
                << page
 
196
                << QVector<Protection>();
 
197
 
 
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>")
 
200
                << page
 
201
                << (QVector<Protection>() << pr1);
 
202
 
 
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>")
 
205
                << page
 
206
                << (QVector<Protection>() << pr1 << pr2);
 
207
    }
 
208
 
 
209
    void cleanupTestCase()
 
210
    {
 
211
        delete this->m_mediaWiki;
 
212
    }
 
213
 
 
214
private:
 
215
 
 
216
    int                  queryInfoCount;
 
217
    Page                 queryInfoResultsPage;
 
218
    QVector <Protection> queryInfoResultsProtections;
 
219
    MediaWiki*           m_mediaWiki;
 
220
};
 
221
 
 
222
QTEST_MAIN(QueryInfoTest)
 
223
 
 
224
#include "queryinfotest.moc"
 
225
 
 
226
#endif // TEST_QUERYINFO_H