Skip to content

Commit

Permalink
Add csrf extension to form factory in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kylekatarnls committed Mar 5, 2023
1 parent 25681ca commit 7615ab4
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
2 changes: 2 additions & 0 deletions tests/Pug/AbstractTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()),
]));
Expand Down
32 changes: 31 additions & 1 deletion tests/Pug/PugSymfonyEngineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 7615ab4

Please sign in to comment.