File indexing completed on 2024-05-12 05:09:57
0001 /*************************************************************************** 0002 Copyright (C) 2015-2022 Robby Stephenson <robby@periapsis.org> 0003 ***************************************************************************/ 0004 0005 /*************************************************************************** 0006 * * 0007 * This program is free software; you can redistribute it and/or * 0008 * modify it under the terms of the GNU General Public License as * 0009 * published by the Free Software Foundation; either version 2 of * 0010 * the License or (at your option) version 3 or any later version * 0011 * accepted by the membership of KDE e.V. (or its successor approved * 0012 * by the membership of KDE e.V.), which shall act as a proxy * 0013 * defined in Section 14 of version 3 of the license. * 0014 * * 0015 * This program is distributed in the hope that it will be useful, * 0016 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 0017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 0018 * GNU General Public License for more details. * 0019 * * 0020 * You should have received a copy of the GNU General Public License * 0021 * along with this program. If not, see <http://www.gnu.org/licenses/>. * 0022 * * 0023 ***************************************************************************/ 0024 0025 #undef QT_NO_CAST_FROM_ASCII 0026 0027 #include "documenttest.h" 0028 #include "../document.h" 0029 #include "../images/imagefactory.h" 0030 #include "../images/image.h" 0031 #include "../config/tellico_config.h" 0032 #include "../collections/bookcollection.h" 0033 #include "../collectionfactory.h" 0034 0035 #include <KLocalizedString> 0036 0037 #include <QTest> 0038 #include <QTemporaryDir> 0039 #include <QTemporaryFile> 0040 #include <QFile> 0041 #include <QStandardPaths> 0042 0043 QTEST_GUILESS_MAIN( DocumentTest ) 0044 0045 void DocumentTest::initTestCase() { 0046 QStandardPaths::setTestModeEnabled(true); 0047 KLocalizedString::setApplicationDomain("tellico"); 0048 Tellico::ImageFactory::init(); 0049 // test case is a book file 0050 Tellico::RegisterCollection<Tellico::Data::BookCollection> registerBook(Tellico::Data::Collection::Book, "book"); 0051 } 0052 0053 void DocumentTest::cleanupTestCase() { 0054 Tellico::ImageFactory::clean(true); 0055 } 0056 0057 void DocumentTest::testImageLocalDirectory() { 0058 Tellico::Config::setImageLocation(Tellico::Config::ImagesInLocalDir); 0059 // the default collection will use a temporary directory as a local image dir 0060 QVERIFY(!Tellico::ImageFactory::localDir().isEmpty()); 0061 0062 QString tempDirName; 0063 0064 QTemporaryDir tempDir; 0065 QVERIFY(tempDir.isValid()); 0066 tempDir.setAutoRemove(true); 0067 tempDirName = tempDir.path(); 0068 QString fileName = tempDirName + "/with-image.tc"; 0069 QString imageDirName = tempDirName + "/with-image_files/"; 0070 0071 // copy a collection file that includes an image into the temporary directory 0072 QVERIFY(QFile::copy(QFINDTESTDATA("data/with-image.tc"), fileName)); 0073 0074 Tellico::Data::Document* doc = Tellico::Data::Document::self(); 0075 QVERIFY(doc->openDocument(QUrl::fromLocalFile(fileName))); 0076 QCOMPARE(Tellico::ImageFactory::localDir(), imageDirName); 0077 0078 Tellico::Data::CollPtr coll = doc->collection(); 0079 QVERIFY(coll); 0080 QCOMPARE(coll->type(), Tellico::Data::Collection::Book); 0081 QCOMPARE(coll->title(), QStringLiteral("My Books")); 0082 QCOMPARE(coll->entries().size(), 1); 0083 0084 Tellico::Data::EntryPtr e = coll->entries().at(0); 0085 QVERIFY(e); 0086 QCOMPARE(e->field(QStringLiteral("cover")), QStringLiteral("17b54b2a742c6d342a75f122d615a793.jpeg")); 0087 0088 // save the document, so the images get copied out of the .tc file into the local image directory 0089 QVERIFY(doc->saveDocument(QUrl::fromLocalFile(fileName))); 0090 // verify that backup file gets created 0091 QVERIFY(QFile::exists(fileName + '~')); 0092 0093 // check that the local image directory is created with the image file inside 0094 QDir imageDir(imageDirName); 0095 QVERIFY(imageDir.exists()); 0096 QVERIFY(imageDir.exists(e->field(QStringLiteral("cover")))); 0097 0098 // clear the internal image cache 0099 Tellico::ImageFactory::clean(true); 0100 0101 // verify that the images are copied from the old directory when saving to a new file 0102 QString fileName2 = tempDirName + "/with-image2.tc"; 0103 QString imageDirName2 = tempDirName + "/with-image2_files/"; 0104 QVERIFY(doc->saveDocument(QUrl::fromLocalFile(fileName2))); 0105 QVERIFY(QFile::exists(fileName2)); 0106 QDir imageDir2(imageDirName2); 0107 QVERIFY(imageDir2.exists()); 0108 QVERIFY(imageDir2.exists(e->field(QStringLiteral("cover")))); 0109 0110 /*************************************************************************/ 0111 /* now also verify image directory when file name has multiple periods */ 0112 /* see https://bugs.kde.org/show_bug.cgi?id=348088 */ 0113 /* also have to check backwards compatibility with prior behavior */ 0114 /*************************************************************************/ 0115 0116 QString fileName3 = tempDirName + "/with-image.1.tc"; 0117 QString imageDirName3 = tempDirName + "/with-image.1_files/"; 0118 0119 // copy the collection file, which no longer contains the images inside 0120 QVERIFY(QFile::copy(fileName, fileName3)); 0121 QVERIFY(doc->openDocument(QUrl::fromLocalFile(fileName3))); 0122 QCOMPARE(Tellico::ImageFactory::localDir(), imageDirName3); 0123 QDir imageDir3(imageDirName3); 0124 0125 // verify that the images can be loaded from the image directory that does NOT have multiple periods 0126 // since that was the behavior prior to the bug being fixed 0127 coll = doc->collection(); 0128 e = coll->entries().at(0); 0129 // image should not be in the next image dir yet since we haven't saved 0130 QVERIFY(!imageDir3.exists(e->field(QStringLiteral("cover")))); 0131 QVERIFY(!Tellico::ImageFactory::imageById(e->field("cover")).isNull()); 0132 0133 // now remove the first image from the first image directory, save the document, and verify that 0134 // the proper image exists and is written 0135 QVERIFY(imageDir.remove(e->field("cover"))); 0136 QVERIFY(!imageDir.exists(e->field(QStringLiteral("cover")))); 0137 QVERIFY(doc->saveDocument(QUrl::fromLocalFile(fileName3))); 0138 // now the file should exist in the proper location 0139 QVERIFY(imageDir3.exists(e->field(QStringLiteral("cover")))); 0140 // clear the cache 0141 Tellico::ImageFactory::clean(true); 0142 QVERIFY(!Tellico::ImageFactory::imageById(e->field("cover")).isNull()); 0143 0144 // sanity check, the directory should not exists after QTemporaryDir destruction 0145 tempDir.remove(); 0146 QVERIFY(!QDir(tempDirName).exists()); 0147 } 0148 0149 void DocumentTest::testSaveTemplate() { 0150 auto doc = Tellico::Data::Document::self(); 0151 QVERIFY(doc); 0152 QVERIFY(doc->newDocument(Tellico::Data::Collection::Book)); 0153 auto coll = doc->collection(); 0154 Tellico::Data::EntryPtr entry1(new Tellico::Data::Entry(coll)); 0155 coll->addEntries(entry1); 0156 entry1->setField(QStringLiteral("title"), QStringLiteral("new title")); 0157 // modify a field too, to check that the template saves the modified field 0158 auto field = coll->fieldByName(QStringLiteral("publisher")); 0159 field->setTitle(QStringLiteral("batman")); 0160 0161 Tellico::FilterRule* rule1 = new Tellico::FilterRule(QStringLiteral("title"), 0162 QStringLiteral("Star Wars"), 0163 Tellico::FilterRule::FuncEquals); 0164 Tellico::FilterPtr filter(new Tellico::Filter(Tellico::Filter::MatchAny)); 0165 filter->append(rule1); 0166 coll->addFilter(filter); 0167 0168 QString templateName(QStringLiteral("my new template")); 0169 QTemporaryFile templateFile(QStringLiteral("documenttest-template.XXXXXX.xml")); 0170 QVERIFY(templateFile.open()); 0171 QUrl templateUrl = QUrl::fromLocalFile(templateFile.fileName()); 0172 QVERIFY(doc->saveDocumentTemplate(templateUrl, templateName)); 0173 0174 QVERIFY(doc->openDocument(templateUrl)); 0175 auto new_coll = doc->collection(); 0176 QVERIFY(new_coll); 0177 QCOMPARE(new_coll->title(), templateName); 0178 QCOMPARE(new_coll->entryCount(), 0); 0179 0180 auto new_field = new_coll->fieldByName(QStringLiteral("publisher")); 0181 QCOMPARE(new_field->title(), QStringLiteral("batman")); 0182 0183 QCOMPARE(new_coll->filters().count(), 1); 0184 }