<?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\EventListener;

use Contao\CoreBundle\ContaoCoreBundle;
use Ferienpass\CmsBundle\EventListener\FragmentFrontendScopeListener;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;

/**
 * Encodes the safety contract behind the fragment-scope workaround:
 * {@see FragmentFrontendScopeListener} marks `/_fragment` sub-requests
 * invoking a CMS fragment controller as frontend-scoped so the
 * `contao_frontend` firewall authenticates the user. Without it,
 * `$this->getUser()` returns null inside the fragment and tenants see an
 * empty user dropdown on logged-in pages.
 */
final class FragmentFrontendScopeListenerTest extends TestCase
{
    public function testScopeIsSetOnCmsFragmentRequest(): void
    {
        $request = Request::create('/_fragment?_path=…');
        $request->attributes->set('_controller', 'Ferienpass\\CmsBundle\\Controller\\Fragment\\UserNavigationController');

        $this->dispatch($request);

        self::assertSame(
            ContaoCoreBundle::SCOPE_FRONTEND,
            $request->attributes->get('_scope'),
            '/_fragment calls to CMS fragment controllers MUST be marked frontend so the contao_frontend firewall authenticates the user',
        );
    }

    public function testExistingScopeIsRespected(): void
    {
        $request = Request::create('/_fragment');
        $request->attributes->set('_controller', 'Ferienpass\\CmsBundle\\Controller\\Fragment\\UserNavigationController');
        $request->attributes->set('_scope', 'backend');

        $this->dispatch($request);

        self::assertSame('backend', $request->attributes->get('_scope'), 'Must not overwrite a scope declared by upstream routing / FragmentReference');
    }

    public function testNonFragmentPathIsIgnored(): void
    {
        $request = Request::create('/account');
        $request->attributes->set('_controller', 'Ferienpass\\CmsBundle\\Controller\\Fragment\\UserNavigationController');

        $this->dispatch($request);

        self::assertFalse($request->attributes->has('_scope'), 'Only /_fragment paths should be touched; other URLs must stay unmarked');
    }

    public function testNonCmsFragmentControllerIsIgnored(): void
    {
        $request = Request::create('/_fragment');
        $request->attributes->set('_controller', 'Ferienpass\\AdminBundle\\Controller\\Fragment\\UserNavigationController');

        $this->dispatch($request);

        self::assertFalse(
            $request->attributes->has('_scope'),
            'Admin-bundle (or any non-CMS) fragment MUST NOT be auto-scoped as frontend — we would authenticate it against the wrong user provider',
        );
    }

    public function testEmptyControllerAttributeIsIgnored(): void
    {
        // FragmentListener (priority 48) runs before us; if it didn't match
        // a signed fragment URI, _controller is empty. Don't fire blindly.
        $request = Request::create('/_fragment');

        $this->dispatch($request);

        self::assertFalse($request->attributes->has('_scope'));
    }

    private function dispatch(Request $request): void
    {
        $listener = new FragmentFrontendScopeListener();
        $kernel = $this->createMock(HttpKernelInterface::class);
        $listener(new RequestEvent($kernel, $request, HttpKernelInterface::MAIN_REQUEST));
    }
}
