/ tests / EpubProcessorTest.php
<?php
/**
 * SeekQuarry/Yioop --
 * Open Source Pure PHP Search Engine, Crawler, and Indexer
 *
 * Copyright (C) 2009 - 2026  Chris Pollett chris@pollett.org
 *
 * LICENSE:
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 *
 * END LICENSE
 *
 * @author Vijeth Patil vijeth.patil@gmail.coms
 * @license https://www.gnu.org/licenses/ GPL3
 * @link https://www.seekquarry.com/
 * @copyright 2009 - 2026
 * @filesource
 */
namespace seekquarry\yioop\tests;

use seekquarry\yioop\configs as C;
use seekquarry\yioop\library as L;
use seekquarry\yioop\library\CrawlConstants;
use seekquarry\yioop\library\processors\EpubProcessor;
use seekquarry\yioop\library\UnitTest;
use seekquarry\yioop\models\Model;

/**
 * UnitTest for the EpubProcessor class. An EpubProcessor is used to process
 * a .epub (ebook publishing standard) file and extract summary from it. This
 * class tests the processing of an .epub file format by EpubProcessor.
 *
 *
 * @author Vijeth Patil
 */
class EpubProcessorTest extends UnitTest implements CrawlConstants
{
    /**
     * Folder under the test files that a case writing files puts them
     * in, so nothing is written outside the tests.
     */
    const TEST_DIR = '/test_files/epub_processor_test';
    /**
     * Creates a new EpubProcessor object so that
     * we can process an .epub format file.
     */
    public function setUp()
    {
        $epub_object = new EpubProcessor();
        $url = "http://www.yioop.com/TestEpubYioop.epub";
        $filename = C\PARENT_DIR."/tests/test_files/TestEpubYioop.epub";
        $page = file_get_contents($filename);
        $summary = $epub_object->process($page, $url);
        $this->test_objects['summary'] = $summary;
        /* A case that writes files puts them in a folder of its own
           under the test files. Left empty, the names it built began at
           the root of the file system, which is not somewhere to write
           and on many machines cannot be written at all. */
        $this->where = __DIR__ . self::TEST_DIR;
        if (!file_exists($this->where)) {
            mkdir($this->where, 0777, true);
        }
    }
    /**
     * Delete any files associated with our test on EpubProcessor
     */
    public function tearDown()
    {
        if (!file_exists($this->where)) {
            return;
        }
        $model = new Model();
        $model->db->unlinkRecursive($this->where);
    }
    /**
     * Test case to check whether the title of the epub document
     * is retrieved correctly.
     */
    public function testEpubTitleTestCase()
    {
        $m = $this->test_objects['summary'];
        $x = $m[self::TITLE];
        $correct_title = "The Test and Textbook for yioop";
        $description = "Test Passed with correct title";
        $this->assertEqual($x, $correct_title, $description);
    }
    /**
     * Test case to check whether the language of the document is
     * retrieved correctly.
     */
    public function testEpubLangTestCase()
    {
        $m = $this->test_objects['summary'] ;
        $x = $m[self::LANG];
        $correct_language = "en";
        $description = "Test Passed with correct Language";
        $this->assertEqual($x, $correct_language, $description);
    }
    /**
     * Test case to check whether the description of the document is
     * not empty.
     */
    public function testEpubDescriptionTestCase()
    {
        $m = $this->test_objects['summary'] ;
        $x = $m[self::DESCRIPTION];
        $description = "Test Passed with Description information not empty";
        $this->assertTrue($x, $description);
    }
    /**
     * Where a case that writes files puts them.
     * @var string
     */
    public $where = "";
    /**
     * Makes a picture of a given size to test with.
     *
     * @param int $width how wide
     * @param int $height how tall
     * @return string the picture's bytes as a jpeg
     */
    public function aPicture($width, $height)
    {
        $made = imagecreatetruecolor($width, $height);
        imagefill($made, 0, 0, imagecolorallocate($made, 30, 90, 200));
        ob_start();
        imagejpeg($made);
        $bytes = ob_get_clean();
        return $bytes;
    }
    /**
     * A book's cover is taken out of the book itself. The book names its
     * cover in its list of contents, and when it does not, the first
     * picture in it is used.
     */
    public function coverIsTakenOutOfTheBookTestCase()
    {
        if (!class_exists("ZipArchive") ||
            !function_exists("imagecreatetruecolor")) {
            $this->assertTrue(true, "no archive reading here to check");
            return;
        }
        $path = $this->where . "/book.epub";
        $book = new \ZipArchive();
        $book->open($path, \ZipArchive::CREATE);
        $book->addFromString("OEBPS/content.opf",
            '<package><metadata><meta name="cover" content="cover-art"/>' .
            '</metadata><manifest>' .
            '<item id="cover-art" href="images/front.jpg" ' .
            'media-type="image/jpeg"/></manifest></package>');
        $book->addFromString("OEBPS/images/front.jpg",
            $this->aPicture(400, 600));
        $book->close();
        $cover = EpubProcessor::coverInBook($path);
        $this->assertTrue($cover !== false, "the cover is found");
        $this->assertEqual(substr($cover, 0, 2), "\xFF\xD8",
            "and it is the picture the book named");
        $thumb = $this->where . "/cover.jpg";
        $this->assertTrue(EpubProcessor::scaleTo($cover, $thumb, 256, 256,
            "jpeg"), "and it scales to a thumbnail");
        $size = getimagesize($thumb);
        $this->assertEqual($size[1], 256, "which fits the height asked for");
    }
}
X