/ tests / WikiElementTest.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\configs as C;
use seekquarry\yioop\library as L;
use seekquarry\yioop\library\UnitTest;
use seekquarry\yioop\views\elements\WikiElement;

/**
 * Checks the screens a secret ballot draws. A screen is only found to
 * be broken when something draws it, and a screen that calls a method
 * which is not there answers a person with an Internal Server Error
 * while every check on the file it lives in still passes.
 *
 * @author Chris Pollett
 */
class WikiElementTest extends UnitTest
{
    /**
     * The element the cases draw with.
     * @var object
     */
    public $element;
    /**
     * Fields a ballot screen is drawn from.
     * @var array
     */
    public $data;
    /**
     * Builds the element and the fields a screen is drawn from.
     */
    public function setUp()
    {
        L\setLocaleObject(C\DEFAULT_LOCALE);
        $this->element = new WikiElement();
        $this->data = ["CONTROLLER" => "group", "PAGE_NAME" => "test2",
            "GROUP" => ["GROUP_ID" => 8], "MODE" => "read",
            C\p('CSRF_TOKEN') => "a-token"];
    }
    /**
     * Nothing is left behind by these cases, so nothing is cleared up.
     */
    public function tearDown()
    {
    }
    /**
     * The address of the page a ballot lives on carries the group and
     * the page, and anything further asked for.
     */
    public function votingPageUrlNamesTheGroupAndPageTestCase()
    {
        $address = $this->element->votingPageUrl($this->data);
        foreach (["c=group", "a=wiki", "arg=read", "group_id=8",
            "page_name=test2"] as $part) {
            $this->assertTrue(strpos($address, $part) !== false,
                "the address carries $part");
        }
        $with_more = $this->element->votingPageUrl($this->data,
            ["WITNESS_EMAIL" => "bob"]);
        $this->assertTrue(strpos($with_more, "WITNESS_EMAIL=bob") !==
            false, "and anything further asked for");
    }
    /**
     * The screen a witness lands on draws, whether their link was good
     * or turned away, and points back at the page the voting happens
     * on. It is drawn here rather than only looked at, since a screen
     * calling a method that is not there passes every other check and
     * answers a person with an error.
     */
    public function witnessScreenDrawsAndPointsBackTestCase()
    {
        $turned_away = $this->data;
        $turned_away['BALLOT_SAYS'] = "This link is no longer good.";
        $turned_away['BALLOT_GOOD'] = false;
        ob_start();
        $this->element->renderBallotPart($turned_away);
        $drawn = ob_get_contents();
        ob_end_clean();
        $this->assertTrue(strpos($drawn, "no longer good") !== false,
            "a witness turned away is told so");
        $this->assertTrue(strpos($drawn, "page_name=test2") !== false,
            "and is still pointed at the page the voting happens on");
        $asked = $this->data;
        $asked['BALLOT_GOOD'] = true;
        ob_start();
        $this->element->renderBallotPart($asked);
        $drawn = ob_get_contents();
        ob_end_clean();
        $this->assertTrue(strpos($drawn, "BALLOT_PASSWORD") !== false,
            "a witness whose link is good is asked for their password");
        $done = $this->data;
        $done['BALLOT_GOOD'] = true;
        $done['BALLOT_DONE'] = true;
        ob_start();
        $this->element->renderBallotPart($done);
        $drawn = ob_get_contents();
        ob_end_clean();
        $this->assertTrue(strpos($drawn, "BALLOT_PASSWORD") === false,
            "and one whose part is in is not asked again");
    }
}
X