File indexing completed on 2025-01-19 03:57:47
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_queryimages.h" 0028 #include "mediawiki_image.h" 0029 0030 using MediaWiki::Iface; 0031 using MediaWiki::QueryImages; 0032 using MediaWiki::Image; 0033 0034 Q_DECLARE_METATYPE(QList<QString>) 0035 Q_DECLARE_METATYPE(QList<Image>) 0036 Q_DECLARE_METATYPE(QList<QList<Image> >) 0037 0038 class Q_DECL_HIDDEN QueryImagesTest : public QObject 0039 { 0040 Q_OBJECT 0041 0042 public: 0043 0044 explicit QueryImagesTest(QObject* const parent = nullptr) 0045 { 0046 Q_UNUSED(parent); 0047 } 0048 0049 public Q_SLOTS: 0050 0051 void imagesHandle(const QList<Image>& images) 0052 { 0053 m_imagesReceivedList.push_back(images); 0054 } 0055 0056 private Q_SLOTS: 0057 0058 void init() 0059 { 0060 m_imagesReceivedList.clear(); 0061 } 0062 0063 void testConstructor() 0064 { 0065 QFETCH(QList<QString>, scenarios); 0066 QFETCH(QString, title); 0067 QFETCH(unsigned int, limit); 0068 QFETCH(QList<QList<Image> >, imagesExpectedList); 0069 0070 // Constructs the fakeserver 0071 FakeServer fakeserver; 0072 0073 for (int i = 0 ; i < scenarios.size() ; ++i) 0074 { 0075 if (i == 0) 0076 { 0077 fakeserver.setScenario(scenarios[i]); 0078 } 0079 else 0080 { 0081 fakeserver.addScenario(scenarios[i]); 0082 } 0083 } 0084 0085 fakeserver.startAndWait(); 0086 0087 // Prepare the job 0088 Iface MediaWiki(QUrl(QStringLiteral("http://127.0.0.1:12566"))); // krazy:exclude=insecurenet 0089 QueryImages* const job = new QueryImages(MediaWiki); 0090 job->setTitle(title); 0091 job->setLimit(limit); 0092 0093 connect(job, SIGNAL(images(QList<Image>)), 0094 this, SLOT(imagesHandle(QList<Image>))); 0095 0096 job->exec(); 0097 0098 // Test job 0099 QCOMPARE(job->error(), int(KJob::NoError)); 0100 0101 // Test requests sent 0102 const QList<FakeServer::Request> requests = fakeserver.getRequest(); 0103 QCOMPARE(requests.size(), imagesExpectedList.size()); 0104 0105 for (int i = 0 ; i < requests.size() ; ++i) 0106 { 0107 QCOMPARE(requests[i].agent, MediaWiki.userAgent()); 0108 QCOMPARE(requests[i].type, QStringLiteral("GET")); 0109 0110 if (i == 0) 0111 { 0112 QCOMPARE(requests[i].value, QString(QStringLiteral("/?format=xml&action=query&titles=") + title + QStringLiteral("&prop=images&imlimit=") + QString::number(limit))); 0113 } 0114 else 0115 { 0116 QCOMPARE(requests[i].value, QString(QStringLiteral("/?format=xml&action=query&titles=") + title + QStringLiteral("&prop=images&imlimit=") + QString::number(limit) + QStringLiteral("&imcontinue=1234%7C") + imagesExpectedList[i][0].title().remove(0, 5))); 0117 } 0118 } 0119 0120 // Test pages received 0121 QCOMPARE(m_imagesReceivedList, imagesExpectedList); 0122 0123 // Test fakeserver 0124 QVERIFY(fakeserver.isAllScenarioDone()); 0125 } 0126 0127 void testConstructor_data() 0128 { 0129 QTest::addColumn<QList<QString> >("scenarios"); 0130 QTest::addColumn<QString>("title"); 0131 QTest::addColumn<unsigned int>("limit"); 0132 QTest::addColumn<QList<QList<Image> > >("imagesExpectedList"); 0133 Image image, image2, image3; 0134 0135 QTest::newRow("Page with no image") 0136 << (QList<QString>() 0137 << QStringLiteral("<?xml version=\"1.0\"?><api><query><pages><page pageid=\"736\" ns=\"1\" title=\"Title-1\"></page></pages></query></api>")) 0138 << QStringLiteral("Title-1") 0139 << 10u 0140 << (QList<QList<Image> >() << QList<Image>()); 0141 0142 image.setNamespaceId(46u); 0143 image.setTitle(QStringLiteral("File:Image-1-1")); 0144 QTest::newRow("Page with one image") 0145 << (QList<QString>() 0146 << QStringLiteral("<?xml version=\"1.0\"?><api><query><pages><page pageid=\"736\" ns=\"1\" title=\"Title-1\"><images><im ns=\"46\" title=\"File:Image-1-1\" /></images></page></pages></query></api>")) 0147 << QStringLiteral("Title-1") 0148 << 10u 0149 << (QList<QList<Image> >() << (QList<Image>() << image)); 0150 0151 image2.setNamespaceId(9997u); 0152 image2.setTitle(QStringLiteral("File:Image-1-2")); 0153 QTest::newRow("Page with two images") 0154 << (QList<QString>() 0155 << QStringLiteral("<?xml version=\"1.0\"?><api><query><pages><page pageid=\"736\" ns=\"1\" title=\"Title-1\"><images><im ns=\"46\" title=\"File:Image-1-1\" /><im ns=\"9997\" title=\"File:Image-1-2\" /></images></page></pages></query></api>")) 0156 << QStringLiteral("Title-1") 0157 << 10u 0158 << (QList<QList<Image> >() << (QList<Image>() << image << image2)); 0159 0160 image.setNamespaceId(8u); 0161 image.setTitle(QStringLiteral("File:Image-2-1")); 0162 0163 image2.setNamespaceId(8998u); 0164 image2.setTitle(QStringLiteral("File:Image-2-2")); 0165 0166 image3.setNamespaceId(38423283u); 0167 image3.setTitle(QStringLiteral("File:Image-2-3")); 0168 0169 QTest::newRow("Page with three images by two signals") 0170 << (QList<QString>() 0171 << QStringLiteral("<?xml version=\"1.0\"?><api><query><pages><page pageid=\"1234\" ns=\"5757\" title=\"Title-2\"><images><im ns=\"8\" title=\"File:Image-2-1\" /><im ns=\"8998\" title=\"File:Image-2-2\" /></images></page></pages></query><query-continue><images imcontinue=\"1234|Image-2-3\" /></query-continue></api>") 0172 << QStringLiteral("<?xml version=\"1.0\"?><api><query><pages><page pageid=\"1234\" ns=\"5757\" title=\"Title-2\"><images><im ns=\"38423283\" title=\"File:Image-2-3\" /></images></page></pages></query></api>")) 0173 << QStringLiteral("Title-2") 0174 << 2u 0175 << (QList<QList<Image> >() 0176 << (QList<Image>() << image << image2) 0177 << (QList<Image>() << image3)); 0178 0179 0180 } 0181 0182 private: 0183 0184 QList<QList<Image> > m_imagesReceivedList; 0185 }; 0186 0187 QTEST_MAIN(QueryImagesTest) 0188 0189 #include "queryimages_utest.moc"