|
13 | 13 |
|
14 | 14 | use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
|
15 | 15 | use Symfony\Component\Security\Core\Authorization\Voter\AbstractVoter;
|
| 16 | +use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface; |
16 | 17 |
|
17 |
| -/** |
18 |
| - * @author Roman Marintšenko <inoryy@gmail.com> |
19 |
| - */ |
20 | 18 | class AbstractVoterTest extends \PHPUnit_Framework_TestCase
|
21 | 19 | {
|
22 |
| - private $token; |
| 20 | + protected $voter; |
| 21 | + protected $object; |
| 22 | + protected $token; |
23 | 23 |
|
24 | 24 | protected function setUp()
|
25 | 25 | {
|
26 |
| - $tokenMock = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface'); |
27 |
| - $tokenMock |
28 |
| - ->expects($this->any()) |
29 |
| - ->method('getUser') |
30 |
| - ->will($this->returnValue('user')); |
31 |
| - |
32 |
| - $this->token = $tokenMock; |
33 |
| - } |
34 |
| - |
35 |
| - /** |
36 |
| - * @dataProvider getData |
37 |
| - */ |
38 |
| - public function testVote($expectedVote, $object, $attributes, $message) |
39 |
| - { |
40 |
| - $voter = new VoterFixture(); |
41 |
| - |
42 |
| - $this->assertEquals($expectedVote, $voter->vote($this->token, $object, $attributes), $message); |
43 |
| - } |
44 |
| - |
45 |
| - /** |
46 |
| - * @dataProvider getData |
47 |
| - * @group legacy |
48 |
| - */ |
49 |
| - public function testVoteUsingDeprecatedIsGranted($expectedVote, $object, $attributes, $message) |
50 |
| - { |
51 |
| - $voter = new DeprecatedVoterFixture(); |
52 |
| - |
53 |
| - $this->assertEquals($expectedVote, $voter->vote($this->token, $object, $attributes), $message); |
54 |
| - } |
55 |
| - |
56 |
| - /** |
57 |
| - * @group legacy |
58 |
| - * @expectedException \BadMethodCallException |
59 |
| - */ |
60 |
| - public function testNoOverriddenMethodsThrowsException() |
61 |
| - { |
62 |
| - $voter = new DeprecatedVoterNothingImplementedFixture(); |
63 |
| - $voter->vote($this->token, new ObjectFixture(), array('foo')); |
| 26 | + $this->voter = new AbstractVoterTest_Voter(); |
| 27 | + $this->object = $this->getMock('AbstractVoterTest_Object'); |
| 28 | + $this->token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface'); |
64 | 29 | }
|
65 | 30 |
|
66 |
| - public function getData() |
| 31 | + public function testAttributeAndClassSupported() |
67 | 32 | {
|
68 |
| - return array( |
69 |
| - array(AbstractVoter::ACCESS_ABSTAIN, null, array(), 'ACCESS_ABSTAIN for null objects'), |
70 |
| - array(AbstractVoter::ACCESS_ABSTAIN, new UnsupportedObjectFixture(), array(), 'ACCESS_ABSTAIN for objects with unsupported class'), |
71 |
| - array(AbstractVoter::ACCESS_ABSTAIN, new ObjectFixture(), array(), 'ACCESS_ABSTAIN for no attributes'), |
72 |
| - array(AbstractVoter::ACCESS_ABSTAIN, new ObjectFixture(), array('foobar'), 'ACCESS_ABSTAIN for unsupported attributes'), |
73 |
| - array(AbstractVoter::ACCESS_GRANTED, new ObjectFixture(), array('foo'), 'ACCESS_GRANTED if attribute grants access'), |
74 |
| - array(AbstractVoter::ACCESS_GRANTED, new ObjectFixture(), array('bar', 'foo'), 'ACCESS_GRANTED if *at least one* attribute grants access'), |
75 |
| - array(AbstractVoter::ACCESS_GRANTED, new ObjectFixture(), array('foobar', 'foo'), 'ACCESS_GRANTED if *at least one* attribute grants access'), |
76 |
| - array(AbstractVoter::ACCESS_DENIED, new ObjectFixture(), array('bar', 'baz'), 'ACCESS_DENIED for if no attribute grants access'), |
77 |
| - ); |
| 33 | + $this->assertEquals(VoterInterface::ACCESS_GRANTED, $this->voter->vote($this->token, $this->object, array('EDIT')), 'ACCESS_GRANTED if attribute grants access'); |
| 34 | + $this->assertEquals(VoterInterface::ACCESS_DENIED, $this->voter->vote($this->token, $this->object, array('CREATE')), 'ACESS_DENIED if attribute denies access'); |
78 | 35 | }
|
79 |
| -} |
80 | 36 |
|
81 |
| -class VoterFixture extends AbstractVoter |
82 |
| -{ |
83 |
| - protected function getSupportedClasses() |
| 37 | + public function testOneAttributeSupported() |
84 | 38 | {
|
85 |
| - return array( |
86 |
| - 'Symfony\Component\Security\Core\Tests\Authorization\Voter\ObjectFixture', |
87 |
| - ); |
| 39 | + $this->assertEquals(VoterInterface::ACCESS_GRANTED, $this->voter->vote($this->token, $this->object, array('DELETE', 'EDIT')), 'ACCESS_GRANTED if supported attribute grants access'); |
| 40 | + $this->assertEquals(VoterInterface::ACCESS_DENIED, $this->voter->vote($this->token, $this->object, array('DELETE', 'CREATE')), 'ACCESS_DENIED if supported attribute denies access'); |
88 | 41 | }
|
89 | 42 |
|
90 |
| - protected function getSupportedAttributes() |
| 43 | + public function testOneAttributeGrantsAccess() |
91 | 44 | {
|
92 |
| - return array('foo', 'bar', 'baz'); |
| 45 | + $this->assertEquals(VoterInterface::ACCESS_GRANTED, $this->voter->vote($this->token, $this->object, array('CREATE', 'EDIT')), 'ACCESS_GRANTED'); |
93 | 46 | }
|
94 | 47 |
|
95 |
| - protected function voteOnAttribute($attribute, $object, TokenInterface $token) |
| 48 | + public function testNoAttributeSupported() |
96 | 49 | {
|
97 |
| - return $attribute === 'foo'; |
| 50 | + $this->assertEquals(VoterInterface::ACCESS_ABSTAIN, $this->voter->vote($this->token, $this->object, array('DELETE')), 'ACCESS_ABSTAIN'); |
98 | 51 | }
|
99 |
| -} |
100 | 52 |
|
101 |
| -class DeprecatedVoterFixture extends AbstractVoter |
102 |
| -{ |
103 |
| - protected function getSupportedClasses() |
| 53 | + public function testClassNotSupported() |
104 | 54 | {
|
105 |
| - return array( |
106 |
| - 'Symfony\Component\Security\Core\Tests\Authorization\Voter\ObjectFixture', |
107 |
| - ); |
| 55 | + $this->assertEquals(VoterInterface::ACCESS_ABSTAIN, $this->voter->vote($this->token, $this->getMock('AbstractVoterTest_Object1'), array('EDIT')), 'ACCESS_ABSTAIN'); |
108 | 56 | }
|
109 | 57 |
|
110 |
| - protected function getSupportedAttributes() |
| 58 | + public function testNullObject() |
111 | 59 | {
|
112 |
| - return array('foo', 'bar', 'baz'); |
| 60 | + $this->assertEquals(VoterInterface::ACCESS_ABSTAIN, $this->voter->vote($this->token, null, array('EDIT')), 'ACCESS_ABSTAIN'); |
113 | 61 | }
|
114 | 62 |
|
115 |
| - protected function isGranted($attribute, $object, $user = null) |
| 63 | + public function testNoAttributes() |
116 | 64 | {
|
117 |
| - return $attribute === 'foo'; |
| 65 | + $this->assertEquals(VoterInterface::ACCESS_ABSTAIN, $this->voter->vote($this->token, $this->object, array()), 'ACCESS_ABSTAIN'); |
118 | 66 | }
|
119 | 67 | }
|
120 | 68 |
|
121 |
| -class DeprecatedVoterNothingImplementedFixture extends AbstractVoter |
| 69 | +class AbstractVoterTest_Voter extends AbstractVoter |
122 | 70 | {
|
123 |
| - protected function getSupportedClasses() |
| 71 | + protected function voteOnAttribute($attribute, $object, TokenInterface $token) |
124 | 72 | {
|
125 |
| - return array( |
126 |
| - 'Symfony\Component\Security\Core\Tests\Authorization\Voter\ObjectFixture', |
127 |
| - ); |
| 73 | + return 'EDIT' === $attribute; |
128 | 74 | }
|
129 | 75 |
|
130 |
| - protected function getSupportedAttributes() |
| 76 | + protected function supports($attribute, $class) |
131 | 77 | {
|
132 |
| - return array('foo', 'bar', 'baz'); |
| 78 | + return $this->isClassInstanceOf($class, 'AbstractVoterTest_Object') |
| 79 | + && in_array($attribute, array('EDIT', 'CREATE')); |
133 | 80 | }
|
134 |
| - |
135 |
| - // this is a bad voter that hasn't overridden isGranted or voteOnAttribute |
136 |
| -} |
137 |
| - |
138 |
| -class ObjectFixture |
139 |
| -{ |
140 |
| -} |
141 |
| - |
142 |
| -class UnsupportedObjectFixture |
143 |
| -{ |
144 | 81 | }
|
0 commit comments