Skip to content

Commit 0c6096f

Browse files
Merge branch '2.7' into 2.8
* 2.7: [DI] Handle root namespace in service definitions Use rawurlencode() to transform the Cookie into a string [Security] Fix authentication.failure event not dispatched on AccountStatusException
2 parents 6735b35 + 87a6845 commit 0c6096f

File tree

8 files changed

+88
-14
lines changed

8 files changed

+88
-14
lines changed

src/Symfony/Component/BrowserKit/Cookie.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public function __construct($name, $value, $expires = null, $path = null, $domai
6262
$this->rawValue = $value;
6363
} else {
6464
$this->value = $value;
65-
$this->rawValue = urlencode($value);
65+
$this->rawValue = rawurlencode($value);
6666
}
6767
$this->name = $name;
6868
$this->path = empty($path) ? '/' : $path;

src/Symfony/Component/BrowserKit/Tests/CookieTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,21 @@
1616

1717
class CookieTest extends TestCase
1818
{
19+
public function testToString()
20+
{
21+
$cookie = new Cookie('foo', 'bar', strtotime('Fri, 20-May-2011 15:25:52 GMT'), '/', '.myfoodomain.com', true);
22+
$this->assertEquals('foo=bar; expires=Fri, 20 May 2011 15:25:52 GMT; domain=.myfoodomain.com; path=/; secure; httponly', (string) $cookie, '->__toString() returns string representation of the cookie');
23+
24+
$cookie = new Cookie('foo', 'bar with white spaces', strtotime('Fri, 20-May-2011 15:25:52 GMT'), '/', '.myfoodomain.com', true);
25+
$this->assertEquals('foo=bar%20with%20white%20spaces; expires=Fri, 20 May 2011 15:25:52 GMT; domain=.myfoodomain.com; path=/; secure; httponly', (string) $cookie, '->__toString() encodes the value of the cookie according to RFC 3986 (white space = %20)');
26+
27+
$cookie = new Cookie('foo', null, 1, '/admin/', '.myfoodomain.com');
28+
$this->assertEquals('foo=; expires=Thu, 01 Jan 1970 00:00:01 GMT; domain=.myfoodomain.com; path=/admin/; httponly', (string) $cookie, '->__toString() returns string representation of a cleared cookie if value is NULL');
29+
30+
$cookie = new Cookie('foo', 'bar', 0, '/', '');
31+
$this->assertEquals('foo=bar; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/; httponly', (string) $cookie);
32+
}
33+
1934
/**
2035
* @dataProvider getTestsForToFromString
2136
*/

src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -377,15 +377,9 @@ private function addServiceReturn($id, $definition)
377377
*/
378378
private function addServiceInstance($id, Definition $definition)
379379
{
380-
$class = $definition->getClass();
381-
382-
if ('\\' === substr($class, 0, 1)) {
383-
$class = substr($class, 1);
384-
}
385-
386-
$class = $this->dumpValue($class);
380+
$class = $this->dumpValue($definition->getClass());
387381

388-
if (0 === strpos($class, "'") && !preg_match('/^\'[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*(\\\{2}[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)*\'$/', $class)) {
382+
if (0 === strpos($class, "'") && !preg_match('/^\'(?:\\\{2})?[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*(?:\\\{2}[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)*\'$/', $class)) {
389383
throw new InvalidArgumentException(sprintf('"%s" is not a valid class name for the "%s" service.', $class, $id));
390384
}
391385

@@ -1460,11 +1454,13 @@ private function dumpLiteralClass($class)
14601454
if (false !== strpos($class, '$')) {
14611455
throw new RuntimeException('Cannot dump definitions which have a variable class name.');
14621456
}
1463-
if (0 !== strpos($class, "'") || !preg_match('/^\'[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*(\\\{2}[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)*\'$/', $class)) {
1457+
if (0 !== strpos($class, "'") || !preg_match('/^\'(?:\\\{2})?[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*(?:\\\{2}[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)*\'$/', $class)) {
14641458
throw new RuntimeException(sprintf('Cannot dump definition because of invalid class name (%s)', $class ?: 'n/a'));
14651459
}
14661460

1467-
return '\\'.substr(str_replace('\\\\', '\\', $class), 1, -1);
1461+
$class = substr(str_replace('\\\\', '\\', $class), 1, -1);
1462+
1463+
return 0 === strpos($class, '\\') ? $class : '\\'.$class;
14681464
}
14691465

14701466
/**

src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,4 +347,17 @@ public function testCircularReferenceAllowanceForInlinedDefinitionsForLazyServic
347347

348348
$this->addToAssertionCount(1);
349349
}
350+
351+
public function testDumpHandlesLiteralClassWithRootNamespace()
352+
{
353+
$container = new ContainerBuilder();
354+
$container->register('foo', '\\stdClass');
355+
356+
$dumper = new PhpDumper($container);
357+
eval('?>'.$dumper->dump(array('class' => 'Symfony_DI_PhpDumper_Test_Literal_Class_With_Root_Namespace')));
358+
359+
$container = new \Symfony_DI_PhpDumper_Test_Literal_Class_With_Root_Namespace();
360+
361+
$this->assertInstanceOf('stdClass', $container->get('foo'));
362+
}
350363
}

src/Symfony/Component/HttpFoundation/Cookie.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public function __toString()
8282
if ('' === (string) $this->getValue()) {
8383
$str .= 'deleted; expires='.gmdate('D, d-M-Y H:i:s T', time() - 31536001);
8484
} else {
85-
$str .= urlencode($this->getValue());
85+
$str .= rawurlencode($this->getValue());
8686

8787
if (0 !== $this->getExpiresTime()) {
8888
$str .= '; expires='.gmdate('D, d-M-Y H:i:s T', $this->getExpiresTime());

src/Symfony/Component/HttpFoundation/Tests/CookieTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,9 @@ public function testToString()
160160
$cookie = new Cookie('foo', 'bar', strtotime('Fri, 20-May-2011 15:25:52 GMT'), '/', '.myfoodomain.com', true);
161161
$this->assertEquals('foo=bar; expires=Fri, 20-May-2011 15:25:52 GMT; path=/; domain=.myfoodomain.com; secure; httponly', (string) $cookie, '->__toString() returns string representation of the cookie');
162162

163+
$cookie = new Cookie('foo', 'bar with white spaces', strtotime('Fri, 20-May-2011 15:25:52 GMT'), '/', '.myfoodomain.com', true);
164+
$this->assertEquals('foo=bar%20with%20white%20spaces; expires=Fri, 20-May-2011 15:25:52 GMT; path=/; domain=.myfoodomain.com; secure; httponly', (string) $cookie, '->__toString() encodes the value of the cookie according to RFC 3986 (white space = %20)');
165+
163166
$cookie = new Cookie('foo', null, 1, '/admin/', '.myfoodomain.com');
164167
$this->assertEquals('foo=deleted; expires='.gmdate('D, d-M-Y H:i:s T', time() - 31536001).'; path=/admin/; domain=.myfoodomain.com; httponly', (string) $cookie, '->__toString() returns string representation of a cleared cookie if value is NULL');
165168

src/Symfony/Component/Security/Core/Authentication/AuthenticationProviderManager.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,9 @@ public function authenticate(TokenInterface $token)
8383
break;
8484
}
8585
} catch (AccountStatusException $e) {
86-
$e->setToken($token);
86+
$lastException = $e;
8787

88-
throw $e;
88+
break;
8989
} catch (AuthenticationException $e) {
9090
$lastException = $e;
9191
}

src/Symfony/Component/Security/Core/Tests/Authentication/AuthenticationProviderManagerTest.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\Security\Core\Authentication\AuthenticationProviderManager;
16+
use Symfony\Component\Security\Core\AuthenticationEvents;
17+
use Symfony\Component\Security\Core\Event\AuthenticationEvent;
18+
use Symfony\Component\Security\Core\Event\AuthenticationFailureEvent;
1619
use Symfony\Component\Security\Core\Exception\ProviderNotFoundException;
1720
use Symfony\Component\Security\Core\Exception\AuthenticationException;
1821
use Symfony\Component\Security\Core\Exception\AccountStatusException;
@@ -124,6 +127,50 @@ public function testEraseCredentialFlag()
124127
$this->assertEquals('bar', $token->getCredentials());
125128
}
126129

130+
public function testAuthenticateDispatchesAuthenticationFailureEvent()
131+
{
132+
$token = new UsernamePasswordToken('foo', 'bar', 'key');
133+
$provider = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Provider\AuthenticationProviderInterface')->getMock();
134+
$provider->expects($this->once())->method('supports')->willReturn(true);
135+
$provider->expects($this->once())->method('authenticate')->willThrowException($exception = new AuthenticationException());
136+
137+
$dispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcherInterface')->getMock();
138+
$dispatcher
139+
->expects($this->once())
140+
->method('dispatch')
141+
->with(AuthenticationEvents::AUTHENTICATION_FAILURE, $this->equalTo(new AuthenticationFailureEvent($token, $exception)));
142+
143+
$manager = new AuthenticationProviderManager(array($provider));
144+
$manager->setEventDispatcher($dispatcher);
145+
146+
try {
147+
$manager->authenticate($token);
148+
$this->fail('->authenticate() should rethrow exceptions');
149+
} catch (AuthenticationException $e) {
150+
$this->assertSame($token, $exception->getToken());
151+
}
152+
}
153+
154+
public function testAuthenticateDispatchesAuthenticationSuccessEvent()
155+
{
156+
$token = new UsernamePasswordToken('foo', 'bar', 'key');
157+
158+
$provider = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Provider\AuthenticationProviderInterface')->getMock();
159+
$provider->expects($this->once())->method('supports')->willReturn(true);
160+
$provider->expects($this->once())->method('authenticate')->willReturn($token);
161+
162+
$dispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcherInterface')->getMock();
163+
$dispatcher
164+
->expects($this->once())
165+
->method('dispatch')
166+
->with(AuthenticationEvents::AUTHENTICATION_SUCCESS, $this->equalTo(new AuthenticationEvent($token)));
167+
168+
$manager = new AuthenticationProviderManager(array($provider));
169+
$manager->setEventDispatcher($dispatcher);
170+
171+
$this->assertSame($token, $manager->authenticate($token));
172+
}
173+
127174
protected function getAuthenticationProvider($supports, $token = null, $exception = null)
128175
{
129176
$provider = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Provider\AuthenticationProviderInterface')->getMock();

0 commit comments

Comments
 (0)