Browse Source

[SecurityBundle] bootstrapped functional test suite

Johannes Schmitt 14 years ago
parent
commit
1ca4dcad91

+ 31 - 0
src/Symfony/Bundle/SecurityBundle/Tests/Functional/Bundle/FormLoginBundle/Controller/LoginController.php

@@ -0,0 +1,31 @@
+<?php
+
+namespace Symfony\Bundle\SecurityBundle\Tests\Functional\Bundle\FormLoginBundle\Controller;
+
+use Symfony\Component\HttpFoundation\Response;
+use Symfony\Component\Security\Core\SecurityContext;
+use Symfony\Component\DependencyInjection\ContainerAware;
+
+class LoginController 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:Login: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()
+    {
+        return new Response('', 400);
+    }
+}

+ 9 - 0
src/Symfony/Bundle/SecurityBundle/Tests/Functional/Bundle/FormLoginBundle/FormLoginBundle.php

@@ -0,0 +1,9 @@
+<?php
+
+namespace Symfony\Bundle\SecurityBundle\Tests\Functional\Bundle\FormLoginBundle;
+
+use Symfony\Component\HttpKernel\Bundle\Bundle;
+
+class FormLoginBundle extends Bundle
+{
+}

+ 7 - 0
src/Symfony/Bundle/SecurityBundle/Tests/Functional/Bundle/FormLoginBundle/Resources/config/routing.yml

@@ -0,0 +1,7 @@
+form_login:
+    pattern: /login
+    defaults: { _controller: FormLoginBundle:Login:login }
+    
+form_login_check:
+    pattern: /login_check
+    defaults: { _controller: FormLoginBundle:Login:loginCheck }

+ 21 - 0
src/Symfony/Bundle/SecurityBundle/Tests/Functional/Bundle/FormLoginBundle/Resources/views/Login/login.html.twig

@@ -0,0 +1,21 @@
+{% extends "::base.html.twig" %}
+
+{% block body %}
+
+    {% if error %}
+        <div>{{ error.message }}</div>
+    {% endif %}
+    
+    <form action="{{ path('form_login_check') }}" 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 %}

+ 40 - 0
src/Symfony/Bundle/SecurityBundle/Tests/Functional/FormLoginTest.php

@@ -0,0 +1,40 @@
+<?php
+
+namespace Symfony\Bundle\SecurityBundle\Tests\Functional;
+
+/**
+ * @group functional
+ */
+class FormLoginTest extends WebTestCase
+{
+    public function testFormLogin()
+    {
+        $client = $this->createClient(array('test_case' => 'StandardFormLogin'));
+
+        $form = $client->request('GET', '/login')->selectButton('login')->form();
+        $form['_username'] = 'johannes';
+        $form['_password'] = 'test';
+        $client->submit($form);
+
+        $this->assertRedirect($client->getResponse(), '/');
+    }
+
+    public function testFormLoginWithCustomTargetPath()
+    {
+        $client = $this->createClient(array('test_case' => 'StandardFormLogin'));
+
+        $form = $client->request('GET', '/login')->selectButton('login')->form();
+        $form['_username'] = 'johannes';
+        $form['_password'] = 'test';
+        $form['_target_path'] = '/foo';
+        $client->submit($form);
+
+        $this->assertRedirect($client->getResponse(), '/foo');
+    }
+
+    protected function tearDown()
+    {
+        parent::tearDown();
+        $this->deleteTmpDir('StandardFormLogin');
+    }
+}

+ 47 - 0
src/Symfony/Bundle/SecurityBundle/Tests/Functional/WebTestCase.php

@@ -0,0 +1,47 @@
+<?php
+
+namespace Symfony\Bundle\SecurityBundle\Tests\Functional;
+
+use Symfony\Component\HttpKernel\Util\Filesystem;
+use Symfony\Bundle\FrameworkBundle\Test\WebTestCase as BaseWebTestCase;
+
+class WebTestCase extends BaseWebTestCase
+{
+    public static function assertRedirect($response, $location)
+    {
+        self::assertTrue($response->isRedirect());
+        self::assertEquals('http://localhost'.$location, $response->headers->get('Location'));
+    }
+
+    protected function deleteTmpDir($testCase)
+    {
+        if (!file_exists($dir = sys_get_temp_dir().'/'.$testCase)) {
+            return;
+        }
+
+        $fs = new Filesystem();
+        $fs->remove($testCase);
+    }
+
+    protected function getKernelClass()
+    {
+        require_once __DIR__.'/app/AppKernel.php';
+
+        return 'Symfony\Bundle\SecurityBundle\Tests\Functional\AppKernel';
+    }
+
+    protected function createKernel(array $options = array())
+    {
+        $class = $this->getKernelClass();
+
+        if (!isset($options['test_case'])) {
+            throw new \InvalidArgumentException('The option "test_case" must be set.');
+        }
+
+        return new $class(
+            $options['test_case'],
+            isset($options['environment']) ? $options['environment'] : 'test',
+            isset($options['debug']) ? $options['debug'] : true
+        );
+    }
+}

+ 69 - 0
src/Symfony/Bundle/SecurityBundle/Tests/Functional/app/AppKernel.php

@@ -0,0 +1,69 @@
+<?php
+
+namespace Symfony\Bundle\SecurityBundle\Tests\Functional;
+
+use Symfony\Component\HttpKernel\Util\Filesystem;
+
+use Symfony\Component\Config\Loader\LoaderInterface;
+use Symfony\Component\HttpKernel\Kernel;
+
+/**
+ * App Test Kernel for functional tests.
+ *
+ * @author Johannes M. Schmitt <schmittjoh@gmail.com>
+ */
+class AppKernel extends Kernel
+{
+    private $testCase;
+
+    public function __construct($testCase, $environment, $debug)
+    {
+        if (!is_dir(__DIR__.'/'.$testCase)) {
+            throw new \InvalidArgumentException(sprintf('The test case "%s" does not exist.', $testCase));
+        }
+        $this->testCase = $testCase;
+
+        parent::__construct($environment, $debug);
+    }
+
+    public function registerBundles()
+    {
+        if (!file_exists($filename = $this->getRootDir().'/'.$this->testCase.'/bundles.php')) {
+            throw new \RuntimeException(sprintf('The bundles file "%s" does not exist.', $filename));
+        }
+
+        return include $filename;
+    }
+
+    public function getRootDir()
+    {
+        return __DIR__;
+    }
+
+    public function getCacheDir()
+    {
+        return sys_get_temp_dir().'/'.$this->testCase.'/cache/'.$this->environment;
+    }
+
+    public function getLogDir()
+    {
+        return sys_get_temp_dir().'/'.$this->testCase.'/logs';
+    }
+
+    public function registerContainerConfiguration(LoaderInterface $loader)
+    {
+        if (!file_exists($filename = $this->getRootDir().'/'.$this->testCase.'/config.yml')) {
+            throw new \RuntimeException(sprintf('The config file "%s" does not exist.', $filename));
+        }
+
+        $loader->load($filename);
+    }
+
+    protected function getKernelParameters()
+    {
+        $parameters = parent::getKernelParameters();
+        $parameters['kernel.test_case'] = $this->testCase;
+
+        return $parameters;
+    }
+}

+ 13 - 0
src/Symfony/Bundle/SecurityBundle/Tests/Functional/app/Resources/views/base.html.twig

@@ -0,0 +1,13 @@
+<!DOCTYPE html>
+<html>
+    <head>
+        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
+        <title>{% block title %}Welcome!{% endblock %}</title>
+        {% block stylesheets %}{% endblock %}
+        <link rel="shortcut icon" href="{{ asset('favicon.ico') }}" />
+    </head>
+    <body>
+        {% block body %}{% endblock %}
+        {% block javascripts %}{% endblock %}
+    </body>
+</html>

+ 13 - 0
src/Symfony/Bundle/SecurityBundle/Tests/Functional/app/StandardFormLogin/bundles.php

@@ -0,0 +1,13 @@
+<?php
+
+use Symfony\Bundle\SecurityBundle\Tests\Functional\Bundle\FormLoginBundle\FormLoginBundle;
+use Symfony\Bundle\TwigBundle\TwigBundle;
+use Symfony\Bundle\SecurityBundle\SecurityBundle;
+use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
+
+return array(
+    new FrameworkBundle(),
+    new SecurityBundle(),
+    new TwigBundle(),
+    new FormLoginBundle(),
+);

+ 23 - 0
src/Symfony/Bundle/SecurityBundle/Tests/Functional/app/StandardFormLogin/config.yml

@@ -0,0 +1,23 @@
+imports:
+    - { type: file, resource: ./../config/default.yml }
+
+security:
+    encoders:
+        Symfony\Component\Security\Core\User\User: plaintext
+        
+    providers:
+        in_memory:
+            users:
+                johannes: { password: test, roles: [ROLE_USER] }
+            
+    firewalls:
+        login_form:
+            pattern: ^/login$
+            security: false
+
+        default:
+            form_login:
+                check_path: /login_check
+
+    access_control:
+        - { path: .*, roles: IS_AUTHENTICATED_FULLY }

+ 2 - 0
src/Symfony/Bundle/SecurityBundle/Tests/Functional/app/StandardFormLogin/routing.yml

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

+ 21 - 0
src/Symfony/Bundle/SecurityBundle/Tests/Functional/app/config/default.yml

@@ -0,0 +1,21 @@
+framework:
+    charset:       UTF-8
+    secret:        test
+    csrf_protection:
+        enabled: true
+    router:        { resource: "%kernel.root_dir%/%kernel.test_case%/routing.yml" }
+    validation:    { enabled: true, enable_annotations: true }
+    templating:    { engines: ['twig'] }
+    form: ~
+    test: ~
+    session:
+        default_locale: en
+        lifetime:       3600
+        auto_start:     true
+        storage_id: session.storage.filesystem
+
+# Twig Configuration 
+twig:
+    debug:            %kernel.debug%
+    strict_variables: %kernel.debug%
+    extensions: []