From 7615ab4a026a8ada108a4ccff5d3ebaecfba5ff0 Mon Sep 17 00:00:00 2001 From: KyleKatarn Date: Sun, 5 Mar 2023 18:44:32 +0100 Subject: [PATCH] Add csrf extension to form factory in tests --- tests/Pug/AbstractTestCase.php | 2 ++ tests/Pug/PugSymfonyEngineTest.php | 32 +++++++++++++++++++++++++++++- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/tests/Pug/AbstractTestCase.php b/tests/Pug/AbstractTestCase.php index 4263a5c..fd8ad59 100644 --- a/tests/Pug/AbstractTestCase.php +++ b/tests/Pug/AbstractTestCase.php @@ -7,6 +7,7 @@ use Symfony\Bridge\Twig\Extension\CsrfExtension; use Symfony\Bridge\Twig\Extension\CsrfRuntime; use Symfony\Bridge\Twig\Extension\FormExtension; +use Symfony\Bridge\Twig\Extension\TranslationExtension; use Symfony\Bridge\Twig\Form\TwigRendererEngine; use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; use Symfony\Component\Filesystem\Filesystem; @@ -35,6 +36,7 @@ protected function getTwigEnvironment(): Environment )); $this->twig->addExtension(new CsrfExtension()); $this->twig->addExtension(new FormExtension()); + $this->twig->addExtension(new TranslationExtension()); $this->twig->addRuntimeLoader(new FactoryRuntimeLoader([ CsrfRuntime::class => static fn () => new CsrfRuntime(new TestCsrfTokenManager()), ])); diff --git a/tests/Pug/PugSymfonyEngineTest.php b/tests/Pug/PugSymfonyEngineTest.php index 97a4b92..7efed79 100644 --- a/tests/Pug/PugSymfonyEngineTest.php +++ b/tests/Pug/PugSymfonyEngineTest.php @@ -24,11 +24,18 @@ use Symfony\Component\Form\Extension\Core\Type\FormType; use Symfony\Component\Form\Extension\Core\Type\SubmitType; use Symfony\Component\Form\Extension\Core\Type\TextType; +use Symfony\Component\Form\Extension\Csrf\CsrfExtension; use Symfony\Component\Form\FormFactory; use Symfony\Component\Form\FormRegistry; use Symfony\Component\Form\ResolvedFormTypeFactory; +use Symfony\Component\HttpFoundation\RequestStack; use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage as BaseTokenStorage; use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; +use Symfony\Component\Security\Csrf\CsrfTokenManager; +use Symfony\Component\Security\Csrf\TokenGenerator\UriSafeTokenGenerator; +use Symfony\Component\Security\Csrf\TokenStorage\NativeSessionTokenStorage; +use Symfony\Component\Security\Csrf\TokenStorage\SessionTokenStorage; +use Symfony\Component\Security\Csrf\TokenStorage\TokenStorageInterface; use Symfony\Component\Security\Http\Logout\LogoutUrlGenerator as BaseLogoutUrlGenerator; use Symfony\Component\Translation\Translator; use Twig\Error\LoaderError; @@ -96,7 +103,30 @@ class TestController { public function index() { - $factory = new FormFactory(new FormRegistry([], new ResolvedFormTypeFactory())); + $csrfGenerator = new UriSafeTokenGenerator(); + $csrfManager = new CsrfTokenManager($csrfGenerator, new class() implements TokenStorageInterface { + public function getToken(string $tokenId): string + { + return "token:$tokenId"; + } + + public function setToken(string $tokenId, #[\SensitiveParameter] string $token) + { + // noop + } + + public function removeToken(string $tokenId): ?string + { + return "token:$tokenId"; + } + + public function hasToken(string $tokenId): bool + { + return true; + } + }); + $extensions = [new CsrfExtension($csrfManager)]; + $factory = new FormFactory(new FormRegistry($extensions, new ResolvedFormTypeFactory())); return $factory->createBuilder(FormType::class, new Task()) ->add('name', TextType::class)