Skip to content

Commit ab2a67c

Browse files
committed
Merge branch '6.4' into 7.0
* 6.4: fix used class after merge fix tests [Console] Only execute additional checks for color support if the output is a TTY fix aircraft inflection [TwigBundle] Fix configuration when 'paths' is null register the MailPaceTransportFactory [String] Correct inflection of axis [Security] Fix `AuthenticationUtils::getLastUsername()` returning null [Process] Fixed inconsistent test
2 parents acc9931 + d82bf7c commit ab2a67c

File tree

2 files changed

+129
-2
lines changed

2 files changed

+129
-2
lines changed

Authentication/AuthenticationUtils.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,10 @@ public function getLastUsername(): string
5353
$request = $this->getRequest();
5454

5555
if ($request->attributes->has(SecurityRequestAttributes::LAST_USERNAME)) {
56-
return $request->attributes->get(SecurityRequestAttributes::LAST_USERNAME, '');
56+
return $request->attributes->get(SecurityRequestAttributes::LAST_USERNAME) ?? '';
5757
}
5858

59-
return $request->hasSession() ? $request->getSession()->get(SecurityRequestAttributes::LAST_USERNAME, '') : '';
59+
return $request->hasSession() ? ($request->getSession()->get(SecurityRequestAttributes::LAST_USERNAME) ?? '') : '';
6060
}
6161

6262
/**
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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\Component\Security\Http\Tests\Authentication;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\HttpFoundation\Request;
16+
use Symfony\Component\HttpFoundation\RequestStack;
17+
use Symfony\Component\HttpFoundation\Session\Session;
18+
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
19+
use Symfony\Component\Security\Core\Exception\AuthenticationException;
20+
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
21+
use Symfony\Component\Security\Http\SecurityRequestAttributes;
22+
23+
class AuthenticationUtilsTest extends TestCase
24+
{
25+
public function testLastAuthenticationErrorWhenRequestHasAttribute()
26+
{
27+
$authenticationError = new AuthenticationException();
28+
$request = Request::create('/');
29+
$request->attributes->set(SecurityRequestAttributes::AUTHENTICATION_ERROR, $authenticationError);
30+
31+
$requestStack = new RequestStack();
32+
$requestStack->push($request);
33+
34+
$utils = new AuthenticationUtils($requestStack);
35+
$this->assertSame($authenticationError, $utils->getLastAuthenticationError());
36+
}
37+
38+
public function testLastAuthenticationErrorInSession()
39+
{
40+
$authenticationError = new AuthenticationException();
41+
42+
$request = Request::create('/');
43+
44+
$session = new Session(new MockArraySessionStorage());
45+
$session->set(SecurityRequestAttributes::AUTHENTICATION_ERROR, $authenticationError);
46+
$request->setSession($session);
47+
48+
$requestStack = new RequestStack();
49+
$requestStack->push($request);
50+
51+
$utils = new AuthenticationUtils($requestStack);
52+
$this->assertSame($authenticationError, $utils->getLastAuthenticationError());
53+
$this->assertFalse($session->has(SecurityRequestAttributes::AUTHENTICATION_ERROR));
54+
}
55+
56+
public function testLastAuthenticationErrorInSessionWithoutClearing()
57+
{
58+
$authenticationError = new AuthenticationException();
59+
60+
$request = Request::create('/');
61+
62+
$session = new Session(new MockArraySessionStorage());
63+
$session->set(SecurityRequestAttributes::AUTHENTICATION_ERROR, $authenticationError);
64+
$request->setSession($session);
65+
66+
$requestStack = new RequestStack();
67+
$requestStack->push($request);
68+
69+
$utils = new AuthenticationUtils($requestStack);
70+
$this->assertSame($authenticationError, $utils->getLastAuthenticationError(false));
71+
$this->assertTrue($session->has(SecurityRequestAttributes::AUTHENTICATION_ERROR));
72+
}
73+
74+
public function testLastUserNameIsDefinedButNull()
75+
{
76+
$request = Request::create('/');
77+
$request->attributes->set(SecurityRequestAttributes::LAST_USERNAME, null);
78+
79+
$requestStack = new RequestStack();
80+
$requestStack->push($request);
81+
82+
$utils = new AuthenticationUtils($requestStack);
83+
$this->assertSame('', $utils->getLastUsername());
84+
}
85+
86+
public function testLastUserNameIsDefined()
87+
{
88+
$request = Request::create('/');
89+
$request->attributes->set(SecurityRequestAttributes::LAST_USERNAME, 'user');
90+
91+
$requestStack = new RequestStack();
92+
$requestStack->push($request);
93+
94+
$utils = new AuthenticationUtils($requestStack);
95+
$this->assertSame('user', $utils->getLastUsername());
96+
}
97+
98+
public function testLastUserNameIsDefinedInSessionButNull()
99+
{
100+
$request = Request::create('/');
101+
102+
$session = new Session(new MockArraySessionStorage());
103+
$session->set(SecurityRequestAttributes::LAST_USERNAME, null);
104+
$request->setSession($session);
105+
106+
$requestStack = new RequestStack();
107+
$requestStack->push($request);
108+
109+
$utils = new AuthenticationUtils($requestStack);
110+
$this->assertSame('', $utils->getLastUsername());
111+
}
112+
113+
public function testLastUserNameIsDefinedInSession()
114+
{
115+
$request = Request::create('/');
116+
117+
$session = new Session(new MockArraySessionStorage());
118+
$session->set(SecurityRequestAttributes::LAST_USERNAME, 'user');
119+
$request->setSession($session);
120+
121+
$requestStack = new RequestStack();
122+
$requestStack->push($request);
123+
124+
$utils = new AuthenticationUtils($requestStack);
125+
$this->assertSame('user', $utils->getLastUsername());
126+
}
127+
}

0 commit comments

Comments
 (0)