File indexing completed on 2024-04-14 14:55:13

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  * @author Copyright (C) 2011 by Manuel Campomanes
0013  *         <a href="mailto:campomanes dot manuel at gmail dot com">campomanes dot manuel at gmail dot com</a>
0014  * @author Copyright (C) 2011 by Hormiere Guillaume
0015  *         <a href="mailto:hormiere dot guillaume at gmail dot com">hormiere dot guillaume at gmail dot com</a>
0016  *
0017  * This program is free software; you can redistribute it
0018  * and/or modify it under the terms of the GNU General
0019  * Public License as published by the Free Software Foundation;
0020  * either version 2, or (at your option)
0021  * any later version.
0022  *
0023  * This program is distributed in the hope that it will be useful,
0024  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0025  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0026  * GNU General Public License for more details.
0027  *
0028  * ============================================================ */
0029 
0030 #ifndef TEST_EDIT_H
0031 #define TEST_EDIT_H
0032 
0033 #include <QObject>
0034 #include <QtTest>
0035 
0036 #include <KJob>
0037 
0038 #include "mediawiki.h"
0039 #include "edit.h"
0040 #include "libmediawikitest/fakeserver.h"
0041 
0042 using mediawiki::MediaWiki;
0043 using mediawiki::Edit;
0044 
0045 Q_DECLARE_METATYPE(FakeServer::Request)
0046 Q_DECLARE_METATYPE(QVariant)
0047 Q_DECLARE_METATYPE(Edit*)
0048 
0049 class EditTest : public QObject
0050 {
0051     Q_OBJECT
0052 
0053 Q_SIGNALS:
0054 
0055     void captchaSignal(const QString & CaptchaAnswer);
0056 
0057 public Q_SLOTS:
0058 
0059     void editHandle(KJob* )
0060     {
0061         editCount++;
0062     }
0063 
0064     void editHandle(const QVariant& captcha)
0065     {
0066         editCount++;
0067         this->CaptchaQuestion = captcha;
0068         emit captchaSignal(this->CaptchaAnswer);
0069     }
0070 
0071 private Q_SLOTS:
0072 
0073     void initTestCase()
0074     {
0075         editCount            = 0;
0076         this->m_mediaWiki    = new MediaWiki(QUrl(QStringLiteral("http://127.0.0.1:12566")));
0077         this->m_infoScenario = 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=\"cecded1f35005d22904a35cc7b736e18+\\\" 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>");
0078     }
0079 
0080     void editSetters()
0081     {
0082         QFETCH(QString, request);
0083         QFETCH(QString, senario);
0084         QFETCH(Edit*, job);
0085 
0086         editCount = 0;
0087         FakeServer fakeserver;
0088         fakeserver.setScenario(m_infoScenario);
0089         fakeserver.addScenario(senario);
0090         fakeserver.startAndWait();
0091 
0092         connect(job, SIGNAL(result(KJob*)),
0093                 this, SLOT(editHandle(KJob*)));
0094 
0095         job->exec();
0096         FakeServer::Request serverrequest = fakeserver.getRequest()[1];
0097         QCOMPARE(serverrequest.type, QStringLiteral("POST"));
0098         QCOMPARE(serverrequest.value, request);
0099         QCOMPARE(job->error(), (int)Edit::NoError);
0100         QCOMPARE(this->editCount, 1);
0101     }
0102     void editSetters_data()
0103     {
0104         QTest::addColumn<QString>("request");
0105         QTest::addColumn<QString>("senario");
0106         QTest::addColumn<Edit*>("job");
0107 
0108         Edit* const e1 = new Edit( *m_mediaWiki, nullptr);
0109         e1->setSection(QStringLiteral("new"));
0110         e1->setSummary(QStringLiteral("Hello World") );
0111         e1->setPageName( QStringLiteral("Talk:Main Page") );
0112         e1->setToken( QStringLiteral("cecded1f35005d22904a35cc7b736e18+\\") );
0113         e1->setBaseTimestamp( QDateTime::fromString(QStringLiteral("2008-03-20T17:26:39Z"),QStringLiteral("yyyy-MM-ddThh:mm:ssZ")) );
0114         e1->setStartTimestamp( QDateTime::fromString(QStringLiteral("2008-03-27T21:15:39Z"),QStringLiteral("yyyy-MM-ddThh:mm:ssZ")) );
0115         e1->setText( QStringLiteral("Hello everyone!") );
0116         QTest::newRow("Text")
0117                 << QStringLiteral("/?format=xml&action=edit&basetimestamp=2008-03-20T17:26:39Z&md5=4d184ec6e8fe61abccb8ff62c4583cd0&section=new&starttimestamp=2008-03-27T21:15:39Z&summary=Hello%20World&text=Hello%20everyone!&title=Talk:Main%20Page&token=cecded1f35005d22904a35cc7b736e18+%5C")
0118                 << QStringLiteral("<api><edit result=\"Success\" pageid=\"12\" title=\"Talk:Main Page\" oldrevid=\"465\" newrevid=\"471\" /></api>")
0119                 << e1;
0120 
0121         Edit* const e2 = new Edit( *m_mediaWiki, nullptr);
0122         e2->setSection(QStringLiteral("new"));
0123         e2->setSummary(QStringLiteral("Hello World"));
0124         e2->setPageName( QStringLiteral("Talk:Main Page") );
0125         e2->setToken( QStringLiteral("cecded1f35005d22904a35cc7b736e18+\\") );
0126         e2->setBaseTimestamp( QDateTime::fromString(QStringLiteral("2008-03-20T17:26:39Z"),QStringLiteral("yyyy-MM-ddThh:mm:ssZ")) );
0127         e2->setStartTimestamp( QDateTime::fromString(QStringLiteral("2008-03-27T21:15:39Z"),QStringLiteral("yyyy-MM-ddThh:mm:ssZ")) );
0128         e2->setAppendText( QStringLiteral("Hello everyone!") );
0129         QTest::newRow("Append")
0130                 << QStringLiteral("/?format=xml&action=edit&appendtext=Hello%20everyone!&basetimestamp=2008-03-20T17:26:39Z&md5=4d184ec6e8fe61abccb8ff62c4583cd0&section=new&starttimestamp=2008-03-27T21:15:39Z&summary=Hello%20World&title=Talk:Main%20Page&token=cecded1f35005d22904a35cc7b736e18+%5C")
0131                 << QStringLiteral("<api><edit result=\"Success\" pageid=\"12\" title=\"Talk:Main Page\" oldrevid=\"465\" newrevid=\"471\" /></api>")
0132                 << e2;
0133 
0134         Edit* const e3 = new Edit( *m_mediaWiki, nullptr);
0135         e3->setSection(QStringLiteral("new"));
0136         e3->setSummary(QStringLiteral("Hello World"));
0137         e3->setPageName( QStringLiteral("Talk:Main Page") );
0138         e3->setToken( QStringLiteral("cecded1f35005d22904a35cc7b736e18+\\") );
0139         e3->setBaseTimestamp( QDateTime::fromString(QStringLiteral("2008-03-20T17:26:39Z"),QStringLiteral("yyyy-MM-ddThh:mm:ssZ")) );
0140         e3->setStartTimestamp( QDateTime::fromString(QStringLiteral("2008-03-27T21:15:39Z"),QStringLiteral("yyyy-MM-ddThh:mm:ssZ")) );
0141         e3->setPrependText( QStringLiteral("Hello everyone!") );
0142         QTest::newRow("Prepend")
0143                 <<  QStringLiteral("/?format=xml&action=edit&basetimestamp=2008-03-20T17:26:39Z&md5=4d184ec6e8fe61abccb8ff62c4583cd0&prependtext=Hello%20everyone!&section=new&starttimestamp=2008-03-27T21:15:39Z&summary=Hello%20World&title=Talk:Main%20Page&token=cecded1f35005d22904a35cc7b736e18+%5C")
0144                 << QStringLiteral("<api><edit result=\"Success\" pageid=\"12\" title=\"Talk:Main Page\" oldrevid=\"465\" newrevid=\"471\" /></api>")
0145                 << e3;
0146 
0147         Edit* const e4 = new Edit( *m_mediaWiki, nullptr);
0148         e4->setSummary(QStringLiteral("Hello World"));
0149         e4->setPageName( QStringLiteral("Talk:Main Page") );
0150         e4->setToken( QStringLiteral("cecded1f35005d22904a35cc7b736e18+\\") );
0151         e4->setBaseTimestamp( QDateTime::fromString(QStringLiteral("2008-03-20T17:26:39Z"),QStringLiteral("yyyy-MM-ddThh:mm:ssZ")) );
0152         e4->setStartTimestamp( QDateTime::fromString(QStringLiteral("2008-03-27T21:15:39Z"),QStringLiteral("yyyy-MM-ddThh:mm:ssZ")) );
0153         e4->setUndo(13585);
0154         QTest::newRow("Undo")
0155                 <<  QStringLiteral("/?format=xml&action=edit&basetimestamp=2008-03-20T17:26:39Z&starttimestamp=2008-03-27T21:15:39Z&summary=Hello%20World&title=Talk:Main%20Page&undo=13585&token=cecded1f35005d22904a35cc7b736e18+%5C")
0156                 << QStringLiteral("<api><edit result=\"Success\" pageid=\"12\" title=\"Talk:Main Page\" oldrevid=\"465\" newrevid=\"471\" /></api>")
0157                 << e4;
0158 
0159         Edit* const e5 = new Edit( *m_mediaWiki, nullptr);
0160         e5->setSummary(QStringLiteral("Hello World"));
0161         e5->setPageName( QStringLiteral("Talk:Main Page") );
0162         e5->setToken( QStringLiteral("cecded1f35005d22904a35cc7b736e18+\\") );
0163         e5->setBaseTimestamp( QDateTime::fromString(QStringLiteral("2008-03-20T17:26:39Z"),QStringLiteral("yyyy-MM-ddThh:mm:ssZ")) );
0164         e5->setStartTimestamp( QDateTime::fromString(QStringLiteral("2008-03-27T21:15:39Z"),QStringLiteral("yyyy-MM-ddThh:mm:ssZ")) );
0165         e5->setUndoAfter(13585);
0166         QTest::newRow("Undo After")
0167                 <<  QStringLiteral("/?format=xml&action=edit&basetimestamp=2008-03-20T17:26:39Z&starttimestamp=2008-03-27T21:15:39Z&summary=Hello%20World&title=Talk:Main%20Page&undoafter=13585&token=cecded1f35005d22904a35cc7b736e18+%5C")
0168                 << QStringLiteral("<api><edit result=\"Success\" pageid=\"12\" title=\"Talk:Main Page\" oldrevid=\"465\" newrevid=\"471\" /></api>")
0169                 << e5;
0170 
0171         Edit* const e6 = new Edit( *m_mediaWiki, nullptr);
0172         e6->setSummary(QStringLiteral("Hello World"));
0173         e6->setPageName( QStringLiteral("Talk:Main Page") );
0174         e6->setToken( QStringLiteral("cecded1f35005d22904a35cc7b736e18+\\") );
0175         e6->setBaseTimestamp( QDateTime::fromString(QStringLiteral("2008-03-20T17:26:39Z"),QStringLiteral("yyyy-MM-ddThh:mm:ssZ")) );
0176         e6->setText( QStringLiteral("Hello everyone!") );
0177         e6->setRecreate(true);
0178         e6->setSection(QStringLiteral("new"));
0179         e6->setStartTimestamp( QDateTime::fromString(QStringLiteral("2008-03-27T21:15:39Z"),QStringLiteral("yyyy-MM-ddThh:mm:ssZ")) );
0180         QTest::newRow("Recreate")
0181                 <<  QStringLiteral("/?format=xml&action=edit&basetimestamp=2008-03-20T17:26:39Z&md5=4d184ec6e8fe61abccb8ff62c4583cd0&recreate=on&section=new&starttimestamp=2008-03-27T21:15:39Z&summary=Hello%20World&text=Hello%20everyone!&title=Talk:Main%20Page&token=cecded1f35005d22904a35cc7b736e18+%5C")
0182                 << QStringLiteral("<api><edit result=\"Success\" pageid=\"12\" title=\"Talk:Main Page\" oldrevid=\"465\" newrevid=\"471\" /></api>")
0183                 << e6;
0184 
0185         Edit* const e7 = new Edit( *m_mediaWiki, nullptr);
0186         e7->setSummary(QStringLiteral("Hello World"));
0187         e7->setPageName( QStringLiteral("Talk:Main Page") );
0188         e7->setToken( QStringLiteral("cecded1f35005d22904a35cc7b736e18+\\") );
0189         e7->setBaseTimestamp( QDateTime::fromString(QStringLiteral("2008-03-20T17:26:39Z"),QStringLiteral("yyyy-MM-ddThh:mm:ssZ")) );
0190         e7->setText( QStringLiteral("Hello everyone!") );
0191         e7->setCreateonly(true);
0192         e7->setSection(QStringLiteral("new"));
0193         e7->setStartTimestamp( QDateTime::fromString(QStringLiteral("2008-03-27T21:15:39Z"),QStringLiteral("yyyy-MM-ddThh:mm:ssZ")) );
0194         QTest::newRow("CreateOnly")
0195                 <<  QStringLiteral("/?format=xml&action=edit&basetimestamp=2008-03-20T17:26:39Z&createonly=on&md5=4d184ec6e8fe61abccb8ff62c4583cd0&section=new&starttimestamp=2008-03-27T21:15:39Z&summary=Hello%20World&text=Hello%20everyone!&title=Talk:Main%20Page&token=cecded1f35005d22904a35cc7b736e18+%5C")
0196                 << QStringLiteral("<api><edit result=\"Success\" pageid=\"12\" title=\"Talk:Main Page\" oldrevid=\"465\" newrevid=\"471\" /></api>")
0197                 << e7;
0198 
0199         Edit* const e8 = new Edit( *m_mediaWiki, nullptr);
0200         e8->setSummary(QStringLiteral("Hello World"));
0201         e8->setPageName( QStringLiteral("Talk:Main Page") );
0202         e8->setToken( QStringLiteral("cecded1f35005d22904a35cc7b736e18+\\") );
0203         e8->setBaseTimestamp( QDateTime::fromString(QStringLiteral("2008-03-20T17:26:39Z"),QStringLiteral("yyyy-MM-ddThh:mm:ssZ")) );
0204         e8->setText( QStringLiteral("Hello everyone!") );
0205         e8->setNocreate(true);
0206         e8->setSection(QStringLiteral("new"));
0207         e8->setStartTimestamp( QDateTime::fromString(QStringLiteral("2008-03-27T21:15:39Z"),QStringLiteral("yyyy-MM-ddThh:mm:ssZ")) );
0208         QTest::newRow("No Create")
0209                 <<  QStringLiteral("/?format=xml&action=edit&basetimestamp=2008-03-20T17:26:39Z&md5=4d184ec6e8fe61abccb8ff62c4583cd0&nocreate=on&section=new&starttimestamp=2008-03-27T21:15:39Z&summary=Hello%20World&text=Hello%20everyone!&title=Talk:Main%20Page&token=cecded1f35005d22904a35cc7b736e18+%5C")
0210                 << QStringLiteral("<api><edit result=\"Success\" pageid=\"12\" title=\"Talk:Main Page\" oldrevid=\"465\" newrevid=\"471\" /></api>")
0211                 << e8;
0212 
0213         Edit* const e9 = new Edit( *m_mediaWiki, nullptr);
0214         e9->setSummary(QStringLiteral("Hello World"));
0215         e9->setPageName( QStringLiteral("Talk:Main Page") );
0216         e9->setToken( QStringLiteral("cecded1f35005d22904a35cc7b736e18+\\") );
0217         e9->setBaseTimestamp( QDateTime::fromString(QStringLiteral("2008-03-20T17:26:39Z"),QStringLiteral("yyyy-MM-ddThh:mm:ssZ")) );
0218         e9->setText( QStringLiteral("Hello everyone!") );
0219         e9->setMinor(true);
0220         e9->setSection(QStringLiteral("new"));
0221         e9->setStartTimestamp( QDateTime::fromString(QStringLiteral("2008-03-27T21:15:39Z"),QStringLiteral("yyyy-MM-ddThh:mm:ssZ")) );
0222         QTest::newRow("Minor")
0223                 <<  QStringLiteral("/?format=xml&action=edit&basetimestamp=2008-03-20T17:26:39Z&md5=4d184ec6e8fe61abccb8ff62c4583cd0&minor=on&section=new&starttimestamp=2008-03-27T21:15:39Z&summary=Hello%20World&text=Hello%20everyone!&title=Talk:Main%20Page&token=cecded1f35005d22904a35cc7b736e18+%5C")
0224                 << QStringLiteral("<api><edit result=\"Success\" pageid=\"12\" title=\"Talk:Main Page\" oldrevid=\"465\" newrevid=\"471\" /></api>")
0225                 << e9;
0226 
0227         Edit* const e10 = new Edit( *m_mediaWiki, nullptr);
0228         e10->setSummary(QStringLiteral("Hello World"));
0229         e10->setPageName( QStringLiteral("Talk:Main Page") );
0230         e10->setToken( QStringLiteral("cecded1f35005d22904a35cc7b736e18+\\") );
0231         e10->setBaseTimestamp( QDateTime::fromString(QStringLiteral("2008-03-20T17:26:39Z"),QStringLiteral("yyyy-MM-ddThh:mm:ssZ")) );
0232         e10->setText( QStringLiteral("Hello everyone!") );
0233         e10->setWatchList(Edit::watch);
0234         QTest::newRow("WatchList watch")
0235                 <<  QStringLiteral("/?format=xml&action=edit&basetimestamp=2008-03-20T17:26:39Z&md5=4d184ec6e8fe61abccb8ff62c4583cd0&summary=Hello%20World&text=Hello%20everyone!&title=Talk:Main%20Page&watchlist=watch&token=cecded1f35005d22904a35cc7b736e18+%5C")
0236                 << QStringLiteral("<api><edit result=\"Success\" pageid=\"12\" title=\"Talk:Main Page\" oldrevid=\"465\" newrevid=\"471\" /></api>")
0237                 << e10;
0238 
0239         Edit* const e11 = new Edit( *m_mediaWiki, nullptr);
0240         e11->setSummary(QStringLiteral("Hello World"));
0241         e11->setPageName( QStringLiteral("Talk:Main Page") );
0242         e11->setToken( QStringLiteral("cecded1f35005d22904a35cc7b736e18+\\") );
0243         e11->setBaseTimestamp( QDateTime::fromString(QStringLiteral("2008-03-20T17:26:39Z"),QStringLiteral("yyyy-MM-ddThh:mm:ssZ")) );
0244         e11->setText( QStringLiteral("Hello everyone!") );
0245         e11->setWatchList(Edit::unwatch);
0246         QTest::newRow("WatchList unwatch")
0247                 <<  QStringLiteral("/?format=xml&action=edit&basetimestamp=2008-03-20T17:26:39Z&md5=4d184ec6e8fe61abccb8ff62c4583cd0&summary=Hello%20World&text=Hello%20everyone!&title=Talk:Main%20Page&watchlist=unwatch&token=cecded1f35005d22904a35cc7b736e18+%5C")
0248                 << QStringLiteral("<api><edit result=\"Success\" pageid=\"12\" title=\"Talk:Main Page\" oldrevid=\"465\" newrevid=\"471\" /></api>")
0249                 << e11;
0250 
0251         Edit* const e12 = new Edit( *m_mediaWiki, nullptr);
0252         e12->setSummary(QStringLiteral("Hello World"));
0253         e12->setPageName( QStringLiteral("Talk:Main Page") );
0254         e12->setToken( QStringLiteral("cecded1f35005d22904a35cc7b736e18+\\") );
0255         e12->setBaseTimestamp( QDateTime::fromString(QStringLiteral("2008-03-20T17:26:39Z"),QStringLiteral("yyyy-MM-ddThh:mm:ssZ")) );
0256         e12->setText( QStringLiteral("Hello everyone!") );
0257         e12->setWatchList(Edit::preferences);
0258         QTest::newRow("WatchList preferences")
0259                 <<  QStringLiteral("/?format=xml&action=edit&basetimestamp=2008-03-20T17:26:39Z&md5=4d184ec6e8fe61abccb8ff62c4583cd0&summary=Hello%20World&text=Hello%20everyone!&title=Talk:Main%20Page&watchlist=preferences&token=cecded1f35005d22904a35cc7b736e18+%5C")
0260                 << QStringLiteral("<api><edit result=\"Success\" pageid=\"12\" title=\"Talk:Main Page\" oldrevid=\"465\" newrevid=\"471\" /></api>")
0261                 << e12;
0262 
0263         Edit* const e13 = new Edit( *m_mediaWiki, nullptr);
0264         e13->setSummary(QStringLiteral("Hello World"));
0265         e13->setPageName( QStringLiteral("Talk:Main Page") );
0266         e13->setToken( QStringLiteral("cecded1f35005d22904a35cc7b736e18+\\") );
0267         e13->setBaseTimestamp( QDateTime::fromString(QStringLiteral("2008-03-20T17:26:39Z"),QStringLiteral("yyyy-MM-ddThh:mm:ssZ")) );
0268         e13->setText( QStringLiteral("Hello everyone!") );
0269         e13->setWatchList(Edit::nochange);
0270         QTest::newRow("WatchList nochange")
0271                 <<  QStringLiteral("/?format=xml&action=edit&basetimestamp=2008-03-20T17:26:39Z&md5=4d184ec6e8fe61abccb8ff62c4583cd0&summary=Hello%20World&text=Hello%20everyone!&title=Talk:Main%20Page&watchlist=nochange&token=cecded1f35005d22904a35cc7b736e18+%5C")
0272                 << QStringLiteral("<api><edit result=\"Success\" pageid=\"12\" title=\"Talk:Main Page\" oldrevid=\"465\" newrevid=\"471\" /></api>")
0273                 << e13;
0274     }
0275 
0276     void error()
0277     {
0278         QFETCH(QString, scenario);
0279         QFETCH(int, error);
0280 
0281 
0282         editCount = 0;
0283         MediaWiki mediawiki(QUrl(QStringLiteral("http://127.0.0.1:12566")));
0284         FakeServer fakeserver;
0285 
0286         if(scenario != QStringLiteral("error serveur"))
0287         {
0288             fakeserver.setScenario(m_infoScenario);
0289             fakeserver.addScenario(scenario);
0290             fakeserver.startAndWait();
0291         }
0292 
0293         Edit* const job = new Edit(mediawiki, nullptr);
0294         job->setSection(QStringLiteral("new"));
0295         job->setSummary(QStringLiteral("Hello World") );
0296         job->setPageName( QStringLiteral("Talk:Main Page") );
0297         job->setToken( QStringLiteral("cecded1f35005d22904a35cc7b736e18+\\") );
0298         job->setBaseTimestamp( QDateTime::fromString(QStringLiteral("2008-03-20T17:26:39Z"),QStringLiteral("yyyy-MM-ddThh:mm:ssZ")) );
0299         job->setStartTimestamp( QDateTime::fromString(QStringLiteral("2008-03-27T21:15:39Z"),QStringLiteral("yyyy-MM-ddThh:mm:ssZ")) );
0300         job->setText( QStringLiteral("Hello everyone!") );
0301 
0302         connect(job,  SIGNAL(result(KJob*)),
0303                 this, SLOT(editHandle(KJob*)));
0304 
0305         job->exec();
0306 
0307         if(scenario != QStringLiteral("error serveur"))
0308         {
0309             QList<FakeServer::Request> requests = fakeserver.getRequest();
0310             QCOMPARE(requests.size(), 2);
0311         }
0312 
0313         QCOMPARE(job->error(), error);
0314         QCOMPARE(editCount, 1);
0315 
0316         if(scenario != QStringLiteral("error serveur"))
0317         {
0318             QVERIFY(fakeserver.isAllScenarioDone());
0319         }
0320     }
0321     void error_data()
0322     {
0323         QTest::addColumn<QString>("scenario");
0324         QTest::addColumn<int>("error");
0325 
0326         QTest::newRow("No text")
0327                 << QStringLiteral("<api><error code=\"notext\" info=\"\" /> </api>")
0328                 << int(Edit::TextMissing);
0329         QTest::newRow("Invalide section")
0330                 << QStringLiteral("<api><error code=\"invalidsection\" info=\"\" /> </api>")
0331                 << int(Edit::InvalidSection);
0332         QTest::newRow("Invalide section")
0333                 << QStringLiteral("<api><error code=\"invalidsection\" info=\"\" /> </api>")
0334                 << int(Edit::InvalidSection);
0335         QTest::newRow("Protected Title")
0336                 << QStringLiteral("<api><error code=\"protectedtitle\" info=\"\" /> </api>")
0337                 << int(Edit::TitleProtected);
0338         QTest::newRow("Cant Create")
0339                 << QStringLiteral("<api><error code=\"cantcreate\" info=\"\" /> </api>")
0340                 << int(Edit::CreatePagePermissionMissing);
0341         QTest::newRow("Cant Create Anonymous")
0342                 << QStringLiteral("<api><error code=\"cantcreate-anon\" info=\"\" /> </api>")
0343                 << int(Edit::AnonymousCreatePagePermissionMissing);
0344         QTest::newRow("Cant Create Anonymous")
0345                 << QStringLiteral("<api><error code=\"cantcreate-anon\" info=\"\" /> </api>")
0346                 << int(Edit::AnonymousCreatePagePermissionMissing);
0347         QTest::newRow("Article Duplication")
0348                 << QStringLiteral("<api><error code=\"articleexists\" info=\"\" /> </api>")
0349                 << int(Edit::ArticleDuplication);
0350         QTest::newRow("Anonymous Create Image Permission Missing")
0351                 << QStringLiteral("<api><error code=\"noimageredirect-anon\" info=\"\" /> </api>")
0352                 << int(Edit::AnonymousCreateImagePermissionMissing);
0353         QTest::newRow("Create Image Permission Missing")
0354                 << QStringLiteral("<api><error code=\"noimageredirect\" info=\"\" /> </api>")
0355                 << int(Edit::CreateImagePermissionMissing);
0356         QTest::newRow("Spam Detected")
0357                 << QStringLiteral("<api><error code=\"spamdetected\" info=\"\" /> </api>")
0358                 << int(Edit::SpamDetected);
0359         QTest::newRow("Filtered")
0360                 << QStringLiteral("<api><error code=\"filtered\" info=\"\" /> </api>")
0361                 << int(Edit::Filtered);
0362         QTest::newRow("Article Size Exceed")
0363                 << QStringLiteral("<api><error code=\"contenttoobig\" info=\"\" /> </api>")
0364                 << int(Edit::ArticleSizeExceed);
0365         QTest::newRow("Anonymous no edit")
0366                 << QStringLiteral("<api><error code=\"noedit-anon\" info=\"\" /> </api>")
0367                 << int(Edit::AnonymousEditPagePermissionMissing);
0368         QTest::newRow("No edit")
0369                 << QStringLiteral("<api><error code=\"noedit\" info=\"\" /> </api>")
0370                 << int(Edit::EditPagePermissionMissing);
0371         QTest::newRow("Page delete")
0372                 << QStringLiteral("<api><error code=\"pagedeleted\" info=\"\" /> </api>")
0373                 << int(Edit::PageDeleted);
0374         QTest::newRow("Empty page")
0375                 << QStringLiteral("<api><error code=\"emptypage\" info=\"\" /> </api>")
0376                 << int(Edit::EmptyPage);
0377         QTest::newRow("Empty section")
0378                 << QStringLiteral("<api><error code=\"emptynewsection\" info=\"\" /> </api>")
0379                 << int(Edit::EmptySection);
0380         QTest::newRow("Edit conflict")
0381                 << QStringLiteral("<api><error code=\"editconflict\" info=\"\" /> </api>")
0382                 << int(Edit::EditConflict);
0383         QTest::newRow("Revision wrong page")
0384                 << QStringLiteral("<api><error code=\"revwrongpage\" info=\"\" /> </api>")
0385                 << int(Edit::RevWrongPage);
0386         QTest::newRow("Undo Failed")
0387                 << QStringLiteral("<api><error code=\"undofailure\" info=\"\" /> </api>")
0388                 << int(Edit::UndoFailed);
0389     }
0390 
0391     void editTestTextCaptchaTrue()
0392     {
0393         editCount = 0;
0394         FakeServer fakeserver;
0395 
0396         this->request = QStringLiteral("/?format=xml&action=edit&basetimestamp=2008-03-20T17:26:39Z&md5=4d184ec6e8fe61abccb8ff62c4583cd0&section=new&starttimestamp=2008-03-27T21:15:39Z&summary=Hello%20World&text=Hello%20everyone!&title=Talk:Main%20Page&token=cecded1f35005d22904a35cc7b736e18+%5C");
0397         QString senario(QStringLiteral("<api><edit result=\"Failure\"><captcha type=\"math\" mime=\"text/tex\" id=\"509895952\" question=\"36 + 4 = \" /></edit></api>"));
0398 
0399         fakeserver.setScenario(m_infoScenario);
0400         fakeserver.addScenario(senario);
0401         senario = QStringLiteral("<api><edit result=\"Success\" pageid=\"12\" title=\"Talk:Main Page\" oldrevid=\"465\" newrevid=\"471\" /></api>");
0402         fakeserver.addScenario(senario);
0403         fakeserver.startAndWait();
0404 
0405         Edit edit( *m_mediaWiki, nullptr);
0406         edit.setSection(QStringLiteral("new"));
0407         edit.setSummary(QStringLiteral("Hello World") );
0408         edit.setPageName( QStringLiteral("Talk:Main Page") );
0409         edit.setToken( QStringLiteral("cecded1f35005d22904a35cc7b736e18+\\") );
0410         edit.setBaseTimestamp( QDateTime::fromString(QStringLiteral("2008-03-20T17:26:39Z"),QStringLiteral("yyyy-MM-ddThh:mm:ssZ")) );
0411         edit.setStartTimestamp( QDateTime::fromString(QStringLiteral("2008-03-27T21:15:39Z"),QStringLiteral("yyyy-MM-ddThh:mm:ssZ")) );
0412         edit.setText( QStringLiteral("Hello everyone!") );
0413 
0414         connect(&edit, SIGNAL(resultCaptcha(QVariant)),
0415                 this, SLOT(editHandle(QVariant)));
0416 
0417         connect(this, SIGNAL(captchaSignal(QString)),
0418                 &edit, SLOT(finishedCaptcha(QString)));
0419 
0420         edit.exec();
0421 
0422         FakeServer::Request serverrequest = fakeserver.getRequest()[1];
0423         QCOMPARE(serverrequest.type, QStringLiteral("POST"));
0424         QCOMPARE(serverrequest.value, this->request);
0425         QCOMPARE(edit.error(), (int)Edit::NoError);
0426         QCOMPARE(this->editCount, 1);
0427     }
0428 
0429     void cleanupTestCase()
0430     {
0431         delete this->m_mediaWiki;
0432     }
0433 
0434 private:
0435 
0436     int        editCount;
0437     QVariant   CaptchaQuestion;
0438     QString    CaptchaAnswer;
0439     QString    request;
0440     QString    m_infoScenario;
0441     MediaWiki* m_mediaWiki;
0442 };
0443 
0444 QTEST_MAIN(EditTest)
0445 
0446 #include "edittest.moc"
0447 
0448 #endif // TEST_EDIT_H