SeekquarrySeekquarry - Yioop Repo - Seekquarry

/ tests / ArcToolTest.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\executables\ArcTool;
use seekquarry\yioop\library\CrawlConstants as CC;
use seekquarry\yioop\library\IndexManager;
use seekquarry\yioop\library\storage_formats\IndexDocumentBundle;
use seekquarry\yioop\library\UnitTest;
use seekquarry\yioop\models\Model;

/**
 * ArcToolTest checks commands of ArcTool, the command line tool for
 * looking into and rebuilding an index, by running ArcTool on a small
 * index made for the test.
 */
class ArcToolTest extends UnitTest
{
    /**
     * TEST_DIR is the folder the test's index is made in, removed when
     * each case ends
     */
    const TEST_DIR = __DIR__ . '/test_files/arc_tool_test';
    /**
     * setUp makes the empty folder the test's index goes in.
     */
    public function setUp()
    {
        if (!file_exists(self::TEST_DIR)) {
            mkdir(self::TEST_DIR, 0777, true);
        }
    }
    /**
     * tearDown deletes the test folder and the index made in it, so the
     * next case starts from an empty folder.
     */
    public function tearDown()
    {
        $model = new Model();
        $model->db->unlinkRecursive(self::TEST_DIR);
    }
    /**
     * rebuildStatusNamesSafePartitionTestCase checks that
     * ArcTool::rebuildStatus, whose lines the rebuild-status command
     * prints, says where a stopped rebuild may safely
     * continue from. Twelve pages go into partitions of four and are
     * merged into the dictionary. The count of merged partitions in
     * next_partition.txt is then set to 0, as a rebuild that has built
     * partitions but merged none leaves it. The command must name
     * partition 0 as the place to continue from, say the partitions
     * after it are already built, and give the remerge command that
     * merges them without building them again.
     */
    public function rebuildStatusNamesSafePartitionTestCase()
    {
        IndexManager::clearCache();
        $bundle_path = self::TEST_DIR . "/IndexData200000004";
        $index_archive = new IndexDocumentBundle($bundle_path, false,
            "TestBundle", 4, 5);
        $docs = [];
        for ($i = 0; $i < 12; $i++) {
            $docs[] = [
                CC::DOC_ID => str_pad("$i", 8, "0", STR_PAD_LEFT) .
                    str_pad("$i", 8, "0", STR_PAD_LEFT) . "d" .
                    str_pad("$i", 7, "0", STR_PAD_LEFT),
                CC::SUMMARY => [
                    CC::DESCRIPTION => "zorp flim $i",
                    CC::HASH => str_pad("$i", 8, "0", STR_PAD_LEFT),
                    CC::TITLE => "Page$i",
                    CC::URL => "https://www.somewhere$i.com/"],
                CC::PAGE => "Page $i"];
        }
        $index_archive->addPages($docs, count($docs));
        $index_archive->updateDictionary();
        $index_archive->forceSave();
        $merged = intval(file_get_contents($bundle_path . "/" .
            IndexDocumentBundle::NEXT_PARTITION_FILE));
        $this->assertTrue($merged >= 2,
            "the test set-up merges at least two partitions");
        file_put_contents($bundle_path . "/" .
            IndexDocumentBundle::NEXT_PARTITION_FILE, "0");
        require_once C\BASE_DIR . "/executables/ArcTool.php";
        $output = (new ArcTool())->rebuildStatus($bundle_path);
        $this->assertTrue(str_contains($output,
            "continue safely from partition 0"),
            "the safe place is the count of merged partitions");
        $this->assertTrue(str_contains($output, "Partitions 0 to "),
            "the built partitions after it are named");
        $this->assertTrue(str_contains($output,
            "remerge IndexData200000004 continue"),
            "the remerge command that reuses them is given");
    }
}