|
11 | 11 |
|
12 | 12 | namespace Symfony\Component\Security\Tests\Http\Firewall;
|
13 | 13 |
|
| 14 | +use Symfony\Component\Security\Http\Event\SwitchUserEvent; |
14 | 15 | use Symfony\Component\Security\Http\Firewall\SwitchUserListener;
|
| 16 | +use Symfony\Component\Security\Http\SecurityEvents; |
15 | 17 |
|
16 | 18 | class SwitchUserListenerTest extends \PHPUnit_Framework_TestCase
|
17 | 19 | {
|
@@ -97,6 +99,56 @@ public function testExitUserUpdatesToken()
|
97 | 99 | $listener->handle($this->event);
|
98 | 100 | }
|
99 | 101 |
|
| 102 | + public function testExitUserDispatchesEventWithRefreshedUser() |
| 103 | + { |
| 104 | + $originalUser = $this->getMock('Symfony\Component\Security\Core\User\UserInterface'); |
| 105 | + $refreshedUser = $this->getMock('Symfony\Component\Security\Core\User\UserInterface'); |
| 106 | + $this |
| 107 | + ->userProvider |
| 108 | + ->expects($this->any()) |
| 109 | + ->method('refreshUser') |
| 110 | + ->with($originalUser) |
| 111 | + ->willReturn($refreshedUser); |
| 112 | + $originalToken = $this->getToken(); |
| 113 | + $originalToken |
| 114 | + ->expects($this->any()) |
| 115 | + ->method('getUser') |
| 116 | + ->willReturn($originalUser); |
| 117 | + $role = $this |
| 118 | + ->getMockBuilder('Symfony\Component\Security\Core\Role\SwitchUserRole') |
| 119 | + ->disableOriginalConstructor() |
| 120 | + ->getMock(); |
| 121 | + $role->expects($this->any())->method('getSource')->willReturn($originalToken); |
| 122 | + $this |
| 123 | + ->securityContext |
| 124 | + ->expects($this->any()) |
| 125 | + ->method('getToken') |
| 126 | + ->willReturn($this->getToken(array($role))); |
| 127 | + $this |
| 128 | + ->request |
| 129 | + ->expects($this->any()) |
| 130 | + ->method('get') |
| 131 | + ->with('_switch_user') |
| 132 | + ->willReturn('_exit'); |
| 133 | + $this |
| 134 | + ->request |
| 135 | + ->expects($this->any()) |
| 136 | + ->method('getUri') |
| 137 | + ->willReturn('/'); |
| 138 | + |
| 139 | + $dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface'); |
| 140 | + $dispatcher |
| 141 | + ->expects($this->once()) |
| 142 | + ->method('dispatch') |
| 143 | + ->with(SecurityEvents::SWITCH_USER, $this->callback(function (SwitchUserEvent $event) use ($refreshedUser) { |
| 144 | + return $event->getTargetUser() === $refreshedUser; |
| 145 | + })) |
| 146 | + ; |
| 147 | + |
| 148 | + $listener = new SwitchUserListener($this->securityContext, $this->userProvider, $this->userChecker, 'provider123', $this->accessDecisionManager, null, '_switch_user', 'ROLE_ALLOWED_TO_SWITCH', $dispatcher); |
| 149 | + $listener->handle($this->event); |
| 150 | + } |
| 151 | + |
100 | 152 | /**
|
101 | 153 | * @expectedException \Symfony\Component\Security\Core\Exception\AccessDeniedException
|
102 | 154 | */
|
|
0 commit comments