File indexing completed on 2024-04-21 15:32:10
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 Ludovic Delfau 0011 * <a href="mailto:ludovicdelfau at gmail dot com">ludovicdelfau at gmail dot com</a> 0012 * 0013 * This program is free software; you can redistribute it 0014 * and/or modify it under the terms of the GNU General 0015 * Public License as published by the Free Software Foundation; 0016 * either version 2, or (at your option) 0017 * any later version. 0018 * 0019 * This program is distributed in the hope that it will be useful, 0020 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0021 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0022 * GNU General Public License for more details. 0023 * 0024 * ============================================================ */ 0025 0026 #include <QObject> 0027 #include <QTest> 0028 0029 #include "libmediawikitest/fakeserver.h" 0030 0031 #include "mediawiki.h" 0032 #include "imageinfo.h" 0033 #include "queryimageinfo.h" 0034 0035 using namespace mediawiki; 0036 0037 class QueryImageinfoTest : public QObject 0038 { 0039 Q_OBJECT 0040 0041 public Q_SLOTS: 0042 0043 void resultHandle(const QList<Imageinfo>& imageinfos) 0044 { 0045 imageinfosReceived.push_back(imageinfos); 0046 } 0047 0048 void missingTitleHandle(const QList<Imageinfo>& imageinfos) 0049 { 0050 Q_UNUSED(imageinfos) 0051 requestWithMissingTitleHasBeenSent = true; 0052 } 0053 0054 private Q_SLOTS: 0055 0056 void init() 0057 { 0058 imageinfosReceived = QList<QList<Imageinfo> >(); 0059 } 0060 0061 void testQuery() 0062 { 0063 // Constructs the fakeserver 0064 FakeServer fakeserver; 0065 fakeserver.setScenario(QStringLiteral("<?xml version=\"1.0\"?><api><query><normalized><n from=\"Image:Image.bmp\" to=\"File:Image.bmp\" /></normalized><pages><page ns=\"6\" title=\"File:Image.bmp\" missing=\"\" imagerepository=\"shared\"><imageinfo><ii timestamp=\"2008-06-06T22:27:45Z\" user=\"User1\" size=\"448798\" width=\"924\" height=\"1203\" url=\"http://url/File:Image.bmp\" thumburl=\"http://thumburl/File:Image.bmp\" thumbwidth=\"78\" thumbheight=\"102\" descriptionurl=\"http://descriptionurl/File:Image.bmp\" comment=\"Comment1\" sha1=\"00be23585fde01190a0f8c60fc4267ea00f3745d\" mime=\"image/bmp\"><metadata><metadata name=\"Name1\" value=\"Value1\" /><metadata name=\"Name2\" value=\"Value2\" /></metadata></ii></imageinfo></page></pages></query><query-continue><imageinfo iistart=\"2007-06-06T22:27:45Z\" /></query-continue></api>")); 0066 fakeserver.addScenario(QStringLiteral("<?xml version=\"1.0\"?><api><query><normalized><n from=\"Image:Image.bmp\" to=\"File:Image.bmp\" /></normalized><pages><page ns=\"6\" title=\"File:Image.bmp\" missing=\"\" imagerepository=\"shared\"><imageinfo><ii timestamp=\"2007-06-06T22:27:45Z\" user=\"User2\" size=\"448798\" width=\"924\" height=\"1203\" url=\"http://url/File:Image.bmp\" descriptionurl=\"http://descriptionurl/File:Image.bmp\" comment=\"Comment2\" sha1=\"00be23585fde01190a0f8c60fc4267ea00f3745d\" mime=\"image/bmp\"><metadata><metadata name=\"Name1\" value=\"Value1\" /><metadata name=\"Name2\" value=\"Value2\" /></metadata></ii></imageinfo></page></pages></query></api>")); 0067 fakeserver.startAndWait(); 0068 0069 // Prepare the job 0070 MediaWiki mediawiki(QUrl(QStringLiteral("http://127.0.0.1:12566"))); 0071 QueryImageinfo* job = new QueryImageinfo(mediawiki); 0072 job->setTitle(QStringLiteral("Image:Image.bmp")); 0073 job->setProperties( 0074 QueryImageinfo::Timestamp| 0075 QueryImageinfo::User| 0076 QueryImageinfo::Comment| 0077 QueryImageinfo::Url| 0078 QueryImageinfo::Size| 0079 QueryImageinfo::Sha1| 0080 QueryImageinfo::Mime| 0081 QueryImageinfo::Metadata); 0082 job->setLimit(1u); 0083 job->setBeginTimestamp(QDateTime(QDate(2008, 06, 06), QTime(22, 27, 45, 0))); 0084 job->setEndTimestamp(QDateTime(QDate(2007, 06, 06), QTime(22, 27, 45, 0))); 0085 job->setWidthScale(78u); 0086 job->setHeightScale(102u); 0087 connect(job, SIGNAL(result(QList<Imageinfo>)), this, SLOT(resultHandle(QList<Imageinfo>))); 0088 job->exec(); 0089 0090 // Test job 0091 QCOMPARE(job->error(), int(KJob::NoError)); 0092 0093 // Test fakeserver 0094 QVERIFY(fakeserver.isAllScenarioDone()); 0095 0096 // Test requests sent 0097 const QList<FakeServer::Request> requests = fakeserver.getRequest(); 0098 QCOMPARE(requests.size(), 2); 0099 for (unsigned int i = 0; i < 2; ++i) 0100 { 0101 QCOMPARE(requests[i].agent, mediawiki.userAgent()); 0102 QCOMPARE(requests[i].type, QStringLiteral("GET")); 0103 } 0104 QCOMPARE(requests[0].value, QStringLiteral("/?format=xml&action=query&titles=Image:Image.bmp&prop=imageinfo&iiprop=timestamp%7Cuser%7Ccomment%7Curl%7Csize%7Csha1%7Cmime%7Cmetadata&iilimit=1&iistart=2008-06-06T22:27:45Z&iiend=2007-06-06T22:27:45Z&iiurlwidth=78&iiurlheight=102")); 0105 QCOMPARE(requests[1].value, QStringLiteral("/?format=xml&action=query&titles=Image:Image.bmp&prop=imageinfo&iiprop=timestamp%7Cuser%7Ccomment%7Curl%7Csize%7Csha1%7Cmime%7Cmetadata&iilimit=1&iistart=2007-06-06T22:27:45Z&iiend=2007-06-06T22:27:45Z&iiurlwidth=78&iiurlheight=102")); 0106 0107 // Test pages received 0108 QList<QList<Imageinfo> > imageinfosExpected; 0109 { 0110 QHash<QString, QVariant> metadata; 0111 metadata[QStringLiteral("Name1")] = QStringLiteral("Value1"); 0112 metadata[QStringLiteral("Name2")] = QStringLiteral("Value2"); 0113 { 0114 Imageinfo imageinfoExpected; 0115 imageinfoExpected.setTimestamp(QDateTime(QDate(2008, 06, 06), QTime(22, 27, 45, 0))); 0116 imageinfoExpected.setUser(QStringLiteral("User1")); 0117 imageinfoExpected.setComment(QStringLiteral("Comment1")); 0118 imageinfoExpected.setUrl(QUrl(QStringLiteral("http://url/File:Image.bmp"))); 0119 imageinfoExpected.setDescriptionUrl(QUrl(QStringLiteral("http://descriptionurl/File:Image.bmp"))); 0120 imageinfoExpected.setThumbUrl(QUrl(QStringLiteral("http://thumburl/File:Image.bmp"))); 0121 imageinfoExpected.setThumbWidth(78); 0122 imageinfoExpected.setThumbHeight(102); 0123 imageinfoExpected.setSize(448798); 0124 imageinfoExpected.setWidth(924); 0125 imageinfoExpected.setHeight(1203); 0126 imageinfoExpected.setSha1(QStringLiteral("00be23585fde01190a0f8c60fc4267ea00f3745d")); 0127 imageinfoExpected.setMime(QStringLiteral("image/bmp")); 0128 imageinfoExpected.setMetadata(metadata); 0129 imageinfosExpected.push_back(QList<Imageinfo>() << imageinfoExpected); 0130 } 0131 { 0132 Imageinfo imageinfoExpected; 0133 imageinfoExpected.setTimestamp(QDateTime(QDate(2007, 06, 06), QTime(22, 27, 45, 0))); 0134 imageinfoExpected.setUser(QStringLiteral("User2")); 0135 imageinfoExpected.setComment(QStringLiteral("Comment2")); 0136 imageinfoExpected.setUrl(QUrl(QStringLiteral("http://url/File:Image.bmp"))); 0137 imageinfoExpected.setDescriptionUrl(QUrl(QStringLiteral("http://descriptionurl/File:Image.bmp"))); 0138 imageinfoExpected.setSize(448798); 0139 imageinfoExpected.setWidth(924); 0140 imageinfoExpected.setHeight(1203); 0141 imageinfoExpected.setSha1(QStringLiteral("00be23585fde01190a0f8c60fc4267ea00f3745d")); 0142 imageinfoExpected.setMime(QStringLiteral("image/bmp")); 0143 imageinfoExpected.setMetadata(metadata); 0144 imageinfosExpected.push_back(QList<Imageinfo>() << imageinfoExpected); 0145 } 0146 } 0147 QCOMPARE(imageinfosReceived, imageinfosExpected); 0148 } 0149 0150 void testMissingTitle() 0151 { 0152 // Constructs the fakeserver 0153 FakeServer fakeserver; 0154 fakeserver.startAndWait(); 0155 0156 // Prepare the job 0157 MediaWiki mediawiki(QUrl(QStringLiteral("http://127.0.0.1:12566"))); 0158 QueryImageinfo* job = new QueryImageinfo(mediawiki); 0159 job->setProperties( 0160 QueryImageinfo::Timestamp| 0161 QueryImageinfo::User| 0162 QueryImageinfo::Comment| 0163 QueryImageinfo::Url| 0164 QueryImageinfo::Size| 0165 QueryImageinfo::Sha1| 0166 QueryImageinfo::Mime| 0167 QueryImageinfo::Metadata); 0168 job->setLimit(1u); 0169 job->setBeginTimestamp(QDateTime(QDate(2008, 06, 06), QTime(22, 27, 45, 0))); 0170 job->setEndTimestamp(QDateTime(QDate(2007, 06, 06), QTime(22, 27, 45, 0))); 0171 job->setWidthScale(78u); 0172 job->setHeightScale(102u); 0173 connect(job, SIGNAL(result(QList<Imageinfo>)), this, SLOT(missingTitleHandle(QList<Imageinfo>))); 0174 job->exec(); 0175 0176 // Test job 0177 QCOMPARE(job->error(), int(QueryImageinfo::MissingMandatoryParameter)); 0178 0179 // Test fakeserver 0180 QCOMPARE(requestWithMissingTitleHasBeenSent, false); 0181 } 0182 0183 private: 0184 0185 QList<QList<Imageinfo> > imageinfosReceived; 0186 bool requestWithMissingTitleHasBeenSent; 0187 }; 0188 0189 QTEST_MAIN(QueryImageinfoTest) 0190 0191 #include "queryimageinfotest.moc"