8
8
9
9
namespace Magento \Customer \Test \Unit \Model \Account ;
10
10
11
- use Magento \Framework \Stdlib \Cookie \CookieMetadataFactory ;
12
11
use Magento \Customer \Model \Account \Redirect ;
13
12
use Magento \Customer \Model \Url as CustomerUrl ;
14
13
use Magento \Framework \Controller \ResultFactory ;
14
+ use Magento \Framework \Stdlib \Cookie \CookieMetadataFactory ;
15
+ use Magento \Framework \Stdlib \Cookie \PublicCookieMetadata ;
16
+ use Magento \Framework \Stdlib \CookieManagerInterface ;
15
17
use Magento \Framework \TestFramework \Unit \Helper \ObjectManager ;
16
18
use Magento \Framework \Url \HostChecker ;
17
19
use Magento \Store \Model \ScopeInterface ;
20
+ use PHPUnit \Framework \TestCase ;
21
+ use PHPUnit \FrameworkMockObject \MockObject ;
18
22
19
23
/**
20
24
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
21
25
*/
22
- class RedirectTest extends \ PHPUnit \ Framework \ TestCase
26
+ class RedirectTest extends TestCase
23
27
{
24
28
/**
25
29
* @var Redirect
26
30
*/
27
31
protected $ model ;
28
32
29
33
/**
30
- * @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Framework\App\RequestInterface
34
+ * @var MockObject| \Magento\Framework\App\RequestInterface
31
35
*/
32
36
protected $ request ;
33
37
34
38
/**
35
- * @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Customer\Model\Session
39
+ * @var MockObject| \Magento\Customer\Model\Session
36
40
*/
37
41
protected $ customerSession ;
38
42
39
43
/**
40
- * @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Framework\App\Config\ScopeConfigInterface
44
+ * @var MockObject| \Magento\Framework\App\Config\ScopeConfigInterface
41
45
*/
42
46
protected $ scopeConfig ;
43
47
44
48
/**
45
- * @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Store\Model\StoreManagerInterface
49
+ * @var MockObject| \Magento\Store\Model\StoreManagerInterface
46
50
*/
47
51
protected $ storeManager ;
48
52
49
53
/**
50
- * @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Store\Model\Store
54
+ * @var MockObject| \Magento\Store\Model\Store
51
55
*/
52
56
protected $ store ;
53
57
54
58
/**
55
- * @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Framework\UrlInterface
59
+ * @var MockObject| \Magento\Framework\UrlInterface
56
60
*/
57
61
protected $ url ;
58
62
59
63
/**
60
- * @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Framework\Url\DecoderInterface
64
+ * @var MockObject| \Magento\Framework\Url\DecoderInterface
61
65
*/
62
66
protected $ urlDecoder ;
63
67
64
68
/**
65
- * @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Customer\Model\Url
69
+ * @var MockObject| \Magento\Customer\Model\Url
66
70
*/
67
71
protected $ customerUrl ;
68
72
69
73
/**
70
- * @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Framework\Controller\Result\Redirect
74
+ * @var MockObject| \Magento\Framework\Controller\Result\Redirect
71
75
*/
72
76
protected $ resultRedirect ;
73
77
74
78
/**
75
- * @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Framework\Controller\Result\Forward
79
+ * @var MockObject| \Magento\Framework\Controller\Result\Forward
76
80
*/
77
81
protected $ resultForward ;
78
82
79
83
/**
80
- * @var ResultFactory | \PHPUnit_Framework_MockObject_MockObject
84
+ * @var ResultFactory|MockObject
81
85
*/
82
86
protected $ resultFactory ;
83
87
84
88
/**
85
- * @var CookieMetadataFactory | \PHPUnit_Framework_MockObject_MockObject
89
+ * @var CookieMetadataFactory|MockObject
86
90
*/
87
91
protected $ cookieMetadataFactory ;
88
92
89
93
/**
90
- * @var HostChecker | \PHPUnit_Framework_MockObject_MockObject
94
+ * @var HostChecker|MockObject
91
95
*/
92
96
private $ hostChecker ;
93
97
98
+ /**
99
+ * @inheritdoc
100
+ */
94
101
protected function setUp ()
95
102
{
96
103
$ this ->request = $ this ->getMockForAbstractClass (\Magento \Framework \App \RequestInterface::class);
97
-
98
104
$ this ->customerSession = $ this ->getMockBuilder (\Magento \Customer \Model \Session::class)
99
105
->disableOriginalConstructor ()
100
106
->setMethods (
@@ -117,44 +123,27 @@ protected function setUp()
117
123
->getMock ();
118
124
119
125
$ this ->scopeConfig = $ this ->getMockForAbstractClass (\Magento \Framework \App \Config \ScopeConfigInterface::class);
120
-
121
- $ this ->store = $ this ->getMockBuilder (\Magento \Store \Model \Store::class)
122
- ->disableOriginalConstructor ()
123
- ->getMock ();
124
-
126
+ $ this ->store = $ this ->createMock (\Magento \Store \Model \Store::class);
125
127
$ this ->storeManager = $ this ->getMockForAbstractClass (\Magento \Store \Model \StoreManagerInterface::class);
126
- $ this ->storeManager ->expects ($ this ->once ())
127
- ->method ('getStore ' )
128
- ->willReturn ($ this ->store );
129
-
130
128
$ this ->url = $ this ->getMockForAbstractClass (\Magento \Framework \UrlInterface::class);
131
129
$ this ->urlDecoder = $ this ->getMockForAbstractClass (\Magento \Framework \Url \DecoderInterface::class);
132
-
133
130
$ this ->customerUrl = $ this ->getMockBuilder (\Magento \Customer \Model \Url::class)
134
- ->setMethods (['DashboardUrl ' , 'getAccountUrl ' , 'getLoginUrl ' , 'getLogoutUrl ' , 'getDashboardUrl ' ])
135
- ->disableOriginalConstructor ()
136
- ->getMock ();
137
-
138
- $ this ->resultRedirect = $ this ->getMockBuilder (\Magento \Framework \Controller \Result \Redirect::class)
139
- ->disableOriginalConstructor ()
140
- ->getMock ();
141
-
142
- $ this ->resultForward = $ this ->getMockBuilder (\Magento \Framework \Controller \Result \Forward::class)
143
- ->disableOriginalConstructor ()
144
- ->getMock ();
145
-
146
- $ this ->resultFactory = $ this ->getMockBuilder (\Magento \Framework \Controller \ResultFactory::class)
147
- ->disableOriginalConstructor ()
148
- ->getMock ();
149
-
150
- $ this ->cookieMetadataFactory = $ this ->getMockBuilder (CookieMetadataFactory::class)
151
- ->disableOriginalConstructor ()
152
- ->getMock ();
153
-
154
- $ this ->hostChecker = $ this ->getMockBuilder (HostChecker::class)
155
- ->disableOriginalConstructor ()
131
+ ->setMethods (
132
+ [
133
+ 'DashboardUrl ' ,
134
+ 'getAccountUrl ' ,
135
+ 'getLoginUrl ' ,
136
+ 'getLogoutUrl ' ,
137
+ 'getDashboardUrl '
138
+ ]
139
+ )->disableOriginalConstructor ()
156
140
->getMock ();
157
141
142
+ $ this ->resultRedirect = $ this ->createMock (\Magento \Framework \Controller \Result \Redirect::class);
143
+ $ this ->resultForward = $ this ->createMock (\Magento \Framework \Controller \Result \Forward::class);
144
+ $ this ->resultFactory = $ this ->createMock (\Magento \Framework \Controller \ResultFactory::class);
145
+ $ this ->cookieMetadataFactory = $ this ->createMock (CookieMetadataFactory::class);
146
+ $ this ->hostChecker = $ this ->createMock (HostChecker::class);
158
147
$ objectManager = new ObjectManager ($ this );
159
148
$ this ->model = $ objectManager ->getObject (
160
149
\Magento \Customer \Model \Account \Redirect::class,
@@ -174,6 +163,8 @@ protected function setUp()
174
163
}
175
164
176
165
/**
166
+ * Verify get redirect method
167
+ *
177
168
* @param int $customerId
178
169
* @param int $lastCustomerId
179
170
* @param string $referer
@@ -214,6 +205,9 @@ public function testGetRedirect(
214
205
->willReturnSelf ();
215
206
216
207
// Preparations for method prepareRedirectUrl()
208
+ $ this ->storeManager ->expects ($ this ->once ())
209
+ ->method ('getStore ' )
210
+ ->willReturn ($ this ->store );
217
211
$ this ->store ->expects ($ this ->once ())->method ('getBaseUrl ' )->willReturn ($ baseUrl );
218
212
219
213
$ this ->customerSession ->expects ($ this ->any ())->method ('getBeforeAuthUrl ' )->willReturn ($ beforeAuthUrl );
@@ -257,6 +251,8 @@ public function testGetRedirect(
257
251
}
258
252
259
253
/**
254
+ * Redirect data provider
255
+ *
260
256
* @return array
261
257
*/
262
258
public function getRedirectDataProvider ()
@@ -305,7 +301,12 @@ public function getRedirectDataProvider()
305
301
];
306
302
}
307
303
308
- public function testBeforeRequestParams ()
304
+ /**
305
+ * Verify before request params
306
+ *
307
+ * @return void
308
+ */
309
+ public function testBeforeRequestParams (): void
309
310
{
310
311
$ requestParams = [
311
312
'param1 ' => 'value1 ' ,
@@ -315,6 +316,9 @@ public function testBeforeRequestParams()
315
316
$ controller = 'controller ' ;
316
317
$ action = 'action ' ;
317
318
319
+ $ this ->storeManager ->expects ($ this ->once ())
320
+ ->method ('getStore ' )
321
+ ->willReturn ($ this ->store );
318
322
$ this ->customerSession ->expects ($ this ->exactly (2 ))
319
323
->method ('getBeforeRequestParams ' )
320
324
->willReturn ($ requestParams );
@@ -327,7 +331,6 @@ public function testBeforeRequestParams()
327
331
$ this ->customerSession ->expects ($ this ->once ())
328
332
->method ('getBeforeAction ' )
329
333
->willReturn ($ action );
330
-
331
334
$ this ->resultForward ->expects ($ this ->once ())
332
335
->method ('setParams ' )
333
336
->with ($ requestParams )
@@ -344,7 +347,6 @@ public function testBeforeRequestParams()
344
347
->method ('forward ' )
345
348
->with ($ action )
346
349
->willReturnSelf ();
347
-
348
350
$ this ->resultFactory ->expects ($ this ->once ())
349
351
->method ('create ' )
350
352
->with (ResultFactory::TYPE_FORWARD )
@@ -353,4 +355,82 @@ public function testBeforeRequestParams()
353
355
$ result = $ this ->model ->getRedirect ();
354
356
$ this ->assertSame ($ this ->resultForward , $ result );
355
357
}
358
+
359
+ /**
360
+ * Verify set redirect cokkie method
361
+ *
362
+ * @return void
363
+ */
364
+ public function testSetRedirectCookie (): void
365
+ {
366
+ $ coockieManagerMock = $ this ->createMock (CookieManagerInterface::class);
367
+ $ publicMetadataMock = $ this ->createMock (PublicCookieMetadata::class);
368
+ $ routeMock = 'route ' ;
369
+
370
+ $ this ->model ->setCookieManager ($ coockieManagerMock );
371
+
372
+ $ this ->storeManager ->expects ($ this ->once ())
373
+ ->method ('getStore ' )
374
+ ->willReturn ($ this ->store );
375
+ $ this ->store ->expects ($ this ->once ())
376
+ ->method ('getStorePath ' )
377
+ ->willReturn ('storePath ' );
378
+ $ this ->cookieMetadataFactory ->expects ($ this ->once ())
379
+ ->method ('createPublicCookieMetadata ' )
380
+ ->willReturn ($ publicMetadataMock );
381
+ $ publicMetadataMock ->expects ($ this ->once ())
382
+ ->method ('setHttpOnly ' )
383
+ ->with (true )
384
+ ->willReturnSelf ();
385
+ $ publicMetadataMock ->expects ($ this ->once ())
386
+ ->method ('setDuration ' )
387
+ ->with (3600 )
388
+ ->willReturnSelf ();
389
+ $ publicMetadataMock ->expects ($ this ->once ())
390
+ ->method ('setPath ' )
391
+ ->with ('storePath ' )
392
+ ->willReturnSelf ();
393
+ $ coockieManagerMock ->expects ($ this ->once ())
394
+ ->method ('setPublicCookie ' )
395
+ ->with (
396
+ Redirect::LOGIN_REDIRECT_URL ,
397
+ $ routeMock ,
398
+ $ publicMetadataMock
399
+ );
400
+ $ this ->model ->setRedirectCookie ($ routeMock );
401
+ }
402
+
403
+ /**
404
+ * Verify clear redirect cookie
405
+ *
406
+ * @return void
407
+ */
408
+ public function testClearRedirectCookie (): void
409
+ {
410
+ $ coockieManagerMock = $ this ->createMock (CookieManagerInterface::class);
411
+ $ publicMetadataMock = $ this ->createMock (PublicCookieMetadata::class);
412
+
413
+ $ this ->model ->setCookieManager ($ coockieManagerMock );
414
+
415
+ $ this ->storeManager ->expects ($ this ->once ())
416
+ ->method ('getStore ' )
417
+ ->willReturn ($ this ->store );
418
+ $ this ->store ->expects ($ this ->once ())
419
+ ->method ('getStorePath ' )
420
+ ->willReturn ('storePath ' );
421
+ $ this ->cookieMetadataFactory ->expects ($ this ->once ())
422
+ ->method ('createPublicCookieMetadata ' )
423
+ ->willReturn ($ publicMetadataMock );
424
+ $ publicMetadataMock ->expects ($ this ->once ())
425
+ ->method ('setPath ' )
426
+ ->with ('storePath ' )
427
+ ->willReturnSelf ();
428
+ $ coockieManagerMock ->expects ($ this ->once ())
429
+ ->method ('deleteCookie ' )
430
+ ->with (
431
+ Redirect::LOGIN_REDIRECT_URL ,
432
+ $ publicMetadataMock
433
+ );
434
+ $ this ->model ->clearRedirectCookie ();
435
+ }
356
436
}
0 commit comments