added some tests

This commit is contained in:
Johannes Schmitt 2011-07-19 14:50:39 +02:00
parent dafa4d28d6
commit a4f05ac7ab
12 changed files with 237 additions and 14 deletions

View File

@ -0,0 +1,46 @@
<?php
namespace Symfony\Bundle\SecurityBundle\Tests\Functional\Bundle\FormLoginBundle\Controller;
use Symfony\Component\Security\Core\SecurityContext;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\DependencyInjection\ContainerAware;
class LocalizedController extends ContainerAware
{
public function loginAction()
{
// get the login error if there is one
if ($this->container->get('request')->attributes->has(SecurityContext::AUTHENTICATION_ERROR)) {
$error = $this->container->get('request')->attributes->get(SecurityContext::AUTHENTICATION_ERROR);
} else {
$error = $this->container->get('request')->getSession()->get(SecurityContext::AUTHENTICATION_ERROR);
}
return $this->container->get('templating')->renderResponse('FormLoginBundle:Localized:login.html.twig', array(
// last username entered by the user
'last_username' => $this->container->get('request')->getSession()->get(SecurityContext::LAST_USERNAME),
'error' => $error,
));
}
public function loginCheckAction()
{
throw new \RuntimeException('loginCheckAction() should never be called.');
}
public function logoutAction()
{
throw new \RuntimeException('logoutAction() should never be called.');
}
public function profileAction()
{
return new Response('Profile');
}
public function homepageAction()
{
return new Response('Homepage');
}
}

View File

@ -0,0 +1,19 @@
localized_login_path:
pattern: /{_locale}/login
defaults: { _controller: FormLoginBundle:Localized:login }
localized_check_path:
pattern: /{_locale}/login_check
defaults: { _controller: FormLoginBundle:Localized:loginCheck }
localized_default_target_path:
pattern: /{_locale}/profile
defaults: { _controller: FormLoginBundle:Localized:profile }
localized_logout_path:
pattern: /{_locale}/logout
defaults: { _controller: FormLoginBundle:Localized:logout }
localized_logout_target_path:
pattern: /{_locale}/
defaults: { _controller: FormLoginBundle:Localized:homepage }

View File

@ -14,6 +14,14 @@ form_login_custom_target_path:
pattern: /foo
defaults: { _controller: FormLoginBundle:Login:afterLogin }
form_login_default_target_path:
pattern: /profile
defaults: { _controller: FormLoginBundle:Login:afterLogin }
form_login_redirect_to_protected_resource_after_login:
pattern: /protected-resource
defaults: { _controller: FormLoginBundle:Login:afterLogin }
form_logout:
pattern: /logout_path

View File

@ -0,0 +1,21 @@
{% extends "::base.html.twig" %}
{% block body %}
{% if error %}
<div>{{ error.message }}</div>
{% endif %}
<form action="{{ path('localized_check_path') }}" method="post">
<label for="username">Username:</label>
<input type="text" id="username" name="_username" value="{{ last_username }}" />
<label for="password">Password:</label>
<input type="password" id="password" name="_password" />
<input type="hidden" name="_target_path" value="" />
<input type="submit" name="login" />
</form>
{% endblock %}

View File

@ -16,25 +16,33 @@ namespace Symfony\Bundle\SecurityBundle\Tests\Functional;
*/
class FormLoginTest extends WebTestCase
{
public function testFormLogin()
/**
* @dataProvider getConfigs
*/
public function testFormLogin($config)
{
$client = $this->createClient(array('test_case' => 'StandardFormLogin'));
$client = $this->createClient(array('test_case' => 'StandardFormLogin', 'root_config' => $config));
$client->insulate();
$form = $client->request('GET', '/login')->selectButton('login')->form();
$form['_username'] = 'johannes';
$form['_password'] = 'test';
$client->submit($form);
$this->assertRedirect($client->getResponse(), '/');
$this->assertRedirect($client->getResponse(), '/profile');
$text = $client->followRedirect()->text();
$this->assertContains('Hello johannes!', $text);
$this->assertContains('You\'re browsing to path "/".', $text);
$this->assertContains('You\'re browsing to path "/profile".', $text);
}
public function testFormLoginWithCustomTargetPath()
/**
* @dataProvider getConfigs
*/
public function testFormLoginWithCustomTargetPath($config)
{
$client = $this->createClient(array('test_case' => 'StandardFormLogin'));
$client = $this->createClient(array('test_case' => 'StandardFormLogin', 'root_config' => $config));
$client->insulate();
$form = $client->request('GET', '/login')->selectButton('login')->form();
$form['_username'] = 'johannes';
@ -49,9 +57,13 @@ class FormLoginTest extends WebTestCase
$this->assertContains('You\'re browsing to path "/foo".', $text);
}
public function testFormLoginRedirectsToProtectedResourceAfterLogin()
/**
* @dataProvider getConfigs
*/
public function testFormLoginRedirectsToProtectedResourceAfterLogin($config)
{
$client = $this->createClient(array('test_case' => 'StandardFormLogin'));
$client = $this->createClient(array('test_case' => 'StandardFormLogin', 'root_config' => $config));
$client->insulate();
$client->request('GET', '/protected-resource');
$this->assertRedirect($client->getResponse(), '/login');
@ -67,6 +79,14 @@ class FormLoginTest extends WebTestCase
$this->assertContains('You\'re browsing to path "/protected-resource".', $text);
}
public function getConfigs()
{
return array(
array('config.yml'),
array('routes_as_path.yml'),
);
}
protected function setUp()
{
parent::setUp();

View File

@ -0,0 +1,47 @@
<?php
namespace Symfony\Bundle\SecurityBundle\Tests\Functional;
class LocalizedRoutesAsPathTest extends WebTestCase
{
/**
* @dataProvider getLocales
*/
public function testLoginLogoutProcedure($locale)
{
$client = $this->createClient(array('test_case' => 'StandardFormLogin', 'root_config' => 'localized_routes.yml'));
$client->insulate();
$crawler = $client->request('GET', '/'.$locale.'/login');
$form = $crawler->selectButton('login')->form();
$form['_username'] = 'johannes';
$form['_password'] = 'test';
$client->submit($form);
$this->assertRedirect($client->getResponse(), '/'.$locale.'/profile');
$this->assertEquals('Profile', $client->followRedirect()->text());
$client->request('GET', '/'.$locale.'/logout');
$this->assertRedirect($client->getResponse(), '/'.$locale.'/');
$this->assertEquals('Homepage', $client->followRedirect()->text());
}
public function getLocales()
{
return array(array('en'), array('de'));
}
protected function setUp()
{
parent::setUp();
$this->deleteTmpDir('StandardFormLogin');
}
protected function tearDown()
{
parent::setUp();
$this->deleteTmpDir('StandardFormLogin');
}
}

View File

@ -11,8 +11,21 @@
namespace Symfony\Bundle\SecurityBundle\Tests\Functional;
use Symfony\Component\HttpKernel\Util\Filesystem;
// get the autoload file
$dir = __DIR__;
$lastDir = null;
while ($dir !== $lastDir) {
$lastDir = $dir;
if (file_exists($dir.'/autoload.php.dist')) {
require_once $dir.'/autoload.php.dist';
break;
}
$dir = dirname($dir);
}
use Symfony\Component\HttpKernel\Util\Filesystem;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\HttpKernel\Kernel;
@ -33,10 +46,11 @@ class AppKernel extends Kernel
}
$this->testCase = $testCase;
if (!file_exists($filename = __DIR__.'/'.$testCase.'/'.$rootConfig)) {
throw new \InvalidArgumentException(sprintf('The root config "%s" does not exist.', $filename));
$fs = new Filesystem();
if (!$fs->isAbsolutePath($rootConfig) && !file_exists($rootConfig = __DIR__.'/'.$testCase.'/'.$rootConfig)) {
throw new \InvalidArgumentException(sprintf('The root config "%s" does not exist.', $rootConfig));
}
$this->rootConfig = $filename;
$this->rootConfig = $rootConfig;
parent::__construct($environment, $debug);
}
@ -74,6 +88,16 @@ class AppKernel extends Kernel
$loader->load($this->rootConfig);
}
public function serialize()
{
return serialize(array($this->testCase, $this->rootConfig, $this->getEnvironment(), $this->isDebug()));
}
public function unserialize($str)
{
call_user_func_array(array($this, '__construct'), unserialize($str));
}
protected function getKernelParameters()
{
$parameters = parent::getKernelParameters();

View File

@ -21,6 +21,7 @@ security:
default:
form_login:
check_path: /login_check
default_target_path: /profile
anonymous: ~
access_control:

View File

@ -0,0 +1,22 @@
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
default_target_path: localized_default_target_path
logout:
path: localized_logout_path
target: localized_logout_target_path
anonymous: ~

View File

@ -0,0 +1,13 @@
imports:
- { resource: ./config.yml }
security:
firewalls:
default:
form_login:
login_path: form_login
check_path: form_login_check
default_target_path: form_login_default_target_path
logout:
path: form_logout
target: form_login_homepage

View File

@ -1,2 +1,5 @@
_form_login_bundle:
resource: @FormLoginBundle/Resources/config/routing.yml
_form_login_localized:
resource: @FormLoginBundle/Resources/config/localized_routing.yml

View File

@ -9,9 +9,8 @@ framework:
test: ~
session:
default_locale: en
lifetime: 3600
auto_start: true
storage_id: session.storage.filesystem
storage_id: session.storage.filesystem
services:
logger: { class: Symfony\Component\HttpKernel\Log\NullLogger }