Skip to content

Commit a4f05ac

Browse files
committed
added some tests
1 parent dafa4d2 commit a4f05ac

File tree

12 files changed

+237
-14
lines changed

12 files changed

+237
-14
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace Symfony\Bundle\SecurityBundle\Tests\Functional\Bundle\FormLoginBundle\Controller;
4+
5+
use Symfony\Component\Security\Core\SecurityContext;
6+
use Symfony\Component\HttpFoundation\Response;
7+
use Symfony\Component\DependencyInjection\ContainerAware;
8+
9+
class LocalizedController extends ContainerAware
10+
{
11+
public function loginAction()
12+
{
13+
// get the login error if there is one
14+
if ($this->container->get('request')->attributes->has(SecurityContext::AUTHENTICATION_ERROR)) {
15+
$error = $this->container->get('request')->attributes->get(SecurityContext::AUTHENTICATION_ERROR);
16+
} else {
17+
$error = $this->container->get('request')->getSession()->get(SecurityContext::AUTHENTICATION_ERROR);
18+
}
19+
20+
return $this->container->get('templating')->renderResponse('FormLoginBundle:Localized:login.html.twig', array(
21+
// last username entered by the user
22+
'last_username' => $this->container->get('request')->getSession()->get(SecurityContext::LAST_USERNAME),
23+
'error' => $error,
24+
));
25+
}
26+
27+
public function loginCheckAction()
28+
{
29+
throw new \RuntimeException('loginCheckAction() should never be called.');
30+
}
31+
32+
public function logoutAction()
33+
{
34+
throw new \RuntimeException('logoutAction() should never be called.');
35+
}
36+
37+
public function profileAction()
38+
{
39+
return new Response('Profile');
40+
}
41+
42+
public function homepageAction()
43+
{
44+
return new Response('Homepage');
45+
}
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
localized_login_path:
2+
pattern: /{_locale}/login
3+
defaults: { _controller: FormLoginBundle:Localized:login }
4+
5+
localized_check_path:
6+
pattern: /{_locale}/login_check
7+
defaults: { _controller: FormLoginBundle:Localized:loginCheck }
8+
9+
localized_default_target_path:
10+
pattern: /{_locale}/profile
11+
defaults: { _controller: FormLoginBundle:Localized:profile }
12+
13+
localized_logout_path:
14+
pattern: /{_locale}/logout
15+
defaults: { _controller: FormLoginBundle:Localized:logout }
16+
17+
localized_logout_target_path:
18+
pattern: /{_locale}/
19+
defaults: { _controller: FormLoginBundle:Localized:homepage }

src/Symfony/Bundle/SecurityBundle/Tests/Functional/Bundle/FormLoginBundle/Resources/config/routing.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ form_login_custom_target_path:
1414
pattern: /foo
1515
defaults: { _controller: FormLoginBundle:Login:afterLogin }
1616

17+
form_login_default_target_path:
18+
pattern: /profile
19+
defaults: { _controller: FormLoginBundle:Login:afterLogin }
20+
1721
form_login_redirect_to_protected_resource_after_login:
1822
pattern: /protected-resource
1923
defaults: { _controller: FormLoginBundle:Login:afterLogin }
24+
25+
form_logout:
26+
pattern: /logout_path
27+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{% extends "::base.html.twig" %}
2+
3+
{% block body %}
4+
5+
{% if error %}
6+
<div>{{ error.message }}</div>
7+
{% endif %}
8+
9+
<form action="{{ path('localized_check_path') }}" method="post">
10+
<label for="username">Username:</label>
11+
<input type="text" id="username" name="_username" value="{{ last_username }}" />
12+
13+
<label for="password">Password:</label>
14+
<input type="password" id="password" name="_password" />
15+
16+
<input type="hidden" name="_target_path" value="" />
17+
18+
<input type="submit" name="login" />
19+
</form>
20+
21+
{% endblock %}

src/Symfony/Bundle/SecurityBundle/Tests/Functional/FormLoginTest.php

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,33 @@
1616
*/
1717
class FormLoginTest extends WebTestCase
1818
{
19-
public function testFormLogin()
19+
/**
20+
* @dataProvider getConfigs
21+
*/
22+
public function testFormLogin($config)
2023
{
21-
$client = $this->createClient(array('test_case' => 'StandardFormLogin'));
24+
$client = $this->createClient(array('test_case' => 'StandardFormLogin', 'root_config' => $config));
25+
$client->insulate();
2226

2327
$form = $client->request('GET', '/login')->selectButton('login')->form();
2428
$form['_username'] = 'johannes';
2529
$form['_password'] = 'test';
2630
$client->submit($form);
2731

28-
$this->assertRedirect($client->getResponse(), '/');
32+
$this->assertRedirect($client->getResponse(), '/profile');
2933

3034
$text = $client->followRedirect()->text();
3135
$this->assertContains('Hello johannes!', $text);
32-
$this->assertContains('You\'re browsing to path "/".', $text);
36+
$this->assertContains('You\'re browsing to path "/profile".', $text);
3337
}
3438

35-
public function testFormLoginWithCustomTargetPath()
39+
/**
40+
* @dataProvider getConfigs
41+
*/
42+
public function testFormLoginWithCustomTargetPath($config)
3643
{
37-
$client = $this->createClient(array('test_case' => 'StandardFormLogin'));
44+
$client = $this->createClient(array('test_case' => 'StandardFormLogin', 'root_config' => $config));
45+
$client->insulate();
3846

3947
$form = $client->request('GET', '/login')->selectButton('login')->form();
4048
$form['_username'] = 'johannes';
@@ -49,9 +57,13 @@ public function testFormLoginWithCustomTargetPath()
4957
$this->assertContains('You\'re browsing to path "/foo".', $text);
5058
}
5159

52-
public function testFormLoginRedirectsToProtectedResourceAfterLogin()
60+
/**
61+
* @dataProvider getConfigs
62+
*/
63+
public function testFormLoginRedirectsToProtectedResourceAfterLogin($config)
5364
{
54-
$client = $this->createClient(array('test_case' => 'StandardFormLogin'));
65+
$client = $this->createClient(array('test_case' => 'StandardFormLogin', 'root_config' => $config));
66+
$client->insulate();
5567

5668
$client->request('GET', '/protected-resource');
5769
$this->assertRedirect($client->getResponse(), '/login');
@@ -67,6 +79,14 @@ public function testFormLoginRedirectsToProtectedResourceAfterLogin()
6779
$this->assertContains('You\'re browsing to path "/protected-resource".', $text);
6880
}
6981

82+
public function getConfigs()
83+
{
84+
return array(
85+
array('config.yml'),
86+
array('routes_as_path.yml'),
87+
);
88+
}
89+
7090
protected function setUp()
7191
{
7292
parent::setUp();
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace Symfony\Bundle\SecurityBundle\Tests\Functional;
4+
5+
class LocalizedRoutesAsPathTest extends WebTestCase
6+
{
7+
/**
8+
* @dataProvider getLocales
9+
*/
10+
public function testLoginLogoutProcedure($locale)
11+
{
12+
$client = $this->createClient(array('test_case' => 'StandardFormLogin', 'root_config' => 'localized_routes.yml'));
13+
$client->insulate();
14+
15+
$crawler = $client->request('GET', '/'.$locale.'/login');
16+
$form = $crawler->selectButton('login')->form();
17+
$form['_username'] = 'johannes';
18+
$form['_password'] = 'test';
19+
$client->submit($form);
20+
21+
$this->assertRedirect($client->getResponse(), '/'.$locale.'/profile');
22+
$this->assertEquals('Profile', $client->followRedirect()->text());
23+
24+
$client->request('GET', '/'.$locale.'/logout');
25+
$this->assertRedirect($client->getResponse(), '/'.$locale.'/');
26+
$this->assertEquals('Homepage', $client->followRedirect()->text());
27+
}
28+
29+
public function getLocales()
30+
{
31+
return array(array('en'), array('de'));
32+
}
33+
34+
protected function setUp()
35+
{
36+
parent::setUp();
37+
38+
$this->deleteTmpDir('StandardFormLogin');
39+
}
40+
41+
protected function tearDown()
42+
{
43+
parent::setUp();
44+
45+
$this->deleteTmpDir('StandardFormLogin');
46+
}
47+
}

src/Symfony/Bundle/SecurityBundle/Tests/Functional/app/AppKernel.php

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,21 @@
1111

1212
namespace Symfony\Bundle\SecurityBundle\Tests\Functional;
1313

14-
use Symfony\Component\HttpKernel\Util\Filesystem;
14+
// get the autoload file
15+
$dir = __DIR__;
16+
$lastDir = null;
17+
while ($dir !== $lastDir) {
18+
$lastDir = $dir;
19+
20+
if (file_exists($dir.'/autoload.php.dist')) {
21+
require_once $dir.'/autoload.php.dist';
22+
break;
23+
}
24+
25+
$dir = dirname($dir);
26+
}
1527

28+
use Symfony\Component\HttpKernel\Util\Filesystem;
1629
use Symfony\Component\Config\Loader\LoaderInterface;
1730
use Symfony\Component\HttpKernel\Kernel;
1831

@@ -33,10 +46,11 @@ public function __construct($testCase, $rootConfig, $environment, $debug)
3346
}
3447
$this->testCase = $testCase;
3548

36-
if (!file_exists($filename = __DIR__.'/'.$testCase.'/'.$rootConfig)) {
37-
throw new \InvalidArgumentException(sprintf('The root config "%s" does not exist.', $filename));
49+
$fs = new Filesystem();
50+
if (!$fs->isAbsolutePath($rootConfig) && !file_exists($rootConfig = __DIR__.'/'.$testCase.'/'.$rootConfig)) {
51+
throw new \InvalidArgumentException(sprintf('The root config "%s" does not exist.', $rootConfig));
3852
}
39-
$this->rootConfig = $filename;
53+
$this->rootConfig = $rootConfig;
4054

4155
parent::__construct($environment, $debug);
4256
}
@@ -74,6 +88,16 @@ public function registerContainerConfiguration(LoaderInterface $loader)
7488
$loader->load($this->rootConfig);
7589
}
7690

91+
public function serialize()
92+
{
93+
return serialize(array($this->testCase, $this->rootConfig, $this->getEnvironment(), $this->isDebug()));
94+
}
95+
96+
public function unserialize($str)
97+
{
98+
call_user_func_array(array($this, '__construct'), unserialize($str));
99+
}
100+
77101
protected function getKernelParameters()
78102
{
79103
$parameters = parent::getKernelParameters();

src/Symfony/Bundle/SecurityBundle/Tests/Functional/app/StandardFormLogin/config.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ security:
2121
default:
2222
form_login:
2323
check_path: /login_check
24+
default_target_path: /profile
2425
anonymous: ~
2526

2627
access_control:
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
imports:
2+
- { resource: ./../config/default.yml }
3+
4+
security:
5+
encoders:
6+
Symfony\Component\Security\Core\User\User: plaintext
7+
8+
providers:
9+
in_memory:
10+
users:
11+
johannes: { password: test, roles: [ROLE_USER] }
12+
13+
firewalls:
14+
default:
15+
form_login:
16+
login_path: localized_login_path
17+
check_path: localized_check_path
18+
default_target_path: localized_default_target_path
19+
logout:
20+
path: localized_logout_path
21+
target: localized_logout_target_path
22+
anonymous: ~
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
imports:
2+
- { resource: ./config.yml }
3+
4+
security:
5+
firewalls:
6+
default:
7+
form_login:
8+
login_path: form_login
9+
check_path: form_login_check
10+
default_target_path: form_login_default_target_path
11+
logout:
12+
path: form_logout
13+
target: form_login_homepage
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
_form_login_bundle:
22
resource: @FormLoginBundle/Resources/config/routing.yml
3+
4+
_form_login_localized:
5+
resource: @FormLoginBundle/Resources/config/localized_routing.yml

src/Symfony/Bundle/SecurityBundle/Tests/Functional/app/config/framework.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@ framework:
99
test: ~
1010
session:
1111
default_locale: en
12-
lifetime: 3600
1312
auto_start: true
14-
storage_id: session.storage.filesystem
13+
storage_id: session.storage.filesystem
1514

1615
services:
1716
logger: { class: Symfony\Component\HttpKernel\Log\NullLogger }

0 commit comments

Comments
 (0)