/ tests / RegisterSuppressionTest.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\UnitTest;
use seekquarry\yioop\controllers\RegisterController;

/**
 * Tests that an email address on the suppression list (one that has
 * turned off all of this site's mail) is refused at registration, while
 * an ordinary address is allowed through.
 *
 * @author Chris Pollett
 */
class RegisterSuppressionTest extends UnitTest
{
    /**
     * The registration controller under test, with stub user and
     * suppression models injected
     * @var object
     */
    public $controller;
    /**
     * Builds the controller and injects a stub user model (no name is
     * ever taken) and a stub suppression list (one fixed address is
     * suppressed).
     */
    public function setUp()
    {
        $reflection = new \ReflectionClass(RegisterController::class);
        $this->controller = $reflection->newInstanceWithoutConstructor();
        $this->controller->model_instances = [
            "user" => new class {
                /**
                 * Stand-in: no username is ever already in use.
                 *
                 * @param string $user the username to look up
                 * @return bool false, meaning the name is free
                 */
                public function getUserId($user)
                {
                    return false;
                }
            },
            "mailSuppression" => new class {
                /**
                 * Stand-in suppression check: one address is suppressed.
                 *
                 * @param string $email address to check
                 * @return bool true when the address is the burned one
                 */
                public function isSuppressed($email)
                {
                    return strtolower(trim((string) $email)) ===
                        "burned@example.com";
                }
            },
        ];
        $_SESSION = [];
        $_REQUEST = [];
    }
    /**
     * No teardown beyond clearing the request is needed.
     */
    public function tearDown()
    {
        $_REQUEST = [];
    }
    /**
     * Runs the registration data check for one email address and returns
     * the resulting data array.
     *
     * @param string $email address to register with
     * @return array the data the check fills in, including SUCCESS
     */
    public function checkEmail($email)
    {
        $_REQUEST = ["a" => "processAccountData", "user" => "newperson",
            "email" => $email, "password" => "Abcdef1!",
            "repassword" => "Abcdef1!", "first" => "New", "last" => "Person"];
        $data = ["check_user" => true, "check_fields" =>
            ["user", "email", "password", "repassword", "first", "last"]];
        $this->controller->dataIntegrityCheck($data);
        return $data;
    }
    /**
     * A suppressed address is refused and no account is allowed.
     */
    public function suppressedEmailRefusedTestCase()
    {
        $data = $this->checkEmail("burned@example.com");
        $this->assertFalse($data['SUCCESS'],
            "registration with a suppressed address fails");
        $this->assertTrue(strpos($data['SCRIPT'], "doMessage") !== false,
            "the user is shown a message explaining the refusal");
    }
    /**
     * An ordinary address passes the check.
     */
    public function ordinaryEmailAllowedTestCase()
    {
        $data = $this->checkEmail("fresh@example.com");
        $this->assertTrue($data['SUCCESS'],
            "registration with an ordinary address passes the check");
    }
    /**
     * The reserved name "bot" (the unsubscribe mailbox) cannot be taken
     * by a new account.
     */
    public function reservedBotNameRefusedTestCase()
    {
        $_REQUEST = ["a" => "createAccount", "user" => "bot",
            "email" => "fresh@example.com", "password" => "Abcdef1!",
            "repassword" => "Abcdef1!", "first" => "New",
            "last" => "Person"];
        $data = ["check_user" => true, "check_fields" =>
            ["user", "email", "password", "repassword", "first", "last"]];
        $this->controller->dataIntegrityCheck($data);
        $this->assertFalse($data['SUCCESS'],
            "the reserved name bot cannot be registered");
    }
}
X