|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 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\SecurityBundle\Tests\DependencyInjection\Compiler; |
| 13 | + |
| 14 | +use PHPUnit\Framework\TestCase; |
| 15 | +use Symfony\Bundle\SecurityBundle\DependencyInjection\SecurityExtension; |
| 16 | +use Symfony\Bundle\SecurityBundle\SecurityBundle; |
| 17 | +use Symfony\Component\DependencyInjection\Compiler\PassConfig; |
| 18 | +use Symfony\Component\DependencyInjection\ContainerBuilder; |
| 19 | +use Symfony\Component\EventDispatcher\DependencyInjection\RegisterListenersPass; |
| 20 | +use Symfony\Component\EventDispatcher\EventDispatcher; |
| 21 | +use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
| 22 | +use Symfony\Component\Security\Http\Event\CheckPassportEvent; |
| 23 | +use Symfony\Component\Security\Http\Event\LoginSuccessEvent; |
| 24 | +use Symfony\Component\Security\Http\Event\LogoutEvent; |
| 25 | + |
| 26 | +class RegisterGlobalSecurtyEventListenersPassTest extends TestCase |
| 27 | +{ |
| 28 | + private $container; |
| 29 | + |
| 30 | + protected function setUp(): void |
| 31 | + { |
| 32 | + $this->container = new ContainerBuilder(); |
| 33 | + $this->container->setParameter('kernel.debug', false); |
| 34 | + $this->container->register('request_stack', \stdClass::class); |
| 35 | + $this->container->register('event_dispatcher', EventDispatcher::class); |
| 36 | + |
| 37 | + $this->container->registerExtension(new SecurityExtension()); |
| 38 | + |
| 39 | + $this->container->addCompilerPass(new RegisterListenersPass(), PassConfig::TYPE_BEFORE_REMOVING); |
| 40 | + $this->container->getCompilerPassConfig()->setRemovingPasses([]); |
| 41 | + $this->container->getCompilerPassConfig()->setAfterRemovingPasses([]); |
| 42 | + |
| 43 | + $securityBundle = new SecurityBundle(); |
| 44 | + $securityBundle->build($this->container); |
| 45 | + } |
| 46 | + |
| 47 | + public function testRegisterCustomListener() |
| 48 | + { |
| 49 | + $this->container->loadFromExtension('security', [ |
| 50 | + 'enable_authenticator_manager' => true, |
| 51 | + 'firewalls' => ['main' => ['pattern' => '/', 'http_basic' => true]], |
| 52 | + ]); |
| 53 | + |
| 54 | + $this->container->register('app.security_listener', \stdClass::class) |
| 55 | + ->addTag('kernel.event_listener', ['method' => 'onLogout', 'event' => LogoutEvent::class]) |
| 56 | + ->addTag('kernel.event_listener', ['method' => 'onLoginSuccess', 'event' => LoginSuccessEvent::class, 'priority' => 20]); |
| 57 | + |
| 58 | + $this->container->compile(); |
| 59 | + |
| 60 | + $this->assertListeners([ |
| 61 | + [LogoutEvent::class, ['app.security_listener', 'onLogout'], 0], |
| 62 | + [LoginSuccessEvent::class, ['app.security_listener', 'onLoginSuccess'], 20], |
| 63 | + ]); |
| 64 | + } |
| 65 | + |
| 66 | + public function testRegisterCustomSubscriber() |
| 67 | + { |
| 68 | + $this->container->loadFromExtension('security', [ |
| 69 | + 'enable_authenticator_manager' => true, |
| 70 | + 'firewalls' => ['main' => ['pattern' => '/', 'http_basic' => true]], |
| 71 | + ]); |
| 72 | + |
| 73 | + $this->container->register(TestSubscriber::class) |
| 74 | + ->addTag('kernel.event_subscriber'); |
| 75 | + |
| 76 | + $this->container->compile(); |
| 77 | + |
| 78 | + $this->assertListeners([ |
| 79 | + [LogoutEvent::class, [TestSubscriber::class, 'onLogout'], -200], |
| 80 | + [CheckPassportEvent::class, [TestSubscriber::class, 'onCheckPassport'], 120], |
| 81 | + [LoginSuccessEvent::class, [TestSubscriber::class, 'onLoginSuccess'], 0], |
| 82 | + ]); |
| 83 | + } |
| 84 | + |
| 85 | + public function testMultipleFirewalls() |
| 86 | + { |
| 87 | + $this->container->loadFromExtension('security', [ |
| 88 | + 'enable_authenticator_manager' => true, |
| 89 | + 'firewalls' => ['main' => ['pattern' => '/', 'http_basic' => true], 'api' => ['pattern' => '/api', 'http_basic' => true]], |
| 90 | + ]); |
| 91 | + |
| 92 | + $this->container->register('security.event_dispatcher.api', EventDispatcher::class) |
| 93 | + ->addTag('security.event_dispatcher') |
| 94 | + ->setPublic(true); |
| 95 | + |
| 96 | + $this->container->register('app.security_listener', \stdClass::class) |
| 97 | + ->addTag('kernel.event_listener', ['method' => 'onLogout', 'event' => LogoutEvent::class]) |
| 98 | + ->addTag('kernel.event_listener', ['method' => 'onLoginSuccess', 'event' => LoginSuccessEvent::class, 'priority' => 20]); |
| 99 | + |
| 100 | + $this->container->compile(); |
| 101 | + |
| 102 | + $this->assertListeners([ |
| 103 | + [LogoutEvent::class, ['app.security_listener', 'onLogout'], 0], |
| 104 | + [LoginSuccessEvent::class, ['app.security_listener', 'onLoginSuccess'], 20], |
| 105 | + ], 'security.event_dispatcher.main'); |
| 106 | + $this->assertListeners([ |
| 107 | + [LogoutEvent::class, ['app.security_listener', 'onLogout'], 0], |
| 108 | + [LoginSuccessEvent::class, ['app.security_listener', 'onLoginSuccess'], 20], |
| 109 | + ], 'security.event_dispatcher.api'); |
| 110 | + } |
| 111 | + |
| 112 | + public function testListenerAlreadySpecific() |
| 113 | + { |
| 114 | + $this->container->loadFromExtension('security', [ |
| 115 | + 'enable_authenticator_manager' => true, |
| 116 | + 'firewalls' => ['main' => ['pattern' => '/', 'http_basic' => true]], |
| 117 | + ]); |
| 118 | + |
| 119 | + $this->container->register('security.event_dispatcher.api', EventDispatcher::class) |
| 120 | + ->addTag('security.event_dispatcher') |
| 121 | + ->setPublic(true); |
| 122 | + |
| 123 | + $this->container->register('app.security_listener', \stdClass::class) |
| 124 | + ->addTag('kernel.event_listener', ['method' => 'onLogout', 'event' => LogoutEvent::class, 'dispatcher' => 'security.event_dispatcher.main']) |
| 125 | + ->addTag('kernel.event_listener', ['method' => 'onLoginSuccess', 'event' => LoginSuccessEvent::class, 'priority' => 20]); |
| 126 | + |
| 127 | + $this->container->compile(); |
| 128 | + |
| 129 | + $this->assertListeners([ |
| 130 | + [LogoutEvent::class, ['app.security_listener', 'onLogout'], 0], |
| 131 | + [LoginSuccessEvent::class, ['app.security_listener', 'onLoginSuccess'], 20], |
| 132 | + ], 'security.event_dispatcher.main'); |
| 133 | + } |
| 134 | + |
| 135 | + private function assertListeners(array $expectedListeners, string $dispatcherId = 'security.event_dispatcher.main') |
| 136 | + { |
| 137 | + $actualListeners = []; |
| 138 | + foreach ($this->container->findDefinition($dispatcherId)->getMethodCalls() as $methodCall) { |
| 139 | + [$method, $arguments] = $methodCall; |
| 140 | + if ('addListener' !== $method) { |
| 141 | + continue; |
| 142 | + } |
| 143 | + |
| 144 | + $arguments[1] = [(string) $arguments[1][0]->getValues()[0], $arguments[1][1]]; |
| 145 | + $actualListeners[] = $arguments; |
| 146 | + } |
| 147 | + |
| 148 | + $foundListeners = array_uintersect($expectedListeners, $actualListeners, function (array $a, array $b) { |
| 149 | + return $a === $b; |
| 150 | + }); |
| 151 | + |
| 152 | + $this->assertEquals($expectedListeners, $foundListeners); |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +class TestSubscriber implements EventSubscriberInterface |
| 157 | +{ |
| 158 | + public static function getSubscribedEvents(): array |
| 159 | + { |
| 160 | + return [ |
| 161 | + LogoutEvent::class => ['onLogout', -200], |
| 162 | + CheckPassportEvent::class => ['onCheckPassport', 120], |
| 163 | + LoginSuccessEvent::class => 'onLoginSuccess', |
| 164 | + ]; |
| 165 | + } |
| 166 | +} |
0 commit comments