1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the OverblogGraphQLBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) Overblog <http://github.com/overblog/> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Overblog\GraphQLBundle\Tests\Error; |
13
|
|
|
|
14
|
|
|
use Overblog\GraphQLBundle\ExpressionLanguage\ExpressionLanguage; |
15
|
|
|
use Overblog\GraphQLBundle\Tests\DIContainerMockTrait; |
16
|
|
|
|
17
|
|
|
class AuthorizationExpressionProviderTest extends \PHPUnit_Framework_TestCase |
18
|
|
|
{ |
19
|
|
|
use DIContainerMockTrait; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var ExpressionLanguage |
23
|
|
|
*/ |
24
|
|
|
private $expressionLanguage; |
25
|
|
|
|
26
|
|
|
public function setUp() |
27
|
|
|
{ |
28
|
|
|
$this->expressionLanguage = new ExpressionLanguage(); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function testHasRole() |
32
|
|
|
{ |
33
|
|
|
$this->assertExpressionEvaluate('hasRole("ROLE_USER")', 'ROLE_USER'); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function testHasAnyRole() |
37
|
|
|
{ |
38
|
|
|
$this->assertExpressionEvaluate('hasAnyRole(["ROLE_ADMIN", "ROLE_USER"])', 'ROLE_ADMIN'); |
39
|
|
|
|
40
|
|
|
$this->assertExpressionEvaluate( |
41
|
|
|
'hasAnyRole(["ROLE_ADMIN", "ROLE_USER"])', |
42
|
|
|
$this->matchesRegularExpression('/^ROLE_(USER|ADMIN)$/'), |
43
|
|
|
[], |
44
|
|
|
$this->exactly(2), |
45
|
|
|
false, |
46
|
|
|
'assertFalse' |
47
|
|
|
); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function testIsAnonymous() |
51
|
|
|
{ |
52
|
|
|
$this->assertExpressionEvaluate('isAnonymous()', 'IS_AUTHENTICATED_ANONYMOUSLY'); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function testIsRememberMe() |
56
|
|
|
{ |
57
|
|
|
$this->assertExpressionEvaluate('isRememberMe()', 'IS_AUTHENTICATED_REMEMBERED'); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function testIsFullyAuthenticated() |
61
|
|
|
{ |
62
|
|
|
$this->assertExpressionEvaluate('isFullyAuthenticated()', 'IS_AUTHENTICATED_FULLY'); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function testIsAuthenticated() |
66
|
|
|
{ |
67
|
|
|
$this->assertExpressionEvaluate('isAuthenticated()', $this->matchesRegularExpression('/^IS_AUTHENTICATED_(REMEMBERED|FULLY)$/')); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function testHasPermission() |
71
|
|
|
{ |
72
|
|
|
$object = new \stdClass(); |
73
|
|
|
|
74
|
|
|
$this->assertExpressionEvaluate( |
75
|
|
|
'hasPermission(object,"OWNER")', |
76
|
|
|
[ |
77
|
|
|
'OWNER', |
78
|
|
|
$this->identicalTo($object), |
79
|
|
|
], |
80
|
|
|
[ |
81
|
|
|
'object' => $object, |
82
|
|
|
] |
83
|
|
|
); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function testHasAnyPermission() |
87
|
|
|
{ |
88
|
|
|
$object = new \stdClass(); |
89
|
|
|
|
90
|
|
|
$this->assertExpressionEvaluate( |
91
|
|
|
'hasAnyPermission(object,["OWNER", "WRITER"])', |
92
|
|
|
[ |
93
|
|
|
$this->matchesRegularExpression('/^(OWNER|WRITER)$/'), |
94
|
|
|
$this->identicalTo($object), |
95
|
|
|
], |
96
|
|
|
[ |
97
|
|
|
'object' => $object, |
98
|
|
|
] |
99
|
|
|
); |
100
|
|
|
|
101
|
|
|
$this->assertExpressionEvaluate( |
102
|
|
|
'hasAnyPermission(object,["OWNER", "WRITER"])', |
103
|
|
|
[ |
104
|
|
|
$this->matchesRegularExpression('/^(OWNER|WRITER)$/'), |
105
|
|
|
$this->identicalTo($object), |
106
|
|
|
], |
107
|
|
|
[ |
108
|
|
|
'object' => $object, |
109
|
|
|
], |
110
|
|
|
$this->exactly(2), |
111
|
|
|
false, |
112
|
|
|
'assertFalse' |
113
|
|
|
); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
private function assertExpressionEvaluate($expression, $with, array $expressionValues = [], $expects = null, $return = true, $assertMethod = 'assertTrue') |
117
|
|
|
{ |
118
|
|
|
$authChecker = $this->getAuthorizationCheckerIsGrantedWithExpectation($with, $expects, $return); |
119
|
|
|
|
120
|
|
|
$container = $this->getDIContainerMock(['security.authorization_checker' => $authChecker]); |
121
|
|
|
$this->expressionLanguage->setContainer($container); |
122
|
|
|
$this->$assertMethod($this->expressionLanguage->evaluate($expression, $expressionValues)); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
private function getAuthorizationCheckerIsGrantedWithExpectation($with, $expects = null, $return = true) |
126
|
|
|
{ |
127
|
|
|
if (null === $expects) { |
128
|
|
|
$expects = $this->once(); |
129
|
|
|
} |
130
|
|
|
$authChecker = $this->getAuthorizationCheckerMock(); |
131
|
|
|
|
132
|
|
|
if ($return instanceof \PHPUnit_Framework_MockObject_Stub_Return) { |
133
|
|
|
$returnValue = $return; |
134
|
|
|
} else { |
135
|
|
|
$returnValue = $this->returnValue($return); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
$methodExpectation = $authChecker |
139
|
|
|
->expects($expects) |
140
|
|
|
->method('isGranted'); |
141
|
|
|
|
142
|
|
|
call_user_func_array([$methodExpectation, 'with'], is_array($with) ? $with : [$with]); |
143
|
|
|
|
144
|
|
|
$methodExpectation->will($returnValue); |
145
|
|
|
|
146
|
|
|
return $authChecker; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
private function getAuthorizationCheckerMock() |
150
|
|
|
{ |
151
|
|
|
$AuthorizationChecker = $this->getMockBuilder('Symfony\\Component\Security\\Core\Authorization\\AuthorizationCheckerInterface') |
152
|
|
|
->disableOriginalConstructor() |
153
|
|
|
->setMethods(['isGranted']) |
154
|
|
|
->getMock() |
155
|
|
|
; |
156
|
|
|
|
157
|
|
|
return $AuthorizationChecker; |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|