Skip to content

Commit 4d81c37

Browse files
committed
Merge branch 'ACP2E-1668' of https://github.com/magento-l3/magento2ce into PR-VK-2023-04-05-CE
2 parents 878d029 + 3a55164 commit 4d81c37

File tree

9 files changed

+465
-72
lines changed

9 files changed

+465
-72
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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\Cms\Model\Page;
9+
10+
use Magento\Framework\UrlInterface;
11+
use Magento\Store\Model\StoreManagerInterface;
12+
13+
/**
14+
* Get target Url from routePath and store code.
15+
*/
16+
class TargetUrlBuilder implements TargetUrlBuilderInterface
17+
{
18+
/**
19+
* @var UrlInterface
20+
*/
21+
private $frontendUrlBuilder;
22+
23+
/**
24+
* Initialize constructor
25+
*
26+
* @param UrlInterface $frontendUrlBuilder
27+
*/
28+
public function __construct(UrlInterface $frontendUrlBuilder)
29+
{
30+
$this->frontendUrlBuilder = $frontendUrlBuilder;
31+
}
32+
33+
/**
34+
* Get target URL
35+
*
36+
* @param string $routePath
37+
* @param string $store
38+
* @return string
39+
*/
40+
public function process(string $routePath, string $store): string
41+
{
42+
return $this->frontendUrlBuilder->getUrl(
43+
$routePath,
44+
[
45+
'_current' => false,
46+
'_nosid' => true,
47+
'_query' => [
48+
StoreManagerInterface::PARAM_NAME => $store
49+
]
50+
]
51+
);
52+
}
53+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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\Cms\Model\Page;
9+
10+
/**
11+
* Provides extension point to generate target url for url builder class
12+
*/
13+
interface TargetUrlBuilderInterface
14+
{
15+
/**
16+
* Get target url from the route and store code
17+
*
18+
* @param string $routePath
19+
* @param string $store
20+
* @return string
21+
*/
22+
public function process(string $routePath, string $store): string;
23+
}

app/code/Magento/Cms/Test/Unit/ViewModel/Page/Grid/UrlBuilderTest.php

Lines changed: 31 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace Magento\Cms\Test\Unit\ViewModel\Page\Grid;
99

10+
use Magento\Cms\Model\Page\TargetUrlBuilderInterface;
1011
use Magento\Cms\ViewModel\Page\Grid\UrlBuilder;
1112
use Magento\Framework\Url\EncoderInterface;
1213
use Magento\Framework\UrlInterface;
@@ -42,23 +43,31 @@ class UrlBuilderTest extends TestCase
4243
*/
4344
private $storeManagerMock;
4445

46+
/**
47+
* @var TargetUrlBuilderInterface
48+
*/
49+
private $getTargetUrlMock;
50+
4551
/**
4652
* Set Up
4753
*/
4854
protected function setUp(): void
4955
{
5056
$this->frontendUrlBuilderMock = $this->getMockBuilder(UrlInterface::class)
51-
->setMethods(['getUrl', 'setScope'])
57+
->onlyMethods(['getUrl', 'setScope'])
5258
->getMockForAbstractClass();
5359
$this->urlEncoderMock = $this->getMockForAbstractClass(EncoderInterface::class);
5460
$this->storeManagerMock = $this->getMockBuilder(StoreManagerInterface::class)
5561
->disableOriginalConstructor()
5662
->getMockForAbstractClass();
57-
63+
$this->getTargetUrlMock = $this->getMockBuilder(TargetUrlBuilderInterface::class)
64+
->disableOriginalConstructor()
65+
->getMockForAbstractClass();
5866
$this->viewModel = new UrlBuilder(
5967
$this->frontendUrlBuilderMock,
6068
$this->urlEncoderMock,
61-
$this->storeManagerMock
69+
$this->storeManagerMock,
70+
$this->getTargetUrlMock
6271
);
6372
}
6473

@@ -109,54 +118,34 @@ public function nonScopedUrlsDataProvider(): array
109118
/**
110119
* Testing url builder with a scope provided
111120
*
112-
* @dataProvider scopedUrlsDataProvider
121+
* @param array $routePaths
122+
* @param array $expectedUrls
113123
*
114-
* @param string $storeCode
115-
* @param string $defaultStoreCode
116-
* @param array $urlParams
117-
* @param string $scope
124+
* @dataProvider scopedUrlsDataProvider
118125
*/
119126
public function testScopedUrlBuilder(
120-
string $storeCode,
121-
string $defaultStoreCode,
122-
array $urlParams,
123-
string $scope = 'store'
127+
array $routePaths,
128+
array $expectedUrls
124129
) {
125130
/** @var StoreInterface|MockObject $storeMock */
126131
$storeMock = $this->getMockForAbstractClass(StoreInterface::class);
127132
$storeMock->expects($this->any())
128133
->method('getCode')
129-
->willReturn($defaultStoreCode);
134+
->willReturn('en');
130135
$this->storeManagerMock->expects($this->once())
131136
->method('getDefaultStoreView')
132137
->willReturn($storeMock);
133-
138+
$this->getTargetUrlMock->expects($this->any())
139+
->method('process')
140+
->withConsecutive([$routePaths[0], 'en'], [$routePaths[1], 'en'])
141+
->willReturnOnConsecutiveCalls($routePaths[0], $routePaths[1]);
134142
$this->frontendUrlBuilderMock->expects($this->any())
135143
->method('getUrl')
136-
->withConsecutive(
137-
[
138-
'test/index',
139-
[
140-
'_current' => false,
141-
'_nosid' => true,
142-
'_query' => [
143-
StoreManagerInterface::PARAM_NAME => $storeCode
144-
]
145-
]
146-
],
147-
[
148-
'stores/store/switch',
149-
$urlParams
150-
]
151-
)
152-
->willReturnOnConsecutiveCalls(
153-
'http://domain.com/test',
154-
'http://domain.com/test/index'
155-
);
156-
157-
$result = $this->viewModel->getUrl('test/index', $scope, $storeCode);
158-
159-
$this->assertSame('http://domain.com/test/index', $result);
144+
->willReturnOnConsecutiveCalls($expectedUrls[0], $expectedUrls[1]);
145+
146+
$result = $this->viewModel->getUrl($routePaths[0], 'store', 'en');
147+
148+
$this->assertSame($expectedUrls[0], $result);
160149
}
161150

162151
/**
@@ -166,28 +155,14 @@ public function testScopedUrlBuilder(
166155
*/
167156
public function scopedUrlsDataProvider(): array
168157
{
169-
$enStoreCode = 'en';
170-
$frStoreCode = 'fr';
171-
$scopedDefaultUrlParams = $defaultUrlParams = [
172-
'_current' => false,
173-
'_nosid' => true,
174-
'_query' => [
175-
'___store' => $enStoreCode,
176-
'uenc' => null,
177-
]
178-
];
179-
$scopedDefaultUrlParams['_query']['___from_store'] = $frStoreCode;
180-
181158
return [
182159
[
183-
$enStoreCode,
184-
$enStoreCode,
185-
$defaultUrlParams,
160+
['test1/index1', 'stores/store/switch'],
161+
['http://domain.com/test1', 'http://domain.com/test1/index1']
186162
],
187163
[
188-
$enStoreCode,
189-
$frStoreCode,
190-
$scopedDefaultUrlParams
164+
['fr/test2/index2', 'stores/store/switch'],
165+
['http://domain.com/fr/test2', 'http://domain.com/fr/test2/index2']
191166
]
192167
];
193168
}

app/code/Magento/Cms/ViewModel/Page/Grid/UrlBuilder.php

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@
77

88
namespace Magento\Cms\ViewModel\Page\Grid;
99

10-
use Magento\Framework\Url\EncoderInterface;
10+
use Magento\Cms\Model\Page\TargetUrlBuilderInterface;
1111
use Magento\Framework\App\ActionInterface;
12+
use Magento\Framework\App\ObjectManager;
13+
use Magento\Framework\Url\EncoderInterface;
14+
use Magento\Framework\UrlInterface;
1215
use Magento\Store\Model\StoreManagerInterface;
1316

1417
/**
@@ -17,7 +20,7 @@
1720
class UrlBuilder
1821
{
1922
/**
20-
* @var \Magento\Framework\UrlInterface
23+
* @var UrlInterface
2124
*/
2225
private $frontendUrlBuilder;
2326

@@ -32,18 +35,27 @@ class UrlBuilder
3235
private $storeManager;
3336

3437
/**
35-
* @param \Magento\Framework\UrlInterface $frontendUrlBuilder
38+
* @var TargetUrlBuilderInterface
39+
*/
40+
private $getTargetUrl;
41+
42+
/**
43+
* @param UrlInterface $frontendUrlBuilder
3644
* @param EncoderInterface $urlEncoder
3745
* @param StoreManagerInterface $storeManager
46+
* @param TargetUrlBuilderInterface|null $getTargetUrl
3847
*/
3948
public function __construct(
40-
\Magento\Framework\UrlInterface $frontendUrlBuilder,
49+
UrlInterface $frontendUrlBuilder,
4150
EncoderInterface $urlEncoder,
42-
StoreManagerInterface $storeManager
51+
StoreManagerInterface $storeManager,
52+
?TargetUrlBuilderInterface $getTargetUrl = null
4353
) {
4454
$this->frontendUrlBuilder = $frontendUrlBuilder;
4555
$this->urlEncoder = $urlEncoder;
4656
$this->storeManager = $storeManager;
57+
$this->getTargetUrl = $getTargetUrl ?:
58+
ObjectManager::getInstance()->get(TargetUrlBuilderInterface::class);
4759
}
4860

4961
/**
@@ -58,16 +70,7 @@ public function getUrl($routePath, $scope, $store)
5870
{
5971
if ($scope) {
6072
$this->frontendUrlBuilder->setScope($scope);
61-
$targetUrl = $this->frontendUrlBuilder->getUrl(
62-
$routePath,
63-
[
64-
'_current' => false,
65-
'_nosid' => true,
66-
'_query' => [
67-
StoreManagerInterface::PARAM_NAME => $store
68-
]
69-
]
70-
);
73+
$targetUrl = $this->getTargetUrl->process($routePath, $store);
7174
$href = $this->frontendUrlBuilder->getUrl(
7275
'stores/store/switch',
7376
[

app/code/Magento/Cms/etc/adminhtml/di.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,10 @@
6262
<argument name="variableConfig" xsi:type="object">Magento\Variable\Model\Variable\Config\Proxy</argument>
6363
</arguments>
6464
</type>
65+
<preference for="Magento\Cms\Model\Page\TargetUrlBuilderInterface" type="Magento\Cms\Model\Page\TargetUrlBuilder"/>
66+
<type name="Magento\Cms\Model\Page\TargetUrlBuilder">
67+
<arguments>
68+
<argument name="frontendUrlBuilder" xsi:type="object">Magento\Framework\Url</argument>
69+
</arguments>
70+
</type>
6571
</config>

0 commit comments

Comments
 (0)