|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\Security\Core\Tests\Authorization\Voter; |
| 13 | + |
| 14 | +use Symfony\Component\Security\Core\Authorization\Voter\ExpressionVoter; |
| 15 | +use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface; |
| 16 | +use Symfony\Component\Security\Core\Role\Role; |
| 17 | + |
| 18 | +class ExpressionVoterTest extends \PHPUnit_Framework_TestCase |
| 19 | +{ |
| 20 | + public function testSupportsAttribute() |
| 21 | + { |
| 22 | + $expression = $this->createExpression(); |
| 23 | + $expressionLanguage = $this->getMock('Symfony\Component\Security\Core\Authorization\ExpressionLanguage'); |
| 24 | + $voter = new ExpressionVoter($expressionLanguage, $this->createTrustResolver(), $this->createRoleHierarchy()); |
| 25 | + |
| 26 | + $this->assertTrue($voter->supportsAttribute($expression)); |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * @dataProvider getVoteTests |
| 31 | + */ |
| 32 | + public function testVote($roles, $attributes, $expected, $tokenExpectsGetRoles = true, $expressionLanguageExpectsEvaluate = true) |
| 33 | + { |
| 34 | + $voter = new ExpressionVoter($this->createExpressionLanguage($expressionLanguageExpectsEvaluate), $this->createTrustResolver()); |
| 35 | + |
| 36 | + $this->assertSame($expected, $voter->vote($this->getToken($roles, $tokenExpectsGetRoles), null, $attributes)); |
| 37 | + } |
| 38 | + |
| 39 | + public function getVoteTests() |
| 40 | + { |
| 41 | + return array( |
| 42 | + array(array(), array(), VoterInterface::ACCESS_ABSTAIN, false, false), |
| 43 | + array(array(), array('FOO'), VoterInterface::ACCESS_ABSTAIN, false, false), |
| 44 | + |
| 45 | + array(array(), array($this->createExpression()), VoterInterface::ACCESS_DENIED, true, false), |
| 46 | + |
| 47 | + array(array('ROLE_FOO'), array($this->createExpression(), $this->createExpression()), VoterInterface::ACCESS_GRANTED), |
| 48 | + array(array('ROLE_BAR', 'ROLE_FOO'), array($this->createExpression()), VoterInterface::ACCESS_GRANTED), |
| 49 | + ); |
| 50 | + } |
| 51 | + |
| 52 | + protected function getToken(array $roles, $tokenExpectsGetRoles = true) |
| 53 | + { |
| 54 | + foreach ($roles as $i => $role) { |
| 55 | + $roles[$i] = new Role($role); |
| 56 | + } |
| 57 | + $token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface'); |
| 58 | + |
| 59 | + if ($tokenExpectsGetRoles) { |
| 60 | + $token->expects($this->once()) |
| 61 | + ->method('getRoles') |
| 62 | + ->will($this->returnValue($roles)); |
| 63 | + } |
| 64 | + |
| 65 | + return $token; |
| 66 | + } |
| 67 | + |
| 68 | + protected function createExpressionLanguage($expressionLanguageExpectsEvaluate = true) |
| 69 | + { |
| 70 | + $mock = $this->getMock('Symfony\Component\Security\Core\Authorization\ExpressionLanguage'); |
| 71 | + |
| 72 | + if ($expressionLanguageExpectsEvaluate) { |
| 73 | + $mock->expects($this->once()) |
| 74 | + ->method('evaluate') |
| 75 | + ->will($this->returnValue(true)); |
| 76 | + } |
| 77 | + |
| 78 | + return $mock; |
| 79 | + } |
| 80 | + |
| 81 | + protected function createTrustResolver() |
| 82 | + { |
| 83 | + return $this->getMock('Symfony\Component\Security\Core\Authentication\AuthenticationTrustResolverInterface'); |
| 84 | + } |
| 85 | + |
| 86 | + protected function createRoleHierarchy() |
| 87 | + { |
| 88 | + return $this->getMock('Symfony\Component\Security\Core\Role\RoleHierarchyInterface'); |
| 89 | + } |
| 90 | + |
| 91 | + protected function createExpression() |
| 92 | + { |
| 93 | + return $this->getMockBuilder('Symfony\Component\ExpressionLanguage\Expression') |
| 94 | + ->disableOriginalConstructor() |
| 95 | + ->getMock(); |
| 96 | + } |
| 97 | +} |
0 commit comments