SeekquarrySeekquarry - Yioop Repo - Seekquarry

/ tests / SearchverticalsModelTest.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/>.
 *
 * @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\UnitTest;
use seekquarry\yioop\models\ProfileModel;
use seekquarry\yioop\models\SearchverticalsModel;
use seekquarry\yioop\models\datasources\Sqlite3Manager;

/**
 * SearchverticalsModelTest checks how SearchverticalsModel tells whether a
 * host is filtered from search results. A search asks about the host of
 * every posting it scores, so the answer must come without a database
 * query for each host.
 *
 * @author Chris Pollett
 */
class SearchverticalsModelTest extends UnitTest
{
    /**
     * $db is the database a case uses, made in a file under the work
     * directory's temp folder, which counts the queries run on it
     * @var object
     */
    public $db;
    /**
     * $db_path is where the database file of a case is
     * @var string
     */
    public $db_path;
    /**
     * $model is the SearchverticalsModel a case asks, set to use $db
     * @var object
     */
    public $model;
    /**
     * setUp makes a database with the GROUP_ITEM table, from
     * ProfileModel's table definitions, and a model that uses it.
     */
    public function setUp()
    {
        $tag = uniqid();
        $this->db_path = C\TEST_DIR . "/test_files/verticals_test_$tag.db";
        $this->db = new class extends Sqlite3Manager {
            /**
             * $num_queries is how many statements execute has run
             * @var int
             */
            public $num_queries = 0;
            /**
             * execute counts the statement, then runs it as
             * Sqlite3Manager does.
             *
             * @param string $sql statement to run
             * @param array $params values for its placeholders
             * @return mixed the result of the statement
             */
            public function execute($sql, $params = [])
            {
                $this->num_queries++;
                return parent::execute($sql, $params);
            }
        };
        $this->db->connect("", "", "", $this->db_path);
        $dbinfo = ["DBMS" => "Sqlite3", "DB_HOST" => ""];
        $profile = new ProfileModel(C\DB_NAME, false);
        $profile->initializeSql($this->db, $dbinfo);
        $this->db->execute($profile->create_statements['GROUP_ITEM']);
        $this->model = new SearchverticalsModel(C\DB_NAME, false);
        $this->model->db = $this->db;
    }
    /**
     * tearDown closes the database and removes its file.
     */
    public function tearDown()
    {
        $this->db->disconnect();
        if (file_exists($this->db_path)) {
            unlink($this->db_path);
        }
    }
    /**
     * filteredHostsAreReadInOneQueryTestCase checks that a filtered host
     * is found and an unfiltered one is not, and that asking about many
     * hosts runs a fixed number of queries rather than one per host. A
     * filter added later through the same model is found at once.
     */
    public function filteredHostsAreReadInOneQueryTestCase()
    {
        $model = $this->model;
        $model->updateUrlResult(0, C\SEARCH_FILTER_GROUP_ITEM,
            "https://blocked.example.com/page", "", "");
        $queries_before = $this->db->num_queries;
        $this->assertTrue($model->isFiltered("https://blocked.example.com/",
            true), "the filtered host is filtered");
        for ($i = 0; $i < 50; $i++) {
            $this->assertFalse($model->isFiltered(
                "https://host$i.example.com/", true),
                "host$i is not filtered");
        }
        $this->assertTrue($this->db->num_queries - $queries_before <= 2,
            "fifty one hosts are checked with at most two queries");
        $model->updateUrlResult(0, C\SEARCH_FILTER_GROUP_ITEM,
            "https://host7.example.com/", "", "");
        $this->assertTrue($model->isFiltered("https://host7.example.com/",
            true), "a host filtered later is filtered");
    }
}