* * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Symfony\Component\Security\Core\Tests\Authorization; use PHPUnit\Framework\TestCase; use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage; use Symfony\Component\Security\Core\Authorization\AuthorizationChecker; class AuthorizationCheckerTest extends TestCase { private $authenticationManager; private $accessDecisionManager; private $authorizationChecker; private $tokenStorage; protected function setUp() { $this->authenticationManager = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface')->getMock(); $this->accessDecisionManager = $this->getMockBuilder('Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface')->getMock(); $this->tokenStorage = new TokenStorage(); $this->authorizationChecker = new AuthorizationChecker( $this->tokenStorage, $this->authenticationManager, $this->accessDecisionManager ); } public function testVoteAuthenticatesTokenIfNecessary() { $token = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')->getMock(); $this->tokenStorage->setToken($token); $newToken = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')->getMock(); $this->authenticationManager ->expects($this->once()) ->method('authenticate') ->with($this->equalTo($token)) ->will($this->returnValue($newToken)); // default with() isn't a strict check $tokenComparison = function ($value) use ($newToken) { // make sure that the new token is used in "decide()" and not the old one return $value === $newToken; }; $this->accessDecisionManager ->expects($this->once()) ->method('decide') ->with($this->callback($tokenComparison)) ->will($this->returnValue(true)); // first run the token has not been re-authenticated yet, after isGranted is called, it should be equal $this->assertNotSame($newToken, $this->tokenStorage->getToken()); $this->assertTrue($this->authorizationChecker->isGranted('foo')); $this->assertSame($newToken, $this->tokenStorage->getToken()); } /** * @expectedException \Symfony\Component\Security\Core\Exception\AuthenticationCredentialsNotFoundException */ public function testVoteWithoutAuthenticationToken() { $this->authorizationChecker->isGranted('ROLE_FOO'); } /** * @dataProvider isGrantedProvider */ public function testIsGranted($decide) { $token = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')->getMock(); $token ->expects($this->once()) ->method('isAuthenticated') ->will($this->returnValue(true)); $this->accessDecisionManager ->expects($this->once()) ->method('decide') ->will($this->returnValue($decide)); $this->tokenStorage->setToken($token); $this->assertSame($decide, $this->authorizationChecker->isGranted('ROLE_FOO')); } public function isGrantedProvider() { return array(array(true), array(false)); } } __halt_compiler();----SIGNATURE:----S22jkaKp8osDjPHUcCTJasp4anKN8wLaJMD3yHbxbH4eFcHB7kAapbaWORjNj5HwT7RjetBhgXjBFxsOyZsnaE7XC5m5Cv4X5XopZk4d9XGs7uhdDmelk2ciDeDLR1RY3PYwvQc/dRN5PGkqSLhFn/X8kieeA6XYo+9uzk/XW+DTVd1s2Q1ZGfwfaWLOhkHY7qPS7rrtFUTPnP+uKJqHTiH2q6qoBu9UcKe9WbvloD0DRF989nnrErg19e7yb4z9SalZDg1uHizduehYpmvRd3/9T9rILBLQ5U32OUdqFQVxu022KB5TnizmpOwpKP5+KMeUyZZAB7YHmYYIsvxbCtxWPRk+rHJpFD+dqfWb+1+ihchfi4M2KQiYZ1RfJqpjqmE2HRMoPjxtxKvJAUINqOBWPu/fd1mcKApejjz+f7f6SlKVuLlQh1GioUY6CBHuO5Eku/2eCgN+EoMQTdwOA1AfYszOUi+oxD9X8M+seiIwz1+L9QigqZbEvjv1F/U7Y2mMIaoUDFOvsYnlW8dT9uWShImXLRMxqQuIzvAjgagsU2clvQj71FS4tAdXjE2TDtfwbfuvopBxogmelkJV7lccvwrBrCPe/Affqh/saSIWtDKyrwbSi40oN2N5s2YNCv5xcoZQ5Qp3Sam9Ex6rvETKeSlkPR+ieemaZT9x/6o=----ATTACHMENT:----ODI5MTc3Njc0MjM0NzcyNiAzNzY5ODA5MzUzNzI3MjUgMzYwMzYwNzIzODYwNjA0MQ==