|
14 | 14 | use PHPUnit\Framework\TestCase; |
15 | 15 | use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
16 | 16 | use Symfony\Component\Security\Core\Authorization\AccessDecisionManager; |
| 17 | +use Symfony\Component\Security\Core\Authorization\Voter\CacheableVoterInterface; |
17 | 18 | use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface; |
18 | 19 |
|
19 | 20 | class AccessDecisionManagerTest extends TestCase |
@@ -95,6 +96,143 @@ public function getStrategyTests() |
95 | 96 | ]; |
96 | 97 | } |
97 | 98 |
|
| 99 | + public function testCacheableVoters() |
| 100 | + { |
| 101 | + $token = $this->createMock(TokenInterface::class); |
| 102 | + $voter = $this->getMockBuilder(CacheableVoterInterface::class)->getMockForAbstractClass(); |
| 103 | + $voter |
| 104 | + ->expects($this->once()) |
| 105 | + ->method('supportsAttribute') |
| 106 | + ->with('foo') |
| 107 | + ->willReturn(true); |
| 108 | + $voter |
| 109 | + ->expects($this->once()) |
| 110 | + ->method('supportsType') |
| 111 | + ->with('string') |
| 112 | + ->willReturn(true); |
| 113 | + $voter |
| 114 | + ->expects($this->once()) |
| 115 | + ->method('vote') |
| 116 | + ->with($token, 'bar', ['foo']) |
| 117 | + ->willReturn(VoterInterface::ACCESS_GRANTED); |
| 118 | + |
| 119 | + $manager = new AccessDecisionManager([$voter]); |
| 120 | + $this->assertTrue($manager->decide($token, ['foo'], 'bar')); |
| 121 | + } |
| 122 | + |
| 123 | + public function testCacheableVotersIgnoresNonStringAttributes() |
| 124 | + { |
| 125 | + $token = $this->createMock(TokenInterface::class); |
| 126 | + $voter = $this->getMockBuilder(CacheableVoterInterface::class)->getMockForAbstractClass(); |
| 127 | + $voter |
| 128 | + ->expects($this->never()) |
| 129 | + ->method('supportsAttribute'); |
| 130 | + $voter |
| 131 | + ->expects($this->once()) |
| 132 | + ->method('supportsType') |
| 133 | + ->with('string') |
| 134 | + ->willReturn(true); |
| 135 | + $voter |
| 136 | + ->expects($this->once()) |
| 137 | + ->method('vote') |
| 138 | + ->with($token, 'bar', [1337]) |
| 139 | + ->willReturn(VoterInterface::ACCESS_GRANTED); |
| 140 | + |
| 141 | + $manager = new AccessDecisionManager([$voter]); |
| 142 | + $this->assertTrue($manager->decide($token, [1337], 'bar')); |
| 143 | + } |
| 144 | + |
| 145 | + public function testCacheableVotersWithMultipleAttributes() |
| 146 | + { |
| 147 | + $token = $this->createMock(TokenInterface::class); |
| 148 | + $voter = $this->getMockBuilder(CacheableVoterInterface::class)->getMockForAbstractClass(); |
| 149 | + $voter |
| 150 | + ->expects($this->exactly(2)) |
| 151 | + ->method('supportsAttribute') |
| 152 | + ->withConsecutive(['foo'], ['bar']) |
| 153 | + ->willReturnOnConsecutiveCalls(false, true); |
| 154 | + $voter |
| 155 | + ->expects($this->once()) |
| 156 | + ->method('supportsType') |
| 157 | + ->with('string') |
| 158 | + ->willReturn(true); |
| 159 | + $voter |
| 160 | + ->expects($this->once()) |
| 161 | + ->method('vote') |
| 162 | + ->with($token, 'bar', ['foo', 'bar']) |
| 163 | + ->willReturn(VoterInterface::ACCESS_GRANTED); |
| 164 | + |
| 165 | + $manager = new AccessDecisionManager([$voter]); |
| 166 | + $this->assertTrue($manager->decide($token, ['foo', 'bar'], 'bar', true)); |
| 167 | + } |
| 168 | + |
| 169 | + public function testCacheableVotersWithEmptyAttributes() |
| 170 | + { |
| 171 | + $token = $this->createMock(TokenInterface::class); |
| 172 | + $voter = $this->getMockBuilder(CacheableVoterInterface::class)->getMockForAbstractClass(); |
| 173 | + $voter |
| 174 | + ->expects($this->never()) |
| 175 | + ->method('supportsAttribute'); |
| 176 | + $voter |
| 177 | + ->expects($this->once()) |
| 178 | + ->method('supportsType') |
| 179 | + ->with('string') |
| 180 | + ->willReturn(true); |
| 181 | + $voter |
| 182 | + ->expects($this->once()) |
| 183 | + ->method('vote') |
| 184 | + ->with($token, 'bar', []) |
| 185 | + ->willReturn(VoterInterface::ACCESS_GRANTED); |
| 186 | + |
| 187 | + $manager = new AccessDecisionManager([$voter]); |
| 188 | + $this->assertTrue($manager->decide($token, [], 'bar')); |
| 189 | + } |
| 190 | + |
| 191 | + public function testCacheableVotersSupportsMethodsCalledOnce() |
| 192 | + { |
| 193 | + $token = $this->createMock(TokenInterface::class); |
| 194 | + $voter = $this->getMockBuilder(CacheableVoterInterface::class)->getMockForAbstractClass(); |
| 195 | + $voter |
| 196 | + ->expects($this->once()) |
| 197 | + ->method('supportsAttribute') |
| 198 | + ->with('foo') |
| 199 | + ->willReturn(true); |
| 200 | + $voter |
| 201 | + ->expects($this->once()) |
| 202 | + ->method('supportsType') |
| 203 | + ->with('string') |
| 204 | + ->willReturn(true); |
| 205 | + $voter |
| 206 | + ->expects($this->exactly(2)) |
| 207 | + ->method('vote') |
| 208 | + ->with($token, 'bar', ['foo']) |
| 209 | + ->willReturn(VoterInterface::ACCESS_GRANTED); |
| 210 | + |
| 211 | + $manager = new AccessDecisionManager([$voter]); |
| 212 | + $this->assertTrue($manager->decide($token, ['foo'], 'bar')); |
| 213 | + $this->assertTrue($manager->decide($token, ['foo'], 'bar')); |
| 214 | + } |
| 215 | + |
| 216 | + public function testCacheableVotersNotCalled() |
| 217 | + { |
| 218 | + $token = $this->createMock(TokenInterface::class); |
| 219 | + $voter = $this->getMockBuilder(CacheableVoterInterface::class)->getMockForAbstractClass(); |
| 220 | + $voter |
| 221 | + ->expects($this->once()) |
| 222 | + ->method('supportsAttribute') |
| 223 | + ->with('foo') |
| 224 | + ->willReturn(false); |
| 225 | + $voter |
| 226 | + ->expects($this->never()) |
| 227 | + ->method('supportsType'); |
| 228 | + $voter |
| 229 | + ->expects($this->never()) |
| 230 | + ->method('vote'); |
| 231 | + |
| 232 | + $manager = new AccessDecisionManager([$voter]); |
| 233 | + $this->assertFalse($manager->decide($token, ['foo'], 'bar')); |
| 234 | + } |
| 235 | + |
98 | 236 | protected function getVoters($grants, $denies, $abstains) |
99 | 237 | { |
100 | 238 | $voters = []; |
|
0 commit comments