|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the FOSHttpCacheBundle package. |
| 5 | + * |
| 6 | + * (c) FriendsOfSymfony <http://friendsofsymfony.github.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 FOS\HttpCacheBundle\Tests\Functional\EventListener; |
| 13 | + |
| 14 | +use FOS\HttpCache\ProxyClient\Varnish; |
| 15 | +use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration; |
| 16 | +use Symfony\Bundle\FrameworkBundle\Client; |
| 17 | +use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; |
| 18 | +use Symfony\Component\BrowserKit\Cookie; |
| 19 | +use Symfony\Component\HttpFoundation\Session\Session; |
| 20 | +use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken; |
| 21 | +use Symfony\Component\Security\Core\User\User; |
| 22 | + |
| 23 | +class SwitchUserListenerTest extends WebTestCase |
| 24 | +{ |
| 25 | + use MockeryPHPUnitIntegration; |
| 26 | + |
| 27 | + public function testSwitchUserCompatibility() |
| 28 | + { |
| 29 | + $client = static::createClient(); |
| 30 | + $session = $client->getContainer()->get('session'); |
| 31 | + $this->loginAsAdmin($client, $session); |
| 32 | + |
| 33 | + $client->request('GET', '/secured_area/switch_user?_switch_user=user'); |
| 34 | + $client->request('GET', '/secured_area/switch_user'); |
| 35 | + $this->assertSame('user', substr($client->getResponse()->getContent(), 0, 2000)); |
| 36 | + |
| 37 | + $client->request('GET', '/secured_area/switch_user?_switch_user=_exit'); |
| 38 | + $client->request('GET', '/secured_area/switch_user'); |
| 39 | + $this->assertSame('admin', substr($client->getResponse()->getContent(), 0, 2000)); |
| 40 | + } |
| 41 | + |
| 42 | + public function testInvalidateContext() |
| 43 | + { |
| 44 | + $client = static::createClient(); |
| 45 | + $session = $client->getContainer()->get('session'); |
| 46 | + $this->loginAsAdmin($client, $session); |
| 47 | + |
| 48 | + $mock = \Mockery::mock(Varnish::class); |
| 49 | + $mock->shouldReceive('invalidateTags') |
| 50 | + ->once() |
| 51 | + ->with(['fos_http_cache_hashlookup-test']); |
| 52 | + |
| 53 | + $mock->shouldReceive('flush') |
| 54 | + ->once() |
| 55 | + ->andReturn(1); |
| 56 | + |
| 57 | + $client->getContainer()->set('fos_http_cache.proxy_client.varnish', $mock); |
| 58 | + $client->request('GET', '/secured_area/switch_user?_switch_user=user'); |
| 59 | + } |
| 60 | + |
| 61 | + private function loginAsAdmin(Client $client, Session $session, $firewallName = 'secured_area', $sessionId = 'test') |
| 62 | + { |
| 63 | + $token = new UsernamePasswordToken(new User('admin', 'admin'), null, $firewallName, ['ROLE_ADMIN', 'ROLE_ALLOWED_TO_SWITCH']); |
| 64 | + |
| 65 | + $session->setId($sessionId); |
| 66 | + $session->set(sprintf('_security_%s', $firewallName), serialize($token)); |
| 67 | + $session->save(); |
| 68 | + |
| 69 | + $client->getCookieJar()->set(new Cookie($session->getName(), $session->getId())); |
| 70 | + } |
| 71 | +} |
0 commit comments