summaryrefslogtreecommitdiffstats
path: root/Core/Tests/LegacySecurityContextTest.php
blob: f86fcad1c4508828b754fdb0a4c5ffa5bfe749f1 (plain)
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
115
116
117
118
119
120
<?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\Core\Tests;

use Symfony\Component\Security\Core\SecurityContext;

/**
 * @group legacy
 */
class LegacySecurityContextTest extends \PHPUnit_Framework_TestCase
{
    private $tokenStorage;
    private $authorizationChecker;
    private $securityContext;

    protected function setUp()
    {
        $this->tokenStorage = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface')->getMock();
        $this->authorizationChecker = $this->getMockBuilder('Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface')->getMock();
        $this->securityContext = new SecurityContext($this->tokenStorage, $this->authorizationChecker);
    }

    public function testGetTokenDelegation()
    {
        $token = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')->getMock();

        $this->tokenStorage
            ->expects($this->once())
            ->method('getToken')
            ->will($this->returnValue($token));

        $this->assertTrue($token === $this->securityContext->getToken());
    }

    public function testSetTokenDelegation()
    {
        $token = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')->getMock();

        $this->tokenStorage
            ->expects($this->once())
            ->method('setToken')
            ->with($token);

        $this->securityContext->setToken($token);
    }

    /**
     * @dataProvider isGrantedDelegationProvider
     */
    public function testIsGrantedDelegation($attributes, $object, $return)
    {
        $this->authorizationChecker
            ->expects($this->once())
            ->method('isGranted')
            ->with($attributes, $object)
            ->will($this->returnValue($return));

        $this->assertEquals($return, $this->securityContext->isGranted($attributes, $object));
    }

    public function isGrantedDelegationProvider()
    {
        return array(
            array(array(), new \stdClass(), true),
            array(array('henk'), new \stdClass(), false),
            array(null, new \stdClass(), false),
            array('henk', null, true),
            array(array(1), 'henk', true),
        );
    }

    /**
     * Test dedicated to check if the backwards compatibility is still working.
     */
    public function testOldConstructorSignature()
    {
        $authenticationManager = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface')->getMock();
        $accessDecisionManager = $this->getMockBuilder('Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface')->getMock();
        new SecurityContext($authenticationManager, $accessDecisionManager);
    }

    /**
     * @dataProvider oldConstructorSignatureFailuresProvider
     * @expectedException \BadMethodCallException
     */
    public function testOldConstructorSignatureFailures($first, $second)
    {
        new SecurityContext($first, $second);
    }

    public function oldConstructorSignatureFailuresProvider()
    {
        $tokenStorage = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface')->getMock();
        $authorizationChecker = $this->getMockBuilder('Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface')->getMock();
        $authenticationManager = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface')->getMock();
        $accessDecisionManager = $this->getMockBuilder('Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface')->getMock();

        return array(
            array(new \stdClass(), new \stdClass()),
            array($tokenStorage, $accessDecisionManager),
            array($accessDecisionManager, $tokenStorage),
            array($authorizationChecker, $accessDecisionManager),
            array($accessDecisionManager, $authorizationChecker),
            array($tokenStorage, $accessDecisionManager),
            array($authenticationManager, $authorizationChecker),
            array('henk', 'hans'),
            array(null, false),
            array(true, null),
        );
    }
}