@@ -18,16 +18,29 @@ class AuthorizationServiceTest extends \PHPUnit_Framework_TestCase
18
18
*/
19
19
const ROLE_ID = 1 ;
20
20
21
- /** @var \PHPUnit_Framework_MockObject_MockObject|Role */
21
+ /**
22
+ * Sample integration id
23
+ */
24
+ const INTEGRATION_ID = 22 ;
25
+
26
+ /**
27
+ * @var \PHPUnit_Framework_MockObject_MockObject|Role
28
+ */
22
29
protected $ roleMock ;
23
30
24
- /** @var AuthorizationService */
31
+ /**
32
+ * @var AuthorizationService
33
+ */
25
34
protected $ integrationAuthorizationService ;
26
35
27
- /** @var \PHPUnit_Framework_MockObject_MockObject|Rules */
36
+ /**
37
+ * @var \PHPUnit_Framework_MockObject_MockObject|Rules
38
+ */
28
39
protected $ rulesMock ;
29
40
30
- /** @var \PHPUnit_Framework_MockObject_MockObject|RootResource */
41
+ /**
42
+ * @var \PHPUnit_Framework_MockObject_MockObject|RootResource
43
+ */
31
44
protected $ rootAclResourceMock ;
32
45
33
46
/**
@@ -39,14 +52,14 @@ protected function setUp()
39
52
{
40
53
$ this ->roleMock = $ this ->getMock (
41
54
'Magento\Authorization\Model\Role ' ,
42
- ['load ' , 'delete ' , '__wakeup ' , 'getId ' ],
55
+ ['load ' , 'delete ' , '__wakeup ' , 'getId ' , ' save ' ],
43
56
[],
44
57
'' ,
45
58
false
46
59
);
47
60
$ this ->roleMock ->expects ($ this ->any ())->method ('load ' )->will ($ this ->returnSelf ());
48
61
$ this ->roleMock ->expects ($ this ->any ())->method ('delete ' )->will ($ this ->returnSelf ());
49
- $ this ->roleMock ->expects ($ this ->any ())->method ('getId ' )->will ($ this ->returnValue ( self :: ROLE_ID ));
62
+ $ this ->roleMock ->expects ($ this ->any ())->method ('save ' )->will ($ this ->returnSelf ( ));
50
63
51
64
/** @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Authorization\Model\RoleFactory $roleFactoryMock */
52
65
$ roleFactoryMock = $ this ->getMock (
@@ -106,45 +119,112 @@ protected function setUp()
106
119
107
120
public function testRemovePermissions ()
108
121
{
109
- $ integrationId = 22 ;
110
- $ roleName = UserContextInterface::USER_TYPE_INTEGRATION . $ integrationId ;
122
+ $ roleName = UserContextInterface::USER_TYPE_INTEGRATION . self ::INTEGRATION_ID ;
111
123
$ this ->roleMock ->expects ($ this ->once ())->method ('load ' )->with ($ roleName )->will ($ this ->returnSelf ());
112
- $ this ->integrationAuthorizationService ->removePermissions ($ integrationId );
124
+ $ this ->integrationAuthorizationService ->removePermissions (self ::INTEGRATION_ID );
125
+ }
126
+
127
+ /**
128
+ * @expectedException \Magento\Framework\Exception\LocalizedException
129
+ * @expectedExceptionMessage Error happened while deleting role and permissions. Check exception log for details.
130
+ */
131
+ public function testRemovePermissionsException ()
132
+ {
133
+ $ roleName = UserContextInterface::USER_TYPE_INTEGRATION . self ::INTEGRATION_ID ;
134
+ $ this ->roleMock ->expects ($ this ->once ())
135
+ ->method ('load ' )
136
+ ->with ($ roleName )
137
+ ->will ($ this ->throwException (new \Exception ));
138
+ $ this ->integrationAuthorizationService ->removePermissions (self ::INTEGRATION_ID );
113
139
}
114
140
115
141
public function testGrantPermissions ()
116
142
{
117
- $ integrationId = 22 ;
118
143
$ this ->resources = [
119
144
'Magento_Sales::sales ' ,
120
145
'Magento_Sales::sales_operations ' ,
121
146
'Magento_Cart::cart ' ,
122
147
'Magento_Cart::manage '
123
148
];
124
149
150
+ $ this ->roleMock ->expects ($ this ->any ())->method ('getId ' )->will ($ this ->returnValue (self ::ROLE_ID ));
125
151
$ this ->rulesMock ->expects ($ this ->any ())->method ('setRoleId ' )->with (self ::ROLE_ID )->will ($ this ->returnSelf ());
126
152
$ this ->rulesMock ->expects ($ this ->any ())
127
153
->method ('setResources ' )
128
154
->with ($ this ->resources )
129
155
->will ($ this ->returnSelf ());
130
156
$ this ->rulesMock ->expects ($ this ->any ())->method ('saveRel ' )->will ($ this ->returnSelf ());
131
157
132
- $ this ->integrationAuthorizationService ->grantPermissions ($ integrationId , $ this ->resources );
158
+ $ this ->integrationAuthorizationService ->grantPermissions (self ::INTEGRATION_ID , $ this ->resources );
159
+ }
160
+
161
+ public function testGrantPermissionsNoRole ()
162
+ {
163
+ $ calculatedRoleId = UserContextInterface::USER_TYPE_INTEGRATION . self ::INTEGRATION_ID ;
164
+
165
+ $ this ->resources = [
166
+ 'Magento_Sales::sales ' ,
167
+ 'Magento_Sales::sales_operations ' ,
168
+ 'Magento_Cart::cart ' ,
169
+ 'Magento_Cart::manage '
170
+ ];
171
+
172
+ //Return invalid role
173
+ $ this ->roleMock ->expects ($ this ->any ())
174
+ ->method ('getId ' )
175
+ ->will ($ this ->onConsecutiveCalls (null , $ calculatedRoleId ));
176
+ // Verify if the method is called with the newly created role
177
+ $ this ->rulesMock ->expects ($ this ->any ())
178
+ ->method ('setRoleId ' )
179
+ ->with ($ calculatedRoleId )
180
+ ->will ($ this ->returnSelf ());
181
+
182
+ $ this ->rulesMock ->expects ($ this ->any ())
183
+ ->method ('setResources ' )
184
+ ->with ($ this ->resources )
185
+ ->will ($ this ->returnSelf ());
186
+ $ this ->rulesMock ->expects ($ this ->any ())->method ('saveRel ' )->will ($ this ->returnSelf ());
187
+
188
+ $ this ->integrationAuthorizationService ->grantPermissions (self ::INTEGRATION_ID , $ this ->resources );
189
+ }
190
+
191
+ /**
192
+ * @expectedException \Magento\Framework\Exception\LocalizedException
193
+ * @expectedExceptionMessage Error happened while granting permissions. Check exception log for details.
194
+ */
195
+ public function testGrantPermissionsException ()
196
+ {
197
+ $ this ->resources = [
198
+ 'Magento_Sales::sales ' ,
199
+ 'Magento_Sales::sales_operations ' ,
200
+ 'Magento_Cart::cart ' ,
201
+ 'Magento_Cart::manage '
202
+ ];
203
+
204
+ $ this ->roleMock ->expects ($ this ->any ())->method ('getId ' )->will ($ this ->returnValue (self ::ROLE_ID ));
205
+ $ this ->rulesMock ->expects ($ this ->any ())->method ('setRoleId ' )->with (self ::ROLE_ID )->will ($ this ->returnSelf ());
206
+ $ this ->rulesMock ->expects ($ this ->any ())
207
+ ->method ('setResources ' )
208
+ ->with ($ this ->resources )
209
+ ->will ($ this ->returnSelf ());
210
+ $ this ->rulesMock ->expects ($ this ->any ())->method ('saveRel ' )->will ($ this ->throwException (new \Exception ));
211
+
212
+ $ this ->integrationAuthorizationService ->grantPermissions (self ::INTEGRATION_ID , $ this ->resources );
133
213
}
134
214
135
215
public function testGrantAllPermissions ()
136
216
{
137
- $ integrationId = 22 ;
138
217
$ rootResource = 'Magento_All:all ' ;
139
218
140
219
$ this ->rootAclResourceMock ->expects ($ this ->any ())->method ('getId ' )->will ($ this ->returnValue ($ rootResource ));
220
+ $ this ->roleMock ->expects ($ this ->any ())->method ('getId ' )->will ($ this ->returnValue (self ::ROLE_ID ));
141
221
$ this ->rulesMock ->expects ($ this ->any ())->method ('setRoleId ' )->with (self ::ROLE_ID )->will ($ this ->returnSelf ());
142
222
$ this ->rulesMock ->expects ($ this ->any ())
143
223
->method ('setResources ' )
144
224
->with ([$ rootResource ])
145
225
->will ($ this ->returnSelf ());
146
226
$ this ->rulesMock ->expects ($ this ->any ())->method ('saveRel ' )->will ($ this ->returnSelf ());
147
227
148
- $ this ->integrationAuthorizationService ->grantAllPermissions ($ integrationId );
228
+ $ this ->integrationAuthorizationService ->grantAllPermissions (self :: INTEGRATION_ID );
149
229
}
150
230
}
0 commit comments