AuthenticatedVoterTest.php 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.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\Tests\Component\Security\Authorization\Voter;
  11. use Symfony\Component\Security\Authentication\AuthenticationTrustResolver;
  12. use Symfony\Component\Security\Authorization\Voter\AuthenticatedVoter;
  13. use Symfony\Component\Security\Authorization\Voter\VoterInterface;
  14. use Symfony\Component\Security\Role\Role;
  15. class AuthenticatedVoterTest extends \PHPUnit_Framework_TestCase
  16. {
  17. public function testSupportsClass()
  18. {
  19. $voter = new AuthenticatedVoter($this->getResolver());
  20. $this->assertTrue($voter->supportsClass('stdClass'));
  21. }
  22. /**
  23. * @dataProvider getVoteTests
  24. */
  25. public function testVote($authenticated, $attributes, $expected)
  26. {
  27. $voter = new AuthenticatedVoter($this->getResolver());
  28. $this->assertSame($expected, $voter->vote($this->getToken($authenticated), null, $attributes));
  29. }
  30. public function getVoteTests()
  31. {
  32. return array(
  33. array('fully', array(), VoterInterface::ACCESS_ABSTAIN),
  34. array('fully', array('FOO'), VoterInterface::ACCESS_ABSTAIN),
  35. array('remembered', array(), VoterInterface::ACCESS_ABSTAIN),
  36. array('remembered', array('FOO'), VoterInterface::ACCESS_ABSTAIN),
  37. array('anonymously', array(), VoterInterface::ACCESS_ABSTAIN),
  38. array('anonymously', array('FOO'), VoterInterface::ACCESS_ABSTAIN),
  39. array('fully', array('IS_AUTHENTICATED_ANONYMOUSLY'), VoterInterface::ACCESS_GRANTED),
  40. array('remembered', array('IS_AUTHENTICATED_ANONYMOUSLY'), VoterInterface::ACCESS_GRANTED),
  41. array('anonymously', array('IS_AUTHENTICATED_ANONYMOUSLY'), VoterInterface::ACCESS_GRANTED),
  42. array('fully', array('IS_AUTHENTICATED_REMEMBERED'), VoterInterface::ACCESS_GRANTED),
  43. array('remembered', array('IS_AUTHENTICATED_REMEMBERED'), VoterInterface::ACCESS_GRANTED),
  44. array('anonymously', array('IS_AUTHENTICATED_REMEMBERED'), VoterInterface::ACCESS_DENIED),
  45. array('fully', array('IS_AUTHENTICATED_FULLY'), VoterInterface::ACCESS_GRANTED),
  46. array('remembered', array('IS_AUTHENTICATED_FULLY'), VoterInterface::ACCESS_DENIED),
  47. array('anonymously', array('IS_AUTHENTICATED_FULLY'), VoterInterface::ACCESS_DENIED),
  48. );
  49. }
  50. protected function getResolver()
  51. {
  52. return new AuthenticationTrustResolver(
  53. 'Symfony\\Component\\Security\\Authentication\\Token\\AnonymousToken',
  54. 'Symfony\\Component\\Security\\Authentication\\Token\\RememberMeToken'
  55. );
  56. }
  57. protected function getToken($authenticated)
  58. {
  59. if ('fully' === $authenticated) {
  60. return $this->getMock('Symfony\Component\Security\Authentication\Token\TokenInterface');
  61. } else if ('remembered' === $authenticated) {
  62. return $this->getMock('Symfony\Component\Security\Authentication\Token\RememberMeToken', array('setPersistent'), array(), '', false);
  63. } else {
  64. return $this->getMock('Symfony\Component\Security\Authentication\Token\AnonymousToken', null, array('', ''));
  65. }
  66. }
  67. }