<?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\UnitTest;
use seekquarry\yioop\models\MailAliasModel;
use seekquarry\yioop\models\ProfileModel;
use seekquarry\yioop\models\datasources\Sqlite3Manager;
/**
* Unit tests for MailAliasModel. Builds a throwaway SQLite alias
* table so the model's reads run against real rows, and checks that
* the list of addresses a user can send as is assembled correctly.
*
* @author Chris Pollett
*/
class MailAliasModelTest extends UnitTest
{
/**
* The model under test
* @var object
*/
public $model;
/**
* Path of the throwaway database file
* @var string
*/
public $db_path;
/**
* Builds a temporary alias table with a couple of rows and points
* the model at it.
*/
public function setUp()
{
$tag = getmypid() . "_" . random_int(1000, 9999);
$this->db_path = C\WORK_DIRECTORY . "/temp/" .
"mail_alias_test_$tag.db";
$public_db = new Sqlite3Manager();
$public_db->connect("", "", "", $this->db_path);
/* Build the table from ProfileModel's definition, the same one the
live database uses, so this test tracks any schema change rather
than a hand-copied CREATE TABLE that could drift. */
$dbinfo = ["DBMS" => "Sqlite3", "DB_HOST" => ""];
$profile = new ProfileModel(C\DB_NAME, false);
$profile->initializeSql($public_db, $dbinfo);
$public_db->execute($profile->create_statements['MAIL_ALIAS']);
$public_db->execute("INSERT INTO MAIL_ALIAS " .
"(USER_ID, ALIAS, DOMAIN) VALUES (?, ?, ?)",
[5, "sales", "shop.example"]);
$public_db->execute("INSERT INTO MAIL_ALIAS " .
"(USER_ID, ALIAS, DOMAIN) VALUES (?, ?, ?)",
[5, "info", "shop.example"]);
$public_db->execute("INSERT INTO MAIL_ALIAS " .
"(USER_ID, ALIAS, DOMAIN) VALUES (?, ?, ?)",
[6, "other", "shop.example"]);
$this->model = new MailAliasModel(C\DB_NAME, false);
$this->model->db = $public_db;
}
/**
* Removes the throwaway database file.
*/
public function tearDown()
{
if ($this->model && $this->model->db) {
$this->model->db->disconnect();
/* Evict this test's own handle (keyed by file path) so a
reused scratch-file name in a later test opens fresh
rather than reusing this still-cached connection; nulling
the model does not evict. Shared handles are untouched. */
unset(Sqlite3Manager::$active_connections[
$this->model->db->connect_string]);
}
$this->model = null;
if (file_exists($this->db_path)) {
unlink($this->db_path);
}
}
/**
* identitiesFor lists the primary address first, then the user's
* own aliases (alphabetical), and excludes other users' aliases.
*/
public function identitiesForTestCase()
{
$identities = $this->model->identitiesFor(5,
"me@shop.example");
$this->assertEqual("me@shop.example", $identities[0],
'the primary address comes first');
$this->assertEqual(
["me@shop.example", "info@shop.example",
"sales@shop.example"], $identities,
'the user owns exactly their two aliases, in order');
}
}