<?php

/*
 * This file is part of Twig.
 *
 * (c) Fabien Potencier
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Twig\Tests;

/*
 * This file is part of Twig.
 *
 * (c) Fabien Potencier
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\TestCase;
use Twig\Environment;
use Twig\Extension\EscaperExtension;
use Twig\Loader\ArrayLoader;
use Twig\Runtime\EscaperRuntime;
use Twig\RuntimeLoader\FactoryRuntimeLoader;

class EscaperTest extends TestCase
{
    /**
     * @dataProvider provideCustomEscaperCases
     *
     * @group legacy
     */
    #[DataProvider('provideCustomEscaperCases'), Group('legacy')]
    public function testCustomEscaper($expected, $string, $strategy)
    {
        $twig = new Environment(new ArrayLoader());
        $escaperExt = $twig->getExtension(EscaperExtension::class);
        $escaperExt->setEscaper('foo', 'Twig\Tests\legacy_escaper');
        $this->assertSame($expected, $twig->getRuntime(EscaperRuntime::class)->escape($string, $strategy, 'ISO-8859-1'));
    }

    public static function provideCustomEscaperCases()
    {
        return [
            ['foo**ISO-8859-1**UTF-8', 'foo', 'foo'],
            ['**ISO-8859-1**UTF-8', null, 'foo'],
            ['42**ISO-8859-1**UTF-8', 42, 'foo'],
        ];
    }

    /**
     * @dataProvider provideCustomEscaperCases
     *
     * @group legacy
     */
    #[DataProvider('provideCustomEscaperCases'), Group('legacy')]
    public function testCustomEscaperWithoutCallingSetEscaperRuntime($expected, $string, $strategy)
    {
        $twig = new Environment(new ArrayLoader());
        $escaperExt = $twig->getExtension(EscaperExtension::class);
        $escaperExt->setEscaper('foo', 'Twig\Tests\legacy_escaper');
        $this->assertSame($expected, $twig->getRuntime(EscaperRuntime::class)->escape($string, $strategy, 'ISO-8859-1'));
    }

    /**
     * @group legacy
     */
    #[Group('legacy')]
    public function testCustomEscapersOnMultipleEnvs()
    {
        $env1 = new Environment(new ArrayLoader());
        $escaperExt1 = $env1->getExtension(EscaperExtension::class);
        $escaperExt1->setEscaper('foo', 'Twig\Tests\legacy_escaper');

        $env2 = new Environment(new ArrayLoader());
        $escaperExt2 = $env2->getExtension(EscaperExtension::class);
        $escaperExt2->setEscaper('foo', 'Twig\Tests\legacy_escaper_again');

        $this->assertSame('foo**ISO-8859-1**UTF-8', $env1->getRuntime(EscaperRuntime::class)->escape('foo', 'foo', 'ISO-8859-1'));
        $this->assertSame('foo**ISO-8859-1**UTF-8**again', $env2->getRuntime(EscaperRuntime::class)->escape('foo', 'foo', 'ISO-8859-1'));
    }

    /**
     * @group legacy
     */
    #[Group('legacy')]
    public function testSafeClassesUseCustomRuntimeLoader()
    {
        $runtime = new EscaperRuntime();
        $twig = new Environment(new ArrayLoader());
        $twig->addRuntimeLoader(new FactoryRuntimeLoader([
            EscaperRuntime::class => static fn () => $runtime,
        ]));

        $escaperExt = $twig->getExtension(EscaperExtension::class);
        $escaperExt->addSafeClass('ThisClassIsSafe', ['html']);

        $this->assertSame($runtime, $twig->getRuntime(EscaperRuntime::class));
        $this->assertArrayHasKey('ThisClassIsSafe', $runtime->safeClasses);
    }

    public function testLastModified()
    {
        $this->assertGreaterThan(1000000000, (new EscaperExtension())->getLastModified());
    }
}

function legacy_escaper(Environment $twig, $string, $charset)
{
    return $string.'**'.$charset.'**'.$twig->getCharset();
}

function legacy_escaper_again(Environment $twig, $string, $charset)
{
    return $string.'**'.$charset.'**'.$twig->getCharset().'**again';
}
