|
15 | 15 | use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
|
16 | 16 | use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
|
17 | 17 | use Symfony\Component\Security\Core\Authorization\AccessDecisionManager;
|
| 18 | +use Symfony\Component\Security\Core\Authorization\Voter\CacheableVoterInterface; |
18 | 19 | use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
|
19 | 20 |
|
20 | 21 | class AccessDecisionManagerTest extends TestCase
|
@@ -120,6 +121,141 @@ public function provideStrategies()
|
120 | 121 | yield [AccessDecisionManager::STRATEGY_PRIORITY];
|
121 | 122 | }
|
122 | 123 |
|
| 124 | + public function testCacheableVoters() |
| 125 | + { |
| 126 | + $token = $this->createMock(TokenInterface::class); |
| 127 | + $voter = $this->getMockBuilder(CacheableVoterInterface::class)->getMockForAbstractClass(); |
| 128 | + $voter |
| 129 | + ->expects($this->once()) |
| 130 | + ->method('supportsAttribute') |
| 131 | + ->with('foo') |
| 132 | + ->willReturn(true); |
| 133 | + $voter |
| 134 | + ->expects($this->once()) |
| 135 | + ->method('supportsType') |
| 136 | + ->with('string') |
| 137 | + ->willReturn(true); |
| 138 | + $voter |
| 139 | + ->expects($this->once()) |
| 140 | + ->method('vote') |
| 141 | + ->with($token, 'bar', ['foo']) |
| 142 | + ->willReturn(VoterInterface::ACCESS_GRANTED); |
| 143 | + |
| 144 | + $manager = new AccessDecisionManager([$voter]); |
| 145 | + $this->assertSame(true, $manager->decide($token, ['foo'], 'bar')); |
| 146 | + } |
| 147 | + |
| 148 | + public function testCacheableVotersIgnoresNonStringAttributes() |
| 149 | + { |
| 150 | + $token = $this->createMock(TokenInterface::class); |
| 151 | + $voter = $this->getMockBuilder(CacheableVoterInterface::class)->getMockForAbstractClass(); |
| 152 | + $voter |
| 153 | + ->expects($this->never()) |
| 154 | + ->method('supportsAttribute'); |
| 155 | + $voter |
| 156 | + ->expects($this->once()) |
| 157 | + ->method('supportsType') |
| 158 | + ->with('string') |
| 159 | + ->willReturn(true); |
| 160 | + $voter |
| 161 | + ->expects($this->once()) |
| 162 | + ->method('vote') |
| 163 | + ->with($token, 'bar', [1337]) |
| 164 | + ->willReturn(VoterInterface::ACCESS_GRANTED); |
| 165 | + |
| 166 | + $manager = new AccessDecisionManager([$voter]); |
| 167 | + $this->assertSame(true, $manager->decide($token, [1337], 'bar')); |
| 168 | + } |
| 169 | + |
| 170 | + public function testCacheableVotersIgnoresMultipleAttributes() |
| 171 | + { |
| 172 | + $token = $this->createMock(TokenInterface::class); |
| 173 | + $voter = $this->getMockBuilder(CacheableVoterInterface::class)->getMockForAbstractClass(); |
| 174 | + $voter |
| 175 | + ->expects($this->never()) |
| 176 | + ->method('supportsAttribute'); |
| 177 | + $voter |
| 178 | + ->expects($this->once()) |
| 179 | + ->method('supportsType') |
| 180 | + ->with('string') |
| 181 | + ->willReturn(true); |
| 182 | + $voter |
| 183 | + ->expects($this->once()) |
| 184 | + ->method('vote') |
| 185 | + ->with($token, 'bar', ['foo', 'bar']) |
| 186 | + ->willReturn(VoterInterface::ACCESS_GRANTED); |
| 187 | + |
| 188 | + $manager = new AccessDecisionManager([$voter]); |
| 189 | + $this->assertSame(true, $manager->decide($token, ['foo', 'bar'], 'bar', true)); |
| 190 | + } |
| 191 | + |
| 192 | + public function testCacheableVotersIgnoresEmptyAttributes() |
| 193 | + { |
| 194 | + $token = $this->createMock(TokenInterface::class); |
| 195 | + $voter = $this->getMockBuilder(CacheableVoterInterface::class)->getMockForAbstractClass(); |
| 196 | + $voter |
| 197 | + ->expects($this->never()) |
| 198 | + ->method('supportsAttribute'); |
| 199 | + $voter |
| 200 | + ->expects($this->once()) |
| 201 | + ->method('supportsType') |
| 202 | + ->with('string') |
| 203 | + ->willReturn(true); |
| 204 | + $voter |
| 205 | + ->expects($this->once()) |
| 206 | + ->method('vote') |
| 207 | + ->with($token, 'bar', []) |
| 208 | + ->willReturn(VoterInterface::ACCESS_GRANTED); |
| 209 | + |
| 210 | + $manager = new AccessDecisionManager([$voter]); |
| 211 | + $this->assertSame(true, $manager->decide($token, [], 'bar')); |
| 212 | + } |
| 213 | + |
| 214 | + public function testCacheableVotersSupportsMethodsCalledOnce() |
| 215 | + { |
| 216 | + $token = $this->createMock(TokenInterface::class); |
| 217 | + $voter = $this->getMockBuilder(CacheableVoterInterface::class)->getMockForAbstractClass(); |
| 218 | + $voter |
| 219 | + ->expects($this->once()) |
| 220 | + ->method('supportsAttribute') |
| 221 | + ->with('foo') |
| 222 | + ->willReturn(true); |
| 223 | + $voter |
| 224 | + ->expects($this->once()) |
| 225 | + ->method('supportsType') |
| 226 | + ->with('string') |
| 227 | + ->willReturn(true); |
| 228 | + $voter |
| 229 | + ->expects($this->exactly(2)) |
| 230 | + ->method('vote') |
| 231 | + ->with($token, 'bar', ['foo']) |
| 232 | + ->willReturn(VoterInterface::ACCESS_GRANTED); |
| 233 | + |
| 234 | + $manager = new AccessDecisionManager([$voter]); |
| 235 | + $this->assertSame(true, $manager->decide($token, ['foo'], 'bar')); |
| 236 | + $this->assertSame(true, $manager->decide($token, ['foo'], 'bar')); |
| 237 | + } |
| 238 | + |
| 239 | + public function testCacheableVotersNotCalled() |
| 240 | + { |
| 241 | + $token = $this->createMock(TokenInterface::class); |
| 242 | + $voter = $this->getMockBuilder(CacheableVoterInterface::class)->getMockForAbstractClass(); |
| 243 | + $voter |
| 244 | + ->expects($this->once()) |
| 245 | + ->method('supportsAttribute') |
| 246 | + ->with('foo') |
| 247 | + ->willReturn(false); |
| 248 | + $voter |
| 249 | + ->expects($this->never()) |
| 250 | + ->method('supportsType'); |
| 251 | + $voter |
| 252 | + ->expects($this->never()) |
| 253 | + ->method('vote'); |
| 254 | + |
| 255 | + $manager = new AccessDecisionManager([$voter]); |
| 256 | + $this->assertSame(false, $manager->decide($token, ['foo'], 'bar')); |
| 257 | + } |
| 258 | + |
123 | 259 | protected function getVoters($grants, $denies, $abstains)
|
124 | 260 | {
|
125 | 261 | $voters = [];
|
|
0 commit comments