1111use Magento \Framework \App \Config \ReinitableConfigInterface ;
1212use Magento \Framework \App \Config \Value ;
1313use Magento \Store \Api \Data \StoreInterface ;
14+ use Magento \Store \Api \StoreRepositoryInterface ;
1415use Magento \Store \Model \ScopeInterface ;
16+ use Magento \Store \Model \StoreManagerInterface ;
1517use Magento \Store \Model \StoreSwitcher ;
1618use Magento \Framework \ObjectManagerInterface as ObjectManager ;
1719use Magento \TestFramework \Helper \Bootstrap ;
1820
21+ /**
22+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
23+ * Test store switching
24+ */
1925class 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,40 @@ 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/two_cms_page_with_same_url_for_different_stores.php
107+ * @magentoDbIsolation disabled
108+ * @return void
109+ */
110+ public function testSwitchCmsPageToAnotherStore (): void
111+ {
112+ $ fromStore = $ this ->getStoreByCode ('default ' );
113+ $ toStore = $ this ->getStoreByCode ('fixture_second_store ' );
114+ $ redirectUrl = "http://localhost/index.php/page100/ " ;
115+ $ expectedUrl = "http://localhost/index.php/page100/ " ;
116+ $ this ->assertEquals ($ expectedUrl , $ this ->storeSwitcher ->switch ($ fromStore , $ toStore , $ redirectUrl ));
117+ }
118+
101119 /**
102120 * Set base url to store.
103121 *
@@ -120,4 +138,16 @@ private function setBaseUrl(StoreInterface $targetStore): void
120138 $ reinitibleConfig = $ this ->objectManager ->create (ReinitableConfigInterface::class);
121139 $ reinitibleConfig ->reinit ();
122140 }
141+
142+ /**
143+ * Get store object by storeCode
144+ *
145+ * @param string $storeCode
146+ * @return StoreInterface
147+ */
148+ private function getStoreByCode (string $ storeCode ): StoreInterface
149+ {
150+ /** @var StoreRepositoryInterface */
151+ return $ this ->storeManager ->getStore ($ storeCode );
152+ }
123153}
0 commit comments