<?php

declare(strict_types=1);

/*
 * This file is part of the Ferienpass package.
 *
 * (c) Richard Henkenjohann <richard@ferienpass.online>
 *
 * For more information visit the project website <https://ferienpass.online>
 * or the documentation under <https://docs.ferienpass.online>.
 */

namespace Ferienpass\CmsBundle\Tests\Filter;

use Ferienpass\CmsBundle\Filter\OfferListFilter;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;

/**
 * Decides which offer list URLs carry `noindex` — see the pages that mount the
 * offer_list fragment.
 */
final class OfferListFilterQueryParamsTest extends TestCase
{
    public function testAFormFieldMarksTheListFiltered(): void
    {
        self::assertTrue($this->filter()->isFiltered(Request::create('/sommerferienpass?kategorie=sport-und-spiel')));
    }

    public function testAPresetParameterMarksTheListFiltered(): void
    {
        // Never reaches the form under this name — instantiateForm() expands it
        // into von/bis — but it is still a distinct, crawlable URL.
        self::assertTrue($this->filter()->isFiltered(Request::create('/sommerferienpass?wann=wochenende')));
    }

    public function testAnEmptyValueStillCounts(): void
    {
        // Same page, different URL. That is the whole problem.
        self::assertTrue($this->filter()->isFiltered(Request::create('/sommerferienpass?titel=')));
    }

    public function testPaginationAloneIsNotFiltered(): void
    {
        // Page 2+ has to stay indexable: it is the only path to the offers,
        // which are absent from the sitemap.
        self::assertFalse($this->filter()->isFiltered(Request::create('/sommerferienpass?page=2')));
    }

    public function testTheBareListIsNotFiltered(): void
    {
        self::assertFalse($this->filter()->isFiltered(Request::create('/sommerferienpass')));
    }

    private function filter(): OfferListFilter
    {
        // Only the keys are read; the tagged iterator is indexed by
        // FilterType::getName().
        return new OfferListFilter(new \ArrayIterator(array_fill_keys(
            ['alter', 'von', 'kategorie', 'gebuehr', 'varianten', 'bis', 'titel', 'kostenlos', 'sofortbuchung', 'rollstuhl', 'ohne_anmeldung'],
            new \stdClass(),
        )));
    }
}
