<?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\models\Model;
use seekquarry\yioop\library\UnitTest;
use seekquarry\yioop\models\datasources\Sqlite3Manager;
/**
* Unit tests for Model, covering what a model made without opening a
* connection hands to its data source managers. Such a model is made by
* the locale upgrade and by several database migrations, which give it
* an open connection of their own afterward, and its managers still
* have to know which database they are for.
*
* @author Chris Pollett
*/
class ModelTest extends UnitTest
{
/**
* No set-up is needed; each case makes the model it reads
*/
public function setUp()
{
}
/**
* No tear-down is needed; a model made here opens nothing
*/
public function tearDown()
{
}
/**
* A model made without connecting still says which database each of
* its managers is for. The first query such a manager runs opens the
* connection from what it holds, and a manager holding nothing asks
* a PDO source for an empty data source name, which it refuses; that
* is why upgrading locales failed on a site whose database is
* reached through PDO.
*/
public function modelMadeWithoutConnectingKnowsItsDatabaseTestCase()
{
$model = new Model(C\p('DB_NAME'), false);
$this->assertEqual(C\p('DB_HOST'), $model->db->db_host,
"The public manager knows the host it is for");
$this->assertEqual(C\p('DB_NAME'), $model->db->db_name,
"The public manager knows the database it is for");
$this->assertEqual(C\p('DB_USER'), $model->db->db_user,
"The public manager knows the user it connects as");
}
/**
* The private database of such a model is the one holding the keys a
* group uses, and a model made without connecting is handed only a
* public connection by its caller, so the private manager has to
* carry the settings the profile and Config.php give it.
*/
public function modelMadeWithoutConnectingKnowsItsPrivateDatabaseTestCase()
{
$model = new Model(C\p('DB_NAME'), false);
$this->assertEqual(C\p('PRIVATE_DB_HOST'),
$model->private_db->db_host,
"The private manager knows the host it is for");
$this->assertEqual(C\PRIVATE_DB_NAME,
$model->private_db->db_name,
"The private manager knows the database it is for");
}
/**
* emptyDatabaseNameIsRefusedTestCase checks that Sqlite3Manager will
* not open a database whose name is empty. SQLite makes a new empty
* file for any path it is given, so an empty name would open a fresh
* database called .db in place of the site's own.
*/
public function emptyDatabaseNameIsRefusedTestCase()
{
$stray = C\DATA_DIR . "/.db";
$was_there = file_exists($stray);
$refused = false;
try {
(new Sqlite3Manager())->connect("", "", "", "");
} catch (\Exception $e) {
$refused = true;
}
$this->assertTrue($refused, "an empty database name is refused");
$this->assertEqual(file_exists($stray), $was_there,
"no database called .db is made");
}
/**
* machineListedByAddressIsThisMachineTestCase checks that
* isSingleLocalhost counts a queue server listed by the address the
* name server's url names as this machine, so a query does not go over
* the network to the site's own web app. It also checks that a machine
* at another address, or at the same address with another path or
* port, still counts as a different machine.
*/
public function machineListedByAddressIsThisMachineTestCase()
{
$model = new Model(C\p('DB_NAME'), false);
$name_server = "https://173.13.143.74/";
$this->assertTrue($model->isSingleLocalhost(
["https://173.13.143.74/"], -1, $name_server),
"the name server's own address is this machine");
$this->assertFalse($model->isSingleLocalhost(
["https://173.13.143.75/"], -1, $name_server),
"another address is another machine");
$this->assertFalse($model->isSingleLocalhost(
["https://173.13.143.74:8443/"], -1, $name_server),
"the same address on another port is another instance");
$this->assertFalse($model->isSingleLocalhost(
["https://173.13.143.74/other/"], -1, $name_server),
"the same address under another path is another instance");
}
}