Skip to content

Commit 75a14ae

Browse files
author
Andrii Lugovyi
committed
Merge remote-tracking branch 'goinc/MAGETWO-44420' into prepared-pull-request
2 parents dc3aee6 + 5b56e03 commit 75a14ae

File tree

5 files changed

+185
-6
lines changed

5 files changed

+185
-6
lines changed

app/code/Magento/Store/Block/Switcher.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,13 +214,15 @@ public function getStoreName()
214214
* Returns target store post data
215215
*
216216
* @param \Magento\Store\Model\Store $store
217+
* @param array $data
217218
* @return string
218219
*/
219-
public function getTargetStorePostData(\Magento\Store\Model\Store $store)
220+
public function getTargetStorePostData(\Magento\Store\Model\Store $store, $data = [])
220221
{
222+
$data[\Magento\Store\Api\StoreResolverInterface::PARAM_NAME] = $store->getCode();
221223
return $this->_postDataHelper->getPostData(
222224
$this->getUrl('stores/store/switch'),
223-
['___store' => $store->getCode(), '___from_store' => $this->getStoreCode()]
225+
$data
224226
);
225227
}
226228
}

app/code/Magento/Store/Test/Unit/Block/SwitcherTest.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,10 @@ public function testGetTargetStorePostData()
4646
{
4747
$store = $this->getMockBuilder('Magento\Store\Model\Store')->disableOriginalConstructor()->getMock();
4848
$store->expects($this->any())->method('getCode')->will($this->returnValue('new-store'));
49-
$currentStore = $this->getMockBuilder('Magento\Store\Model\Store')->disableOriginalConstructor()->getMock();
50-
$currentStore->expects($this->any())->method('getCode')->will($this->returnValue('current-store'));
51-
$this->storeManager->expects($this->any())->method('getStore')->will($this->returnValue($currentStore));
5249
$storeSwitchUrl = 'stores/store/switch';
5350
$this->urlBuilder->expects($this->any())->method('getUrl')->with($storeSwitchUrl)->willReturnArgument(0);
5451
$this->corePostDataHelper->expects($this->any())->method('getPostData')
55-
->with($storeSwitchUrl, ['___store' => 'new-store', '___from_store' => 'current-store']);
52+
->with($storeSwitchUrl, ['___store' => 'new-store']);
5653

5754
$this->switcher->getTargetStorePostData($store);
5855
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\UrlRewrite\Block\Plugin\Store\Switcher;
7+
8+
use Magento\Framework\Url\Helper\Data as UrlHelper;
9+
use Magento\Framework\UrlInterface;
10+
use Magento\UrlRewrite\Model\UrlFinderInterface;
11+
use Magento\UrlRewrite\Service\V1\Data\UrlRewrite;
12+
use Magento\Framework\App\RequestInterface;
13+
use Magento\Framework\App\ActionInterface;
14+
15+
class SetRedirectUrl
16+
{
17+
/** @var UrlHelper */
18+
private $urlHelper;
19+
20+
/** @var UrlInterface */
21+
private $urlBuilder;
22+
23+
/** @var UrlFinderInterface */
24+
private $urlFinder;
25+
26+
/** @var RequestInterface */
27+
private $request;
28+
29+
/**
30+
* @param UrlHelper $urlHelper
31+
* @param UrlInterface $urlBuilder
32+
* @param UrlFinderInterface $urlFinder
33+
* @param RequestInterface $request
34+
*/
35+
public function __construct(
36+
UrlHelper $urlHelper,
37+
UrlInterface $urlBuilder,
38+
UrlFinderInterface $urlFinder,
39+
RequestInterface $request
40+
) {
41+
$this->urlHelper = $urlHelper;
42+
$this->urlBuilder = $urlBuilder;
43+
$this->urlFinder = $urlFinder;
44+
$this->request = $request;
45+
}
46+
47+
/**
48+
* Set redirect url for store view based on request path info
49+
*
50+
* @param \Magento\Store\Block\Switcher $switcher
51+
* @param \Magento\Store\Model\Store $store
52+
* @param array $data
53+
* @return array
54+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
55+
*/
56+
public function beforeGetTargetStorePostData(
57+
\Magento\Store\Block\Switcher $switcher,
58+
\Magento\Store\Model\Store $store,
59+
$data = []
60+
) {
61+
$urlRewrite = $this->urlFinder->findOneByData([
62+
UrlRewrite::TARGET_PATH => $this->trimSlashInPath($this->request->getPathInfo()),
63+
UrlRewrite::STORE_ID => $store->getId(),
64+
]);
65+
if ($urlRewrite) {
66+
$data[ActionInterface::PARAM_NAME_URL_ENCODED] = $this->urlHelper->getEncodedUrl(
67+
$this->trimSlashInPath($this->urlBuilder->getUrl($urlRewrite->getRequestPath()))
68+
);
69+
}
70+
return [$store, $data];
71+
}
72+
73+
/**
74+
* @param string $path
75+
* @return string
76+
*/
77+
private function trimSlashInPath($path)
78+
{
79+
return trim($path, '/');
80+
}
81+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\UrlRewrite\Test\Unit\Block\Plugin\Store\Switcher;
8+
9+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
10+
11+
class SetRedirectUrlTest extends \PHPUnit_Framework_TestCase
12+
{
13+
/** @var \Magento\UrlRewrite\Block\Plugin\Store\Switcher\SetRedirectUrl */
14+
protected $unit;
15+
16+
/** @var \PHPUnit_Framework_MockObject_MockObject */
17+
protected $urlFinder;
18+
19+
/** @var \PHPUnit_Framework_MockObject_MockObject */
20+
protected $urlHelper;
21+
22+
/** @var \Magento\Store\Block\Switcher|\PHPUnit_Framework_MockObject_MockObject */
23+
protected $switcher;
24+
25+
/** @var \Magento\Store\Model\Store|\PHPUnit_Framework_MockObject_MockObject */
26+
protected $store;
27+
28+
/** @var \PHPUnit_Framework_MockObject_MockObject */
29+
protected $request;
30+
31+
/** @var \Magento\Framework\UrlInterface|\PHPUnit_Framework_MockObject_MockObject */
32+
protected $urlBuilder;
33+
34+
protected function setUp()
35+
{
36+
$this->store = $this->getMock('Magento\Store\Model\Store', [], [], '', false);
37+
$this->request = $this->getMock('Magento\Framework\App\Request\Http', [], [], '', false);
38+
$this->urlBuilder = $this->getMock('Magento\Framework\UrlInterface');
39+
$this->urlHelper = $this->getMock('Magento\Framework\Url\Helper\Data', [], [], '', false);
40+
$this->urlFinder = $this->getMock('Magento\UrlRewrite\Model\UrlFinderInterface', [], [], '', false);
41+
$this->switcher = $this->getMock('Magento\Store\Block\Switcher', [], [], '', false);
42+
43+
$this->unit = (new ObjectManager($this))->getObject(
44+
'Magento\UrlRewrite\Block\Plugin\Store\Switcher\SetRedirectUrl',
45+
[
46+
'urlFinder' => $this->urlFinder,
47+
'urlHelper' => $this->urlHelper,
48+
'urlBuilder' => $this->urlBuilder,
49+
'request' => $this->request,
50+
]
51+
);
52+
}
53+
54+
public function testNoUrlRewriteForSpecificStoreOnGetTargetStorePostData()
55+
{
56+
$this->request->expects($this->once())->method('getPathInfo')->willReturn('path');
57+
$this->urlFinder->expects($this->once())->method('findOneByData')->willReturn(null);
58+
$this->urlHelper->expects($this->never())->method('getEncodedUrl');
59+
$this->assertEquals(
60+
[$this->store, []],
61+
$this->unit->beforeGetTargetStorePostData($this->switcher, $this->store, [])
62+
);
63+
}
64+
65+
public function testTrimPathInfoForGetTargetStorePostData()
66+
{
67+
$this->request->expects($this->once())->method('getPathInfo')->willReturn('path/with/trim/');
68+
$this->store->expects($this->once())->method('getId')->willReturn(1);
69+
$this->urlFinder->expects($this->once())->method('findOneByData')
70+
->with([
71+
\Magento\UrlRewrite\Service\V1\Data\UrlRewrite::TARGET_PATH => 'path/with/trim',
72+
\Magento\UrlRewrite\Service\V1\Data\UrlRewrite::STORE_ID => 1,
73+
])
74+
->willReturn(null);
75+
$this->urlHelper->expects($this->never())->method('getEncodedUrl');
76+
$this->assertEquals(
77+
[$this->store, []],
78+
$this->unit->beforeGetTargetStorePostData($this->switcher, $this->store, [])
79+
);
80+
}
81+
82+
public function testGetTargetStorePostData()
83+
{
84+
$urlRewrite = $this->getMock('Magento\UrlRewrite\Service\V1\Data\UrlRewrite');
85+
$urlRewrite->expects($this->once())->method('getRequestPath')->willReturn('path');
86+
87+
$this->request->expects($this->once())->method('getPathInfo')->willReturn('path');
88+
$this->urlFinder->expects($this->once())->method('findOneByData')->willReturn($urlRewrite);
89+
$this->urlHelper->expects($this->once())->method('getEncodedUrl')->willReturn('encoded-path');
90+
$this->urlBuilder->expects($this->once())->method('getUrl')->willReturn('path');
91+
$this->assertEquals(
92+
[$this->store, [\Magento\Framework\App\ActionInterface::PARAM_NAME_URL_ENCODED => 'encoded-path']],
93+
$this->unit->beforeGetTargetStorePostData($this->switcher, $this->store, [])
94+
);
95+
}
96+
}

app/code/Magento/UrlRewrite/etc/frontend/di.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,7 @@
1717
</argument>
1818
</arguments>
1919
</type>
20+
<type name="Magento\Store\Block\Switcher">
21+
<plugin name="setStoreSpecificRedirectUrl" type="Magento\UrlRewrite\Block\Plugin\Store\Switcher\SetRedirectUrl"/>
22+
</type>
2023
</config>

0 commit comments

Comments
 (0)