Skip to content

Commit f19c780

Browse files
committed
MC-22567: 404 page while switching the store from Product detail page
- Move HashGenerator to the end of storeswitcher handlers pool to prevent changing the target URL
1 parent efd77d3 commit f19c780

File tree

2 files changed

+52
-5
lines changed
  • app/code/Magento/Store/etc
  • dev/tests/integration/testsuite/Magento/UrlRewrite/Model/StoreSwitcher

2 files changed

+52
-5
lines changed

app/code/Magento/Store/etc/di.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@
436436
<item name="cleanTargetUrl" xsi:type="object">Magento\Store\Model\StoreSwitcher\CleanTargetUrl</item>
437437
<item name="manageStoreCookie" xsi:type="object">Magento\Store\Model\StoreSwitcher\ManageStoreCookie</item>
438438
<item name="managePrivateContent" xsi:type="object">Magento\Store\Model\StoreSwitcher\ManagePrivateContent</item>
439-
<item name="hashGenerator" xsi:type="object">Magento\Store\Model\StoreSwitcher\HashGenerator</item>
439+
<item name="hashGenerator" xsi:type="object" sortOrder="1000">Magento\Store\Model\StoreSwitcher\HashGenerator</item>
440440
</argument>
441441
</arguments>
442442
</type>

dev/tests/integration/testsuite/Magento/UrlRewrite/Model/StoreSwitcher/RewriteUrlTest.php

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,28 @@
88
namespace Magento\UrlRewrite\Model\StoreSwitcher;
99

1010
use Magento\Catalog\Api\ProductRepositoryInterface;
11+
use Magento\Customer\Api\CustomerRepositoryInterface;
12+
use Magento\Customer\Api\Data\CustomerInterface;
13+
use Magento\Customer\Model\Session;
14+
use Magento\Framework\App\ActionInterface;
1115
use Magento\Framework\App\Config\ReinitableConfigInterface;
1216
use Magento\Framework\App\Config\Value;
17+
use Magento\Framework\Exception\NoSuchEntityException;
18+
use Magento\Framework\ObjectManagerInterface as ObjectManager;
19+
use Magento\Framework\Url\DecoderInterface;
1320
use Magento\Store\Api\Data\StoreInterface;
1421
use Magento\Store\Api\StoreRepositoryInterface;
1522
use Magento\Store\Model\ScopeInterface;
1623
use Magento\Store\Model\StoreManagerInterface;
1724
use Magento\Store\Model\StoreSwitcher;
18-
use Magento\Framework\ObjectManagerInterface as ObjectManager;
1925
use Magento\TestFramework\Helper\Bootstrap;
26+
use PHPUnit\Framework\TestCase;
2027

2128
/**
2229
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2330
* Test store switching
2431
*/
25-
class RewriteUrlTest extends \PHPUnit\Framework\TestCase
32+
class RewriteUrlTest extends TestCase
2633
{
2734
/**
2835
* @var StoreSwitcher
@@ -66,7 +73,7 @@ protected function setUp()
6673
* @magentoAppIsolation enabled
6774
* @return void
6875
* @throws StoreSwitcher\CannotSwitchStoreException
69-
* @throws \Magento\Framework\Exception\NoSuchEntityException
76+
* @throws NoSuchEntityException
7077
*/
7178
public function testSwitchToNonExistingPage(): void
7279
{
@@ -91,7 +98,7 @@ public function testSwitchToNonExistingPage(): void
9198
* @magentoDbIsolation disabled
9299
* @return void
93100
* @throws StoreSwitcher\CannotSwitchStoreException
94-
* @throws \Magento\Framework\Exception\NoSuchEntityException
101+
* @throws NoSuchEntityException
95102
*/
96103
public function testSwitchToExistingPage(): void
97104
{
@@ -120,6 +127,46 @@ public function testSwitchCmsPageToAnotherStore(): void
120127
$this->assertEquals($expectedUrl, $this->storeSwitcher->switch($fromStore, $toStore, $redirectUrl));
121128
}
122129

130+
/**
131+
* Test store switching with logged in customer on cms page with different url_key
132+
*
133+
* @magentoDataFixture Magento/UrlRewrite/_files/url_rewrite.php
134+
* @magentoDataFixture Magento/Customer/_files/customer.php
135+
* @magentoDbIsolation disabled
136+
* @magentoAppArea frontend
137+
* @return void
138+
*/
139+
public function testSwitchCmsPageToAnotherStoreAsCustomer(): void
140+
{
141+
/** @var CustomerRepositoryInterface $repository */
142+
$repository = $this->objectManager->create(CustomerRepositoryInterface::class);
143+
$this->loginAsCustomer($repository->get('customer@example.com'));
144+
$fromStore = $this->getStoreByCode('default');
145+
$toStore = $this->getStoreByCode('fixture_second_store');
146+
147+
$redirectUrl = "http://localhost/index.php/page-c/";
148+
$expectedUrl = "http://localhost/index.php/page-c-on-2nd-store";
149+
/** @var DecoderInterface $decoder */
150+
$decoder = $this->objectManager->create(DecoderInterface::class);
151+
$secureRedirectUrl = $this->storeSwitcher->switch($fromStore, $toStore, $redirectUrl);
152+
parse_str(parse_url($secureRedirectUrl, PHP_URL_QUERY), $secureRedirectUrlQueryParams);
153+
$encodedActualUrl = $secureRedirectUrlQueryParams[ActionInterface::PARAM_NAME_URL_ENCODED];
154+
$actualUrl = $decoder->decode($encodedActualUrl);
155+
$this->assertEquals($expectedUrl, $actualUrl);
156+
}
157+
158+
/**
159+
* Login as customer
160+
*
161+
* @param CustomerInterface $customer
162+
*/
163+
private function loginAsCustomer($customer)
164+
{
165+
/** @var Session $session */
166+
$session = $this->objectManager->get(Session::class);
167+
$session->setCustomerDataAsLoggedIn($customer);
168+
}
169+
123170
/**
124171
* Set base url to store.
125172
*

0 commit comments

Comments
 (0)