SeekquarrySeekquarry - Yioop Repo - Seekquarry

/ tests / SitemapProcessorTest.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\library\CrawlConstants as CC;
use seekquarry\yioop\library\processors\XmlProcessor;
use seekquarry\yioop\library\UnitTest;

/**
 * SitemapProcessorTest checks the links XmlProcessor and SitemapProcessor
 * take from sitemaps, among them sitemaps cut off where the fetcher stops
 * uncompressing them.
 */
class SitemapProcessorTest extends UnitTest
{
    /**
     * Url the test sitemaps are said to come from
     */
    const SITEMAP_URL = "https://www.example.com/sitemap/00218.xml";
    /**
     * Most links the test's processors take from one sitemap
     */
    const MAX_LINKS = 320;
    /**
     * setUp makes the processor the cases share.
     */
    public function setUp()
    {
        $this->test_objects['PROCESSOR'] = new XmlProcessor([], 2000,
            self::MAX_LINKS);
    }
    /**
     * tearDown has nothing to remove.
     */
    public function tearDown()
    {
    }
    /**
     * Makes the text of a sitemap of $num_entries urls, each with an image
     * as a stock photo site lists them.
     *
     * @param int $num_entries how many url entries the sitemap holds
     * @return string the sitemap
     */
    public function sitemap($num_entries)
    {
        $sitemap = '<?xml version="1.0" encoding="UTF-8"?>' . "\n" .
            '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" ' .
            'xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">';
        for ($i = 0; $i < $num_entries; $i++) {
            $sitemap .= "\n<url><loc>https://www.example.com/photo/$i</loc>" .
                "<image:image><image:loc>https://media.example.com/$i.jpg" .
                "</image:loc></image:image></url>";
        }
        return $sitemap . "\n</urlset>\n";
    }
    /**
     * Gives the urls of the summary XmlProcessor makes of a sitemap.
     *
     * @param string $page the sitemap
     * @return array the urls of the summary's links, in order
     */
    public function linkUrls($page)
    {
        $summary = $this->test_objects['PROCESSOR']->process($page,
            self::SITEMAP_URL);
        return array_keys($summary[CC::LINKS] ?? []);
    }
    /**
     * cutOffSitemapGivesNoPartialUrlTestCase cuts off a sitemap of 12
     * entries in the url of its eleventh, as the fetcher cuts a sitemap
     * where it stops uncompressing it. The ten whole entries give links.
     * The eleventh gave a link too, to the piece of url before the cut,
     * https://www.example.com/htt. Cut just after the eleventh entry's loc
     * element, the eleventh entry gives its link as well.
     */
    public function cutOffSitemapGivesNoPartialUrlTestCase()
    {
        $sitemap = $this->sitemap(12);
        $page = substr($sitemap, 0,
            strpos($sitemap, "/photo/10</loc>") - 20);
        $expected = [];
        for ($i = 0; $i < 10; $i++) {
            $expected[] = "https://www.example.com/photo/$i";
        }
        $this->assertEqual($expected, $this->linkUrls($page),
            "the ten whole entries give links and nothing else does");
        $page = substr($sitemap, 0,
            strpos($sitemap, "/photo/10</loc>") + 20);
        $expected[] = "https://www.example.com/photo/10";
        $this->assertEqual($expected, $this->linkUrls($page),
            "an entry cut after its whole loc gives its link");
    }
}