8000 [HttpKernel] Fix CacheAttributeListener priority · symfony/symfony@6395a4c · GitHub
[go: up one dir, main page]

Skip to content

Commit 6395a4c

Browse files
committed
[HttpKernel] Fix CacheAttributeListener priority
1 parent 3c1afda commit 6395a4c

File tree

5 files changed

+116
-1
lines changed

5 files changed

+116
-1
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\FrameworkBundle\Tests\Functional;
13+
14+
use Symfony\Component\HttpFoundation\Response;
15+
use Symfony\Component\HttpKernel\Attribute\Cache;
16+
use Symfony\Component\Security\Core\User\InMemoryUser;
17+
use Symfony\Component\Security\Http\Attribute\IsGranted;
18+
19+
class AttributeListenerPriorityTest extends AbstractWebTestCase
20+
{
21+
public function testAnonimousUserWithEtag()
22+
{
23+
$client = self::createClient(['test_case' => 'AttributeListenerPriority']);
24+
25+
$client->request('GET', '/12345', server: ['HTTP_IF_NONE_MATCH' => sprintf('"%s"', hash('sha256', '12345'))]);
26+
27+
self::assertTrue($client->getResponse()->isRedirect('http://localhost/login'));
28+
}
29+
30+
public function testAnonimousUserWithoutEtag()
31+
{
32+
$client = self::createClient(['test_case' => 'AttributeListenerPriority']);
33+
34+
$client->request('GET', '/12345');
35+
36+
self::assertTrue($client->getResponse()->isRedirect('http://localhost/login'));
37+
}
38+
39+
public function testLoggedInUserWithEtag()
40+
{
41+
$client = self::createClient(['test_case' => 'AttributeListenerPriority']);
42+
43+
$client->loginUser(new InMemoryUser('the-username', 'the-password', ['ROLE_USER']));
44+
$client->request('GET', '/12345', server: ['HTTP_IF_NONE_MATCH' => sprintf('"%s"', hash('sha256', '12345'))]);
45+
46+
$response = $client->getResponse();
47+
48+
self::assertSame(304, $response->getStatusCode());
49+
self::assertSame('', $response->getContent());
50+
}
51+
52+
public function testLoggedInUserWithoutEtag()
53+
{
54+
$client = self::createClient(['test_case' => 'AttributeListenerPriority']);
55+
56+
$client->loginUser(new InMemoryUser('the-username', 'the-password', ['ROLE_USER']));
57+
$client->request('GET', '/12345');
58+
59+
$response = $client->getResponse();
60+
61+
self::assertSame(200, $response->getStatusCode());
62+
self::assertSame('Hi there!', $response->getContent());
63+
}
64+
}
65+
66+
class WithAttributesController
67+
{
68+
#[IsGranted('ROLE_USER')]
69+
#[Cache(etag: 'etag')]
70+
public function __invoke(): Response
71+
{
72+
return new Response('Hi there!');
73+
}
74+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
13+
use Symfony\Bundle\SecurityBundle\SecurityBundle;
14+
15+
return [
16+
new FrameworkBundle(),
17+
new SecurityBundle(),
18+
];
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
imports:
2+
- { resource: ../config/default.yml }
3+
4+
services:
5+
Symfony\Bundle\FrameworkBundle\Tests\Functional\WithAttributesController:
6+
public: true
7+
8+
security:
9+
providers:
10+
main:
11+
memory:
12+
users:
13+
the-username: { password: the-password, roles: [ 'ROLE_USER' ] }
14+
15+
firewalls:
16+
main:
17+
pattern: ^/
18+
form_login:
19+
login_path: /login
20+
provider: main
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
with_attributes_controller:
2+
path: /{etag}
3+
controller: Symfony\Bundle\FrameworkBundle\Tests\Functional\WithAttributesController

src/Symfony/Component/HttpKernel/EventListener/CacheAttributeListener.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ public function onKernelResponse(ResponseEvent $event)
169169
public static function getSubscribedEvents(): array
170170
{
171171
return [
172-
KernelEvents::CONTROLLER_ARGUMENTS => ['onKernelControllerArguments', 10],
172+
KernelEvents::CONTROLLER_ARGUMENTS => ['onKernelControllerArguments', 5],
173173
KernelEvents::RESPONSE => ['onKernelResponse', -10],
174174
];
175175
}

0 commit comments

Comments
 (0)
0