File indexing completed on 2024-05-12 16:46:16

0001 /***************************************************************************
0002     Copyright (C) 2015 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 <QTest>
0036 #include <QTemporaryDir>
0037 #include <QFile>
0038 #include <QStandardPaths>
0039 
0040 QTEST_GUILESS_MAIN( DocumentTest )
0041 
0042 void DocumentTest::initTestCase() {
0043   QStandardPaths::setTestModeEnabled(true);
0044   Tellico::ImageFactory::init();
0045   // test case is a book file
0046   Tellico::RegisterCollection<Tellico::Data::BookCollection> registerBook(Tellico::Data::Collection::Book, "book");
0047 }
0048 
0049 void DocumentTest::cleanupTestCase() {
0050   Tellico::ImageFactory::clean(true);
0051 }
0052 
0053 void DocumentTest::testImageLocalDirectory() {
0054   Tellico::Config::setImageLocation(Tellico::Config::ImagesInLocalDir);
0055   // the default collection will use a temporary directory as a local image dir
0056   QVERIFY(!Tellico::ImageFactory::localDir().isEmpty());
0057 
0058   QString tempDirName;
0059 
0060   QTemporaryDir tempDir;
0061   QVERIFY(tempDir.isValid());
0062   tempDir.setAutoRemove(true);
0063   tempDirName = tempDir.path();
0064   QString fileName = tempDirName + "/with-image.tc";
0065   QString imageDirName = tempDirName + "/with-image_files/";
0066 
0067   // copy a collection file that includes an image into the temporary directory
0068   QVERIFY(QFile::copy(QFINDTESTDATA("data/with-image.tc"), fileName));
0069 
0070   Tellico::Data::Document* doc = Tellico::Data::Document::self();
0071   QVERIFY(doc->openDocument(QUrl::fromLocalFile(fileName)));
0072   QCOMPARE(Tellico::ImageFactory::localDir(), imageDirName);
0073 
0074   Tellico::Data::CollPtr coll = doc->collection();
0075   QVERIFY(coll);
0076   QCOMPARE(coll->type(), Tellico::Data::Collection::Book);
0077   QCOMPARE(coll->title(), QStringLiteral("My Books"));
0078   QCOMPARE(coll->entries().size(), 1);
0079 
0080   Tellico::Data::EntryPtr e = coll->entries().at(0);
0081   QVERIFY(e);
0082   QCOMPARE(e->field(QStringLiteral("cover")), QStringLiteral("17b54b2a742c6d342a75f122d615a793.jpeg"));
0083 
0084   // save the document, so the images get copied out of the .tc file into the local image directory
0085   QVERIFY(doc->saveDocument(QUrl::fromLocalFile(fileName)));
0086   // verify that backup file gets created
0087   QVERIFY(QFile::exists(fileName + '~'));
0088 
0089   // check that the local image directory is created with the image file inside
0090   QDir imageDir(imageDirName);
0091   QVERIFY(imageDir.exists());
0092   QVERIFY(imageDir.exists(e->field(QStringLiteral("cover"))));
0093 
0094   // clear the internal image cache
0095   Tellico::ImageFactory::clean(true);
0096 
0097   // verify that the images are copied from the old directory when saving to a new file
0098   QString fileName2 = tempDirName + "/with-image2.tc";
0099   QString imageDirName2 = tempDirName + "/with-image2_files/";
0100   QVERIFY(doc->saveDocument(QUrl::fromLocalFile(fileName2)));
0101   QVERIFY(QFile::exists(fileName2));
0102   QDir imageDir2(imageDirName2);
0103   QVERIFY(imageDir2.exists());
0104   QVERIFY(imageDir2.exists(e->field(QStringLiteral("cover"))));
0105 
0106   /*************************************************************************/
0107   /* now also verify image directory when file name has multiple periods */
0108   /* see https://bugs.kde.org/show_bug.cgi?id=348088 */
0109   /* also have to check backwards compatibility with prior behavior */
0110   /*************************************************************************/
0111 
0112   QString fileName3 = tempDirName + "/with-image.1.tc";
0113   QString imageDirName3 = tempDirName + "/with-image.1_files/";
0114 
0115   // copy the collection file, which no longer contains the images inside
0116   QVERIFY(QFile::copy(fileName, fileName3));
0117   QVERIFY(doc->openDocument(QUrl::fromLocalFile(fileName3)));
0118   QCOMPARE(Tellico::ImageFactory::localDir(), imageDirName3);
0119   QDir imageDir3(imageDirName3);
0120 
0121   // verify that the images can be loaded from the image directory that does NOT have multiple periods
0122   // since that was the behavior prior to the bug being fixed
0123   coll = doc->collection();
0124   e = coll->entries().at(0);
0125   // image should not be in the next image dir yet since we haven't saved
0126   QVERIFY(!imageDir3.exists(e->field(QStringLiteral("cover"))));
0127   QVERIFY(!Tellico::ImageFactory::imageById(e->field("cover")).isNull());
0128 
0129   // now remove the first image from the first image directory, save the document, and verify that
0130   // the proper image exists and is written
0131   QVERIFY(imageDir.remove(e->field("cover")));
0132   QVERIFY(!imageDir.exists(e->field(QStringLiteral("cover"))));
0133   QVERIFY(doc->saveDocument(QUrl::fromLocalFile(fileName3)));
0134   // now the file should exist in the proper location
0135   QVERIFY(imageDir3.exists(e->field(QStringLiteral("cover"))));
0136   // clear the cache
0137   Tellico::ImageFactory::clean(true);
0138   QVERIFY(!Tellico::ImageFactory::imageById(e->field("cover")).isNull());
0139 
0140   // sanity check, the directory should not exists after QTemporaryDir destruction
0141   tempDir.remove();
0142   QVERIFY(!QDir(tempDirName).exists());
0143 }