Вход Регистрация
Файл: symfony-2.7/src/Symfony/Component/Security/Http/Tests/Firewall/ContextListenerTest.php
Строк: 545
<?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 SymfonyComponentSecurityHttpTestsFirewall;

use 
SymfonyComponentHttpFoundationRequest;
use 
SymfonyComponentHttpFoundationResponse;
use 
SymfonyComponentHttpFoundationSessionSession;
use 
SymfonyComponentHttpFoundationSessionStorageMockArraySessionStorage;
use 
SymfonyComponentHttpKernelEventFilterResponseEvent;
use 
SymfonyComponentHttpKernelHttpKernelInterface;
use 
SymfonyComponentHttpKernelKernelEvents;
use 
SymfonyComponentSecurityCoreAuthenticationTokenStorageTokenStorage;
use 
SymfonyComponentSecurityCoreAuthenticationTokenUsernamePasswordToken;
use 
SymfonyComponentSecurityHttpFirewallContextListener;

class 
ContextListenerTest extends PHPUnit_Framework_TestCase
{
    
/**
     * @expectedException InvalidArgumentException
     * @expectedExceptionMessage $contextKey must not be empty
     */
    
public function testItRequiresContextKey()
    {
        new 
ContextListener(
            
$this->getMock('SymfonyComponentSecurityCoreAuthenticationTokenStorageTokenStorageInterface'),
            array(),
            
''
        
);
    }

    
/**
     * @expectedException InvalidArgumentException
     * @expectedExceptionMessage User provider "stdClass" must implement "SymfonyComponentSecurityCoreUserUserProviderInterface
     */
    
public function testUserProvidersNeedToImplementAnInterface()
    {
        new 
ContextListener(
            
$this->getMock('SymfonyComponentSecurityCoreAuthenticationTokenStorageTokenStorageInterface'),
            array(new 
stdClass()),
            
'key123'
        
);
    }

    public function 
testOnKernelResponseWillAddSession()
    {
        
$session $this->runSessionOnKernelResponse(
            new 
UsernamePasswordToken('test1''pass1''phpunit'),
            
null
        
);

        
$token unserialize($session->get('_security_session'));
        
$this->assertInstanceOf('SymfonyComponentSecurityCoreAuthenticationTokenUsernamePasswordToken'$token);
        
$this->assertEquals('test1'$token->getUsername());
    }

    public function 
testOnKernelResponseWillReplaceSession()
    {
        
$session $this->runSessionOnKernelResponse(
            new 
UsernamePasswordToken('test1''pass1''phpunit'),
            
'C:10:"serialized"'
        
);

        
$token unserialize($session->get('_security_session'));
        
$this->assertInstanceOf('SymfonyComponentSecurityCoreAuthenticationTokenUsernamePasswordToken'$token);
        
$this->assertEquals('test1'$token->getUsername());
    }

    public function 
testOnKernelResponseWillRemoveSession()
    {
        
$session $this->runSessionOnKernelResponse(
            
null,
            
'C:10:"serialized"'
        
);

        
$this->assertFalse($session->has('_security_session'));
    }

    public function 
testOnKernelResponseWithoutSession()
    {
        
$tokenStorage = new TokenStorage();
        
$tokenStorage->setToken(new UsernamePasswordToken('test1''pass1''phpunit'));
        
$request = new Request();
        
$session = new Session(new MockArraySessionStorage());
        
$request->setSession($session);

        
$event = new FilterResponseEvent(
            
$this->getMock('SymfonyComponentHttpKernelHttpKernelInterface'),
            
$request,
            
HttpKernelInterface::MASTER_REQUEST,
            new 
Response()
        );

        
$listener = new ContextListener($tokenStorage, array(), 'session');
        
$listener->onKernelResponse($event);

        
$this->assertTrue($session->isStarted());
    }

    public function 
testOnKernelResponseWithoutSessionNorToken()
    {
        
$request = new Request();
        
$session = new Session(new MockArraySessionStorage());
        
$request->setSession($session);

        
$event = new FilterResponseEvent(
            
$this->getMock('SymfonyComponentHttpKernelHttpKernelInterface'),
            
$request,
            
HttpKernelInterface::MASTER_REQUEST,
            new 
Response()
        );

        
$listener = new ContextListener(new TokenStorage(), array(), 'session');
        
$listener->onKernelResponse($event);

        
$this->assertFalse($session->isStarted());
    }

    
/**
     * @dataProvider provideInvalidToken
     */
    
public function testInvalidTokenInSession($token)
    {
        
$tokenStorage $this->getMock('SymfonyComponentSecurityCoreAuthenticationTokenStorageTokenStorageInterface');
        
$event $this->getMockBuilder('SymfonyComponentHttpKernelEventGetResponseEvent')
            ->
disableOriginalConstructor()
            ->
getMock();
        
$request $this->getMock('SymfonyComponentHttpFoundationRequest');
        
$session $this->getMock('SymfonyComponentHttpFoundationSessionSessionInterface');

        
$event->expects($this->any())
            ->
method('getRequest')
            ->
will($this->returnValue($request));
        
$request->expects($this->any())
            ->
method('hasPreviousSession')
            ->
will($this->returnValue(true));
        
$request->expects($this->any())
            ->
method('getSession')
            ->
will($this->returnValue($session));
        
$session->expects($this->any())
            ->
method('get')
            ->
with('_security_key123')
            ->
will($this->returnValue($token));
        
$tokenStorage->expects($this->once())
            ->
method('setToken')
            ->
with(null);

        
$listener = new ContextListener($tokenStorage, array(), 'key123');
        
$listener->handle($event);
    }

    public function 
provideInvalidToken()
    {
        return array(
            array(
serialize(new __PHP_Incomplete_Class())),
            array(
serialize(null)),
            array(
null),
        );
    }

    public function 
testHandleAddsKernelResponseListener()
    {
        
$tokenStorage $this->getMock('SymfonyComponentSecurityCoreAuthenticationTokenStorageTokenStorageInterface');
        
$dispatcher $this->getMock('SymfonyComponentEventDispatcherEventDispatcherInterface');
        
$event $this->getMockBuilder('SymfonyComponentHttpKernelEventGetResponseEvent')
            ->
disableOriginalConstructor()
            ->
getMock();

        
$listener = new ContextListener($tokenStorage, array(), 'key123'null$dispatcher);

        
$event->expects($this->any())
            ->
method('isMasterRequest')
            ->
will($this->returnValue(true));
        
$event->expects($this->any())
            ->
method('getRequest')
            ->
will($this->returnValue($this->getMock('SymfonyComponentHttpFoundationRequest')));

        
$dispatcher->expects($this->once())
            ->
method('addListener')
            ->
with(KernelEvents::RESPONSE, array($listener'onKernelResponse'));

        
$listener->handle($event);
    }

    public function 
testHandleRemovesTokenIfNoPreviousSessionWasFound()
    {
        
$request $this->getMock('SymfonyComponentHttpFoundationRequest');
        
$request->expects($this->any())->method('hasPreviousSession')->will($this->returnValue(false));

        
$event $this->getMockBuilder('SymfonyComponentHttpKernelEventGetResponseEvent')
            ->
disableOriginalConstructor()
            ->
getMock();
        
$event->expects($this->any())->method('getRequest')->will($this->returnValue($request));

        
$tokenStorage $this->getMock('SymfonyComponentSecurityCoreAuthenticationTokenStorageTokenStorageInterface');
        
$tokenStorage->expects($this->once())->method('setToken')->with(null);

        
$listener = new ContextListener($tokenStorage, array(), 'key123');
        
$listener->handle($event);
    }

    protected function 
runSessionOnKernelResponse($newToken$original null)
    {
        
$session = new Session(new MockArraySessionStorage());

        if (
$original !== null) {
            
$session->set('_security_session'$original);
        }

        
$tokenStorage = new TokenStorage();
        
$tokenStorage->setToken($newToken);

        
$request = new Request();
        
$request->setSession($session);
        
$request->cookies->set('MOCKSESSID'true);

        
$event = new FilterResponseEvent(
            
$this->getMock('SymfonyComponentHttpKernelHttpKernelInterface'),
            
$request,
            
HttpKernelInterface::MASTER_REQUEST,
            new 
Response()
        );

        
$listener = new ContextListener($tokenStorage, array(), 'session');
        
$listener->onKernelResponse($event);

        return 
$session;
    }
}
Онлайн: 1
Реклама