SeekquarrySeekquarry - Yioop Repo - Seekquarry

/ tests / CompressedProcessorTest.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 Chris Pollett chris@pollett.org
 * @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 as CC;
use seekquarry\yioop\library\processors\CompressedProcessor;
use seekquarry\yioop\library\processors\HtmlProcessor;
use seekquarry\yioop\library\processors\PageProcessor;
use seekquarry\yioop\library\UnitTest;
use seekquarry\yioop\models\Model;

/**
 * CompressedProcessorTest checks the summary CompressedProcessor makes of
 * a gzipped page by handing the uncompressed page to the processor for its
 * kind.
 */
class CompressedProcessorTest extends UnitTest
{
    /**
     * Most links the test's processor takes from one page
     */
    const MAX_LINKS = 3;
    /**
     * TEMP is the folder, under tests/test_files, that the processors
     * write their temporary files in during these cases.
     */
    const TEMP = C\TEST_DIR . "/test_files/compressedprocessor_temp";
    /**
     * setUp makes the processors the cases use and points them at TEMP, a
     * folder in the tests folder, for the temporary files they write.
     */
    public function setUp()
    {
        L\makePath(self::TEMP);
        PageProcessor::$temp_dir = self::TEMP;
        new HtmlProcessor([], 2000, self::MAX_LINKS);
        $this->test_objects['PROCESSOR'] = new CompressedProcessor([], 2000,
            self::MAX_LINKS);
    }
    /**
     * tearDown deletes TEMP and points the page processors back at
     * Yioop's temporary folder.
     */
    public function tearDown()
    {
        (new Model())->db->unlinkRecursive(self::TEMP);
        PageProcessor::$temp_dir = C\TEMP_DIR;
    }
    /**
     * innerProcessorKeepsTheLinkLimitTestCase gzips a page of ten links.
     * The processor for the uncompressed page is made with the link limit
     * of the CompressedProcessor, so the summary has the links an
     * HtmlProcessor with that limit takes from the page itself, 4 here. It
     * was handed the summarizer option, 'dk', in the place of the limit,
     * so it took 11 links, and every processor kept 'dk' as its limit
     * afterwards.
     */
    public function innerProcessorKeepsTheLinkLimitTestCase()
    {
        $links = "";
        for ($i = 0; $i < 10; $i++) {
            $links .= "<p><a href='https://www.example.com/page$i'>" .
                "Page number $i of the list</a></p>";
        }
        $page = "<html><head><title>Links</title></head><body>" .
            "$links</body></html>";
        $summary = $this->test_objects['PROCESSOR']->process(
            gzencode($page), "https://www.example.com/links.html.gz");
        $limit_after = PageProcessor::$max_links_to_extract;
        $plain = (new HtmlProcessor([], 2000, self::MAX_LINKS))->process(
            $page, "https://www.example.com/links.html");
        $this->assertEqual(array_keys($plain[CC::LINKS]),
            array_keys($summary[CC::LINKS] ?? []),
            "the summary has the links of the uncompressed page");
        $this->assertEqual(self::MAX_LINKS, $limit_after,
            "and processors still have the limit afterwards");
    }
}