|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/* |
| 6 | + * This file is part of the EcommitCrudBundle package. |
| 7 | + * |
| 8 | + * (c) E-commit <contact@e-commit.fr> |
| 9 | + * |
| 10 | + * For the full copyright and license information, please view the LICENSE |
| 11 | + * file that was distributed with this source code. |
| 12 | + */ |
| 13 | + |
| 14 | +namespace Ecommit\CrudBundle\Tests\Functional\Controller; |
| 15 | + |
| 16 | +use Ecommit\CrudBundle\Tests\Functional\App\StatelessCsrfKernel; |
| 17 | +use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; |
| 18 | +use Symfony\Component\HttpKernel\Kernel; |
| 19 | + |
| 20 | +class StatelessCsrfTest extends WebTestCase |
| 21 | +{ |
| 22 | + protected function setUp(): void |
| 23 | + { |
| 24 | + if (Kernel::VERSION_ID < 70200) { // @legacy |
| 25 | + $this->markTestSkipped('The stateless CSRF protection requires Symfony 7.2 or later.'); |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + protected static function getKernelClass(): string |
| 30 | + { |
| 31 | + return StatelessCsrfKernel::class; |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * @dataProvider getTestFormsUseStatelessCsrfTokenProvider |
| 36 | + */ |
| 37 | + public function testFormsUseStatelessCsrfToken(string $tokenId): void |
| 38 | + { |
| 39 | + $client = static::createClient(); |
| 40 | + $crawler = $client->request('GET', '/user'); |
| 41 | + |
| 42 | + $this->assertResponseIsSuccessful(); |
| 43 | + // When the token is not stateless, the value is a token stored in the session |
| 44 | + $this->assertSame(StatelessCsrfKernel::COOKIE_NAME, $crawler->filterXPath(\sprintf('//input[@id="%s"]', $tokenId))->attr('value')); |
| 45 | + } |
| 46 | + |
| 47 | + public static function getTestFormsUseStatelessCsrfTokenProvider(): array |
| 48 | + { |
| 49 | + return [ |
| 50 | + ['crud_search_user__token'], |
| 51 | + ['crud_display_settings_user__token'], |
| 52 | + ]; |
| 53 | + } |
| 54 | +} |
0 commit comments