From a4f05ac7ab35e9c0e0be245ec3cc5033ae13ca48 Mon Sep 17 00:00:00 2001 From: Johannes Schmitt Date: Tue, 19 Jul 2011 14:50:39 +0200 Subject: [PATCH] added some tests --- .../Controller/LocalizedController.php | 46 ++++++++++++++++++ .../Resources/config/localized_routing.yml | 19 ++++++++ .../Resources/config/routing.yml | 8 ++++ .../Resources/views/Localized/login.html.twig | 21 +++++++++ .../Tests/Functional/FormLoginTest.php | 36 ++++++++++---- .../Functional/LocalizedRoutesAsPathTest.php | 47 +++++++++++++++++++ .../Tests/Functional/app/AppKernel.php | 32 +++++++++++-- .../app/StandardFormLogin/config.yml | 1 + .../StandardFormLogin/localized_routes.yml | 22 +++++++++ .../app/StandardFormLogin/routes_as_path.yml | 13 +++++ .../app/StandardFormLogin/routing.yml | 3 ++ .../Tests/Functional/app/config/framework.yml | 3 +- 12 files changed, 237 insertions(+), 14 deletions(-) create mode 100644 src/Symfony/Bundle/SecurityBundle/Tests/Functional/Bundle/FormLoginBundle/Controller/LocalizedController.php create mode 100644 src/Symfony/Bundle/SecurityBundle/Tests/Functional/Bundle/FormLoginBundle/Resources/config/localized_routing.yml create mode 100644 src/Symfony/Bundle/SecurityBundle/Tests/Functional/Bundle/FormLoginBundle/Resources/views/Localized/login.html.twig create mode 100644 src/Symfony/Bundle/SecurityBundle/Tests/Functional/LocalizedRoutesAsPathTest.php create mode 100644 src/Symfony/Bundle/SecurityBundle/Tests/Functional/app/StandardFormLogin/localized_routes.yml create mode 100644 src/Symfony/Bundle/SecurityBundle/Tests/Functional/app/StandardFormLogin/routes_as_path.yml diff --git a/src/Symfony/Bundle/SecurityBundle/Tests/Functional/Bundle/FormLoginBundle/Controller/LocalizedController.php b/src/Symfony/Bundle/SecurityBundle/Tests/Functional/Bundle/FormLoginBundle/Controller/LocalizedController.php new file mode 100644 index 0000000000..ee4c47469c --- /dev/null +++ b/src/Symfony/Bundle/SecurityBundle/Tests/Functional/Bundle/FormLoginBundle/Controller/LocalizedController.php @@ -0,0 +1,46 @@ +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'); + } +} \ No newline at end of file diff --git a/src/Symfony/Bundle/SecurityBundle/Tests/Functional/Bundle/FormLoginBundle/Resources/config/localized_routing.yml b/src/Symfony/Bundle/SecurityBundle/Tests/Functional/Bundle/FormLoginBundle/Resources/config/localized_routing.yml new file mode 100644 index 0000000000..2c38fee9be --- /dev/null +++ b/src/Symfony/Bundle/SecurityBundle/Tests/Functional/Bundle/FormLoginBundle/Resources/config/localized_routing.yml @@ -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 } \ No newline at end of file diff --git a/src/Symfony/Bundle/SecurityBundle/Tests/Functional/Bundle/FormLoginBundle/Resources/config/routing.yml b/src/Symfony/Bundle/SecurityBundle/Tests/Functional/Bundle/FormLoginBundle/Resources/config/routing.yml index 21c0841d75..5fab7f17d1 100644 --- a/src/Symfony/Bundle/SecurityBundle/Tests/Functional/Bundle/FormLoginBundle/Resources/config/routing.yml +++ b/src/Symfony/Bundle/SecurityBundle/Tests/Functional/Bundle/FormLoginBundle/Resources/config/routing.yml @@ -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 + diff --git a/src/Symfony/Bundle/SecurityBundle/Tests/Functional/Bundle/FormLoginBundle/Resources/views/Localized/login.html.twig b/src/Symfony/Bundle/SecurityBundle/Tests/Functional/Bundle/FormLoginBundle/Resources/views/Localized/login.html.twig new file mode 100644 index 0000000000..60dd2f1ffa --- /dev/null +++ b/src/Symfony/Bundle/SecurityBundle/Tests/Functional/Bundle/FormLoginBundle/Resources/views/Localized/login.html.twig @@ -0,0 +1,21 @@ +{% extends "::base.html.twig" %} + +{% block body %} + + {% if error %} +
{{ error.message }}
+ {% endif %} + +
+ + + + + + + + + +
+ +{% endblock %} diff --git a/src/Symfony/Bundle/SecurityBundle/Tests/Functional/FormLoginTest.php b/src/Symfony/Bundle/SecurityBundle/Tests/Functional/FormLoginTest.php index 5f0ad88097..fe50eb2672 100644 --- a/src/Symfony/Bundle/SecurityBundle/Tests/Functional/FormLoginTest.php +++ b/src/Symfony/Bundle/SecurityBundle/Tests/Functional/FormLoginTest.php @@ -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(); diff --git a/src/Symfony/Bundle/SecurityBundle/Tests/Functional/LocalizedRoutesAsPathTest.php b/src/Symfony/Bundle/SecurityBundle/Tests/Functional/LocalizedRoutesAsPathTest.php new file mode 100644 index 0000000000..c4502c60dd --- /dev/null +++ b/src/Symfony/Bundle/SecurityBundle/Tests/Functional/LocalizedRoutesAsPathTest.php @@ -0,0 +1,47 @@ +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'); + } +} \ No newline at end of file diff --git a/src/Symfony/Bundle/SecurityBundle/Tests/Functional/app/AppKernel.php b/src/Symfony/Bundle/SecurityBundle/Tests/Functional/app/AppKernel.php index 7ed45efa1c..cd2445a6da 100644 --- a/src/Symfony/Bundle/SecurityBundle/Tests/Functional/app/AppKernel.php +++ b/src/Symfony/Bundle/SecurityBundle/Tests/Functional/app/AppKernel.php @@ -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(); diff --git a/src/Symfony/Bundle/SecurityBundle/Tests/Functional/app/StandardFormLogin/config.yml b/src/Symfony/Bundle/SecurityBundle/Tests/Functional/app/StandardFormLogin/config.yml index e121c0b4d8..7c60e3c3e4 100644 --- a/src/Symfony/Bundle/SecurityBundle/Tests/Functional/app/StandardFormLogin/config.yml +++ b/src/Symfony/Bundle/SecurityBundle/Tests/Functional/app/StandardFormLogin/config.yml @@ -21,6 +21,7 @@ security: default: form_login: check_path: /login_check + default_target_path: /profile anonymous: ~ access_control: diff --git a/src/Symfony/Bundle/SecurityBundle/Tests/Functional/app/StandardFormLogin/localized_routes.yml b/src/Symfony/Bundle/SecurityBundle/Tests/Functional/app/StandardFormLogin/localized_routes.yml new file mode 100644 index 0000000000..7157f29592 --- /dev/null +++ b/src/Symfony/Bundle/SecurityBundle/Tests/Functional/app/StandardFormLogin/localized_routes.yml @@ -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: ~ \ No newline at end of file diff --git a/src/Symfony/Bundle/SecurityBundle/Tests/Functional/app/StandardFormLogin/routes_as_path.yml b/src/Symfony/Bundle/SecurityBundle/Tests/Functional/app/StandardFormLogin/routes_as_path.yml new file mode 100644 index 0000000000..800b8ac4e7 --- /dev/null +++ b/src/Symfony/Bundle/SecurityBundle/Tests/Functional/app/StandardFormLogin/routes_as_path.yml @@ -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 \ No newline at end of file diff --git a/src/Symfony/Bundle/SecurityBundle/Tests/Functional/app/StandardFormLogin/routing.yml b/src/Symfony/Bundle/SecurityBundle/Tests/Functional/app/StandardFormLogin/routing.yml index d3d46d054b..e4f13cf060 100644 --- a/src/Symfony/Bundle/SecurityBundle/Tests/Functional/app/StandardFormLogin/routing.yml +++ b/src/Symfony/Bundle/SecurityBundle/Tests/Functional/app/StandardFormLogin/routing.yml @@ -1,2 +1,5 @@ _form_login_bundle: resource: @FormLoginBundle/Resources/config/routing.yml + +_form_login_localized: + resource: @FormLoginBundle/Resources/config/localized_routing.yml \ No newline at end of file diff --git a/src/Symfony/Bundle/SecurityBundle/Tests/Functional/app/config/framework.yml b/src/Symfony/Bundle/SecurityBundle/Tests/Functional/app/config/framework.yml index 77e9f2241d..4cafd76671 100644 --- a/src/Symfony/Bundle/SecurityBundle/Tests/Functional/app/config/framework.yml +++ b/src/Symfony/Bundle/SecurityBundle/Tests/Functional/app/config/framework.yml @@ -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 }