/
PublishableVoterTest.php
110 lines (99 loc) · 3.47 KB
/
PublishableVoterTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
/*
* This file is part of the Symfony CMF package.
*
* (c) 2011-2017 Symfony CMF
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Cmf\Bundle\CoreBundle\Tests\Unit\PublishWorkflow\Voter;
use Symfony\Cmf\Bundle\CoreBundle\PublishWorkflow\PublishableReadInterface;
use Symfony\Cmf\Bundle\CoreBundle\PublishWorkflow\PublishWorkflowChecker;
use Symfony\Cmf\Bundle\CoreBundle\PublishWorkflow\Voter\PublishableVoter;
use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
class PublishableVoterTest extends \PHPUnit_Framework_TestCase
{
/**
* @var PublishableVoter
*/
private $voter;
/**
* @var TokenInterface
*/
private $token;
public function setUp()
{
$this->voter = new PublishableVoter();
$this->token = new AnonymousToken('', '');
}
public function providePublishWorkflowChecker()
{
return [
[
'expected' => VoterInterface::ACCESS_GRANTED,
'isPublishable' => true,
'attributes' => PublishWorkflowChecker::VIEW_ATTRIBUTE,
],
[
'expected' => VoterInterface::ACCESS_DENIED,
'isPublishable' => false,
'attributes' => PublishWorkflowChecker::VIEW_ATTRIBUTE,
],
[
'expected' => VoterInterface::ACCESS_GRANTED,
'isPublishable' => true,
'attributes' => [
PublishWorkflowChecker::VIEW_ANONYMOUS_ATTRIBUTE,
PublishWorkflowChecker::VIEW_ATTRIBUTE,
],
],
[
'expected' => VoterInterface::ACCESS_DENIED,
'isPublishable' => false,
'attributes' => PublishWorkflowChecker::VIEW_ANONYMOUS_ATTRIBUTE,
],
[
'expected' => VoterInterface::ACCESS_ABSTAIN,
'isPublishable' => true,
'attributes' => 'other',
],
[
'expected' => VoterInterface::ACCESS_ABSTAIN,
'isPublishable' => true,
'attributes' => [PublishWorkflowChecker::VIEW_ATTRIBUTE, 'other'],
],
];
}
/**
* @dataProvider providePublishWorkflowChecker
*
* use for voters!
*/
public function testPublishWorkflowChecker($expected, $isPublishable, $attributes)
{
$attributes = (array) $attributes;
$doc = $this->createMock(PublishableReadInterface::class);
$doc->expects($this->any())
->method('isPublishable')
->will($this->returnValue($isPublishable))
;
$this->assertEquals($expected, $this->voter->vote($this->token, $doc, $attributes));
}
public function testUnsupportedClass()
{
$result = $this->voter->vote(
$this->token,
$this,
[PublishWorkflowChecker::VIEW_ATTRIBUTE]
);
$this->assertEquals(VoterInterface::ACCESS_ABSTAIN, $result);
}
public function testNonClassSubject()
{
$result = $this->voter->vote($this->token, array(1, 2, 3), array(PublishWorkflowChecker::VIEW_ATTRIBUTE));
$this->assertEquals(VoterInterface::ACCESS_ABSTAIN, $result);
}
}