AuthenticatedVoterTest.php 3.3 KB

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