SeekquarrySeekquarry - Yioop Repo - Seekquarry

/ 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");
    }
    /**
     * localeDropdownsAreDrawnTestCase checks renderLocaleDropdown, which
     * dynamicSubstitutions calls for each locale dropdown a page asks for.
     * Asked for names, as [{locale}] and [{locale|names}] do, it lists
     * each locale by name; for flags, as [{locale|flags}] does, by flag,
     * showing the tag of a locale with no flag; for flags-and-names, by
     * both. The page's locale is the one chosen, choosing another reloads
     * the page with page_locale, and with no locales nothing is drawn.
     */
    public function localeDropdownsAreDrawnTestCase()
    {
        $data = $this->data;
        $data['LOCALE_TAG'] = "fr-FR";
        $data['WIKI_LOCALES'] = [
            ['LOCALE_TAG' => "en-US", 'LOCALE_NAME' => "English",
                'FLAG' => "🇺🇸"],
            ['LOCALE_TAG' => "fr-FR", 'LOCALE_NAME' => "Français",
                'FLAG' => ""]];
        $element = $this->element;
        foreach (["", "names"] as $shows) {
            $this->assertTrue(str_contains($element->renderLocaleDropdown(
                $data, $shows), "<option value='en-US'>English</option>"),
                "asked for '$shows', each locale is listed by name");
        }
        $flags = $element->renderLocaleDropdown($data, "flags");
        $this->assertTrue(str_contains($flags, ">🇺🇸</option>"),
            "asked for flags, a locale's flag is shown");
        $this->assertTrue(str_contains($flags,
            "<option value='fr-FR' selected='selected'>fr-FR</option>"),
            "a locale with no flag shows its tag, and the page's locale " .
            "is chosen");
        $this->assertTrue(str_contains($element->renderLocaleDropdown(
            $data, "flags-and-names"), ">🇺🇸 English</option>"),
            "asked for flags and names, both are shown");
        $this->assertTrue(str_contains($flags,
            "address.searchParams.set('page_locale', this.value)"),
            "choosing a locale reloads the page with page_locale");
        unset($data['WIKI_LOCALES']);
        $this->assertEqual("", $element->renderLocaleDropdown($data, ""),
            "with no locales nothing is drawn");
    }
    /**
     * sectionsAreNumberedTestCase checks numberSections: each heading of a
     * drawn page gets an empty span with the id section-N for its position,
     * counted across heading levels, and the heading keeps its own id.
     */
    public function sectionsAreNumberedTestCase()
    {
        $numbered = $this->element->numberSections(
            "<h1 id=\"One\">One</h1><p>x</p><h2 id=\"Two\">Two</h2>" .
            "<h3 id=\"Three\">Three</h3>");
        $this->assertTrue(str_contains($numbered,
            "<h1 id=\"One\"><span id=\"section-1\"></span>One</h1>"),
            "the first heading is section 1 and keeps its id");
        $this->assertTrue(str_contains($numbered,
            "<h3 id=\"Three\"><span id=\"section-3\"></span>Three</h3>"),
            "headings are counted across levels");
        $this->assertEqual(substr_count($numbered, "section-"), 3,
            "each heading gets one number");
    }
}