Skip to content

Commit 511e16a

Browse files
committed
refactor cover changes with unit test
1 parent f021f1e commit 511e16a

File tree

3 files changed

+150
-65
lines changed

3 files changed

+150
-65
lines changed

app/code/Magento/Customer/Model/Account/Redirect.php

100755100644
Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,19 @@
77

88
use Magento\Customer\Model\Session;
99
use Magento\Customer\Model\Url as CustomerUrl;
10+
use Magento\Framework\App\Config\ScopeConfigInterface;
11+
use Magento\Framework\App\ObjectManager;
1012
use Magento\Framework\App\RequestInterface;
13+
use Magento\Framework\Controller\Result\Forward as ResultForward;
14+
use Magento\Framework\Controller\Result\Redirect as ResultRedirect;
1115
use Magento\Framework\Controller\ResultFactory;
1216
use Magento\Framework\Stdlib\Cookie\CookieMetadataFactory;
17+
use Magento\Framework\Stdlib\CookieManagerInterface;
18+
use Magento\Framework\Url\DecoderInterface;
1319
use Magento\Framework\Url\HostChecker;
1420
use Magento\Framework\UrlInterface;
1521
use Magento\Store\Model\ScopeInterface;
1622
use Magento\Store\Model\StoreManagerInterface;
17-
use Magento\Framework\App\Config\ScopeConfigInterface;
18-
use Magento\Framework\Controller\Result\Redirect as ResultRedirect;
19-
use Magento\Framework\Controller\Result\Forward as ResultForward;
20-
use Magento\Framework\Url\DecoderInterface;
21-
use Magento\Framework\App\ObjectManager;
22-
use Magento\Framework\Stdlib\CookieManagerInterface;
2323

2424
/**
2525
* Account Redirect
@@ -298,11 +298,14 @@ public function getRedirectCookie()
298298
*/
299299
public function setRedirectCookie($route)
300300
{
301-
$cookieMetadata = $this->cookieMetadataFactory->createPublicCookieMetadata()
302-
->setHttpOnly(true)
303-
->setDuration(3600)
304-
->setPath($this->storeManager->getStore()->getStorePath());
305-
$this->getCookieManager()->setPublicCookie(self::LOGIN_REDIRECT_URL, $route, $cookieMetadata);
301+
$this->getCookieManager()->setPublicCookie(
302+
self::LOGIN_REDIRECT_URL,
303+
$route,
304+
$this->cookieMetadataFactory->createPublicCookieMetadata()
305+
->setHttpOnly(true)
306+
->setDuration(3600)
307+
->setPath($this->storeManager->getStore()->getStorePath())
308+
);
306309
}
307310

308311
/**
@@ -314,8 +317,10 @@ public function setRedirectCookie($route)
314317
*/
315318
public function clearRedirectCookie()
316319
{
317-
$cookieMetadata = $this->cookieMetadataFactory->createPublicCookieMetadata()
318-
->setPath($this->storeManager->getStore()->getStorePath());
319-
$this->getCookieManager()->deleteCookie(self::LOGIN_REDIRECT_URL, $cookieMetadata);
320+
$this->getCookieManager()->deleteCookie(
321+
self::LOGIN_REDIRECT_URL,
322+
$this->cookieMetadataFactory->createPublicCookieMetadata()
323+
->setPath($this->storeManager->getStore()->getStorePath())
324+
);
320325
}
321326
}

app/code/Magento/Customer/Test/Unit/Model/Account/RedirectTest.php

100755100644
Lines changed: 131 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -8,93 +8,99 @@
88

99
namespace Magento\Customer\Test\Unit\Model\Account;
1010

11-
use Magento\Framework\Stdlib\Cookie\CookieMetadataFactory;
1211
use Magento\Customer\Model\Account\Redirect;
1312
use Magento\Customer\Model\Url as CustomerUrl;
1413
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;
1517
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
1618
use Magento\Framework\Url\HostChecker;
1719
use Magento\Store\Model\ScopeInterface;
20+
use PHPUnit\Framework\TestCase;
21+
use PHPUnit\FrameworkMockObject\MockObject;
1822

1923
/**
2024
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2125
*/
22-
class RedirectTest extends \PHPUnit\Framework\TestCase
26+
class RedirectTest extends TestCase
2327
{
2428
/**
2529
* @var Redirect
2630
*/
2731
protected $model;
2832

2933
/**
30-
* @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Framework\App\RequestInterface
34+
* @var MockObject|\Magento\Framework\App\RequestInterface
3135
*/
3236
protected $request;
3337

3438
/**
35-
* @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Customer\Model\Session
39+
* @var MockObject|\Magento\Customer\Model\Session
3640
*/
3741
protected $customerSession;
3842

3943
/**
40-
* @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Framework\App\Config\ScopeConfigInterface
44+
* @var MockObject|\Magento\Framework\App\Config\ScopeConfigInterface
4145
*/
4246
protected $scopeConfig;
4347

4448
/**
45-
* @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Store\Model\StoreManagerInterface
49+
* @var MockObject|\Magento\Store\Model\StoreManagerInterface
4650
*/
4751
protected $storeManager;
4852

4953
/**
50-
* @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Store\Model\Store
54+
* @var MockObject|\Magento\Store\Model\Store
5155
*/
5256
protected $store;
5357

5458
/**
55-
* @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Framework\UrlInterface
59+
* @var MockObject|\Magento\Framework\UrlInterface
5660
*/
5761
protected $url;
5862

5963
/**
60-
* @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Framework\Url\DecoderInterface
64+
* @var MockObject|\Magento\Framework\Url\DecoderInterface
6165
*/
6266
protected $urlDecoder;
6367

6468
/**
65-
* @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Customer\Model\Url
69+
* @var MockObject|\Magento\Customer\Model\Url
6670
*/
6771
protected $customerUrl;
6872

6973
/**
70-
* @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Framework\Controller\Result\Redirect
74+
* @var MockObject|\Magento\Framework\Controller\Result\Redirect
7175
*/
7276
protected $resultRedirect;
7377

7478
/**
75-
* @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Framework\Controller\Result\Forward
79+
* @var MockObject|\Magento\Framework\Controller\Result\Forward
7680
*/
7781
protected $resultForward;
7882

7983
/**
80-
* @var ResultFactory | \PHPUnit_Framework_MockObject_MockObject
84+
* @var ResultFactory|MockObject
8185
*/
8286
protected $resultFactory;
8387

8488
/**
85-
* @var CookieMetadataFactory | \PHPUnit_Framework_MockObject_MockObject
89+
* @var CookieMetadataFactory|MockObject
8690
*/
8791
protected $cookieMetadataFactory;
8892

8993
/**
90-
* @var HostChecker | \PHPUnit_Framework_MockObject_MockObject
94+
* @var HostChecker|MockObject
9195
*/
9296
private $hostChecker;
9397

98+
/**
99+
* @inheritdoc
100+
*/
94101
protected function setUp()
95102
{
96103
$this->request = $this->getMockForAbstractClass(\Magento\Framework\App\RequestInterface::class);
97-
98104
$this->customerSession = $this->getMockBuilder(\Magento\Customer\Model\Session::class)
99105
->disableOriginalConstructor()
100106
->setMethods(
@@ -117,44 +123,27 @@ protected function setUp()
117123
->getMock();
118124

119125
$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);
125127
$this->storeManager = $this->getMockForAbstractClass(\Magento\Store\Model\StoreManagerInterface::class);
126-
$this->storeManager->expects($this->once())
127-
->method('getStore')
128-
->willReturn($this->store);
129-
130128
$this->url = $this->getMockForAbstractClass(\Magento\Framework\UrlInterface::class);
131129
$this->urlDecoder = $this->getMockForAbstractClass(\Magento\Framework\Url\DecoderInterface::class);
132-
133130
$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()
156140
->getMock();
157141

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);
158147
$objectManager = new ObjectManager($this);
159148
$this->model = $objectManager->getObject(
160149
\Magento\Customer\Model\Account\Redirect::class,
@@ -174,6 +163,8 @@ protected function setUp()
174163
}
175164

176165
/**
166+
* Verify get redirect method
167+
*
177168
* @param int $customerId
178169
* @param int $lastCustomerId
179170
* @param string $referer
@@ -214,6 +205,9 @@ public function testGetRedirect(
214205
->willReturnSelf();
215206

216207
// Preparations for method prepareRedirectUrl()
208+
$this->storeManager->expects($this->once())
209+
->method('getStore')
210+
->willReturn($this->store);
217211
$this->store->expects($this->once())->method('getBaseUrl')->willReturn($baseUrl);
218212

219213
$this->customerSession->expects($this->any())->method('getBeforeAuthUrl')->willReturn($beforeAuthUrl);
@@ -257,6 +251,8 @@ public function testGetRedirect(
257251
}
258252

259253
/**
254+
* Redirect data provider
255+
*
260256
* @return array
261257
*/
262258
public function getRedirectDataProvider()
@@ -305,7 +301,12 @@ public function getRedirectDataProvider()
305301
];
306302
}
307303

308-
public function testBeforeRequestParams()
304+
/**
305+
* Verify before request params
306+
*
307+
* @return void
308+
*/
309+
public function testBeforeRequestParams(): void
309310
{
310311
$requestParams = [
311312
'param1' => 'value1',
@@ -315,6 +316,9 @@ public function testBeforeRequestParams()
315316
$controller = 'controller';
316317
$action = 'action';
317318

319+
$this->storeManager->expects($this->once())
320+
->method('getStore')
321+
->willReturn($this->store);
318322
$this->customerSession->expects($this->exactly(2))
319323
->method('getBeforeRequestParams')
320324
->willReturn($requestParams);
@@ -327,7 +331,6 @@ public function testBeforeRequestParams()
327331
$this->customerSession->expects($this->once())
328332
->method('getBeforeAction')
329333
->willReturn($action);
330-
331334
$this->resultForward->expects($this->once())
332335
->method('setParams')
333336
->with($requestParams)
@@ -344,7 +347,6 @@ public function testBeforeRequestParams()
344347
->method('forward')
345348
->with($action)
346349
->willReturnSelf();
347-
348350
$this->resultFactory->expects($this->once())
349351
->method('create')
350352
->with(ResultFactory::TYPE_FORWARD)
@@ -353,4 +355,82 @@ public function testBeforeRequestParams()
353355
$result = $this->model->getRedirect();
354356
$this->assertSame($this->resultForward, $result);
355357
}
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+
}
356436
}

app/code/Magento/Customer/etc/di.xml

100755100644
File mode changed.

0 commit comments

Comments
 (0)