SeekquarrySeekquarry - Yioop Repo - Seekquarry

/ tests / WebSiteMimeTypeTest.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/ GPLv3
 * @link https://www.seekquarry.com/
 * @copyright 2009 - 2026
 * @package seek_quarry\test
 */
namespace seekquarry\yioop\tests;

use seekquarry\atto\WebSite;
use seekquarry\yioop\library\UnitTest;

/**
 * Unit tests for saying what kind of thing a file holds. The answer is read
 * from the file's name, not its bytes, and a page decides whether to show a
 * picture or merely link to it on the strength of the answer, so a wrong one
 * is the difference between a screenshot and a line of blue text.
 *
 * @author Chris Pollett
 */
class WebSiteMimeTypeTest extends UnitTest
{
    /**
     * Nothing to set up: the kind is read from a name, not a file on disk.
     */
    public function setUp()
    {
    }
    /**
     * Nothing to tear down: no files are written.
     */
    public function tearDown()
    {
    }
    /**
     * The names of the picture formats a page may hold are known. A format
     * missing from this list is not a picture as far as a page is
     * concerned, which is what once left a screenshot showing as a link.
     */
    public function pictureNamesAreKnownTestCase()
    {
        $pictures = [
            "shot.webp" => "image/webp",
            "shot.avif" => "image/avif",
            "shot.svg" => "image/svg+xml",
            "shot.tif" => "image/tiff",
            "shot.tiff" => "image/tiff",
            "shot.png" => "image/png",
            "shot.jpg" => "image/jpeg",
            "shot.gif" => "image/gif"
        ];
        foreach ($pictures as $name => $kind) {
            $this->assertEqual($kind, WebSite::mimeType($name),
                "$name is known to be a picture");
        }
    }
    /**
     * The kind is read from the name whether or not a file of that name is
     * on disk, since the bytes are never opened; a name of no known kind is
     * unknown, and a name never written but of a known kind is still read
     * from its extension rather than opened.
     */
    public function theKindIsReadFromTheNameNotTheFileTestCase()
    {
        $this->assertEqual("unknown", WebSite::mimeType("mystery.qqq"),
            "a name of no known kind is unknown");
        $this->assertEqual("image/webp",
            WebSite::mimeType(sys_get_temp_dir() . "/never_written.webp"),
            "a known extension is read from the name with no file present");
    }
    /**
     * The answer carries the kind of thing alone, with no note of the
     * character set it is written in, because callers compare it against a
     * kind whole and an answer carrying more than the kind fails them.
     */
    public function theKindStandsAloneTestCase()
    {
        $kind = WebSite::mimeType("an_archive.zip");
        $this->assertEqual("application/zip", $kind,
            "a zip is named application/zip");
        $this->assertTrue(!str_contains($kind, ";"),
            "the kind stands alone, with no character set after it");
    }
}