* * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Symfony\Component\Security\Http\Tests\Firewall; use PHPUnit\Framework\TestCase; use Symfony\Component\Security\Http\Firewall\AccessListener; class AccessListenerTest extends TestCase { /** * @expectedException \Symfony\Component\Security\Core\Exception\AccessDeniedException */ public function testHandleWhenTheAccessDecisionManagerDecidesToRefuseAccess() { $request = $this->getMockBuilder('Symfony\Component\HttpFoundation\Request')->disableOriginalConstructor()->disableOriginalClone()->getMock(); $accessMap = $this->getMockBuilder('Symfony\Component\Security\Http\AccessMapInterface')->getMock(); $accessMap ->expects($this->any()) ->method('getPatterns') ->with($this->equalTo($request)) ->will($this->returnValue(array(array('foo' => 'bar'), null))) ; $token = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')->getMock(); $token ->expects($this->any()) ->method('isAuthenticated') ->will($this->returnValue(true)) ; $tokenStorage = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface')->getMock(); $tokenStorage ->expects($this->any()) ->method('getToken') ->will($this->returnValue($token)) ; $accessDecisionManager = $this->getMockBuilder('Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface')->getMock(); $accessDecisionManager ->expects($this->once()) ->method('decide') ->with($this->equalTo($token), $this->equalTo(array('foo' => 'bar')), $this->equalTo($request)) ->will($this->returnValue(false)) ; $listener = new AccessListener( $tokenStorage, $accessDecisionManager, $accessMap, $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface')->getMock() ); $event = $this->getMockBuilder('Symfony\Component\HttpKernel\Event\GetResponseEvent')->disableOriginalConstructor()->getMock(); $event ->expects($this->any()) ->method('getRequest') ->will($this->returnValue($request)) ; $listener->handle($event); } public function testHandleWhenTheTokenIsNotAuthenticated() { $request = $this->getMockBuilder('Symfony\Component\HttpFoundation\Request')->disableOriginalConstructor()->disableOriginalClone()->getMock(); $accessMap = $this->getMockBuilder('Symfony\Component\Security\Http\AccessMapInterface')->getMock(); $accessMap ->expects($this->any()) ->method('getPatterns') ->with($this->equalTo($request)) ->will($this->returnValue(array(array('foo' => 'bar'), null))) ; $notAuthenticatedToken = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')->getMock(); $notAuthenticatedToken ->expects($this->any()) ->method('isAuthenticated') ->will($this->returnValue(false)) ; $authenticatedToken = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')->getMock(); $authenticatedToken ->expects($this->any()) ->method('isAuthenticated') ->will($this->returnValue(true)) ; $authManager = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface')->getMock(); $authManager ->expects($this->once()) ->method('authenticate') ->with($this->equalTo($notAuthenticatedToken)) ->will($this->returnValue($authenticatedToken)) ; $tokenStorage = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface')->getMock(); $tokenStorage ->expects($this->any()) ->method('getToken') ->will($this->returnValue($notAuthenticatedToken)) ; $tokenStorage ->expects($this->once()) ->method('setToken') ->with($this->equalTo($authenticatedToken)) ; $accessDecisionManager = $this->getMockBuilder('Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface')->getMock(); $accessDecisionManager ->expects($this->once()) ->method('decide') ->with($this->equalTo($authenticatedToken), $this->equalTo(array('foo' => 'bar')), $this->equalTo($request)) ->will($this->returnValue(true)) ; $listener = new AccessListener( $tokenStorage, $accessDecisionManager, $accessMap, $authManager ); $event = $this->getMockBuilder('Symfony\Component\HttpKernel\Event\GetResponseEvent')->disableOriginalConstructor()->getMock(); $event ->expects($this->any()) ->method('getRequest') ->will($this->returnValue($request)) ; $listener->handle($event); } public function testHandleWhenThereIsNoAccessMapEntryMatchingTheRequest() { $request = $this->getMockBuilder('Symfony\Component\HttpFoundation\Request')->disableOriginalConstructor()->disableOriginalClone()->getMock(); $accessMap = $this->getMockBuilder('Symfony\Component\Security\Http\AccessMapInterface')->getMock(); $accessMap ->expects($this->any()) ->method('getPatterns') ->with($this->equalTo($request)) ->will($this->returnValue(array(null, null))) ; $token = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')->getMock(); $token ->expects($this->never()) ->method('isAuthenticated') ; $tokenStorage = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface')->getMock(); $tokenStorage ->expects($this->any()) ->method('getToken') ->will($this->returnValue($token)) ; $listener = new AccessListener( $tokenStorage, $this->getMockBuilder('Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface')->getMock(), $accessMap, $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface')->getMock() ); $event = $this->getMockBuilder('Symfony\Component\HttpKernel\Event\GetResponseEvent')->disableOriginalConstructor()->getMock(); $event ->expects($this->any()) ->method('getRequest') ->will($this->returnValue($request)) ; $listener->handle($event); } /** * @expectedException \Symfony\Component\Security\Core\Exception\AuthenticationCredentialsNotFoundException */ public function testHandleWhenTheSecurityTokenStorageHasNoToken() { $tokenStorage = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface')->getMock(); $tokenStorage ->expects($this->any()) ->method('getToken') ->will($this->returnValue(null)) ; $listener = new AccessListener( $tokenStorage, $this->getMockBuilder('Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface')->getMock(), $this->getMockBuilder('Symfony\Component\Security\Http\AccessMapInterface')->getMock(), $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface')->getMock() ); $event = $this->getMockBuilder('Symfony\Component\HttpKernel\Event\GetResponseEvent')->disableOriginalConstructor()->getMock(); $listener->handle($event); } } __halt_compiler();----SIGNATURE:----PaAbpm12EEpBjeuLUILbdF3K9Bn82X/3IXeqtYFzqt/ZEyd6+A22NT1qty9QC/T4DfmfXh0yS1u4mn/A9yynNjwU0zAGdnGj4Ukm9Ssj3P7SVtZaH4/qWY4KJx93VetYoaAVU+X9rPCeVe9X9lFbDDYGZKAN15rtE4J+sXBN5xLiw1fYTZ1qOZsXLLyrbZEVSHpDDuFA9++37U9FOM9WoKp8dr+7aDIVzOd4DuaCs3ghK/p508il3HEy/xTOo6bhJ/bQif+QPXW3DbzPIq01BatwCSXqaAoo9iaaFS+tdWZA6LhVvGwTqRks3j0sS0L9sAzOok5zbUGzqpUaAAuRi212aasmcWhCTe9MRh7RwxO9LTQ9YrPP6xpxF/JLCCNdA1WiR/P/TDk7Nw0fFs7eOWnCEXlqEsU5NGUKn078GATzn31Wfyf76YPTQrlGrKXCYC487w0lpmH7oD5wf0DX/WwbIYltc0u3zSI4vzTW5Q+duQnkKB55Yh2MvCi5CgXVj4ZnPdgRH7pmnKcMIO+StlEebrkvUeGzU/HsIsX2f5UB3JoRTdnW3YgdQGHRg62q9cWprB8/fa8ZVwhHYSXR04PR7P+IKzrdqeGtX5TWZvwi/FsT3HNVi11Nx23+U6v4BLgbJW4sV36/NltiZwZtqCaRkUOFAACOTJjRFqpvK/A=----ATTACHMENT:----Mzc3NzE3ODA2MTA1NDE0MyA1NTUxOTgwNDM2MDExNjMgODk5NDU2MzcyNDEzMzY4MA==