/
LogoutUrlGeneratorTest.php
115 lines (93 loc) · 4.05 KB
/
LogoutUrlGeneratorTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Security\Http\Tests\Logout;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Symfony\Component\Security\Http\Logout\LogoutUrlGenerator;
/**
* @author Maxime Steinhausser <maxime.steinhausser@gmail.com>
*/
class LogoutUrlGeneratorTest extends TestCase
{
/** @var TokenStorage */
private $tokenStorage;
/** @var LogoutUrlGenerator */
private $generator;
protected function setUp()
{
$requestStack = $this->getMockBuilder(RequestStack::class)->getMock();
$request = $this->getMockBuilder(Request::class)->getMock();
$requestStack->method('getCurrentRequest')->willReturn($request);
$this->tokenStorage = new TokenStorage();
$this->generator = new LogoutUrlGenerator($requestStack, null, $this->tokenStorage);
}
public function testGetLogoutPath()
{
$this->generator->registerListener('secured_area', '/logout', null, null);
$this->assertSame('/logout', $this->generator->getLogoutPath('secured_area'));
}
/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage No LogoutListener found for firewall key "unregistered_key".
*/
public function testGetLogoutPathWithoutLogoutListenerRegisteredForKeyThrowsException()
{
$this->generator->registerListener('secured_area', '/logout', null, null, null);
$this->generator->getLogoutPath('unregistered_key');
}
public function testGuessFromToken()
{
$this->tokenStorage->setToken(new UsernamePasswordToken('user', 'password', 'secured_area'));
$this->generator->registerListener('secured_area', '/logout', null, null);
$this->assertSame('/logout', $this->generator->getLogoutPath());
}
/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Unable to generate a logout url for an anonymous token.
*/
public function testGuessFromAnonymousTokenThrowsException()
{
$this->tokenStorage->setToken(new AnonymousToken('default', 'anon.'));
$this->generator->getLogoutPath();
}
public function testGuessFromCurrentFirewallKey()
{
$this->generator->registerListener('secured_area', '/logout', null, null);
$this->generator->setCurrentFirewall('secured_area');
$this->assertSame('/logout', $this->generator->getLogoutPath());
}
public function testGuessFromCurrentFirewallContext()
{
$this->generator->registerListener('secured_area', '/logout', null, null, null, 'secured');
$this->generator->setCurrentFirewall('admin', 'secured');
$this->assertSame('/logout', $this->generator->getLogoutPath());
}
public function testGuessFromTokenWithoutProviderKeyFallbacksToCurrentFirewall()
{
$this->tokenStorage->setToken($this->getMockBuilder(TokenInterface::class)->getMock());
$this->generator->registerListener('secured_area', '/logout', null, null);
$this->generator->setCurrentFirewall('secured_area');
$this->assertSame('/logout', $this->generator->getLogoutPath());
}
/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Unable to find the current firewall LogoutListener, please provide the provider key manually
*/
public function testUnableToGuessThrowsException()
{
$this->generator->registerListener('secured_area', '/logout', null, null);
$this->generator->getLogoutPath();
}
}