AccessDecisionManagerTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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;
  10. use Symfony\Component\Security\Authorization\AccessDecisionManager;
  11. use Symfony\Component\Security\Authorization\Voter\VoterInterface;
  12. class AccessDecisionManagerTest extends \PHPUnit_Framework_TestCase
  13. {
  14. /**
  15. * @dataProvider getStrategyTests
  16. */
  17. public function testStrategies($strategy, $voters, $allowIfAllAbstainDecisions, $allowIfEqualGrantedDeniedDecisions, $expected)
  18. {
  19. $token = $this->getMock('Symfony\Component\Security\Authentication\Token\TokenInterface');
  20. $manager = new AccessDecisionManager($voters, $strategy, $allowIfAllAbstainDecisions, $allowIfEqualGrantedDeniedDecisions);
  21. $this->assertSame($expected, $manager->decide($token, array('ROLE_FOO')));
  22. }
  23. public function getStrategyTests()
  24. {
  25. return array(
  26. // affirmative
  27. array('affirmative', $this->getVoters(1, 0, 0), false, true, true),
  28. array('affirmative', $this->getVoters(1, 2, 0), false, true, true),
  29. array('affirmative', $this->getVoters(0, 1, 0), false, true, false),
  30. array('affirmative', $this->getVoters(0, 0, 0), false, true, false),
  31. array('affirmative', $this->getVoters(0, 0, 1), false, true, false),
  32. array('affirmative', $this->getVoters(0, 0, 1), true, true, true),
  33. // consensus
  34. array('consensus', $this->getVoters(1, 0, 0), false, true, true),
  35. array('consensus', $this->getVoters(1, 2, 0), false, true, false),
  36. array('consensus', $this->getVoters(2, 1, 0), false, true, true),
  37. array('consensus', $this->getVoters(0, 0, 0), false, true, false),
  38. array('consensus', $this->getVoters(0, 0, 1), false, true, false),
  39. array('consensus', $this->getVoters(0, 0, 0), true, true, true),
  40. array('consensus', $this->getVoters(0, 0, 1), true, true, true),
  41. array('consensus', $this->getVoters(2, 2, 0), false, true, true),
  42. array('consensus', $this->getVoters(2, 2, 1), false, true, true),
  43. array('consensus', $this->getVoters(2, 2, 0), false, false, false),
  44. array('consensus', $this->getVoters(2, 2, 1), false, false, false),
  45. // unanimous
  46. array('unanimous', $this->getVoters(1, 0, 0), false, true, true),
  47. array('unanimous', $this->getVoters(1, 0, 1), false, true, true),
  48. array('unanimous', $this->getVoters(1, 1, 0), false, true, false),
  49. array('unanimous', $this->getVoters(0, 0, 0), false, true, false),
  50. array('unanimous', $this->getVoters(0, 0, 0), true, true, true),
  51. array('unanimous', $this->getVoters(0, 0, 2), false, true, false),
  52. array('unanimous', $this->getVoters(0, 0, 2), true, true, true),
  53. );
  54. }
  55. protected function getVoters($grants, $denies, $abstains)
  56. {
  57. $voters = array();
  58. for ($i = 0; $i < $grants; $i++) {
  59. $voters[] = $this->getVoter(VoterInterface::ACCESS_GRANTED);
  60. }
  61. for ($i = 0; $i < $denies; $i++) {
  62. $voters[] = $this->getVoter(VoterInterface::ACCESS_DENIED);
  63. }
  64. for ($i = 0; $i < $abstains; $i++) {
  65. $voters[] = $this->getVoter(VoterInterface::ACCESS_ABSTAIN);
  66. }
  67. return $voters;
  68. }
  69. protected function getVoter($vote)
  70. {
  71. $voter = $this->getMock('Symfony\Component\Security\Authorization\Voter\VoterInterface');
  72. $voter->expects($this->any())
  73. ->method('vote')
  74. ->will($this->returnValue($vote));
  75. ;
  76. return $voter;
  77. }
  78. }