File indexing completed on 2025-01-05 03:58:13
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 <QList> 0021 #include <QMap> 0022 #include <QObject> 0023 #include <QString> 0024 #include <QUrl> 0025 #include <QTest> 0026 #include <QFile> 0027 #include <QTextStream> 0028 0029 // KDE includes 0030 0031 #include <kjob.h> 0032 0033 // Local includes 0034 0035 #include "fakeserver/fakeserver.h" 0036 #include "mediawiki_iface.h" 0037 #include "mediawiki_queryrevision.h" 0038 #include "mediawiki_revision.h" 0039 #include "dtestdatadir.h" 0040 #include "digikam_debug.h" 0041 0042 using MediaWiki::Iface; 0043 using MediaWiki::QueryRevision; 0044 using MediaWiki::Revision; 0045 0046 Q_DECLARE_METATYPE(QList<Revision>) 0047 Q_DECLARE_METATYPE(FakeServer::Request) 0048 Q_DECLARE_METATYPE(QueryRevision*) 0049 Q_DECLARE_METATYPE(QueryRevision::Properties) 0050 0051 Revision constructRevision(int i, int p, int s, bool m, 0052 const QString& u, const QDateTime& t, 0053 const QString& cm, const QString& ct, 0054 const QString& pt, const QString& r) 0055 { 0056 Revision rev; 0057 rev.setRevisionId(i); 0058 rev.setParentId(p); 0059 rev.setSize(s); 0060 rev.setMinorRevision(m); 0061 rev.setUser(u); 0062 rev.setTimestamp(t); 0063 rev.setComment(cm); 0064 rev.setContent(ct); 0065 rev.setParseTree(pt); 0066 rev.setRollback(r); 0067 0068 return rev; 0069 } 0070 0071 QString QStringFromFile(const QString& fileName) 0072 { 0073 QFile file( fileName ); 0074 file.open( QFile::ReadOnly ); 0075 QTextStream in(&file); 0076 QString scenario; 0077 0078 // When loading from files we never have the authentication phase 0079 // force jumping directly to authenticated state. 0080 0081 while ( !in.atEnd() ) 0082 { 0083 scenario.append( in.readLine() ); 0084 } 0085 0086 file.close(); 0087 0088 return scenario; 0089 } 0090 0091 // --------------------------------------------------------------------------------- 0092 0093 class Q_DECL_HIDDEN QueryRevisionTest : public QObject 0094 { 0095 Q_OBJECT 0096 0097 public: 0098 0099 explicit QueryRevisionTest(QObject* const parent = nullptr) 0100 { 0101 Q_UNUSED(parent); 0102 m_revisionCount = 0; 0103 } 0104 0105 public Q_SLOTS: 0106 0107 void revisionHandle(const QList<Revision>& revision) 0108 { 0109 ++m_revisionCount; 0110 m_revisionResults = revision; 0111 } 0112 0113 private Q_SLOTS: 0114 0115 void init() 0116 { 0117 m_revisionCount = 0; 0118 m_revisionResults.clear(); 0119 } 0120 0121 void testrvpropall() 0122 { 0123 QFETCH(QString, scenario); 0124 QFETCH(FakeServer::Request, requestTrue); 0125 QFETCH(QString, title); 0126 QFETCH(int, error); 0127 QFETCH(QueryRevision::Properties, rvprop); 0128 QFETCH(int, size); 0129 QFETCH(QList<Revision>, results); 0130 0131 Iface MediaWiki(QUrl(QStringLiteral("http://127.0.0.1:12566"))); // krazy:exclude=insecurenet 0132 FakeServer fakeserver; 0133 fakeserver.setScenario(scenario); 0134 fakeserver.startAndWait(); 0135 0136 QueryRevision* const job = new QueryRevision(MediaWiki); 0137 job->setProperties(rvprop); 0138 job->setPageName(title); 0139 0140 connect(job, SIGNAL(revision(QList<Revision>)), 0141 this, SLOT(revisionHandle(QList<Revision>))); 0142 0143 job->exec(); // krazy:exclude=crashy; 0144 0145 QList<FakeServer::Request> requests = fakeserver.getRequest(); 0146 QCOMPARE(requests.size(), 1); 0147 0148 FakeServer::Request request = requests[0]; 0149 QCOMPARE( requestTrue.type, request.type); 0150 QCOMPARE(job->error(), error); 0151 QCOMPARE(m_revisionCount, 1); 0152 QCOMPARE(requestTrue.value, request.value); 0153 QCOMPARE(m_revisionResults.size(), size); 0154 0155 QCOMPARE(m_revisionResults, results); 0156 0157 QVERIFY(fakeserver.isAllScenarioDone()); 0158 } 0159 0160 void testrvpropall_data() 0161 { 0162 QTest::addColumn<QString>("scenario"); 0163 QTest::addColumn<FakeServer::Request>("requestTrue"); 0164 QTest::addColumn<QString>("title"); 0165 QTest::addColumn<int>("error"); 0166 QTest::addColumn<QueryRevision::Properties>("rvprop"); 0167 QTest::addColumn<int>("size"); 0168 QTest::addColumn< QList<Revision> >("results"); 0169 0170 QTest::newRow("All rvprop enable") 0171 << QStringFromFile(queryRevisionTestBasePath() + QStringLiteral(".rc")) 0172 << FakeServer::Request(QStringLiteral("GET"), 0173 QString(), 0174 QStringLiteral("/?format=xml&action=query&prop=revisions&rvprop=ids%7Cflags%7Ctimestamp%7Cuser%7Ccomment%7Csize%7Ccontent&titles=API%7CMain%20Page")) 0175 << QStringLiteral("API|Main%20Page") 0176 << int(KJob::NoError) 0177 << QueryRevision::Properties(QueryRevision::Ids | QueryRevision::Flags | QueryRevision::Timestamp | QueryRevision::User | QueryRevision::Comment | QueryRevision::Size | QueryRevision::Content) 0178 << 2 0179 << (QList<Revision>() 0180 << constructRevision(367741756, 367741564, 70, false, QStringLiteral("Graham87"), 0181 QDateTime::fromString(QStringLiteral("2010-06-13T08:41:17Z"), 0182 QStringLiteral("yyyy-MM-ddThh:mm:ssZ")), 0183 QStringLiteral("Protected API: restore protection ([edit=sysop] (indefinite) [move=sysop] (indefinite))"), 0184 QStringLiteral("#REDIRECT [[Application programming interface]]{{R from abbreviation}}"),QString(),QString()) 0185 << constructRevision(387545037, 387542946, 5074, false, QStringLiteral("Rich Farmbrough"), 0186 QDateTime::fromString(QStringLiteral("2010-09-28T15:21:07Z"), 0187 QStringLiteral("yyyy-MM-ddThh:mm:ssZ")), 0188 QStringLiteral("[[Help:Reverting|Reverted]] edits by [[Special:Contributions/Rich Farmbrough|Rich Farmbrough]] ([[User talk:Rich Farmbrough|talk]]) to last version by David Levy"), 0189 QStringFromFile(queryRevisionTestBasePath() + QStringLiteral("_content.rc")),QString(),QString())); 0190 0191 QTest::newRow("One title") 0192 << QStringFromFile(queryRevisionTestBasePath() + QStringLiteral("_onetitle.rc")) 0193 << FakeServer::Request(QStringLiteral("GET"), 0194 QString(), 0195 QStringLiteral("/?format=xml&action=query&prop=revisions&rvprop=ids%7Cflags%7Ctimestamp%7Cuser%7Ccomment%7Csize%7Ccontent&titles=API")) 0196 << QStringLiteral("API") 0197 << int(KJob::NoError) 0198 << QueryRevision::Properties(QueryRevision::Ids | QueryRevision::Flags | QueryRevision::Timestamp | QueryRevision::User | QueryRevision::Comment | QueryRevision::Size | QueryRevision::Content) 0199 << 1 0200 << (QList<Revision>() 0201 << constructRevision(367741756, 367741564, 70, false, QStringLiteral("Graham87"), 0202 QDateTime::fromString(QStringLiteral("2010-06-13T08:41:17Z"), 0203 QStringLiteral("yyyy-MM-ddThh:mm:ssZ")), 0204 QStringLiteral("Protected API: restore protection ([edit=sysop] (indefinite) [move=sysop] (indefinite))"), 0205 QStringLiteral("#REDIRECT [[Application programming interface]]{{R from abbreviation}}"),QString(),QString())); 0206 0207 QTest::newRow("Timestamp only") 0208 << QStringFromFile(queryRevisionTestBasePath() + QStringLiteral("_timestamponly.rc")) 0209 << FakeServer::Request(QStringLiteral("GET"), 0210 QString(), 0211 QStringLiteral("/?format=xml&action=query&prop=revisions&rvprop=timestamp&titles=API%7CMain%20Page")) 0212 << QStringLiteral("API|Main%20Page") 0213 << int(KJob::NoError) 0214 << QueryRevision::Properties(QueryRevision::Timestamp) 0215 << 2 0216 << (QList<Revision>() 0217 << constructRevision(-1, -1, -1, false, QString(), 0218 QDateTime::fromString(QStringLiteral("2010-06-13T08:41:17Z"), 0219 QStringLiteral("yyyy-MM-ddThh:mm:ssZ")), 0220 QString(), 0221 QString(),QString(),QString()) 0222 << constructRevision(-1, -1, -1, false, QString(), 0223 QDateTime::fromString(QStringLiteral("2010-09-28T15:21:07Z"), 0224 QStringLiteral("yyyy-MM-ddThh:mm:ssZ")), 0225 QString(), 0226 QString(),QString(),QString())); 0227 QTest::newRow("User only") 0228 << QStringFromFile(queryRevisionTestBasePath() + QStringLiteral("_useronly.rc")) 0229 << FakeServer::Request(QStringLiteral("GET"), 0230 QString(), 0231 QStringLiteral("/?format=xml&action=query&prop=revisions&rvprop=user&titles=API%7CMain%20Page")) 0232 << QStringLiteral("API|Main%20Page") 0233 << int(KJob::NoError) 0234 << QueryRevision::Properties(QueryRevision::User) 0235 << 2 0236 << (QList<Revision>() 0237 << constructRevision(-1, -1, -1, false, QStringLiteral("Graham87"), 0238 QDateTime(), 0239 QString(), 0240 QString(),QString(),QString()) 0241 << constructRevision(-1, -1, -1, false, QStringLiteral("Rich Farmbrough"), 0242 QDateTime(), 0243 QString(), 0244 QString(),QString(),QString())); 0245 } 0246 0247 void testerror() 0248 { 0249 QFETCH(QString, scenario); 0250 QFETCH(int, error); 0251 0252 Iface MediaWiki(QUrl(QStringLiteral("http://127.0.0.1:12566"))); // krazy:exclude=insecurenet 0253 FakeServer fakeserver; 0254 0255 if (scenario != QStringLiteral("error serveur")) 0256 { 0257 fakeserver.setScenario(scenario); 0258 fakeserver.startAndWait(); 0259 } 0260 0261 QueryRevision* const job = new QueryRevision(MediaWiki); 0262 job->setProperties( QueryRevision::Size | QueryRevision::Content ); 0263 job->setPageName(QStringLiteral("title")); 0264 0265 connect(job, SIGNAL(revision(QList<Revision>)), 0266 this, SLOT(revisionHandle(QList<Revision>))); 0267 0268 job->exec(); // krazy:exclude=crashy; 0269 0270 if (scenario != QStringLiteral("error serveur")) 0271 { 0272 QList<FakeServer::Request> requests = fakeserver.getRequest(); 0273 QCOMPARE(requests.size(), 1); 0274 } 0275 0276 QCOMPARE(job->error(), error); 0277 QCOMPARE(m_revisionCount, 0); 0278 QCOMPARE(m_revisionResults.size(), 0); 0279 0280 if (scenario != QStringLiteral("error serveur")) 0281 { 0282 QVERIFY(fakeserver.isAllScenarioDone()); 0283 } 0284 } 0285 0286 void testerror_data() 0287 { 0288 QTest::addColumn<QString>("scenario"); 0289 QTest::addColumn<int>("error"); 0290 0291 QTest::newRow("XML") 0292 << QStringFromFile(queryRevisionTestBasePath() + QStringLiteral("_cuted.rc")) 0293 << int(QueryRevision::XmlError); 0294 0295 QTest::newRow("Network") 0296 << QStringLiteral("error serveur") 0297 << int(QueryRevision::NetworkError); 0298 0299 QTest::newRow("Revision Ids") 0300 << QStringLiteral("<api><error code=\"rvrevids\" info=\"\"/></api>") 0301 << int(QueryRevision::WrongRevisionId); 0302 0303 QTest::newRow("Multilple pages") 0304 << QStringLiteral("<api><error code=\"rvmultpages\" info=\"\"/></api>") 0305 << int(QueryRevision::MultiPagesNotAllowed); 0306 0307 QTest::newRow("Access Denied") 0308 << QStringLiteral("<api><error code=\"rvaccessdenied\" info=\"\"/></api>") 0309 << int(QueryRevision::TitleAccessDenied); 0310 0311 QTest::newRow("Add Parameters") 0312 << QStringLiteral("<api><error code=\"rvbadparams\" info=\"\"/></api>") 0313 << int(QueryRevision::TooManyParams); 0314 0315 QTest::newRow("No Such Section") 0316 << QStringLiteral("<api><error code=\"rvnosuchsection\" info=\"\"/></api>") 0317 << int(QueryRevision::SectionNotFound); 0318 } 0319 0320 void testRvLimit() 0321 { 0322 Iface MediaWiki(QUrl(QStringLiteral("http://127.0.0.1:12566"))); // krazy:exclude=insecurenet 0323 FakeServer::Request requestSend(QStringLiteral("GET"), 0324 QString(), 0325 QStringLiteral("/?format=xml&action=query&prop=revisions&rvlimit=2&titles=API")); 0326 QueryRevision job(MediaWiki); 0327 job.setPageName(QStringLiteral("API")); 0328 job.setLimit(2); 0329 0330 FakeServer fakeserver; 0331 fakeserver.startAndWait(); 0332 0333 connect(&job, SIGNAL(revision(QList<Revision>)), 0334 this, SLOT(revisionHandle(QList<Revision>))); 0335 0336 job.exec(); // krazy:exclude=crashy 0337 0338 QList<FakeServer::Request> requests = fakeserver.getRequest(); 0339 QCOMPARE(requests.size(), 1); 0340 QCOMPARE(requests[0].value, requestSend.value); 0341 QCOMPARE(requests[0].type, requestSend.type); 0342 QVERIFY(fakeserver.isAllScenarioDone()); 0343 } 0344 0345 void testRvStartId() 0346 { 0347 Iface MediaWiki(QUrl(QStringLiteral("http://127.0.0.1:12566"))); // krazy:exclude=insecurenet 0348 FakeServer::Request requestSend(QStringLiteral("GET"), 0349 QString(), 0350 QStringLiteral("/?format=xml&action=query&prop=revisions&rvstartid=2&titles=API")); 0351 QueryRevision job(MediaWiki); 0352 job.setPageName(QStringLiteral("API")); 0353 job.setStartId(2); 0354 0355 FakeServer fakeserver; 0356 fakeserver.startAndWait(); 0357 0358 connect(&job, SIGNAL(revision(QList<Revision>)), 0359 this, SLOT(revisionHandle(QList<Revision>))); 0360 0361 job.exec(); // krazy:exclude=crashy 0362 0363 QList<FakeServer::Request> requests = fakeserver.getRequest(); 0364 QCOMPARE(requests.size(), 1); 0365 QCOMPARE(requests[0].value, requestSend.value); 0366 QCOMPARE(requests[0].type, requestSend.type); 0367 QVERIFY(fakeserver.isAllScenarioDone()); 0368 } 0369 0370 void testRvEndId() 0371 { 0372 Iface MediaWiki(QUrl(QStringLiteral("http://127.0.0.1:12566"))); // krazy:exclude=insecurenet 0373 FakeServer::Request requestSend(QStringLiteral("GET"), 0374 QString(), 0375 QStringLiteral("/?format=xml&action=query&prop=revisions&rvendid=2&titles=API")); 0376 QueryRevision job(MediaWiki); 0377 job.setPageName(QStringLiteral("API")); 0378 job.setEndId(2); 0379 0380 FakeServer fakeserver; 0381 fakeserver.startAndWait(); 0382 0383 connect(&job, SIGNAL(revision(QList<Revision>)), 0384 this, SLOT(revisionHandle(QList<Revision>))); 0385 0386 job.exec(); // krazy:exclude=crashy 0387 0388 QList<FakeServer::Request> requests = fakeserver.getRequest(); 0389 QCOMPARE(requests.size(), 1); 0390 QCOMPARE(requests[0].value, requestSend.value); 0391 QCOMPARE(requests[0].type, requestSend.type); 0392 QVERIFY(fakeserver.isAllScenarioDone()); 0393 } 0394 0395 void testRvStart() 0396 { 0397 Iface MediaWiki(QUrl(QStringLiteral("http://127.0.0.1:12566"))); // krazy:exclude=insecurenet 0398 FakeServer::Request requestSend(QStringLiteral("GET"), 0399 QString(), 0400 QStringLiteral("/?format=xml&action=query&prop=revisions&rvstart=2010-09-28T15:21:07Z&titles=API")); 0401 QueryRevision job(MediaWiki); 0402 job.setPageName(QStringLiteral("API")); 0403 job.setStartTimestamp(QDateTime::fromString(QStringLiteral("2010-09-28T15:21:07Z"), 0404 QStringLiteral("yyyy-MM-ddThh:mm:ssZ"))); 0405 0406 FakeServer fakeserver; 0407 fakeserver.startAndWait(); 0408 0409 connect(&job, SIGNAL(revision(QList<Revision>)), 0410 this, SLOT(revisionHandle(QList<Revision>))); 0411 0412 job.exec(); // krazy:exclude=crashy 0413 0414 QList<FakeServer::Request> requests = fakeserver.getRequest(); 0415 QCOMPARE(requests.size(), 1); 0416 QCOMPARE(requests[0].value, requestSend.value); 0417 QCOMPARE(requests[0].type, requestSend.type); 0418 QVERIFY(fakeserver.isAllScenarioDone()); 0419 } 0420 0421 void testRvEnd() 0422 { 0423 Iface MediaWiki(QUrl(QStringLiteral("http://127.0.0.1:12566"))); // krazy:exclude=insecurenet 0424 FakeServer::Request requestSend(QStringLiteral("GET"), 0425 QString(), 0426 QStringLiteral("/?format=xml&action=query&prop=revisions&rvend=2010-09-28T15:21:07Z&titles=API")); 0427 QueryRevision job(MediaWiki); 0428 job.setPageName(QStringLiteral("API")); 0429 job.setEndTimestamp(QDateTime::fromString(QStringLiteral("2010-09-28T15:21:07Z"), 0430 QStringLiteral("yyyy-MM-ddThh:mm:ssZ"))); 0431 0432 FakeServer fakeserver; 0433 fakeserver.startAndWait(); 0434 0435 connect(&job, SIGNAL(revision(QList<Revision>)), 0436 this, SLOT(revisionHandle(QList<Revision>))); 0437 0438 job.exec(); // krazy:exclude=crashy 0439 0440 QList<FakeServer::Request> requests = fakeserver.getRequest(); 0441 QCOMPARE(requests.size(), 1); 0442 QCOMPARE(requests[0].value, requestSend.value); 0443 QCOMPARE(requests[0].type, requestSend.type); 0444 QVERIFY(fakeserver.isAllScenarioDone()); 0445 } 0446 0447 void testRvUser() 0448 { 0449 Iface MediaWiki(QUrl(QStringLiteral("http://127.0.0.1:12566"))); // krazy:exclude=insecurenet 0450 FakeServer::Request requestSend(QStringLiteral("GET"), 0451 QString(), 0452 QStringLiteral("/?format=xml&action=query&prop=revisions&rvuser=martine&titles=API")); 0453 QueryRevision job(MediaWiki); 0454 job.setPageName(QStringLiteral("API")); 0455 job.setUser(QStringLiteral("martine")); 0456 0457 FakeServer fakeserver; 0458 fakeserver.startAndWait(); 0459 0460 connect(&job, SIGNAL(revision(QList<Revision>)), 0461 this, SLOT(revisionHandle(QList<Revision>))); 0462 0463 job.exec(); // krazy:exclude=crashy 0464 0465 QList<FakeServer::Request> requests = fakeserver.getRequest(); 0466 QCOMPARE(requests.size(), 1); 0467 QCOMPARE(requests[0].value, requestSend.value); 0468 QCOMPARE(requests[0].type, requestSend.type); 0469 QVERIFY(fakeserver.isAllScenarioDone()); 0470 } 0471 0472 void testRvExcludeUser() 0473 { 0474 Iface MediaWiki(QUrl(QStringLiteral("http://127.0.0.1:12566"))); // krazy:exclude=insecurenet 0475 FakeServer::Request requestSend(QStringLiteral("GET"), 0476 QString(), 0477 QStringLiteral("/?format=xml&action=query&prop=revisions&rvexcludeuser=martine&titles=API")); 0478 QueryRevision job(MediaWiki); 0479 job.setPageName(QStringLiteral("API")); 0480 job.setExcludeUser(QStringLiteral("martine")); 0481 0482 FakeServer fakeserver; 0483 fakeserver.startAndWait(); 0484 0485 connect(&job, SIGNAL(revision(QList<Revision>)), 0486 this, SLOT(revisionHandle(QList<Revision>))); 0487 0488 job.exec(); // krazy:exclude=crashy 0489 0490 QList<FakeServer::Request> requests = fakeserver.getRequest(); 0491 QCOMPARE(requests.size(), 1); 0492 QCOMPARE(requests[0].value, requestSend.value); 0493 QCOMPARE(requests[0].type, requestSend.type); 0494 QVERIFY(fakeserver.isAllScenarioDone()); 0495 } 0496 0497 void testRvDirOlder() 0498 { 0499 Iface MediaWiki(QUrl(QStringLiteral("http://127.0.0.1:12566"))); // krazy:exclude=insecurenet 0500 FakeServer::Request requestSend(QStringLiteral("GET"), 0501 QString(), 0502 QStringLiteral("/?format=xml&action=query&prop=revisions&rvdir=older&titles=API")); 0503 QueryRevision job(MediaWiki); 0504 job.setPageName(QStringLiteral("API")); 0505 job.setDirection(QueryRevision::Older); 0506 0507 FakeServer fakeserver; 0508 fakeserver.startAndWait(); 0509 0510 connect(&job, SIGNAL(revision(QList<Revision>)), 0511 this, SLOT(revisionHandle(QList<Revision>))); 0512 0513 job.exec(); // krazy:exclude=crashy 0514 0515 QList<FakeServer::Request> requests = fakeserver.getRequest(); 0516 QCOMPARE(requests.size(), 1); 0517 QCOMPARE(requests[0].value, requestSend.value); 0518 QCOMPARE(requests[0].type, requestSend.type); 0519 QVERIFY(fakeserver.isAllScenarioDone()); 0520 } 0521 0522 void testRvDirNewer() 0523 { 0524 Iface MediaWiki(QUrl(QStringLiteral("http://127.0.0.1:12566"))); // krazy:exclude=insecurenet 0525 FakeServer::Request requestSend(QStringLiteral("GET"), 0526 QString(), 0527 QStringLiteral("/?format=xml&action=query&prop=revisions&rvdir=newer&titles=API")); 0528 QueryRevision job(MediaWiki); 0529 job.setPageName(QStringLiteral("API")); 0530 job.setDirection(QueryRevision::Newer); 0531 0532 FakeServer fakeserver; 0533 fakeserver.startAndWait(); 0534 0535 connect(&job, SIGNAL(revision(QList<Revision>)), 0536 this, SLOT(revisionHandle(QList<Revision>))); 0537 0538 job.exec(); // krazy:exclude=crashy 0539 0540 QList<FakeServer::Request> requests = fakeserver.getRequest(); 0541 QCOMPARE(requests.size(), 1); 0542 QCOMPARE(requests[0].value, requestSend.value); 0543 QCOMPARE(requests[0].type, requestSend.type); 0544 QVERIFY(fakeserver.isAllScenarioDone()); 0545 } 0546 0547 void testGenerateXML() 0548 { 0549 QString scenario = QStringFromFile(queryRevisionTestBasePath() + QStringLiteral("_parsetree.rc")); 0550 FakeServer::Request requestTrue(QStringLiteral("GET"), 0551 QString(), 0552 QStringLiteral("/?format=xml&action=query&prop=revisions&rvgeneratexml=on&rvprop=timestamp%7Cuser%7Ccomment%7Ccontent&titles=API")); 0553 QString title = QStringLiteral("API"); 0554 int error = 0; 0555 QueryRevision::Properties rvprop = QueryRevision::Timestamp | QueryRevision::User | QueryRevision::Comment | QueryRevision::Content; 0556 int size = 2; 0557 QList<Revision> results; 0558 0559 Revision rev; 0560 rev.setUser(QStringLiteral("martine")); 0561 rev.setTimestamp(QDateTime::fromString(QStringLiteral("2010-06-13T08:41:17Z"), 0562 QStringLiteral("yyyy-MM-ddThh:mm:ssZ"))); 0563 rev.setComment(QStringLiteral("Protected API: restore protection ([edit=sysop] (indefinite) [move=sysop] (indefinite))")); 0564 rev.setContent(QStringLiteral("#REDIRECT [[Application programming interface]]{{R from abbreviation}}")); 0565 rev.setParseTree(QStringLiteral("<root>#REDIRECT [[Application programming interface]]<template><title>R from abbreviation</title></template></root>")); 0566 results << rev; 0567 Revision rev2; 0568 rev2.setUser(QStringLiteral("Graham87")); 0569 rev2.setTimestamp(QDateTime::fromString(QStringLiteral("2010-06-13T08:41:17Z"), 0570 QStringLiteral("yyyy-MM-ddThh:mm:ssZ"))); 0571 rev2.setComment(QStringLiteral("Protected API: restore protection ([edit=sysop] (indefinite) [move=sysop] (indefinite))")); 0572 rev2.setContent(QStringLiteral("#REDIRECT [[Application programming interface]]{{R from abbreviation}}")); 0573 rev2.setParseTree(QStringLiteral("<root>#REDIRECT [[Application programming interface]]<template><title>R from abbreviation</title></template></root>")); 0574 results << rev2; 0575 Iface MediaWiki(QUrl(QStringLiteral("http://127.0.0.1:12566"))); // krazy:exclude=insecurenet 0576 0577 FakeServer fakeserver; 0578 fakeserver.setScenario(scenario); 0579 fakeserver.startAndWait(); 0580 0581 QueryRevision* const job = new QueryRevision(MediaWiki); 0582 job->setProperties( rvprop ); 0583 job->setPageName(title); 0584 job->setGenerateXML(true); 0585 0586 connect(job, SIGNAL(revision(QList<Revision>)), 0587 this, SLOT(revisionHandle(QList<Revision>))); 0588 0589 job->exec(); // krazy:exclude=crashy; 0590 0591 QList<FakeServer::Request> requests = fakeserver.getRequest(); 0592 QCOMPARE(requests.size(), 1); 0593 0594 FakeServer::Request request = requests[0]; 0595 QCOMPARE(request.type, requestTrue.type); 0596 QCOMPARE(m_revisionCount, 1); 0597 QCOMPARE(request.value, requestTrue.value); 0598 0599 QCOMPARE(job->error(), error); 0600 QCOMPARE(m_revisionResults.size(), size); 0601 QCOMPARE(m_revisionResults, results); 0602 0603 QVERIFY(fakeserver.isAllScenarioDone()); 0604 } 0605 0606 void testRvSection() 0607 { 0608 Iface MediaWiki(QUrl(QStringLiteral("http://127.0.0.1:12566"))); // krazy:exclude=insecurenet 0609 FakeServer::Request requestSend(QStringLiteral("GET"), 0610 QString(), 0611 QStringLiteral("/?format=xml&action=query&prop=revisions&rvsection=1&titles=API")); 0612 QueryRevision job(MediaWiki); 0613 job.setPageName(QStringLiteral("API")); 0614 job.setSection(1); 0615 0616 FakeServer fakeserver; 0617 fakeserver.startAndWait(); 0618 0619 connect(&job, SIGNAL(revision(QList<Revision>)), 0620 this, SLOT(revisionHandle(QList<Revision>))); 0621 0622 job.exec(); // krazy:exclude=crashy 0623 0624 QList<FakeServer::Request> requests = fakeserver.getRequest(); 0625 QCOMPARE(requests.size(), 1); 0626 QCOMPARE(requests[0].value, requestSend.value); 0627 QCOMPARE(requests[0].type, requestSend.type); 0628 QVERIFY(fakeserver.isAllScenarioDone()); 0629 } 0630 0631 void testRvToken() 0632 { 0633 QString scenario = QStringFromFile(queryRevisionTestBasePath() + QStringLiteral("_rollback.rc")); 0634 FakeServer::Request requestTrue(QStringLiteral("GET"), 0635 QString(), 0636 QStringLiteral("/?format=xml&action=query&prop=revisions&rvtoken=rollback&titles=API")); 0637 QString title = QStringLiteral("API"); 0638 int error = 0; 0639 int size = 1; 0640 QList<Revision> results; 0641 results << constructRevision(-1, -1, -1, false, QString(), 0642 QDateTime(), 0643 QString(), 0644 QString(), 0645 QString(), 0646 QStringLiteral("094a45ddbbd5e90d55d79d2a23a8c921+\\")); 0647 0648 0649 Iface MediaWiki(QUrl(QStringLiteral("http://127.0.0.1:12566"))); // krazy:exclude=insecurenet 0650 0651 FakeServer fakeserver; 0652 fakeserver.setScenario(scenario); 0653 fakeserver.startAndWait(); 0654 0655 QueryRevision* const job = new QueryRevision(MediaWiki); 0656 job->setPageName(title); 0657 0658 job->setToken(QueryRevision::Rollback); 0659 0660 connect(job, SIGNAL(revision(QList<Revision>)), 0661 this, SLOT(revisionHandle(QList<Revision>))); 0662 0663 job->exec(); // krazy:exclude=crashy; 0664 0665 QList<FakeServer::Request> requests = fakeserver.getRequest(); 0666 QCOMPARE(requests.size(), 1); 0667 0668 FakeServer::Request request = requests[0]; 0669 QCOMPARE( requestTrue.type, request.type); 0670 QCOMPARE(m_revisionCount, 1); 0671 QCOMPARE(requestTrue.value, request.value); 0672 0673 QCOMPARE(job->error(), error); 0674 QCOMPARE(m_revisionResults.size(), size); 0675 QCOMPARE(m_revisionResults, results); 0676 0677 QVERIFY(fakeserver.isAllScenarioDone()); 0678 } 0679 0680 void testRvExpandTemplates() 0681 { 0682 Iface MediaWiki(QUrl(QStringLiteral("http://127.0.0.1:12566"))); // krazy:exclude=insecurenet 0683 FakeServer::Request requestSend(QStringLiteral("GET"), 0684 QString(), 0685 QStringLiteral("/?format=xml&action=query&prop=revisions&rvexpandtemplates=on&titles=API")); 0686 QueryRevision job(MediaWiki); 0687 job.setPageName(QStringLiteral("API")); 0688 job.setExpandTemplates(true); 0689 0690 FakeServer fakeserver; 0691 fakeserver.startAndWait(); 0692 0693 connect(&job, SIGNAL(revision(QList<Revision>)), 0694 this, SLOT(revisionHandle(QList<Revision>))); 0695 0696 job.exec(); // krazy:exclude=crashy 0697 0698 QList<FakeServer::Request> requests = fakeserver.getRequest(); 0699 QCOMPARE(requests.size(), 1); 0700 QCOMPARE(requests[0].value, requestSend.value); 0701 QCOMPARE(requests[0].type, requestSend.type); 0702 QVERIFY(fakeserver.isAllScenarioDone()); 0703 } 0704 0705 void testRvPageId() 0706 { 0707 FakeServer::Request requestTrue(QStringLiteral("GET"), 0708 QString(), 0709 QStringLiteral("/?format=xml&action=query&prop=revisions&pageids=2993&rvprop=timestamp%7Cuser%7Ccomment%7Ccontent")); 0710 QueryRevision::Properties rvprop = QueryRevision::Timestamp | QueryRevision::User | QueryRevision::Comment | QueryRevision::Content; 0711 int id = 2993; 0712 0713 Iface MediaWiki(QUrl(QStringLiteral("http://127.0.0.1:12566"))); // krazy:exclude=insecurenet 0714 0715 FakeServer fakeserver; 0716 fakeserver.startAndWait(); 0717 0718 QueryRevision* const job = new QueryRevision(MediaWiki); 0719 job->setProperties( rvprop ); 0720 job->setPageId(id); 0721 0722 connect(job, SIGNAL(revision(QList<Revision>)), 0723 this, SLOT(revisionHandle(QList<Revision>))); 0724 0725 job->exec(); // krazy:exclude=crashy; 0726 0727 QList<FakeServer::Request> requests = fakeserver.getRequest(); 0728 QCOMPARE(requests.size(), 1); 0729 0730 FakeServer::Request request = requests[0]; 0731 QCOMPARE( requestTrue.type, request.type); 0732 QCOMPARE(m_revisionCount, 0); 0733 QCOMPARE(requestTrue.value, request.value); 0734 0735 QVERIFY(fakeserver.isAllScenarioDone()); 0736 } 0737 0738 void testRvRevisionId() 0739 { 0740 FakeServer::Request requestTrue(QStringLiteral("GET"), 0741 QString(), 0742 QStringLiteral("/?format=xml&action=query&prop=revisions&revids=2993&rvprop=timestamp%7Cuser%7Ccomment%7Ccontent")); 0743 QueryRevision::Properties rvprop = QueryRevision::Timestamp |QueryRevision::User | QueryRevision::Comment | QueryRevision::Content; 0744 int id = 2993; 0745 0746 Iface MediaWiki(QUrl(QStringLiteral("http://127.0.0.1:12566"))); // krazy:exclude=insecurenet 0747 0748 FakeServer fakeserver; 0749 fakeserver.startAndWait(); 0750 0751 QueryRevision* const job = new QueryRevision(MediaWiki); 0752 job->setProperties( rvprop ); 0753 job->setRevisionId(id); 0754 0755 connect(job, SIGNAL(revision(QList<Revision>)), 0756 this, SLOT(revisionHandle(QList<Revision>))); 0757 0758 job->exec(); // krazy:exclude=crashy; 0759 0760 QList<FakeServer::Request> requests = fakeserver.getRequest(); 0761 QCOMPARE(requests.size(), 1); 0762 0763 FakeServer::Request request = requests[0]; 0764 QCOMPARE(requestTrue.type, request.type); 0765 QCOMPARE(m_revisionCount, 0); 0766 QCOMPARE(requestTrue.value, request.value); 0767 0768 QVERIFY(fakeserver.isAllScenarioDone()); 0769 } 0770 0771 private: 0772 0773 QString queryRevisionTestBasePath() 0774 { 0775 const QString filePath = DTestDataDir::TestData(QString::fromUtf8("core/tests/mediawiki")) 0776 .root().path() + QLatin1Char('/') + QLatin1String("queryrevisiontest"); 0777 qCDebug(DIGIKAM_TESTS_LOG) << "Test Data File:" << filePath; 0778 0779 return filePath; 0780 } 0781 0782 private: 0783 0784 int m_revisionCount; 0785 QList<Revision> m_revisionResults; 0786 }; 0787 0788 QTEST_MAIN(QueryRevisionTest) 0789 0790 #include "queryrevision_utest.moc"