SecurityIdentityRetrievalStrategyTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. namespace Symfony\Tests\Component\Security\Acl\Domain;
  3. use Symfony\Component\Security\Acl\Domain\RoleSecurityIdentity;
  4. use Symfony\Component\Security\Acl\Domain\UserSecurityIdentity;
  5. use Symfony\Component\Security\Acl\Domain\SecurityIdentityRetrievalStrategy;
  6. class SecurityIdentityRetrievalStrategyTest extends \PHPUnit_Framework_TestCase
  7. {
  8. /**
  9. * @dataProvider getSecurityIdentityRetrievalTests
  10. */
  11. public function testGetSecurityIdentities($username, array $roles, $authenticationStatus, array $sids)
  12. {
  13. $strategy = $this->getStrategy($roles, $authenticationStatus);
  14. $token = $this->getMock('Symfony\Component\Security\Authentication\Token\TokenInterface');
  15. if ('anonymous' !== $authenticationStatus) {
  16. $token
  17. ->expects($this->once())
  18. ->method('__toString')
  19. ->will($this->returnValue($username))
  20. ;
  21. }
  22. $token
  23. ->expects($this->once())
  24. ->method('getRoles')
  25. ->will($this->returnValue(array('foo')))
  26. ;
  27. $extractedSids = $strategy->getSecurityIdentities($token);
  28. foreach ($extractedSids as $index => $extractedSid) {
  29. if (!isset($sids[$index])) {
  30. $this->fail(sprintf('Expected SID at index %d, but there was none.', true));
  31. }
  32. if (false === $sids[$index]->equals($extractedSid)) {
  33. $this->fail(sprintf('Index: %d, expected SID "%s", but got "%s".', $index, $sids[$index], $extractedSid));
  34. }
  35. }
  36. }
  37. public function getSecurityIdentityRetrievalTests()
  38. {
  39. return array(
  40. array('johannes', array('ROLE_USER', 'ROLE_SUPERADMIN'), 'fullFledged', array(
  41. new UserSecurityIdentity('johannes'),
  42. new RoleSecurityIdentity('ROLE_USER'),
  43. new RoleSecurityIdentity('ROLE_SUPERADMIN'),
  44. new RoleSecurityIdentity('IS_AUTHENTICATED_FULLY'),
  45. new RoleSecurityIdentity('IS_AUTHENTICATED_REMEMBERED'),
  46. new RoleSecurityIdentity('IS_AUTHENTICATED_ANONYMOUSLY'),
  47. )),
  48. array('foo', array('ROLE_FOO'), 'rememberMe', array(
  49. new UserSecurityIdentity('foo'),
  50. new RoleSecurityIdentity('ROLE_FOO'),
  51. new RoleSecurityIdentity('IS_AUTHENTICATED_REMEMBERED'),
  52. new RoleSecurityIdentity('IS_AUTHENTICATED_ANONYMOUSLY'),
  53. )),
  54. array('guest', array('ROLE_FOO'), 'anonymous', array(
  55. new RoleSecurityIdentity('ROLE_FOO'),
  56. new RoleSecurityIdentity('IS_AUTHENTICATED_ANONYMOUSLY'),
  57. ))
  58. );
  59. }
  60. protected function getStrategy(array $roles = array(), $authenticationStatus = 'fullFledged')
  61. {
  62. $roleHierarchy = $this->getMock('Symfony\Component\Security\Role\RoleHierarchyInterface');
  63. $roleHierarchy
  64. ->expects($this->once())
  65. ->method('getReachableRoles')
  66. ->with($this->equalTo(array('foo')))
  67. ->will($this->returnValue($roles))
  68. ;
  69. $trustResolver = $this->getMock('Symfony\Component\Security\Authentication\AuthenticationTrustResolver', array(), array('', ''));
  70. $trustResolver
  71. ->expects($this->at(0))
  72. ->method('isAnonymous')
  73. ->will($this->returnValue('anonymous' === $authenticationStatus))
  74. ;
  75. if ('fullFledged' === $authenticationStatus) {
  76. $trustResolver
  77. ->expects($this->once())
  78. ->method('isFullFledged')
  79. ->will($this->returnValue(true))
  80. ;
  81. $trustResolver
  82. ->expects($this->never())
  83. ->method('isRememberMe')
  84. ;
  85. } else if ('rememberMe' === $authenticationStatus) {
  86. $trustResolver
  87. ->expects($this->once())
  88. ->method('isFullFledged')
  89. ->will($this->returnValue(false))
  90. ;
  91. $trustResolver
  92. ->expects($this->once())
  93. ->method('isRememberMe')
  94. ->will($this->returnValue(true))
  95. ;
  96. } else {
  97. $trustResolver
  98. ->expects($this->at(1))
  99. ->method('isAnonymous')
  100. ->will($this->returnValue(true))
  101. ;
  102. $trustResolver
  103. ->expects($this->once())
  104. ->method('isFullFledged')
  105. ->will($this->returnValue(false))
  106. ;
  107. $trustResolver
  108. ->expects($this->once())
  109. ->method('isRememberMe')
  110. ->will($this->returnValue(false))
  111. ;
  112. }
  113. return new SecurityIdentityRetrievalStrategy($roleHierarchy, $trustResolver);
  114. }
  115. }