File indexing completed on 2025-01-05 03:58:12

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 <QObject>
0021 #include <QTest>
0022 
0023 // Local includes
0024 
0025 #include "fakeserver/fakeserver.h"
0026 #include "mediawiki_iface.h"
0027 #include "mediawiki_imageinfo.h"
0028 #include "mediawiki_queryimageinfo.h"
0029 
0030 using namespace MediaWiki;
0031 
0032 class Q_DECL_HIDDEN QueryImageinfoTest : public QObject
0033 {
0034     Q_OBJECT
0035 
0036 public:
0037 
0038     explicit QueryImageInfoTest(QObject* const parent = nullptr)
0039     {
0040         Q_UNUSED(parent);
0041     }
0042 
0043 public Q_SLOTS:
0044 
0045     void resultHandle(const QList<Imageinfo>& imageinfos)
0046     {
0047         m_imageinfosReceived.push_back(imageinfos);
0048     }
0049 
0050     void missingTitleHandle(const QList<Imageinfo>& imageinfos)
0051     {
0052         Q_UNUSED(imageinfos)
0053         m_requestWithMissingTitleHasBeenSent = true;
0054     }
0055 
0056 private Q_SLOTS:
0057 
0058     void init()
0059     {
0060         m_imageinfosReceived = QList<QList<Imageinfo> >();
0061     }
0062 
0063     void testQuery()
0064     {
0065         // Constructs the fakeserver
0066         FakeServer fakeserver;
0067         fakeserver.setScenario(QStringLiteral("<?xml version=\"1.0\"?><api><query><normalized><n from=\"Image:Image.bmp\" "
0068                                               "to=\"File:Image.bmp\" /></normalized><pages><page ns=\"6\" title=\"File:Image.bmp\" "
0069                                               "missing=\"\" imagerepository=\"shared\"><imageinfo><ii timestamp=\"2008-06-06T22:27:45Z\" "
0070                                               "user=\"User1\" size=\"448798\" width=\"924\" height=\"1203\" "
0071                                               "url=\"http://url/File:Image.bmp\" "                              // krazy:exclude=insecurenet
0072                                               "thumburl=\"http://thumburl/File:Image.bmp\" "                    // krazy:exclude=insecurenet
0073                                               "thumbwidth=\"78\" thumbheight=\"102\" "
0074                                               "descriptionurl=\"http://descriptionurl/File:Image.bmp\" "        // krazy:exclude=insecurenet
0075                                               "comment=\"Comment1\" sha1=\"00be23585fde01190a0f8c60fc4267ea00f3745d\" "
0076                                               "mime=\"image/bmp\"><metadata><metadata name=\"Name1\" value=\"Value1\" "
0077                                               "/><metadata name=\"Name2\" value=\"Value2\" /></metadata></ii></imageinfo></page>"
0078                                               "</pages></query><query-continue><imageinfo iistart=\"2007-06-06T22:27:45Z\" "
0079                                               "/></query-continue></api>"));
0080         fakeserver.addScenario(QStringLiteral("<?xml version=\"1.0\"?><api><query><normalized><n from=\"Image:Image.bmp\" "
0081                                               "to=\"File:Image.bmp\" /></normalized><pages><page ns=\"6\" title=\"File:Image.bmp\" "
0082                                               "missing=\"\" imagerepository=\"shared\"><imageinfo><ii timestamp=\"2007-06-06T22:27:45Z\" "
0083                                               "user=\"User2\" size=\"448798\" width=\"924\" height=\"1203\" "
0084                                               "url=\"http://url/File:Image.bmp\" "                              // krazy:exclude=insecurenet
0085                                               "descriptionurl=\"http://descriptionurl/File:Image.bmp\" "        // krazy:exclude=insecurenet
0086                                               "comment=\"Comment2\" sha1=\"00be23585fde01190a0f8c60fc4267ea00f3745d\" "
0087                                               "mime=\"image/bmp\"><metadata><metadata name=\"Name1\" value=\"Value1\" "
0088                                               "/><metadata name=\"Name2\" value=\"Value2\" /></metadata></ii></imageinfo>"
0089                                               "</page></pages></query></api>"));
0090         fakeserver.startAndWait();
0091 
0092         // Prepare the job
0093         Iface MediaWiki(QUrl(QStringLiteral("http://127.0.0.1:12566")));        // krazy:exclude=insecurenet
0094         QueryImageinfo* const job = new QueryImageinfo(MediaWiki);
0095         job->setTitle(QStringLiteral("Image:Image.bmp"));
0096 
0097         job->setProperties(
0098             QueryImageinfo::Timestamp |
0099             QueryImageinfo::User      |
0100             QueryImageinfo::Comment   |
0101             QueryImageinfo::Url       |
0102             QueryImageinfo::Size      |
0103             QueryImageinfo::Sha1      |
0104             QueryImageinfo::Mime      |
0105             QueryImageinfo::Metadata);
0106 
0107         job->setLimit(1u);
0108         job->setBeginTimestamp(QDateTime(QDate(2008, 06, 06), QTime(22, 27, 45, 0)));
0109         job->setEndTimestamp(QDateTime(QDate(2007, 06, 06), QTime(22, 27, 45, 0)));
0110         job->setWidthScale(78u);
0111         job->setHeightScale(102u);
0112 
0113         connect(job, SIGNAL(result(QList<Imageinfo>)),
0114                 this, SLOT(resultHandle(QList<Imageinfo>)));
0115 
0116         job->exec();
0117 
0118         // Test job
0119         QCOMPARE(job->error(), int(KJob::NoError));
0120 
0121         // Test fakeserver
0122         QVERIFY(fakeserver.isAllScenarioDone());
0123 
0124         // Test requests sent
0125         const QList<FakeServer::Request> requests = fakeserver.getRequest();
0126         QCOMPARE(requests.size(), 2);
0127 
0128         for (unsigned int i = 0 ; i < 2 ; ++i)
0129         {
0130             QCOMPARE(requests[i].agent, MediaWiki.userAgent());
0131             QCOMPARE(requests[i].type, QStringLiteral("GET"));
0132         }
0133 
0134         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"));
0135         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"));
0136 
0137         // Test pages received
0138         QList<QList<Imageinfo> > imageinfosExpected;
0139         {
0140             QHash<QString, QVariant> metadata;
0141             metadata[QStringLiteral("Name1")] = QStringLiteral("Value1");
0142             metadata[QStringLiteral("Name2")] = QStringLiteral("Value2");
0143 
0144             {
0145                 Imageinfo imageinfoExpected;
0146                 imageinfoExpected.setTimestamp(QDateTime(QDate(2008, 06, 06), QTime(22, 27, 45, 0)));
0147                 imageinfoExpected.setUser(QStringLiteral("User1"));
0148                 imageinfoExpected.setComment(QStringLiteral("Comment1"));
0149                 imageinfoExpected.setUrl(QUrl(QStringLiteral("http://url/File:Image.bmp")));                        // krazy:exclude=insecurenet
0150                 imageinfoExpected.setDescriptionUrl(QUrl(QStringLiteral("http://descriptionurl/File:Image.bmp")));  // krazy:exclude=insecurenet
0151                 imageinfoExpected.setThumbUrl(QUrl(QStringLiteral("http://thumburl/File:Image.bmp")));              // krazy:exclude=insecurenet
0152                 imageinfoExpected.setThumbWidth(78);
0153                 imageinfoExpected.setThumbHeight(102);
0154                 imageinfoExpected.setSize(448798);
0155                 imageinfoExpected.setWidth(924);
0156                 imageinfoExpected.setHeight(1203);
0157                 imageinfoExpected.setSha1(QStringLiteral("00be23585fde01190a0f8c60fc4267ea00f3745d"));
0158                 imageinfoExpected.setMime(QStringLiteral("image/bmp"));
0159                 imageinfoExpected.setMetadata(metadata);
0160                 imageinfosExpected.push_back(QList<Imageinfo>() << imageinfoExpected);
0161             }
0162 
0163             {
0164                 Imageinfo imageinfoExpected;
0165                 imageinfoExpected.setTimestamp(QDateTime(QDate(2007, 06, 06), QTime(22, 27, 45, 0)));
0166                 imageinfoExpected.setUser(QStringLiteral("User2"));
0167                 imageinfoExpected.setComment(QStringLiteral("Comment2"));
0168                 imageinfoExpected.setUrl(QUrl(QStringLiteral("http://url/File:Image.bmp")));                        // krazy:exclude=insecurenet
0169                 imageinfoExpected.setDescriptionUrl(QUrl(QStringLiteral("http://descriptionurl/File:Image.bmp")));  // krazy:exclude=insecurenet
0170                 imageinfoExpected.setSize(448798);
0171                 imageinfoExpected.setWidth(924);
0172                 imageinfoExpected.setHeight(1203);
0173                 imageinfoExpected.setSha1(QStringLiteral("00be23585fde01190a0f8c60fc4267ea00f3745d"));
0174                 imageinfoExpected.setMime(QStringLiteral("image/bmp"));
0175                 imageinfoExpected.setMetadata(metadata);
0176                 imageinfosExpected.push_back(QList<Imageinfo>() << imageinfoExpected);
0177             }
0178         }
0179 
0180         QCOMPARE(m_imageinfosReceived, imageinfosExpected);
0181     }
0182 
0183     void testMissingTitle()
0184     {
0185         // Constructs the fakeserver
0186         FakeServer fakeserver;
0187         fakeserver.startAndWait();
0188 
0189         // Prepare the job
0190         Iface MediaWiki(QUrl(QStringLiteral("http://127.0.0.1:12566")));    // krazy:exclude=insecurenet
0191         QueryImageinfo* const job = new QueryImageinfo(MediaWiki);
0192 
0193         job->setProperties(
0194             QueryImageinfo::Timestamp |
0195             QueryImageinfo::User      |
0196             QueryImageinfo::Comment   |
0197             QueryImageinfo::Url       |
0198             QueryImageinfo::Size      |
0199             QueryImageinfo::Sha1      |
0200             QueryImageinfo::Mime      |
0201             QueryImageinfo::Metadata);
0202 
0203         job->setLimit(1u);
0204         job->setBeginTimestamp(QDateTime(QDate(2008, 06, 06), QTime(22, 27, 45, 0)));
0205         job->setEndTimestamp(QDateTime(QDate(2007, 06, 06), QTime(22, 27, 45, 0)));
0206         job->setWidthScale(78u);
0207         job->setHeightScale(102u);
0208 
0209         connect(job, SIGNAL(result(QList<Imageinfo>)),
0210                 this, SLOT(missingTitleHandle(QList<Imageinfo>)));
0211 
0212         job->exec();
0213 
0214         // Test job
0215         QCOMPARE(job->error(), int(QueryImageinfo::MissingMandatoryParameter));
0216 
0217         // Test fakeserver
0218         QCOMPARE(m_requestWithMissingTitleHasBeenSent, false);
0219     }
0220 
0221 private:
0222 
0223     // Disable
0224     QueryImageInfoTest() = delete;
0225 
0226 private:
0227 
0228     QList<QList<Imageinfo> > m_imageinfosReceived;
0229     bool                     m_requestWithMissingTitleHasBeenSent = false;
0230 };
0231 
0232 QTEST_MAIN(QueryImageinfoTest)
0233 
0234 #include "queryimageinfo_utest.moc"