Skip to content

Commit 0b0f7c8

Browse files
committed
Merge remote-tracking branch '36263/issue-35838' into community_prs_april
2 parents cf8f716 + 3d6a5de commit 0b0f7c8

File tree

2 files changed

+145
-5
lines changed

2 files changed

+145
-5
lines changed

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

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,29 @@
1717
* Class Customer url model
1818
*
1919
* @api
20+
* @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
2021
*/
2122
class Url
2223
{
24+
/**
25+
* No-route url constants
26+
*/
27+
private const XML_PATH_WEB_DEFAULT_NO_ROUTE = 'web/default/no_route';
28+
2329
/**
2430
* Route for customer account login page
2531
*/
26-
const ROUTE_ACCOUNT_LOGIN = 'customer/account/login';
32+
public const ROUTE_ACCOUNT_LOGIN = 'customer/account/login';
2733

2834
/**
2935
* Config name for Redirect Customer to Account Dashboard after Logging in setting
3036
*/
31-
const XML_PATH_CUSTOMER_STARTUP_REDIRECT_TO_DASHBOARD = 'customer/startup/redirect_dashboard';
37+
public const XML_PATH_CUSTOMER_STARTUP_REDIRECT_TO_DASHBOARD = 'customer/startup/redirect_dashboard';
3238

3339
/**
3440
* Query param name for last url visited
3541
*/
36-
const REFERER_QUERY_PARAM_NAME = 'referer';
42+
public const REFERER_QUERY_PARAM_NAME = 'referer';
3743

3844
/**
3945
* @var UrlInterface
@@ -126,8 +132,10 @@ public function getLoginUrlParams()
126132
&& !$this->customerSession->getNoReferer()
127133
&& $this->request->isGet()
128134
) {
129-
$referer = $this->urlBuilder->getUrl('*/*/*', ['_current' => true, '_use_rewrite' => true]);
130-
$referer = $this->urlEncoder->encode($referer);
135+
$refererUrl = $this->urlBuilder->getUrl('*/*/*', ['_current' => true, '_use_rewrite' => true]);
136+
if (!$this->isNoRouteUrl($refererUrl)) {
137+
$referer = $this->urlEncoder->encode($refererUrl);
138+
}
131139
}
132140

133141
if ($referer) {
@@ -246,6 +254,8 @@ public function getEmailConfirmationUrl($email = null)
246254
}
247255

248256
/**
257+
* Getting request referrer
258+
*
249259
* @return mixed|null
250260
*/
251261
private function getRequestReferrer()
@@ -256,4 +266,23 @@ private function getRequestReferrer()
256266
}
257267
return null;
258268
}
269+
270+
/**
271+
* Check if Referrer url is no route url
272+
*
273+
* @param string $url
274+
* @return bool
275+
*/
276+
private function isNoRouteUrl($url)
277+
{
278+
$defaultNoRouteUrl = $this->scopeConfig->getValue(
279+
self::XML_PATH_WEB_DEFAULT_NO_ROUTE,
280+
ScopeInterface::SCOPE_STORE
281+
);
282+
$noRouteUrl = $this->urlBuilder->getUrl($defaultNoRouteUrl);
283+
if (strpos($url, $noRouteUrl) !== false) {
284+
return true;
285+
}
286+
return false;
287+
}
259288
}
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)