<?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\views\layouts\WebLayout;

/**
 * Checks the classes a wiki page being read puts on the body, which are
 * what lets a domain's own stylesheet reach reading alone, and reach one
 * group or one page within it.
 *
 * @author Chris Pollett
 */
class WebLayoutTest extends UnitTest
{
    /**
     * The layout whose classes are under test.
     * @var WebLayout
     */
    public $layout;
    /**
     * Builds the layout without its constructor, which would want a view
     * the classes have nothing to do with.
     */
    public function setUp()
    {
        $reflection = new \ReflectionClass(WebLayout::class);
        $this->layout = $reflection->newInstanceWithoutConstructor();
    }
    /**
     * Nothing to take down between test cases.
     */
    public function tearDown()
    {
    }
    /**
     * A name becomes the part of a class that stands for it: spaces and
     * underscores become hyphens, since neither can stand in a class
     * name, and what a class name cannot carry is dropped.
     */
    public function nameBecomesPartOfAClassTestCase()
    {
        $this->assertEqual($this->layout->nameForClass("My Group"),
            "My-Group", "a space becomes a hyphen");
        $this->assertEqual($this->layout->nameForClass("My_Group"),
            "My-Group", "an underscore becomes a hyphen");
        $this->assertEqual($this->layout->nameForClass("My _ Group"),
            "My-Group", "a run of them becomes one hyphen");
        $this->assertEqual($this->layout->nameForClass("  Trimmed  "),
            "Trimmed", "space at either end is dropped");
        $this->assertEqual($this->layout->nameForClass("Odd/Name?Here"),
            "OddNameHere", "what a class cannot carry is dropped");
        $this->assertEqual($this->layout->nameForClass("Already-Fine"),
            "Already-Fine", "a name needing nothing is left alone");
    }
    /**
     * A wiki page being read adds the three classes, each with a space
     * in front so they join what the body already carries.
     */
    public function readingAWikiPageAddsItsClassesTestCase()
    {
        $added = $this->layout->wikiReadingClasses(['MODE' => 'read',
            'PAGE_NAME' => 'Main Page',
            'GROUP' => ['GROUP_NAME' => 'Public Group']]);
        $this->assertEqual($added,
            " read-wiki group-Public-Group page-Main-Page",
            "reading adds the three, each with a space in front");
    }
    /**
     * A page read outside any group still says it is being read and
     * which page it is.
     */
    public function pageWithNoGroupStillSaysWhichPageTestCase()
    {
        $added = $this->layout->wikiReadingClasses(['MODE' => 'read',
            'PAGE_NAME' => 'Solo']);
        $this->assertEqual($added, " read-wiki page-Solo",
            "the group is left out when there is none");
    }
    /**
     * Anything that is not a wiki page being read adds nothing, so an
     * ordinary page's body carries what it always did.
     */
    public function anythingElseAddsNothingTestCase()
    {
        $this->assertEqual($this->layout->wikiReadingClasses(
            ['MODE' => 'edit', 'PAGE_NAME' => 'Main']), "",
            "a page being edited adds nothing");
        $this->assertEqual($this->layout->wikiReadingClasses(
            ['MODE' => 'read']), "",
            "reading something that is not a wiki page adds nothing");
        $this->assertEqual($this->layout->wikiReadingClasses([]), "",
            "a page with neither adds nothing");
    }
}
