3
3
* Copyright © Magento, Inc. All rights reserved.
4
4
* See COPYING.txt for license details.
5
5
*/
6
+ declare (strict_types=1 );
7
+
6
8
namespace Magento \Backend \Test \Unit \Model \Auth ;
7
9
10
+ use Magento \Backend \App \Config ;
8
11
use Magento \Backend \Model \Auth \Session ;
12
+ use Magento \Framework \Acl ;
13
+ use Magento \Framework \Acl \Builder ;
14
+ use Magento \Framework \Session \Storage ;
15
+ use Magento \Framework \Stdlib \Cookie \CookieMetadataFactory ;
16
+ use Magento \Framework \Stdlib \Cookie \PhpCookieManager ;
17
+ use Magento \Framework \Stdlib \Cookie \PublicCookieMetadata ;
18
+ use Magento \Framework \Stdlib \CookieManagerInterface ;
9
19
use Magento \Framework \TestFramework \Unit \Helper \ObjectManager ;
20
+ use Magento \User \Model \User ;
21
+ use PHPUnit \Framework \MockObject \MockObject ;
22
+ use PHPUnit \Framework \TestCase ;
10
23
11
24
/**
12
25
* Class SessionTest tests Magento\Backend\Model\Auth\Session
13
26
*
14
27
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
15
28
*/
16
- class SessionTest extends \ PHPUnit \ Framework \ TestCase
29
+ class SessionTest extends TestCase
17
30
{
18
31
/**
19
- * @var \Magento\Backend\App\ Config | \PHPUnit_Framework_MockObject_MockObject
32
+ * @var Config|MockObject
20
33
*/
21
34
private $ config ;
22
35
23
36
/**
24
- * @var \Magento\Framework\Session\Config | \PHPUnit_Framework_MockObject_MockObject
37
+ * @var \Magento\Framework\Session\Config|MockObject
25
38
*/
26
39
private $ sessionConfig ;
27
40
28
41
/**
29
- * @var \Magento\Framework\Stdlib\ CookieManagerInterface | \PHPUnit_Framework_MockObject_MockObject
42
+ * @var CookieManagerInterface|MockObject
30
43
*/
31
44
private $ cookieManager ;
32
45
33
46
/**
34
- * @var \Magento\Framework\Stdlib\Cookie\ CookieMetadataFactory | \PHPUnit_Framework_MockObject_MockObject
47
+ * @var CookieMetadataFactory|MockObject
35
48
*/
36
49
private $ cookieMetadataFactory ;
37
50
38
51
/**
39
- * @var \Magento\Framework\Session\ Storage | \PHPUnit_Framework_MockObject_MockObject
52
+ * @var Storage|MockObject
40
53
*/
41
54
private $ storage ;
42
55
43
56
/**
44
- * @var \Magento\Framework\Acl\ Builder | \PHPUnit_Framework_MockObject_MockObject
57
+ * @var Builder|MockObject
45
58
*/
46
59
private $ aclBuilder ;
47
60
@@ -53,32 +66,38 @@ class SessionTest extends \PHPUnit\Framework\TestCase
53
66
/**
54
67
* @inheritdoc
55
68
*/
56
- protected function setUp ()
69
+ protected function setUp (): void
57
70
{
58
71
$ this ->cookieMetadataFactory = $ this ->createPartialMock (
59
- \ Magento \ Framework \ Stdlib \ Cookie \ CookieMetadataFactory::class,
72
+ CookieMetadataFactory::class,
60
73
['createPublicCookieMetadata ' ]
61
74
);
62
75
63
- $ this ->config = $ this ->createPartialMock (\ Magento \ Backend \ App \ Config::class, ['getValue ' ]);
76
+ $ this ->config = $ this ->createPartialMock (Config::class, ['getValue ' ]);
64
77
$ this ->cookieManager = $ this ->createPartialMock (
65
- \ Magento \ Framework \ Stdlib \ Cookie \ PhpCookieManager::class,
78
+ PhpCookieManager::class,
66
79
['getCookie ' , 'setPublicCookie ' ]
67
80
);
68
81
$ this ->storage = $ this ->createPartialMock (
69
- \ Magento \ Framework \ Session \ Storage::class,
82
+ Storage::class,
70
83
['getUser ' , 'getAcl ' , 'setAcl ' ]
71
84
);
72
85
$ this ->sessionConfig = $ this ->createPartialMock (
73
86
\Magento \Framework \Session \Config::class,
74
- ['getCookiePath ' , 'getCookieDomain ' , 'getCookieSecure ' , 'getCookieHttpOnly ' ]
87
+ [
88
+ 'getCookiePath ' ,
89
+ 'getCookieDomain ' ,
90
+ 'getCookieSecure ' ,
91
+ 'getCookieHttpOnly ' ,
92
+ 'getCookieSameSite '
93
+ ]
75
94
);
76
- $ this ->aclBuilder = $ this ->getMockBuilder (\ Magento \ Framework \ Acl \ Builder::class)
95
+ $ this ->aclBuilder = $ this ->getMockBuilder (Builder::class)
77
96
->disableOriginalConstructor ()
78
97
->getMock ();
79
98
$ objectManager = new ObjectManager ($ this );
80
99
$ this ->session = $ objectManager ->getObject (
81
- \ Magento \ Backend \ Model \ Auth \ Session::class,
100
+ Session::class,
82
101
[
83
102
'config ' => $ this ->config ,
84
103
'sessionConfig ' => $ this ->sessionConfig ,
@@ -90,7 +109,7 @@ protected function setUp()
90
109
);
91
110
}
92
111
93
- protected function tearDown ()
112
+ protected function tearDown (): void
94
113
{
95
114
$ this ->config = null ;
96
115
$ this ->sessionConfig = null ;
@@ -103,9 +122,11 @@ protected function tearDown()
103
122
*/
104
123
public function testRefreshAcl ($ isUserPassedViaParams )
105
124
{
106
- $ aclMock = $ this ->getMockBuilder (\Magento \Framework \Acl::class)->disableOriginalConstructor ()->getMock ();
125
+ $ aclMock = $ this ->getMockBuilder (Acl::class)
126
+ ->disableOriginalConstructor ()
127
+ ->getMock ();
107
128
$ this ->aclBuilder ->expects ($ this ->any ())->method ('getAcl ' )->willReturn ($ aclMock );
108
- $ userMock = $ this ->getMockBuilder (\ Magento \ User \ Model \ User::class)
129
+ $ userMock = $ this ->getMockBuilder (User::class)
109
130
->setMethods (['getReloadAclFlag ' , 'setReloadAclFlag ' , 'unsetData ' , 'save ' ])
110
131
->disableOriginalConstructor ()
111
132
->getMock ();
@@ -136,14 +157,14 @@ public function refreshAclDataProvider()
136
157
137
158
public function testIsLoggedInPositive ()
138
159
{
139
- $ user = $ this ->createPartialMock (\ Magento \ User \ Model \ User::class, ['getId ' , '__wakeup ' ]);
160
+ $ user = $ this ->createPartialMock (User::class, ['getId ' , '__wakeup ' ]);
140
161
$ user ->expects ($ this ->once ())
141
162
->method ('getId ' )
142
- ->will ( $ this -> returnValue ( 1 ) );
163
+ ->willReturn ( 1 );
143
164
144
165
$ this ->storage ->expects ($ this ->any ())
145
166
->method ('getUser ' )
146
- ->will ( $ this -> returnValue ( $ user) );
167
+ ->willReturn ( $ user );
147
168
148
169
$ this ->assertTrue ($ this ->session ->isLoggedIn ());
149
170
}
@@ -160,54 +181,55 @@ public function testProlong()
160
181
161
182
$ this ->config ->expects ($ this ->once ())
162
183
->method ('getValue ' )
163
- ->with (\ Magento \ Backend \ Model \ Auth \ Session::XML_PATH_SESSION_LIFETIME )
184
+ ->with (Session::XML_PATH_SESSION_LIFETIME )
164
185
->willReturn ($ lifetime );
165
- $ cookieMetadata = $ this ->createMock (\ Magento \ Framework \ Stdlib \ Cookie \ PublicCookieMetadata::class);
186
+ $ cookieMetadata = $ this ->createMock (PublicCookieMetadata::class);
166
187
$ cookieMetadata ->expects ($ this ->once ())
167
188
->method ('setDuration ' )
168
- ->with ($ lifetime )
169
- ->will ($ this ->returnSelf ());
189
+ ->with ($ lifetime )->willReturnSelf ();
170
190
$ cookieMetadata ->expects ($ this ->once ())
171
191
->method ('setPath ' )
172
- ->with ($ path )
173
- ->will ($ this ->returnSelf ());
192
+ ->with ($ path )->willReturnSelf ();
174
193
$ cookieMetadata ->expects ($ this ->once ())
175
194
->method ('setDomain ' )
176
- ->with ($ domain )
177
- ->will ($ this ->returnSelf ());
195
+ ->with ($ domain )->willReturnSelf ();
178
196
$ cookieMetadata ->expects ($ this ->once ())
179
197
->method ('setSecure ' )
180
- ->with ($ secure )
181
- ->will ($ this ->returnSelf ());
198
+ ->with ($ secure )->willReturnSelf ();
182
199
$ cookieMetadata ->expects ($ this ->once ())
183
200
->method ('setHttpOnly ' )
184
- ->with ($ httpOnly )
185
- ->will ($ this ->returnSelf ());
201
+ ->with ($ httpOnly )->willReturnSelf ();
202
+ $ cookieMetadata ->expects ($ this ->once ())
203
+ ->method ('setSameSite ' )
204
+ ->willReturnSelf ();
186
205
187
206
$ this ->cookieMetadataFactory ->expects ($ this ->once ())
188
207
->method ('createPublicCookieMetadata ' )
189
- ->will ( $ this -> returnValue ( $ cookieMetadata) );
208
+ ->willReturn ( $ cookieMetadata );
190
209
191
210
$ this ->cookieManager ->expects ($ this ->once ())
192
211
->method ('getCookie ' )
193
212
->with ($ name )
194
- ->will ( $ this -> returnValue ( $ cookie) );
213
+ ->willReturn ( $ cookie );
195
214
$ this ->cookieManager ->expects ($ this ->once ())
196
215
->method ('setPublicCookie ' )
197
216
->with ($ name , $ cookie , $ cookieMetadata );
198
217
199
218
$ this ->sessionConfig ->expects ($ this ->once ())
200
219
->method ('getCookiePath ' )
201
- ->will ( $ this -> returnValue ( $ path) );
220
+ ->willReturn ( $ path );
202
221
$ this ->sessionConfig ->expects ($ this ->once ())
203
222
->method ('getCookieDomain ' )
204
- ->will ( $ this -> returnValue ( $ domain) );
223
+ ->willReturn ( $ domain );
205
224
$ this ->sessionConfig ->expects ($ this ->once ())
206
225
->method ('getCookieSecure ' )
207
- ->will ( $ this -> returnValue ( $ secure) );
226
+ ->willReturn ( $ secure );
208
227
$ this ->sessionConfig ->expects ($ this ->once ())
209
228
->method ('getCookieHttpOnly ' )
210
- ->will ($ this ->returnValue ($ httpOnly ));
229
+ ->willReturn ($ httpOnly );
230
+ $ this ->sessionConfig ->expects ($ this ->once ())
231
+ ->method ('getCookieSameSite ' )
232
+ ->willReturn ('Lax ' );
211
233
212
234
$ this ->session ->prolong ();
213
235
@@ -225,15 +247,21 @@ public function testIsAllowed($isUserDefined, $isAclDefined, $isAllowed, $expect
225
247
{
226
248
$ userAclRole = 'userAclRole ' ;
227
249
if ($ isAclDefined ) {
228
- $ aclMock = $ this ->getMockBuilder (\Magento \Framework \Acl::class)->disableOriginalConstructor ()->getMock ();
250
+ $ aclMock = $ this ->getMockBuilder (Acl::class)
251
+ ->disableOriginalConstructor ()
252
+ ->getMock ();
229
253
$ this ->storage ->expects ($ this ->any ())->method ('getAcl ' )->willReturn ($ aclMock );
230
254
}
231
255
if ($ isUserDefined ) {
232
- $ userMock = $ this ->getMockBuilder (\Magento \User \Model \User::class)->disableOriginalConstructor ()->getMock ();
256
+ $ userMock = $ this ->getMockBuilder (User::class)
257
+ ->disableOriginalConstructor ()
258
+ ->getMock ();
233
259
$ this ->storage ->expects ($ this ->once ())->method ('getUser ' )->willReturn ($ userMock );
234
260
}
235
261
if ($ isAclDefined && $ isUserDefined ) {
262
+ // phpstan:ignore
236
263
$ userMock ->expects ($ this ->any ())->method ('getAclRole ' )->willReturn ($ userAclRole );
264
+ // phpstan:ignore
237
265
$ aclMock ->expects ($ this ->once ())->method ('isAllowed ' )->with ($ userAclRole )->willReturn ($ isAllowed );
238
266
}
239
267
0 commit comments