fixed previous merges partially, there still seems to be a problem with the test client

This commit is contained in:
Johannes Schmitt 2011-10-24 01:27:48 +02:00 committed by Fabien Potencier
parent d29715f128
commit 46e5fa5c87
7 changed files with 100 additions and 6 deletions

View File

@ -0,0 +1,19 @@
<?php
namespace Symfony\Bundle\SecurityBundle\Tests\Functional\Bundle\FormLoginBundle\DependencyInjection;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
class FormLoginExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container)
{
$container
->register('localized_form_failure_handler', 'Symfony\Bundle\SecurityBundle\Tests\Functional\Bundle\FormLoginBundle\Security\LocalizedFormFailureHandler')
->addArgument(new Reference('router'))
;
}
}

View File

@ -0,0 +1,24 @@
<?php
namespace Symfony\Bundle\SecurityBundle\Tests\Functional\Bundle\FormLoginBundle\Security;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface;
class LocalizedFormFailureHandler implements AuthenticationFailureHandlerInterface
{
private $router;
public function __construct(RouterInterface $router)
{
$this->router = $router;
}
public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
{
return new RedirectResponse($this->router->generate('localized_login_path', array(), true));
}
}

View File

@ -26,6 +26,23 @@ class LocalizedRoutesAsPathTest extends WebTestCase
$this->assertEquals('Homepage', $client->followRedirect()->text());
}
/**
* @dataProvider getLocales
*/
public function testLoginFailureWithLocalizedFailurePath($locale)
{
$client = $this->createClient(array('test_case' => 'StandardFormLogin', 'root_config' => 'localized_form_failure_handler.yml'));
$client->insulate();
$crawler = $client->request('GET', '/'.$locale.'/login');
$form = $crawler->selectButton('login')->form();
$form['_username'] = 'johannes';
$form['_password'] = 'foobar';
$client->submit($form);
$this->assertRedirect($client->getResponse(), '/'.$locale.'/login');
}
/**
* @dataProvider getLocales
*/

View File

@ -18,7 +18,7 @@ class WebTestCase extends BaseWebTestCase
{
static public function assertRedirect($response, $location)
{
self::assertTrue($response->isRedirect(), 'Response is not a redirect, got status code: '.$response->getStatusCode());
self::assertTrue($response->isRedirect(), 'Response is not a redirect, got status code: '.substr($response, 0, 2000));
self::assertEquals('http://localhost'.$location, $response->headers->get('Location'));
}

View File

@ -0,0 +1,19 @@
imports:
- { resource: ./../config/default.yml }
security:
encoders:
Symfony\Component\Security\Core\User\User: plaintext
providers:
in_memory:
users:
johannes: { password: test, roles: [ROLE_USER] }
firewalls:
default:
form_login:
login_path: localized_login_path
check_path: localized_check_path
failure_handler: localized_form_failure_handler
anonymous: ~

View File

@ -27,7 +27,8 @@ class LocaleListener implements EventSubscriberInterface
private $router;
private $defaultLocale;
public function __construct($defaultLocale = 'en', RouterInterface $router = null)
public function __construct($defaultLocale = 'en', RouterInterface $router
= null)
{
$this->defaultLocale = $defaultLocale;
$this->router = $router;
@ -37,10 +38,17 @@ class LocaleListener implements EventSubscriberInterface
{
$request = $event->getRequest();
if ($request->hasPreviousSession()) {
$request->setDefaultLocale($request->getSession()->get('_locale', $this->defaultLocale));
$request->setDefaultLocale($request->getSession()->get('_locale',
$this->defaultLocale));
} else {
$request->setDefaultLocale($this->defaultLocale);
}
if (null !== $this->router && ($context = $this->router->getContext())
&& !$context->hasParameter('_locale')) {
$this->router->getContext()->setParameter('_locale',
$request->getLocale());
}
}
public function onKernelRequest(GetResponseEvent $event)
@ -59,14 +67,18 @@ class LocaleListener implements EventSubscriberInterface
}
if (null !== $this->router) {
$this->router->getContext()->setParameter('_locale', $request->getLocale());
$this->router->getContext()->setParameter('_locale',
$request->getLocale());
}
}
static public function getSubscribedEvents()
{
return array(
KernelEvents::REQUEST => array(array('onEarlyKernelRequest', 255), array('onKernelRequest', -1)),
KernelEvents::REQUEST => array(
array('onEarlyKernelRequest', 253),
array('onKernelRequest', -1)
),
);
}
}

View File

@ -93,7 +93,10 @@ class RouterListener implements EventSubscriberInterface
static public function getSubscribedEvents()
{
return array(
KernelEvents::REQUEST => array(array('onEarlyKernelRequest', 255), array('onKernelRequest', 10)),
KernelEvents::REQUEST => array(
array('onEarlyKernelRequest', 255),
array('onKernelRequest', 10)
),
);
}
}