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