<?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 as L;
use seekquarry\yioop\library\UnitTest;
use seekquarry\yioop\views\helpers\HelpbuttonHelper;

/**
 * Checks the mark that opens a help article. The mark carries where the
 * reader came from, so they are brought back there afterwards, and an
 * address can hold a list of values as well as single ones: the edit
 * link a help callout draws carries one. Handing such a list to the
 * cleaning as though it were a word ended the request, so every screen
 * reached from a callout answered with an error as soon as it drew a
 * mark of its own.
 *
 * @author Chris Pollett
 */
class HelpbuttonHelperTest extends UnitTest
{
    /**
     * The helper the cases draw with.
     * @var object
     */
    public $helper;
    /**
     * What the address held before a case changed it.
     * @var array
     */
    public $address_before;
    /**
     * Builds the helper and remembers the address.
     */
    public function setUp()
    {
        L\setLocaleObject(C\DEFAULT_LOCALE);
        $this->helper = new HelpbuttonHelper();
        $this->address_before = [$_GET, $_REQUEST];
    }
    /**
     * Puts the address back as it was.
     */
    public function tearDown()
    {
        list($_GET, $_REQUEST) = $this->address_before;
    }
    /**
     * The mark draws from values the controller has already made safe,
     * and reads nothing out of the address itself. A helper that
     * cleaned its own values was handed a list from a help callout's
     * address and ended the request, which a reader saw as an Internal
     * Server Error.
     */
    public function markDrawsWhatItIsHandedTestCase()
    {
        $this->helper->safe_request = ["controller" => "admin",
            "activity" => "userRolesGroups",
            "open_help_page" => "System Emails",
            "back" => ["group_id" => "3", "arg" => "read"]];
        $drawn = $this->helper->render("System Emails", "a-token");
        $this->assertTrue(strpos($drawn, "admin") !== false,
            "the mark carries the controller it was handed");
        $this->assertTrue(strpos($drawn, "userRolesGroups") !== false,
            "and the activity");
        $this->assertTrue(strpos($drawn, "group_id") !== false,
            "and where the reader came from, so they can be sent back");
        $this->assertTrue(strpos($this->helper->script,
            "System Emails") !== false,
            "a help page asked for by the address is opened on arrival");
        $this->assertTrue(!method_exists($this->helper, "clean"),
            "the helper has no cleaning of its own");
    }
    /**
     * An address holding a list of values draws its mark rather than
     * ending the request, and the list is left out of where the reader
     * came from while the single values are kept.
     */
    public function anAddressHoldingAListStillDrawsTestCase()
    {
        $_GET = ["c" => "group", "a" => "wiki", "arg" => "edit",
            "page_name" => "Account_Registration",
            "back_params" => ["c" => "admin", "a" => "manageAccount"]];
        $_REQUEST = $_GET;
        /* What a controller would hand over for such an address: the
           list among the values is left out of it. */
        $this->helper->safe_request = ["controller" => "group",
            "activity" => "wiki", "open_help_page" => "",
            "back" => ["arg" => "edit",
            "page_name" => "Account_Registration"]];
        $drawn = "";
        $went_wrong = "";
        try {
            $drawn = $this->helper->render("System Emails", "a-token");
        } catch (\Throwable $trouble) {
            $went_wrong = get_class($trouble) . ": " .
                $trouble->getMessage();
        }
        $this->assertEqual("", $went_wrong,
            "an address holding a list does not end the request");
        $this->assertTrue(strpos($drawn, "System Emails") !== false,
            "and the mark names the article it opens");
        $this->assertTrue(strpos($drawn, "Array") === false,
            "the list is left out rather than written as the word Array");
        $this->assertTrue(strpos($drawn, "Account_Registration") !== false,
            "while the single values are still carried");
    }
}
