Classes List

Symfony\Component\Security\Http\Tests\Authentication\DefaultAuthenticationSuccessHandlerTest

 1 
 2 
 3 
 4 
 5 
 6 
 7 
 8 
 9 
 10 
 11 
 12 
 13 
 14 
 15 
 16 
 17 
 18 
 19 
 20 
 21 
 22 
 23 
 24 
 25 
 26 
 27 
 28 
 29 
 30 
 31 
 32 
 33 
 34 
 35 
 36 
 37 
 38 
 39 
 40 
 41 
 42 
 43 
 44 
 45 
 46 
 47 
 48 
 49 
 50 
 51 
 52 
 53 
 54 
 55 
 56 
 57 
 58 
 59 
 60 
 61 
 62 
 63 
 64 
 65 
 66 
 67 
 68 
 69 
 70 
 71 
 72 
 73 
 74 
 75 
 76 
 77 
 78 
 79 
 80 
 81 
 82 
 83 
 84 
 85 
 86 
 87 
 88 
 89 
 90 
 91 
 92 
 93 
 94 
 95 
 96 
 97 
 98 
 99 
 100 
 101 
 102 
 103 
 104 
 105 
 106 
 107 
 108 
 109 
 110 
 111 
 112 
 113 
 114 
<?php

/* * This file is part of the Symfony package. * * (c) Fabien Potencier <fabien@symfony.com> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */


namespace Symfony\Component\Security\Http\Tests\Authentication;

use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Http\Authentication\DefaultAuthenticationSuccessHandler;
use Symfony\Component\Security\Http\HttpUtils;

class DefaultAuthenticationSuccessHandlerTest extends TestCase
{
    /** * @dataProvider getRequestRedirections */
    public function testRequestRedirections(Request $request$options$redirectedUrl)
    {
        $urlGenerator = $this->getMockBuilder('Symfony\Component\Routing\Generator\UrlGeneratorInterface')->getMock();
        $urlGenerator->expects($this->any())->method('generate')->will($this->returnValue('http://localhost/login'));
        $httpUtils = new HttpUtils($urlGenerator);
        $token = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')->getMock();
        $handler = new DefaultAuthenticationSuccessHandler($httpUtils$options);
        if ($request->hasSession()) {
            $handler->setProviderKey('admin');
        }
        $this->assertSame('http://localhost'.$redirectedUrl$handler->onAuthenticationSuccess($request$token)->getTargetUrl());
    }

    public function getRequestRedirections()
    {
        $session = $this->getMockBuilder('Symfony\Component\HttpFoundation\Session\SessionInterface')->getMock();
        $session->expects($this->once())->method('get')->with('_security.admin.target_path')->will($this->returnValue('/admin/dashboard'));
        $session->expects($this->once())->method('remove')->with('_security.admin.target_path');
        $requestWithSession = Request::create('/');
        $requestWithSession->setSession($session);

        return array(
            'default' => array(
                Request::create('/'),
                array(),
                '/',
            ),
            'forced target path' => array(
                Request::create('/'),
                array('always_use_default_target_path' => true'default_target_path' => '/dashboard'),
                '/dashboard',
            ),
            'target path as query string' => array(
                Request::create('/?_target_path=/dashboard'),
                array(),
                '/dashboard',
            ),
            'target path name as query string is customized' => array(
                Request::create('/?_my_target_path=/dashboard'),
                array('target_path_parameter' => '_my_target_path'),
                '/dashboard',
            ),
            'target path name as query string is customized and nested' => array(
                Request::create('/?_target_path[value]=/dashboard'),
                array('target_path_parameter' => '_target_path[value]'),
                '/dashboard',
            ),
            'target path in session' => array(
                $requestWithSession,
                array(),
                '/admin/dashboard',
            ),
            'target path as referer' => array(
                Request::create('/''GET'array(), array(), array(), array('HTTP_REFERER' => 'http://localhost/dashboard')),
                array('use_referer' => true),
                '/dashboard',
            ),
            'target path as referer is ignored if not configured' => array(
                Request::create('/''GET'array(), array(), array(), array('HTTP_REFERER' => 'http://localhost/dashboard')),
                array(),
                '/',
            ),
            'target path as referer when referer not set' => array(
                Request::create('/'),
                array('use_referer' => true),
                '/',
            ),
            'target path as referer when referer is ?' => array(
                Request::create('/''GET'array(), array(), array(), array('HTTP_REFERER' => '?')),
                array('use_referer' => true),
                '/',
            ),
            'target path should be different than login URL' => array(
                Request::create('/''GET'array(), array(), array(), array('HTTP_REFERER' => 'http://localhost/login')),
                array('use_referer' => true'login_path' => '/login'),
                '/',
            ),
            'target path should be different than login URL (query string does not matter)' => array(
                Request::create('/''GET'array(), array(), array(), array('HTTP_REFERER' => 'http://localhost/login?t=1&p=2')),
                array('use_referer' => true'login_path' => '/login'),
                '/',
            ),
            'target path should be different than login URL (login_path as a route)' => array(
                Request::create('/''GET'array(), array(), array(), array('HTTP_REFERER' => 'http://localhost/login?t=1&p=2')),
                array('use_referer' => true'login_path' => 'login_route'),
                '/',
            ),
        );
    }
}