|
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,143 @@ 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->assertTrue($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->assertTrue($manager->decide($token, [1337], 'bar')); |
| 168 | + } |
| 169 | + |
| 170 | + public function testCacheableVotersWithMultipleAttributes() |
| 171 | + { |
| 172 | + $token = $this->createMock(TokenInterface::class); |
| 173 | + $voter = $this->getMockBuilder(CacheableVoterInterface::class)->getMockForAbstractClass(); |
| 174 | + $voter |
| 175 | + ->expects($this->exactly(2)) |
| 176 | + ->method('supportsAttribute') |
| 177 | + ->withConsecutive(['foo'], ['bar']) |
| 178 | + ->willReturnOnConsecutiveCalls(false, true); |
| 179 | + $voter |
| 180 | + ->expects($this->once()) |
| 181 | + ->method('supportsType') |
| 182 | + ->with('string') |
| 183 | + ->willReturn(true); |
| 184 | + $voter |
| 185 | + ->expects($this->once()) |
| 186 | + ->method('vote') |
| 187 | + ->with($token, 'bar', ['foo', 'bar']) |
| 188 | + ->willReturn(VoterInterface::ACCESS_GRANTED); |
| 189 | + |
| 190 | + $manager = new AccessDecisionManager([$voter]); |
| 191 | + $this->assertTrue($manager->decide($token, ['foo', 'bar'], 'bar', true)); |
| 192 | + } |
| 193 | + |
| 194 | + public function testCacheableVotersWithEmptyAttributes() |
| 195 | + { |
| 196 | + $token = $this->createMock(TokenInterface::class); |
| 197 | + $voter = $this->getMockBuilder(CacheableVoterInterface::class)->getMockForAbstractClass(); |
| 198 | + $voter |
| 199 | + ->expects($this->never()) |
| 200 | + ->method('supportsAttribute'); |
| 201 | + $voter |
| 202 | + ->expects($this->once()) |
| 203 | + ->method('supportsType') |
| 204 | + ->with('string') |
| 205 | + ->willReturn(true); |
| 206 | + $voter |
| 207 | + ->expects($this->once()) |
| 208 | + ->method('vote') |
| 209 | + ->with($token, 'bar', []) |
| 210 | + ->willReturn(VoterInterface::ACCESS_GRANTED); |
| 211 | + |
| 212 | + $manager = new AccessDecisionManager([$voter]); |
| 213 | + $this->assertTrue($manager->decide($token, [], 'bar')); |
| 214 | + } |
| 215 | + |
| 216 | + public function testCacheableVotersSupportsMethodsCalledOnce() |
| 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(true); |
| 225 | + $voter |
| 226 | + ->expects($this->once()) |
| 227 | + ->method('supportsType') |
| 228 | + ->with('string') |
| 229 | + ->willReturn(true); |
| 230 | + $voter |
| 231 | + ->expects($this->exactly(2)) |
| 232 | + ->method('vote') |
| 233 | + ->with($token, 'bar', ['foo']) |
| 234 | + ->willReturn(VoterInterface::ACCESS_GRANTED); |
| 235 | + |
| 236 | + $manager = new AccessDecisionManager([$voter]); |
| 237 | + $this->assertTrue($manager->decide($token, ['foo'], 'bar')); |
| 238 | + $this->assertTrue($manager->decide($token, ['foo'], 'bar')); |
| 239 | + } |
| 240 | + |
| 241 | + public function testCacheableVotersNotCalled() |
| 242 | + { |
| 243 | + $token = $this->createMock(TokenInterface::class); |
| 244 | + $voter = $this->getMockBuilder(CacheableVoterInterface::class)->getMockForAbstractClass(); |
| 245 | + $voter |
| 246 | + ->expects($this->once()) |
| 247 | + ->method('supportsAttribute') |
| 248 | + ->with('foo') |
| 249 | + ->willReturn(false); |
| 250 | + $voter |
| 251 | + ->expects($this->never()) |
| 252 | + ->method('supportsType'); |
| 253 | + $voter |
| 254 | + ->expects($this->never()) |
| 255 | + ->method('vote'); |
| 256 | + |
| 257 | + $manager = new AccessDecisionManager([$voter]); |
| 258 | + $this->assertFalse($manager->decide($token, ['foo'], 'bar')); |
| 259 | + } |
| 260 | + |
123 | 261 | protected function getVoters($grants, $denies, $abstains) |
124 | 262 | { |
125 | 263 | $voters = []; |
|
0 commit comments