Skip to content

Commit 433d292

Browse files
committed
Merge branch 'ACP2E-3373' of https://github.com/adobe-commerce-tier-4/magento2ce into PR-12-05-2024
2 parents 7989cc9 + 50c9f36 commit 433d292

File tree

2 files changed

+47
-62
lines changed

2 files changed

+47
-62
lines changed

app/code/Magento/Backend/App/Area/FrontNameResolver.php

+17-20
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,10 @@ public function getFrontName($checkHost = false)
121121
*/
122122
public function isHostBackend()
123123
{
124+
if (!$this->request->getServer('HTTP_HOST')) {
125+
return false;
126+
}
127+
124128
if ($this->scopeConfig->getValue(self::XML_PATH_USE_CUSTOM_ADMIN_URL, ScopeInterface::SCOPE_STORE)) {
125129
$backendUrl = $this->scopeConfig->getValue(self::XML_PATH_CUSTOM_ADMIN_URL, ScopeInterface::SCOPE_STORE);
126130
} else {
@@ -132,28 +136,21 @@ public function isHostBackend()
132136
);
133137
}
134138
}
135-
$host = (string) $this->request->getServer('HTTP_HOST', '');
136-
$hostWithPort = $this->getHostWithPort($backendUrl);
137-
138-
return !($hostWithPort === null || $host === '') && stripos($hostWithPort, $host) !== false;
139-
}
139+
$this->uri->parse($backendUrl);
140+
$configuredHost = $this->uri->getHost();
141+
if (!$configuredHost) {
142+
return false;
143+
}
140144

141-
/**
142-
* Get host with port
143-
*
144-
* @param string $url
145-
* @return mixed|string
146-
*/
147-
private function getHostWithPort($url)
148-
{
149-
$this->uri->parse($url);
150-
$scheme = $this->uri->getScheme();
145+
$configuredPort = $this->uri->getPort() ?: ($this->standardPorts[$this->uri->getScheme()] ?? null);
146+
$uri = ($this->request->isSecure() ? 'https' : 'http') . '://' . $this->request->getServer('HTTP_HOST');
147+
$this->uri->parse($uri);
151148
$host = $this->uri->getHost();
152-
$port = $this->uri->getPort();
153-
154-
if (!$port) {
155-
$port = $this->standardPorts[$scheme] ?? null;
149+
if ($configuredPort) {
150+
$configuredHost .= ':' . $configuredPort;
151+
$host .= ':' . ($this->uri->getPort() ?: $this->standardPorts[$this->uri->getScheme()]);
156152
}
157-
return $port !== null ? $host . ':' . $port : $host;
153+
154+
return strcasecmp($configuredHost, $host) === 0;
158155
}
159156
}

app/code/Magento/Backend/Test/Unit/App/Area/FrontNameResolverTest.php

+30-42
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2013 Adobe
4+
* All Rights Reserved.
55
*/
66
declare(strict_types=1);
77

@@ -62,12 +62,10 @@ protected function setUp(): void
6262
->method('get')
6363
->with(ConfigOptionsList::CONFIG_PATH_BACKEND_FRONTNAME)
6464
->willReturn($this->_defaultFrontName);
65-
$this->uri = $this->createMock(Uri::class);
66-
65+
$this->uri = $this->createPartialMock(Uri::class, ['parse']);
6766
$this->request = $this->createMock(Http::class);
68-
6967
$this->configMock = $this->createMock(Config::class);
70-
$this->scopeConfigMock = $this->getMockForAbstractClass(ScopeConfigInterface::class);
68+
$this->scopeConfigMock = $this->createMock(ScopeConfigInterface::class);
7169
$this->model = new FrontNameResolver(
7270
$this->configMock,
7371
$deploymentConfigMock,
@@ -111,6 +109,7 @@ public function testIfCustomPathNotUsed(): void
111109
/**
112110
* @param string $url
113111
* @param string|null $host
112+
* @param bool $isHttps
114113
* @param string $useCustomAdminUrl
115114
* @param string $customAdminUrl
116115
* @param bool $expectedValue
@@ -121,12 +120,12 @@ public function testIfCustomPathNotUsed(): void
121120
public function testIsHostBackend(
122121
string $url,
123122
?string $host,
123+
bool $isHttps,
124124
string $useCustomAdminUrl,
125125
string $customAdminUrl,
126126
bool $expectedValue
127127
): void {
128-
$this->scopeConfigMock->expects($this->exactly(2))
129-
->method('getValue')
128+
$this->scopeConfigMock->method('getValue')
130129
->willReturnMap(
131130
[
132131
[Store::XML_PATH_UNSECURE_BASE_URL, ScopeInterface::SCOPE_STORE, null, $url],
@@ -145,41 +144,24 @@ public function testIsHostBackend(
145144
]
146145
);
147146

148-
$this->request->expects($this->any())
147+
$this->request->expects($this->atLeastOnce())
149148
->method('getServer')
150-
->willReturn($host);
151-
152-
$urlParts = [];
153-
$this->uri->expects($this->once())
154-
->method('parse')
155-
->willReturnCallback(
156-
function ($url) use (&$urlParts) {
157-
$urlParts = parse_url($url);
158-
}
159-
);
160-
$this->uri->expects($this->once())
161-
->method('getScheme')
162-
->willReturnCallback(
163-
function () use (&$urlParts) {
164-
return array_key_exists('scheme', $urlParts) ? $urlParts['scheme'] : '';
165-
}
166-
);
167-
$this->uri->expects($this->once())
168-
->method('getHost')
169-
->willReturnCallback(
170-
function () use (&$urlParts) {
171-
return array_key_exists('host', $urlParts) ? $urlParts['host'] : '';
172-
}
149+
->willReturnMap(
150+
[
151+
['HTTP_HOST', null, $host],
152+
]
173153
);
174-
$this->uri->expects($this->once())
175-
->method('getPort')
154+
$this->request->method('isSecure')
155+
->willReturn($isHttps);
156+
157+
$this->uri->method('parse')
176158
->willReturnCallback(
177-
function () use (&$urlParts) {
178-
return array_key_exists('port', $urlParts) ? $urlParts['port'] : '';
179-
}
159+
fn ($url) => $this->uri->setScheme(parse_url($url, PHP_URL_SCHEME))
160+
->setHost(parse_url($url, PHP_URL_HOST))
161+
->setPort(parse_url($url, PHP_URL_PORT))
180162
);
181163

182-
$this->assertEquals($this->model->isHostBackend(), $expectedValue);
164+
$this->assertEquals($expectedValue, $this->model->isHostBackend());
183165
}
184166

185167
/**
@@ -192,11 +174,8 @@ public function testIsHostBackendWithEmptyHost(): void
192174
$this->request->expects($this->any())
193175
->method('getServer')
194176
->willReturn('magento2.loc');
195-
$this->uri->expects($this->once())
196-
->method('getHost')
197-
->willReturn(null);
198177

199-
$this->assertEquals($this->model->isHostBackend(), false);
178+
$this->assertFalse($this->model->isHostBackend());
200179
}
201180

202181
/**
@@ -208,62 +187,71 @@ public static function hostsDataProvider(): array
208187
'withoutPort' => [
209188
'url' => 'http://magento2.loc/',
210189
'host' => 'magento2.loc',
190+
'isHttps' => false,
211191
'useCustomAdminUrl' => '0',
212192
'customAdminUrl' => '',
213193
'expectedValue' => true
214194
],
215195
'withPort' => [
216196
'url' => 'http://magento2.loc:8080/',
217197
'host' => 'magento2.loc:8080',
198+
'isHttps' => false,
218199
'useCustomAdminUrl' => '0',
219200
'customAdminUrl' => '',
220201
'expectedValue' => true
221202
],
222203
'withStandartPortInUrlWithoutPortInHost' => [
223204
'url' => 'http://magento2.loc:80/',
224205
'host' => 'magento2.loc',
206+
'isHttps' => false,
225207
'useCustomAdminUrl' => '0',
226208
'customAdminUrl' => '',
227209
'expectedValue' => true
228210
],
229211
'withoutStandartPortInUrlWithPortInHost' => [
230212
'url' => 'https://magento2.loc/',
231213
'host' => 'magento2.loc:443',
214+
'isHttps' => true,
232215
'useCustomAdminUrl' => '0',
233216
'customAdminUrl' => '',
234217
'expectedValue' => true
235218
],
236219
'differentHosts' => [
237220
'url' => 'http://m2.loc/',
238221
'host' => 'magento2.loc',
222+
'isHttps' => false,
239223
'useCustomAdminUrl' => '0',
240224
'customAdminUrl' => '',
241225
'expectedValue' => false
242226
],
243227
'differentPortsOnOneHost' => [
244228
'url' => 'http://magento2.loc/',
245229
'host' => 'magento2.loc:8080',
230+
'isHttps' => false,
246231
'useCustomAdminUrl' => '0',
247232
'customAdminUrl' => '',
248233
'expectedValue' => false
249234
],
250235
'withCustomAdminUrl' => [
251236
'url' => 'http://magento2.loc/',
252237
'host' => 'myhost.loc',
238+
'isHttps' => true,
253239
'useCustomAdminUrl' => '1',
254240
'customAdminUrl' => 'https://myhost.loc/',
255241
'expectedValue' => true
256242
],
257243
'withCustomAdminUrlWrongHost' => [
258244
'url' => 'http://magento2.loc/',
259245
'host' => 'SomeOtherHost.loc',
246+
'isHttps' => false,
260247
'useCustomAdminUrl' => '1',
261248
'customAdminUrl' => 'https://myhost.loc/',
262249
'expectedValue' => false
263250
],
264251
'withEmptyHost' => [
265252
'url' => 'http://magento2.loc/',
266253
'host' => null,
254+
'isHttps' => false,
267255
'useCustomAdminUrl' => '0',
268256
'customAdminUrl' => '',
269257
'expectedValue' => false

0 commit comments

Comments
 (0)