diff options
author | Harald Eilertsen <haraldei@anduin.net> | 2025-02-04 22:51:59 +0100 |
---|---|---|
committer | Harald Eilertsen <haraldei@anduin.net> | 2025-02-12 20:05:42 +0100 |
commit | 0dd456c65399f320062a23a06aca912e298c9bd1 (patch) | |
tree | 94a2ad9bd848698af1ba9e1bd1a444f669ef961b /tests/unit | |
parent | 97ba14cbe05c4a679c523f2d9b1237610093fe6a (diff) | |
download | volse-hubzilla-0dd456c65399f320062a23a06aca912e298c9bd1.tar.gz volse-hubzilla-0dd456c65399f320062a23a06aca912e298c9bd1.tar.bz2 volse-hubzilla-0dd456c65399f320062a23a06aca912e298c9bd1.zip |
Replace the the code to extract epub thumbnails
The PHP Epub Meta library has a dependency that prevents deployment on
32bit architectures. We also don't need all the functionality in that
library, so this patch replaces it with our own simplified code for
fetching the cover embedded in Epub archives.
We also expand the test suite and clean up some minor issues in the
Epubthumbnail class.
Diffstat (limited to 'tests/unit')
-rw-r--r-- | tests/unit/Thumb/EpubthumbTest.php | 145 |
1 files changed, 141 insertions, 4 deletions
diff --git a/tests/unit/Thumb/EpubthumbTest.php b/tests/unit/Thumb/EpubthumbTest.php index 5dabaf359..d381d940e 100644 --- a/tests/unit/Thumb/EpubthumbTest.php +++ b/tests/unit/Thumb/EpubthumbTest.php @@ -8,14 +8,151 @@ namespace Zotlabs\Tests\Unit\Thumbs; +use PHPUnit\Framework\Attributes\{AfterClass, Before, BeforeClass}; use Zotlabs\Thumbs\Epubthumb; use Zotlabs\Tests\Unit\UnitTestCase; +use ZipArchive; + class EpubthumbTest extends UnitTestCase { - function testEpubThumbMatch(): void { - $thumbnailer = new Epubthumb(); + private const TMPDIR = __DIR__ . '/tmp'; + + private Epubthumb $thumbnailer; + + /** + * Create a temp dir to use for the tests in this class. + */ + #[BeforeClass] + static function setupTmpDir(): void { + if (!is_dir(self::TMPDIR)) { + mkdir(self::TMPDIR); + } + } + + /** + * Clean up and remove the temp dir after the tests. + */ + #[AfterClass] + static function cleanupTmpDir(): void { + $files = scandir(self::TMPDIR); + if ($files !== false) { + foreach($files as $f) { + if ($f[0] !== '.') { + unlink(self::TMPDIR . '/' . $f); + } + } + } + rmdir(self::TMPDIR); + } + + /** + * Create the thumbnailer object for tests. + * + * This is run before each test, so that each test has it's own + * instance of the thumbnailer. + */ + #[Before] + function createThumbnailer(): void { + $this->thumbnailer = new Epubthumb(); + } + + /* + * Tests + */ + + public function testEpubThumbMatch(): void { + $this->assertTrue($this->thumbnailer->Match('application/epub+zip')); + $this->assertFalse($this->thumbnailer->Match('application/zip')); + } + + public function testNoThumbnailCreatedForFileThatDontExist(): void { + $this->checkCreateThumbnail(self::TMPDIR . '/nonexisting.epub', false); + } + + public function testNoThumbnailCreatedIfNotAZipArchive(): void { + $filename = self::TMPDIR . '/notazip.epub'; + + file_put_contents($filename, 'This is not a ZIP file!'); + + $this->checkCreateThumbnail($filename, false); + } + + public function testNoThumbnailCreatedIfInvalidEpub(): void { + $filename = self::TMPDIR . '/nocontainer.epub'; + + $epub = new ZipArchive(); + $epub->open($filename, ZipArchive::CREATE); + $epub->addFromString('somefile.txt', 'It was a dark an stormy night...'); + $epub->close(); + + $this->checkCreateThumbnail($filename, false); + } + + public function testNoThumbnailCreatedIfCoverFileMissing(): void { + $filename = self::TMPDIR . '/good.epub'; + + $epub = new ZipArchive(); + $epub->open($filename, ZipArchive::CREATE); + $this->addEpubContainer($epub); + $this->addEpubPackage($epub); + $epub->close(); + + $this->checkCreateThumbnail($filename, false); + } + + public function testCreateCoverFromEpub(): void { + $filename = self::TMPDIR . '/good.epub'; + + $epub = new ZipArchive(); + $epub->open($filename, ZipArchive::CREATE); + $this->addEpubContainer($epub); + $this->addEpubPackage($epub); + $epub->addFile(PROJECT_BASE . '/images/red-koala.png', 'EPUB/cover.png'); + $epub->close(); + + $this->checkCreateThumbnail($filename, true); + } + + /* + * Helper functions + */ + + private function checkCreateThumbnail(string $filename, bool $expectThumbnail): void { + $attach = [ 'content' => $filename ]; + $this->thumbnailer->Thumb($attach, 0); + + $this->assertEquals($expectThumbnail, file_exists($filename . '.thumb')); + } + + private function addEpubContainer(ZipArchive $epub): void { + $xml = <<<XML + <?xml version="1.0" encoding="UTF-8"?> + <container version="1.0" xmlns="urn:oasis:names:tc:opendocument:xmlns:container"> + <rootfiles> + <rootfile full-path="EPUB/package.opf" media-type="application/oebps-package+xml"/> + </rootfiles> + </container> + XML; + + $epub->addEmptyDir('META-INF'); + $epub->addFromString('META-INF/container.xml', $xml); + } + + private function addEpubPackage(ZipArchive $epub): void { + $xml = <<<XML + <?xml version="1.0" encoding="UTF-8"?> + <package xmlns="http://www.idpf.org/2007/opf" version="3.0" unique-identifier="pub-identifier"> + <manifest> + <item + properties="cover-image" + id="ci" + href="cover.png" + media-type="image/png" /> + </manifest> + </package> + XML; - $this->assertTrue($thumbnailer->Match('application/epub+zip')); - $this->assertFalse($thumbnailer->Match('application/zip')); + $epub->addEmptyDir('EPUB'); + $epub->addFromString('EPUB/package.opf', $xml); } } |