8000 minor #15942 [Security] Improve AbstractVoter tests (WouterJ) · symfony/security@fea9945 · GitHub
[go: up one dir, main page]

Skip to content
This repository was archived by the owner on May 31, 2024. It is now read-only.

Commit fea9945

Browse files
committed
minor #15942 [Security] Improve AbstractVoter tests (WouterJ)
This PR was merged into the 2.7 branch. Discussion ---------- [Security] Improve AbstractVoter tests Applying the improved tests from symfony/symfony#15932 into the oldest possible branch. Merge conflicts from 2.7 into 2.8 caused by this PR do not need to be done carefully, I'll create a new PR for 2.8 updating the tests as soon as these changes are merged up. | Q | A | ------------- | --- | Fixed tickets | - | License | MIT Commits ------- 5ff741d Readd the correct tests
2 parents a9d8a5f + 72c92c0 commit fea9945

File tree

1 file changed

+30
-46
lines changed

1 file changed

+30
-46
lines changed

Core/Tests/Authorization/Voter/AbstractVoterTest.php

Lines changed: 30 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -12,79 +12,63 @@
1212
namespace Symfony\Component\Security\Core\Tests\Authorization\Voter;
1313

1414
use Symfony\Component\Security\Core\Authorization\Voter\AbstractVoter;
15+
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
1516

16-
/**
17-
* @author Roman Marintšenko <inoryy@gmail.com>
18-
*/
1917
class AbstractVoterTest extends \PHPUnit_Framework_TestCase
2018
{
21-
/**
22-
* @var AbstractVoter
23-
*/
24-
private $voter;
25-
26-
private $token;
19+
protected $token;
2720

2821
protected function setUp()
2922
{
30-
$this->voter = new VoterFixture();
23+
$this->token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
24+
}
3125

32-
$tokenMock = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
33-
$tokenMock
34-
->expects($this->any())
35-
->method('getUser')
36-
->will($this->returnValue('user'));
26+
public function getTests()
27+
{
28+
return array(
29+
array(array('EDIT'), VoterInterface::ACCESS_GRANTED, new \stdClass(), 'ACCESS_GRANTED if attribute and class are supported and attribute grants access'),
30+
array(array('CREATE'), VoterInterface::ACCESS_DENIED, new \stdClass(), 'ACCESS_DENIED if attribute and class are supported and attribute does not grant access'),
3731

38-
$this->token = $tokenMock;
32+
array(array('DELETE', 'EDIT'), VoterInterface::ACCESS_GRANTED, new \stdClass(), 'ACCESS_GRANTED if one attribute is supported and grants access'),
33+
array(array('DELETE', 'CREATE'), VoterInterface::ACCESS_DENIED, new \stdClass(), 'ACCESS_DENIED if one attribute is supported and denies access'),
34+
35+
array(array('CREATE', 'EDIT'), VoterInterface::ACCESS_GRANTED, new \stdClass(), 'ACCESS_GRANTED if one attribute grants access'),
36+
37+
array(array('DELETE'), VoterInterface::ACCESS_ABSTAIN, new \stdClass(), 'ACCESS_ABSTAIN if no attribute is supported'),
38+
39+
array(array('EDIT'), VoterInterface::ACCESS_ABSTAIN, $this, 'ACCESS_ABSTAIN if class is not supported'),
40+
41+
array(array('EDIT'), VoterInterface::ACCESS_ABSTAIN, null, 'ACCESS_ABSTAIN if object is null'),
42+
43+
array(array(), VoterInterface::ACCESS_ABSTAIN, new \stdClass(), 'ACCESS_ABSTAIN if no attributes were provided'),
44+
);
3945
}
4046

4147
/**
42-
* @dataProvider getData
48+
* @dataProvider getTests
4349
*/
44-
public function testVote($expectedVote, $object, $attributes, $message)
50+
public function testVote(array $attributes, $expectedVote, $object, $message)
4551
{
46-
$this->assertEquals($expectedVote, $this->voter->vote($this->token, $object, $attributes), $message);
47-
}
52+
$voter = new AbstractVoterTest_Voter();
4853

49-
public function getData()
50-
{
51-
return array(
52-
array(AbstractVoter::ACCESS_ABSTAIN, null, array(), 'ACCESS_ABSTAIN for null objects'),
53-
array(AbstractVoter::ACCESS_ABSTAIN, new UnsupportedObjectFixture(), array(), 'ACCESS_ABSTAIN for objects with unsupported class'),
54-
array(AbstractVoter::ACCESS_ABSTAIN, new ObjectFixture(), array(), 'ACCESS_ABSTAIN for no attributes'),
55-
array(AbstractVoter::ACCESS_ABSTAIN, new ObjectFixture(), array('foobar'), 'ACCESS_ABSTAIN for unsupported attributes'),
56-
array(AbstractVoter::ACCESS_GRANTED, new ObjectFixture(), array('foo'), 'ACCESS_GRANTED if attribute grants access'),
57-
array(AbstractVoter::ACCESS_GRANTED, new ObjectFixture(), array('bar', 'foo'), 'ACCESS_GRANTED if *at least one* attribute grants access'),
58-
array(AbstractVoter::ACCESS_GRANTED, new ObjectFixture(), array('foobar', 'foo'), 'ACCESS_GRANTED if *at least one* attribute grants access'),
59-
array(AbstractVoter::ACCESS_DENIED, new ObjectFixture(), array('bar', 'baz'), 'ACCESS_DENIED for if no attribute grants access'),
60-
);
54+
$this->assertEquals($expectedVote, $voter->vote($this->token, $object, $attributes), $message);
6155
}
6256
}
6357

64-
class VoterFixture extends AbstractVoter
58+
class AbstractVoterTest_Voter extends AbstractVoter
6559
{
6660
protected function getSupportedClasses()
6761
{
68-
return array(
69-
'Symfony\Component\Security\Core\Tests\Authorization\Voter\ObjectFixture',
70-
);
62+
return array('stdClass');
7163
}
7264

7365
protected function getSupportedAttributes()
7466
{
75-
return array('foo', 'bar', 'baz');
67+
return array('EDIT', 'CREATE');
7668
}
7769

7870
protected function isGranted($attribute, $object, $user = null)
7971
{
80-
return $attribute === 'foo';
72+
return 'EDIT' === $attribute;
8173
}
8274
}
83-
84-
class ObjectFixture
85-
{
86-
}
87-
88< 41A3 /td>-
class UnsupportedObjectFixture
89-
{
90-
}

0 commit comments

Comments
 (0)
0