File indexing completed on 2024-05-05 03:54:35

0001 /*
0002     SPDX-FileCopyrightText: 2014 Vishesh Handa <me@vhanda.in>
0003     SPDX-FileCopyrightText: 2016 Christoph Cullmann <cullmann@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.1-or-later
0006 */
0007 
0008 #include "simpleextractionresult.h"
0009 #include "indexerextractortestsconfig.h"
0010 #include "extractors/odfextractor.h"
0011 #include "mimeutils.h"
0012 
0013 #include <QMimeDatabase>
0014 #include <QObject>
0015 #include <QTest>
0016 
0017 class OdfExtractorTest : public QObject
0018 {
0019     Q_OBJECT
0020 private:
0021     QString testFilePath(const QString& fileName) const;
0022 
0023 private Q_SLOTS:
0024     void testNoExtraction_data();
0025     void testNoExtraction();
0026 
0027     void testText_data();
0028     void testText();
0029     void testTextMetaDataOnly();
0030 
0031     void testPresentation_data();
0032     void testPresentation();
0033 
0034     void testGraphic_data();
0035     void testGraphic();
0036 
0037     void testSpreadsheet_data();
0038     void testSpreadsheet();
0039 
0040     void testTextMissingMetaNoCrash();
0041     void testTextMissingContentNoCrash();
0042 
0043 private:
0044     QMimeDatabase mimeDb;
0045 };
0046 
0047 using namespace KFileMetaData;
0048 
0049 Q_DECLARE_METATYPE(Type::Type)
0050 
0051 QString OdfExtractorTest::testFilePath(const QString& fileName) const
0052 {
0053     return QLatin1String(INDEXER_TESTS_SAMPLE_FILES_PATH) + QLatin1Char('/') + fileName;
0054 }
0055 
0056 void OdfExtractorTest::testNoExtraction_data()
0057 {
0058     QTest::addColumn<QString>("fileName");
0059     QTest::addColumn<QList<Type::Type>>("types");
0060 
0061     QTest::newRow("regular text") << QStringLiteral("test.odt") << QList<Type::Type>{Type::Document};
0062     QTest::newRow("flat xml text") << QStringLiteral("test.fodt") << QList<Type::Type>{Type::Document};
0063 
0064     QTest::newRow("regular presentation") << QStringLiteral("test.odp") << QList<Type::Type>{Type::Document, Type::Presentation};
0065     QTest::newRow("flat xml presentation") << QStringLiteral("test.fodp") << QList<Type::Type>{Type::Document, Type::Presentation};
0066 
0067     QTest::newRow("regular graphic") << QStringLiteral("test.odg") << QList<Type::Type>{Type::Document, Type::Image};
0068     QTest::newRow("flat xml graphic") << QStringLiteral("test.fodg") << QList<Type::Type>{Type::Document, Type::Image};
0069 }
0070 
0071 void OdfExtractorTest::testNoExtraction()
0072 {
0073     QFETCH(QString, fileName);
0074     QFETCH(QList<Type::Type>, types);
0075 
0076     OdfExtractor plugin{this};
0077 
0078     const QString path = testFilePath(fileName);
0079     const QString mimeType = MimeUtils::strictMimeType(path, mimeDb).name();
0080     QVERIFY(plugin.mimetypes().contains(mimeType));
0081 
0082     SimpleExtractionResult result(path, mimeType, ExtractionResult::ExtractNothing);
0083     plugin.extract(&result);
0084 
0085     QCOMPARE(result.types().size(), types.size());
0086     QCOMPARE(result.types(), types);
0087     QCOMPARE(result.properties().size(),0);
0088 }
0089 
0090 void OdfExtractorTest::testText_data()
0091 {
0092     QTest::addColumn<QString>("fileName");
0093 
0094     QTest::newRow("regular") << QStringLiteral("test.odt");
0095     QTest::newRow("flat xml") << QStringLiteral("test.fodt");
0096 }
0097 
0098 void OdfExtractorTest::testText()
0099 {
0100     QFETCH(QString, fileName);
0101 
0102     OdfExtractor plugin{this};
0103 
0104     const QString path = testFilePath(fileName);
0105     const QString mimeType = MimeUtils::strictMimeType(path, mimeDb).name();
0106     QVERIFY(plugin.mimetypes().contains(mimeType));
0107 
0108     SimpleExtractionResult result(path, mimeType);
0109     plugin.extract(&result);
0110 
0111     QCOMPARE(result.types().size(), 1);
0112     QCOMPARE(result.types().at(0), Type::Document);
0113 
0114     QCOMPARE(result.properties().value(Property::Title), QVariant(QStringLiteral("KFileMetaData Title")));
0115     QCOMPARE(result.properties().value(Property::Subject), QVariant(QStringLiteral("KFileMetaData Subject")));
0116     QCOMPARE(result.properties().value(Property::Keywords), QVariant(QStringLiteral("KFileMetaData keyword")));
0117     QCOMPARE(result.properties().value(Property::Description), QVariant(QStringLiteral("KFileMetaData description")));
0118     QVERIFY(result.properties().value(Property::Generator).toString().contains(QStringLiteral("LibreOffice")));
0119 
0120     QDateTime dt(QDate(2014, 07, 01), QTime(17, 37, 40, 690));
0121     QCOMPARE(result.properties().value(Property::CreationDate), QVariant(dt));
0122 
0123     QCOMPARE(result.properties().value(Property::WordCount), QVariant(4));
0124     QCOMPARE(result.properties().value(Property::PageCount), QVariant(1));
0125     QCOMPARE(result.text(), QStringLiteral("Test file for KFileMetaData. "));
0126     QCOMPARE(result.properties().size(), 8);
0127 }
0128 
0129 void OdfExtractorTest::testTextMetaDataOnly()
0130 {
0131     OdfExtractor plugin{this};
0132 
0133     SimpleExtractionResult result(testFilePath(QStringLiteral("test.odt")), QStringLiteral("application/vnd.oasis.opendocument.text"), ExtractionResult::ExtractMetaData);
0134     plugin.extract(&result);
0135 
0136     QCOMPARE(result.types().size(), 1);
0137     QCOMPARE(result.properties().size(), 8);
0138     QVERIFY(result.text().isEmpty());
0139 }
0140 
0141 void OdfExtractorTest::testPresentation_data()
0142 {
0143     QTest::addColumn<QString>("fileName");
0144 
0145     QTest::newRow("regular") << QStringLiteral("test.odp");
0146     QTest::newRow("flat xml") << QStringLiteral("test.fodp");
0147 }
0148 
0149 void OdfExtractorTest::testPresentation()
0150 {
0151     QFETCH(QString, fileName);
0152 
0153     OdfExtractor plugin{this};
0154 
0155     const QString path = testFilePath(fileName);
0156     const QString mimeType = MimeUtils::strictMimeType(path, mimeDb).name();
0157     QVERIFY(plugin.mimetypes().contains(mimeType));
0158 
0159     SimpleExtractionResult result(path, mimeType);
0160     plugin.extract(&result);
0161 
0162     QCOMPARE(result.types().size(), 2);
0163     QCOMPARE(result.types().at(0), Type::Document);
0164     QCOMPARE(result.types().at(1), Type::Presentation);
0165 
0166     QVERIFY(result.properties().value(Property::Generator).toString().contains(QStringLiteral("LibreOffice")));
0167     QDateTime dt(QDate(2014, 07, 02), QTime(10, 59, 23, 434));
0168     QCOMPARE(result.properties().value(Property::CreationDate), QVariant(dt));
0169 
0170     QCOMPARE(result.text(), QStringLiteral("KFileMetaData Pres "));
0171 }
0172 
0173 void OdfExtractorTest::testGraphic_data()
0174 {
0175     QTest::addColumn<QString>("fileName");
0176 
0177     QTest::newRow("regular") << QStringLiteral("test.odg");
0178     QTest::newRow("flat xml") << QStringLiteral("test.fodg");
0179 }
0180 
0181 void OdfExtractorTest::testGraphic()
0182 {
0183     QFETCH(QString, fileName);
0184 
0185     OdfExtractor plugin{this};
0186 
0187     const QString path = testFilePath(fileName);
0188     const QString mimeType = MimeUtils::strictMimeType(path, mimeDb).name();
0189     QVERIFY(plugin.mimetypes().contains(mimeType));
0190 
0191     SimpleExtractionResult result(path, mimeType);
0192     plugin.extract(&result);
0193 
0194     QCOMPARE(result.types().size(), 2);
0195     QCOMPARE(result.types().at(0), Type::Document);
0196     QCOMPARE(result.types().at(1), Type::Image);
0197 
0198     QVERIFY(result.properties().value(Property::Generator).toString().contains(QStringLiteral("LibreOffice")));
0199     QDateTime dt(QDate(2022, 11, 8), QTime(15, 27, 0, 660));
0200     QCOMPARE(result.properties().value(Property::CreationDate), QVariant(dt));
0201 
0202     QCOMPARE(result.text(), QStringLiteral("KFileMetaData Graphic "));
0203 }
0204 
0205 void OdfExtractorTest::testSpreadsheet_data()
0206 {
0207     QTest::addColumn<QString>("fileName");
0208 
0209     QTest::newRow("regular") << QStringLiteral("test.ods");
0210 }
0211 
0212 void OdfExtractorTest::testSpreadsheet()
0213 {
0214     QFETCH(QString, fileName);
0215 
0216     OdfExtractor plugin{this};
0217 
0218     const QString path = testFilePath(fileName);
0219     const QString mimeType = MimeUtils::strictMimeType(path, mimeDb).name();
0220     QVERIFY(plugin.mimetypes().contains(mimeType));
0221 
0222     SimpleExtractionResult result(path, mimeType);
0223     plugin.extract(&result);
0224 
0225     QCOMPARE(result.types().size(), 2);
0226     QCOMPARE(result.types().at(0), Type::Document);
0227     QCOMPARE(result.types().at(1), Type::Spreadsheet);
0228 
0229     QVERIFY(result.properties().value(Property::Generator).toString().contains(QStringLiteral("LibreOffice")));
0230     QDateTime dt(QDate(2023, 11, 12), QTime(05, 59, 53, 986));
0231     QCOMPARE(result.properties().value(Property::CreationDate), QVariant(dt));
0232     QCOMPARE(result.properties().value(Property::Author), QStringLiteral("Author KFM"));
0233     QCOMPARE(result.properties().value(Property::Title), QVariant(QStringLiteral("KFileMetaData Title")));
0234     QCOMPARE(result.properties().value(Property::Subject), QVariant(QStringLiteral("KFileMetaData Subject")));
0235     QCOMPARE(result.properties().value(Property::Keywords), QVariant(QStringLiteral("KFileMetaData keyword")));
0236     QCOMPARE(result.properties().value(Property::Description), QVariant(QStringLiteral("Some KFileMetaData comment")));
0237     QCOMPARE(result.text(), QStringLiteral("First cell Second cell Second row "));
0238 }
0239 
0240 void OdfExtractorTest::testTextMissingMetaNoCrash()
0241 {
0242     OdfExtractor plugin{this};
0243 
0244     SimpleExtractionResult result(testFilePath(QStringLiteral("test_missing_meta.odt")), QStringLiteral("application/vnd.oasis.opendocument.text"));
0245     plugin.extract(&result);
0246 }
0247 
0248 void OdfExtractorTest::testTextMissingContentNoCrash()
0249 {
0250     OdfExtractor plugin{this};
0251 
0252     SimpleExtractionResult result(testFilePath(QStringLiteral("test_missing_content.odt")), QStringLiteral("application/vnd.oasis.opendocument.text"));
0253     plugin.extract(&result);
0254 }
0255 
0256 QTEST_GUILESS_MAIN(OdfExtractorTest)
0257 
0258 #include "odfextractortest.moc"