Skip to content

Commit ce86fc6

Browse files
authored
MCLOUD-5883: UrlManager must respect routes with the attribute primary=true (magento#725)
1 parent f5859ee commit ce86fc6

File tree

2 files changed

+126
-4
lines changed

2 files changed

+126
-4
lines changed

src/Test/Unit/Util/UrlManagerTest.php

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,20 @@ public function testGetUrls(array $routes, array $expectedResult): void
164164
$this->assertEquals($expectedResult, $this->manager->getUrls());
165165
}
166166

167+
/**
168+
* @param array $routes
169+
* @param array $expectedResult
170+
* @dataProvider getPrimaryUrlsDataProvider
171+
*/
172+
public function testGetPrimaryUrls(array $routes, array $expectedResult): void
173+
{
174+
$this->environmentMock->expects($this->once())
175+
->method('getRoutes')
176+
->willReturn($routes);
177+
178+
$this->assertEquals($expectedResult, $this->manager->getUrls());
179+
}
180+
167181
public function testGetUrlsException(): void
168182
{
169183
$this->expectException(\RuntimeException::class);
@@ -295,6 +309,10 @@ private function unsecureUrlExample(): array
295309
];
296310
}
297311

312+
/**
313+
* DataProvider for testGetUrls
314+
* @return array
315+
*/
298316
public function getUrlsDataProvider(): array
299317
{
300318
return [
@@ -386,6 +404,103 @@ public function getUrlsDataProvider(): array
386404
];
387405
}
388406

407+
/**
408+
* DataProvider for testGetPrimaryUrls
409+
* @return array
410+
*/
411+
public function getPrimaryUrlsDataProvider(): array
412+
{
413+
return [
414+
'with unsecure primary' => [
415+
'routes' => [
416+
'http://example.com/' => [
417+
'original_url' => 'http://{default}/',
418+
'type' => 'upstream',
419+
'primary' => false,
420+
],
421+
'http://www.custom.example.com/' => [
422+
'original_url' => 'http://{all}/',
423+
'type' => 'upstream',
424+
'primary' => false,
425+
],
426+
'http://custom.example.com/' => [
427+
'original_url' => 'http://{default}/',
428+
'type' => 'upstream',
429+
'primary' => true,
430+
],
431+
'https://french.example.com/' => [
432+
'original_url' => 'https://french.{default}/',
433+
'type' => 'upstream',
434+
'primary' => false,
435+
],
436+
],
437+
'expectedResult' => [
438+
'secure' => [
439+
'' => 'https://custom.example.com/',
440+
],
441+
'unsecure' => [
442+
'' => 'http://custom.example.com/',
443+
],
444+
],
445+
],
446+
'secure primary' => [
447+
'routes' => [
448+
'http://example.com/' => [
449+
'original_url' => 'http://{default}/',
450+
'type' => 'upstream',
451+
'primary' => false,
452+
],
453+
'http://www.example.com/' => [
454+
'original_url' => 'http://{all}/',
455+
'type' => 'upstream',
456+
'primary' => false,
457+
],
458+
'https://custom.example.com/' => [
459+
'original_url' => 'http://{default}/',
460+
'type' => 'upstream',
461+
'primary' => true,
462+
],
463+
],
464+
'expectedResult' => [
465+
'secure' => [
466+
'' => 'https://custom.example.com/',
467+
],
468+
'unsecure' => [
469+
'' => 'https://custom.example.com/',
470+
],
471+
],
472+
],
473+
'all primary false and one secure' => [
474+
'routes' => [
475+
'http://example.com/' => [
476+
'original_url' => 'http://{default}/',
477+
'type' => 'upstream',
478+
'primary' => false,
479+
],
480+
'http://www.example.com/' => [
481+
'original_url' => 'http://{all}/',
482+
'type' => 'upstream',
483+
'primary' => false,
484+
],
485+
'https://www.example.com/' => [
486+
'original_url' => 'http://{all}/',
487+
'type' => 'upstream',
488+
'primary' => false,
489+
],
490+
],
491+
'expectedResult' => [
492+
'secure' => [
493+
'{all}' => 'https://www.example.com/',
494+
],
495+
'unsecure' => [
496+
'' => 'http://example.com/',
497+
'{all}' => 'http://www.example.com/',
498+
],
499+
],
500+
],
501+
];
502+
}
503+
389504
public function testGetBaseUrl(): void
390505
{
391506
$processMock = $this->getMockForAbstractClass(ProcessInterface::class);

src/Util/UrlManager.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ public function __construct(
7777
private function parseRoutes(array $routes): array
7878
{
7979
$urls = ['secure' => [], 'unsecure' => []];
80+
$primaryUrls = [];
8081

8182
foreach ($routes as $key => $val) {
8283
if ($val['type'] !== 'upstream') {
@@ -89,16 +90,22 @@ private function parseRoutes(array $routes): array
8990

9091
if (strpos($key, self::PREFIX_UNSECURE) === 0) {
9192
$urls['unsecure'][$originalUrl] = $key;
93+
if (!empty($val['primary'])) {
94+
$primaryUrls['unsecure'][$originalUrl] = $key;
95+
}
9296
continue;
9397
}
9498

9599
if (strpos($key, self::PREFIX_SECURE) === 0) {
96100
$urls['secure'][$originalUrl] = $key;
101+
if (!empty($val['primary'])) {
102+
$primaryUrls['secure'][$originalUrl] = $key;
103+
}
97104
continue;
98105
}
99106
}
100107

101-
return $urls;
108+
return $primaryUrls ?: $urls;
102109
}
103110

104111
/**
@@ -116,14 +123,14 @@ public function getUrls(): array
116123

117124
$urls = $this->parseRoutes($this->environment->getRoutes());
118125

119-
if (0 === count($urls['unsecure']) && 0 === count($urls['secure'])) {
126+
if (empty($urls['unsecure']) && empty($urls['secure'])) {
120127
throw new \RuntimeException('Expected at least one valid unsecure or secure route. None found.');
121128
}
122-
if (0 === count($urls['unsecure'])) {
129+
if (empty($urls['unsecure'])) {
123130
$urls['unsecure'] = $urls['secure'];
124131
}
125132

126-
if (0 === count($urls['secure'])) {
133+
if (empty($urls['secure'])) {
127134
$urls['secure'] = substr_replace($urls['unsecure'], self::PREFIX_SECURE, 0, strlen(self::PREFIX_UNSECURE));
128135
}
129136

0 commit comments

Comments
 (0)