Skip to content

Commit 67f340b

Browse files
committed
Issue 35838: add unit test
1 parent e0324ae commit 67f340b

File tree

2 files changed

+125
-5
lines changed

2 files changed

+125
-5
lines changed

app/code/Magento/Customer/Model/Url.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,24 @@
1717
* Class Customer url model
1818
*
1919
* @api
20+
* @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
2021
*/
2122
class Url
2223
{
2324
/**
2425
* Route for customer account login page
2526
*/
26-
const ROUTE_ACCOUNT_LOGIN = 'customer/account/login';
27+
public const ROUTE_ACCOUNT_LOGIN = 'customer/account/login';
2728

2829
/**
2930
* Config name for Redirect Customer to Account Dashboard after Logging in setting
3031
*/
31-
const XML_PATH_CUSTOMER_STARTUP_REDIRECT_TO_DASHBOARD = 'customer/startup/redirect_dashboard';
32+
public const XML_PATH_CUSTOMER_STARTUP_REDIRECT_TO_DASHBOARD = 'customer/startup/redirect_dashboard';
3233

3334
/**
3435
* Query param name for last url visited
3536
*/
36-
const REFERER_QUERY_PARAM_NAME = 'referer';
37+
public const REFERER_QUERY_PARAM_NAME = 'referer';
3738

3839
/**
3940
* @var UrlInterface
@@ -127,7 +128,7 @@ public function getLoginUrlParams()
127128
&& $this->request->isGet()
128129
) {
129130
$refererUrl = $this->urlBuilder->getUrl('*/*/*', ['_current' => true, '_use_rewrite' => true]);
130-
if(!$this->isNoRouteUrl($refererUrl)) {
131+
if (!$this->isNoRouteUrl($refererUrl)) {
131132
$referer = $this->urlEncoder->encode($refererUrl);
132133
}
133134
}
@@ -248,6 +249,8 @@ public function getEmailConfirmationUrl($email = null)
248249
}
249250

250251
/**
252+
* Getting request referrer
253+
*
251254
* @return mixed|null
252255
*/
253256
private function getRequestReferrer()
@@ -259,6 +262,12 @@ private function getRequestReferrer()
259262
return null;
260263
}
261264

265+
/**
266+
* Check if Referrer url is no route url
267+
*
268+
* @param string $url
269+
* @return bool
270+
*/
262271
private function isNoRouteUrl($url)
263272
{
264273
$defaultNoRouteUrl = $this->scopeConfig->getValue(
@@ -268,7 +277,7 @@ private function isNoRouteUrl($url)
268277
$noRouteUrl = $this->urlBuilder->getUrl($defaultNoRouteUrl);
269278
if (strpos($url, $noRouteUrl) !== false) {
270279
return true;
271-
};
280+
}
272281
return false;
273282
}
274283
}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Customer\Test\Unit\Model;
9+
10+
use Magento\Customer\Model\Session;
11+
use Magento\Customer\Model\Url;
12+
use Magento\Framework\App\Config\ScopeConfigInterface;
13+
use Magento\Framework\App\RequestInterface;
14+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
15+
use Magento\Framework\UrlInterface;
16+
use PHPUnit\Framework\MockObject\MockObject;
17+
use PHPUnit\Framework\TestCase;
18+
19+
/**
20+
* Unit test for \Magento\Customer\Model\Url
21+
*/
22+
class UrlTest extends TestCase
23+
{
24+
/**
25+
* @var ScopeConfigInterface|MockObject
26+
*/
27+
protected $scopeConfigMock;
28+
29+
/**
30+
* @var RequestInterface|MockObject
31+
*/
32+
protected $requestMock;
33+
34+
/**
35+
* @var Session|MockObject
36+
*/
37+
protected $customerSessionMock;
38+
39+
/**
40+
* @var UrlInterface|MockObject
41+
*/
42+
protected $urlBuilderMock;
43+
44+
/**
45+
* @var ObjectManager
46+
*/
47+
protected $objectManager;
48+
49+
/**
50+
* @var Url
51+
*/
52+
protected $model;
53+
54+
/**
55+
* @inheritdoc
56+
*/
57+
protected function setUp(): void
58+
{
59+
$this->objectManager = new ObjectManager($this);
60+
61+
$this->scopeConfigMock = $this->getMockBuilder(ScopeConfigInterface::class)
62+
->disableOriginalConstructor()
63+
->getMock();
64+
$this->requestMock = $this->getMockBuilder(RequestInterface::class)
65+
->disableOriginalConstructor()
66+
->setMethods(['isGet'])
67+
->getMockForAbstractClass();
68+
$this->customerSessionMock = $this->getMockBuilder(Session::class)
69+
->disableOriginalConstructor()
70+
->setMethods(['getNoReferer'])
71+
->getMock();
72+
$this->urlBuilderMock = $this->getMockBuilder(UrlInterface::class)
73+
->disableOriginalConstructor()
74+
->getMock();
75+
76+
$this->model = $this->objectManager->getObject(
77+
Url::class,
78+
[
79+
'scopeConfig' => $this->scopeConfigMock,
80+
'request' => $this->requestMock,
81+
'customerSession' => $this->customerSessionMock,
82+
'urlBuilder' => $this->urlBuilderMock
83+
]
84+
);
85+
}
86+
87+
/**
88+
* @return void
89+
*/
90+
public function testGetLoginUrlParamsForNoRouteReferrer()
91+
{
92+
$this->requestMock->expects($this->any())
93+
->method('getParam')
94+
->with(Url::REFERER_QUERY_PARAM_NAME)
95+
->willReturn(null);
96+
$this->scopeConfigMock->expects($this->any())
97+
->method('isSetFlag')
98+
->willReturn(false);
99+
$this->customerSessionMock->expects($this->any())
100+
->method('getNoReferer')
101+
->willReturn(false);
102+
$this->requestMock->expects($this->any())
103+
->method('isGet')
104+
->willReturn(true);
105+
$this->urlBuilderMock->expects($this->any())
106+
->method('getUrl')
107+
->willReturn('cms/noroute/index');
108+
109+
$this->assertEquals([], $this->model->getLoginUrlParams());
110+
}
111+
}

0 commit comments

Comments
 (0)