LegacySecurityContextTest.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Security\Core\Tests;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Security\Core\Security;
  13. use Symfony\Component\Security\Core\SecurityContext;
  14. use Symfony\Component\Security\Core\SecurityContextInterface;
  15. /**
  16. * @group legacy
  17. */
  18. class LegacySecurityContextTest extends TestCase
  19. {
  20. private $tokenStorage;
  21. private $authorizationChecker;
  22. private $securityContext;
  23. protected function setUp()
  24. {
  25. $this->tokenStorage = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface')->getMock();
  26. $this->authorizationChecker = $this->getMockBuilder('Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface')->getMock();
  27. $this->securityContext = new SecurityContext($this->tokenStorage, $this->authorizationChecker);
  28. }
  29. public function testGetTokenDelegation()
  30. {
  31. $token = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')->getMock();
  32. $this->tokenStorage
  33. ->expects($this->once())
  34. ->method('getToken')
  35. ->will($this->returnValue($token));
  36. $this->assertSame($token, $this->securityContext->getToken());
  37. }
  38. public function testSetTokenDelegation()
  39. {
  40. $token = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')->getMock();
  41. $this->tokenStorage
  42. ->expects($this->once())
  43. ->method('setToken')
  44. ->with($token);
  45. $this->securityContext->setToken($token);
  46. }
  47. /**
  48. * @dataProvider isGrantedDelegationProvider
  49. */
  50. public function testIsGrantedDelegation($attributes, $object, $return)
  51. {
  52. $this->authorizationChecker
  53. ->expects($this->once())
  54. ->method('isGranted')
  55. ->with($attributes, $object)
  56. ->will($this->returnValue($return));
  57. $this->assertEquals($return, $this->securityContext->isGranted($attributes, $object));
  58. }
  59. public function isGrantedDelegationProvider()
  60. {
  61. return array(
  62. array(array(), new \stdClass(), true),
  63. array(array('henk'), new \stdClass(), false),
  64. array(null, new \stdClass(), false),
  65. array('henk', null, true),
  66. array(array(1), 'henk', true),
  67. );
  68. }
  69. /**
  70. * Test dedicated to check if the backwards compatibility is still working.
  71. */
  72. public function testOldConstructorSignature()
  73. {
  74. $authenticationManager = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface')->getMock();
  75. $accessDecisionManager = $this->getMockBuilder('Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface')->getMock();
  76. $this->assertInstanceOf('Symfony\Component\Security\Core\SecurityContext', new SecurityContext($authenticationManager, $accessDecisionManager));
  77. }
  78. /**
  79. * @dataProvider oldConstructorSignatureFailuresProvider
  80. * @expectedException \BadMethodCallException
  81. */
  82. public function testOldConstructorSignatureFailures($first, $second)
  83. {
  84. new SecurityContext($first, $second);
  85. }
  86. public function oldConstructorSignatureFailuresProvider()
  87. {
  88. $tokenStorage = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface')->getMock();
  89. $authorizationChecker = $this->getMockBuilder('Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface')->getMock();
  90. $authenticationManager = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface')->getMock();
  91. $accessDecisionManager = $this->getMockBuilder('Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface')->getMock();
  92. return array(
  93. array(new \stdClass(), new \stdClass()),
  94. array($tokenStorage, $accessDecisionManager),
  95. array($accessDecisionManager, $tokenStorage),
  96. array($authorizationChecker, $accessDecisionManager),
  97. array($accessDecisionManager, $authorizationChecker),
  98. array($tokenStorage, $accessDecisionManager),
  99. array($authenticationManager, $authorizationChecker),
  100. array('henk', 'hans'),
  101. array(null, false),
  102. array(true, null),
  103. );
  104. }
  105. /**
  106. * Test if the BC Layer is working as intended.
  107. */
  108. public function testConstantSync()
  109. {
  110. $this->assertSame(Security::ACCESS_DENIED_ERROR, SecurityContextInterface::ACCESS_DENIED_ERROR);
  111. $this->assertSame(Security::AUTHENTICATION_ERROR, SecurityContextInterface::AUTHENTICATION_ERROR);
  112. $this->assertSame(Security::LAST_USERNAME, SecurityContextInterface::LAST_USERNAME);
  113. }
  114. }