8000 [Security] Adding tests and then fixing bug where ContextListener did… · lidaa/symfony@bad1cb6 · GitHub
[go: up one dir, main page]

Skip to content

Commit bad1cb6

Browse files
committed
[Security] Adding tests and then fixing bug where ContextListener did no logging
1 parent 9aa8083 commit bad1cb6

File tree

2 files changed

+123
-0
lines changed

2 files changed

+123
-0
lines changed

src/Symfony/Component/Security/Http/Firewall/ContextListener.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public function __construct(SecurityContext $context, array $userProviders, $con
4747
$this->context = $context;
4848
$this->userProviders = $userProviders;
4949
$this->contextKey = $contextKey;
50+
$this->logger = $logger;
5051

5152
if (null !== $dispatcher) {
5253
$dispatcher->addListener(Events::onCoreResponse, $this);
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
<?php
2+
3+
namespace Symfony\Tests\Component\Security\Http\Firewall;
4+
5+
use Symfony\Component\HttpKernel\HttpKernelInterface;
6+
use Symfony\Component\HttpKernel\Events;
7+
use Symfony\Component\HttpFoundation\Response;
8+
use Symfony\Component\Security\Core\Exception\AuthenticationException;
9+
use Symfony\Component\Security\Http\Firewall\ContextListener;
10+
use Symfony\Component\HttpFoundation\Request;
11+
use Symfony\Component\HttpFoundation\Session;
12+
13+
class ContextListenerTest extends \PHPUnit_Framework_TestCase
14+
{
15+
// test that if the session has a token, it's set on the context
16+
public function testOnCoreRequestRestoresToken()
17+
{
18+
list($listener, $context, $provider, $contextKey, $logger, $dispatcher) = $this->getListener();
19+
20+
list($request, $session) = $this->getRequest();
21+
22+
// create a real token, mocking this and then serializing it did not work
23+
$token = new \Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken('foo', 'bar', 'baz');
24+
$request
25+
->expects($this->once())
26+
->method('hasPreviousSession')
27+
->will($this->returnValue(true));
28+
$session
29+
->expects($this->once())
30+
->method('get')
31+
->with('_security_'.$contextKey)
32+
->will($this->returnValue(serialize($token)));
33+
34+
$event = $this->getGetResponseEvent();
35+
$event
36+
->expects($this->once())
37+
->method('getRequest')
38+
->will($this->returnValue($request))
39+
;
40+
41+
// the end goal is that the token is set on the context
42+
$context
43+
->expects($this->once())
44+
->method('setToken')
45+
->with($token)
46+
;
47+
48+
// since a logger is injected, debug should be called at least once
49+
$logger
50+
->expects($this->atLeastOnce())
51+
->method('debug')
52+
;
53+
54+
$listener->handle($event);
55+
}
56+
57+
protected function getGetResponseEvent()
58+
{
59+
return $this->getMock('Symfony\Component\HttpKernel\Event\GetResponseEvent', array(), array(), '', false);
60+
}
61+
62+
protected function getFilterResponseEvent()
63+
{
64+
return $this->getMock('Symfony\Component\HttpKernel\Event\FilterResponseEvent', array(), array(), '', false);
65+
}
66+
67+
protected function getListener($withLogger = true)
68+
{
69+
$logger = $withLogger ? $this->getLogger() : null;
70+
71+
$listener = new ContextListener(
72+
$context = $this->getContext(),
73+
array($provider = $this->getUserProvider()),
74+
$key = 'context_key',
75+
$logger,
76+
$dispatcher = $this->getDispatcher()
77+
);
78+
79+
return array($listener, $context, $provider, $key, $logger, $dispatcher);
80+
}
81+
82+
protected function getLogger()
83+
{
84+
return $this->getMock('Symfony\Component\HttpKernel\Log\LoggerInterface');
85+
}
86+
87+
protected function getManager()
88+
{
89+
return $this->getMock('Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface');
90+
}
91+
92+
protected function getUserProvider()
93+
{
94+
return $this->getMock('Symfony\Component\Security\Core\User\UserProviderInterface');
95+
}
96+
97+
protected function getContext()
98+
{
99+
return $this->getMockBuilder('Symfony\Component\Security\Core\SecurityContext')
100+
->disableOriginalConstructor()
101+
->getMock();
102+
}
103+
104+
protected function getDispatcher()
105+
{
106+
return $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
107+
}
108+
109+
protected function getRequest()
110+
{
111+
$request = $this->getMock('Symfony\Component\HttpFoundation\Request');
112+
$session = $this->getMockBuilder('Symfony\Component\HttpFoundation\Session')
113+
->disableOriginalConstructor()
114+
->getMock();
115+
116+
$request->expects($this->any())
117+
->method('getSession')
118+
->will($this->returnValue($session));
119+
120+
return array($request, $session);
121+
}
122+
}

0 commit comments

Comments
 (0)
0