Skip to content

Commit 03287f0

Browse files
MC-19585: CMS page redirects to homepage after changing store view
1 parent 0788dd4 commit 03287f0

File tree

1 file changed

+70
-18
lines changed

1 file changed

+70
-18
lines changed

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

Lines changed: 70 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,20 @@
88
namespace Magento\UrlRewrite\Model\StoreSwitcher;
99

1010
use Magento\Catalog\Api\ProductRepositoryInterface;
11+
use Magento\Cms\Model\Page;
1112
use Magento\Framework\App\Config\ReinitableConfigInterface;
1213
use Magento\Framework\App\Config\Value;
1314
use Magento\Store\Api\Data\StoreInterface;
15+
use Magento\Store\Api\StoreRepositoryInterface;
1416
use Magento\Store\Model\ScopeInterface;
17+
use Magento\Store\Model\StoreManagerInterface;
1518
use Magento\Store\Model\StoreSwitcher;
1619
use Magento\Framework\ObjectManagerInterface as ObjectManager;
1720
use Magento\TestFramework\Helper\Bootstrap;
1821

22+
/**
23+
* Test store switching
24+
*/
1925
class RewriteUrlTest extends \PHPUnit\Framework\TestCase
2026
{
2127
/**
@@ -33,6 +39,11 @@ class RewriteUrlTest extends \PHPUnit\Framework\TestCase
3339
*/
3440
private $productRepository;
3541

42+
/**
43+
* @var StoreManagerInterface
44+
*/
45+
private $storeManager;
46+
3647
/**
3748
* Class dependencies initialization
3849
*
@@ -43,9 +54,12 @@ protected function setUp()
4354
$this->objectManager = Bootstrap::getObjectManager();
4455
$this->storeSwitcher = $this->objectManager->get(StoreSwitcher::class);
4556
$this->productRepository = $this->objectManager->create(ProductRepositoryInterface::class);
57+
$this->storeManager = $this->objectManager->create(StoreManagerInterface::class);
4658
}
4759

4860
/**
61+
* Test switching stores with non-existent cms pages and then redirecting to the homepage
62+
*
4963
* @magentoDataFixture Magento/UrlRewrite/_files/url_rewrite.php
5064
* @magentoDataFixture Magento/Catalog/_files/category_product.php
5165
* @return void
@@ -54,15 +68,8 @@ protected function setUp()
5468
*/
5569
public function testSwitchToNonExistingPage(): void
5670
{
57-
$fromStoreCode = 'default';
58-
/** @var \Magento\Store\Api\StoreRepositoryInterface $storeRepository */
59-
$storeRepository = $this->objectManager->create(\Magento\Store\Api\StoreRepositoryInterface::class);
60-
$fromStore = $storeRepository->get($fromStoreCode);
61-
62-
$toStoreCode = 'fixture_second_store';
63-
/** @var \Magento\Store\Api\StoreRepositoryInterface $storeRepository */
64-
$storeRepository = $this->objectManager->create(\Magento\Store\Api\StoreRepositoryInterface::class);
65-
$toStore = $storeRepository->get($toStoreCode);
71+
$fromStore = $this->getStoreByCode('default');
72+
$toStore = $this->getStoreByCode('fixture_second_store');
6673

6774
$this->setBaseUrl($toStore);
6875

@@ -75,29 +82,43 @@ public function testSwitchToNonExistingPage(): void
7582
}
7683

7784
/**
85+
* Testing store switching with existing cms pages
86+
*
7887
* @magentoDataFixture Magento/UrlRewrite/_files/url_rewrite.php
7988
* @return void
8089
* @throws StoreSwitcher\CannotSwitchStoreException
8190
* @throws \Magento\Framework\Exception\NoSuchEntityException
8291
*/
8392
public function testSwitchToExistingPage(): void
8493
{
85-
$fromStoreCode = 'default';
86-
/** @var \Magento\Store\Api\StoreRepositoryInterface $storeRepository */
87-
$storeRepository = $this->objectManager->create(\Magento\Store\Api\StoreRepositoryInterface::class);
88-
$fromStore = $storeRepository->get($fromStoreCode);
89-
90-
$toStoreCode = 'fixture_second_store';
91-
/** @var \Magento\Store\Api\StoreRepositoryInterface $storeRepository */
92-
$storeRepository = $this->objectManager->create(\Magento\Store\Api\StoreRepositoryInterface::class);
93-
$toStore = $storeRepository->get($toStoreCode);
94+
$fromStore = $this->getStoreByCode('default');
95+
$toStore = $this->getStoreByCode('fixture_second_store');
9496

9597
$redirectUrl = "http://localhost/index.php/page-c/";
9698
$expectedUrl = "http://localhost/index.php/page-c-on-2nd-store";
9799

98100
$this->assertEquals($expectedUrl, $this->storeSwitcher->switch($fromStore, $toStore, $redirectUrl));
99101
}
100102

103+
/**
104+
* Testing store switching using cms pages with the same url_key but with different page_id
105+
*
106+
* @magentoDataFixture Magento/Cms/_files/pages.php
107+
* @magentoDataFixture Magento/Store/_files/second_store.php
108+
* @magentoDbIsolation disabled
109+
* @return void
110+
*/
111+
public function testSwitchCmsPageToAnotherStore(): void
112+
{
113+
$storeId = (int)$this->storeManager->getStore('fixture_second_store')->getId();
114+
$this->createCmsPage($storeId);
115+
$fromStore = $this->getStoreByCode('default');
116+
$toStore = $this->getStoreByCode('fixture_second_store');
117+
$redirectUrl = "http://localhost/index.php/page100/";
118+
$expectedUrl = "http://localhost/index.php/page100/";
119+
$this->assertEquals($expectedUrl, $this->storeSwitcher->switch($fromStore, $toStore, $redirectUrl));
120+
}
121+
101122
/**
102123
* Set base url to store.
103124
*
@@ -120,4 +141,35 @@ private function setBaseUrl(StoreInterface $targetStore): void
120141
$reinitibleConfig = $this->objectManager->create(ReinitableConfigInterface::class);
121142
$reinitibleConfig->reinit();
122143
}
144+
145+
/**
146+
* Get store object by storeCode
147+
*
148+
* @param string $storeCode
149+
* @return StoreInterface
150+
*/
151+
private function getStoreByCode(string $storeCode): StoreInterface
152+
{
153+
/** @var StoreRepositoryInterface $storeRepository */
154+
$storeRepository = $this->objectManager->create(StoreRepositoryInterface::class);
155+
return $storeRepository->get($storeCode);
156+
}
157+
158+
/**
159+
* Create cms page for store with store id from parameters
160+
*
161+
* @param int $storeId
162+
* @return void
163+
*/
164+
private function createCmsPage(int $storeId): void
165+
{
166+
/** @var $page \Magento\Cms\Model\Page */
167+
$page = $this->objectManager->create(Page::class);
168+
$page->setTitle('Test cms page')
169+
->setIdentifier('page100')
170+
->setStores([$storeId])
171+
->setIsActive(1)
172+
->setPageLayout('1column')
173+
->save();
174+
}
123175
}

0 commit comments

Comments
 (0)