11
11
use Magento \Framework \App \Config \ReinitableConfigInterface ;
12
12
use Magento \Framework \App \Config \Value ;
13
13
use Magento \Store \Api \Data \StoreInterface ;
14
+ use Magento \Store \Api \StoreRepositoryInterface ;
14
15
use Magento \Store \Model \ScopeInterface ;
16
+ use Magento \Store \Model \StoreManagerInterface ;
15
17
use Magento \Store \Model \StoreSwitcher ;
16
18
use Magento \Framework \ObjectManagerInterface as ObjectManager ;
17
19
use Magento \TestFramework \Helper \Bootstrap ;
18
20
21
+ /**
22
+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
23
+ * Test store switching
24
+ */
19
25
class RewriteUrlTest extends \PHPUnit \Framework \TestCase
20
26
{
21
27
/**
@@ -33,6 +39,11 @@ class RewriteUrlTest extends \PHPUnit\Framework\TestCase
33
39
*/
34
40
private $ productRepository ;
35
41
42
+ /**
43
+ * @var StoreManagerInterface
44
+ */
45
+ private $ storeManager ;
46
+
36
47
/**
37
48
* Class dependencies initialization
38
49
*
@@ -43,9 +54,12 @@ protected function setUp()
43
54
$ this ->objectManager = Bootstrap::getObjectManager ();
44
55
$ this ->storeSwitcher = $ this ->objectManager ->get (StoreSwitcher::class);
45
56
$ this ->productRepository = $ this ->objectManager ->create (ProductRepositoryInterface::class);
57
+ $ this ->storeManager = $ this ->objectManager ->create (StoreManagerInterface::class);
46
58
}
47
59
48
60
/**
61
+ * Test switching stores with non-existent cms pages and then redirecting to the homepage
62
+ *
49
63
* @magentoDataFixture Magento/UrlRewrite/_files/url_rewrite.php
50
64
* @magentoDataFixture Magento/Catalog/_files/category_product.php
51
65
* @return void
@@ -54,15 +68,8 @@ protected function setUp()
54
68
*/
55
69
public function testSwitchToNonExistingPage (): void
56
70
{
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 ' );
66
73
67
74
$ this ->setBaseUrl ($ toStore );
68
75
@@ -75,29 +82,40 @@ public function testSwitchToNonExistingPage(): void
75
82
}
76
83
77
84
/**
85
+ * Testing store switching with existing cms pages
86
+ *
78
87
* @magentoDataFixture Magento/UrlRewrite/_files/url_rewrite.php
79
88
* @return void
80
89
* @throws StoreSwitcher\CannotSwitchStoreException
81
90
* @throws \Magento\Framework\Exception\NoSuchEntityException
82
91
*/
83
92
public function testSwitchToExistingPage (): void
84
93
{
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 ' );
94
96
95
97
$ redirectUrl = "http://localhost/index.php/page-c/ " ;
96
98
$ expectedUrl = "http://localhost/index.php/page-c-on-2nd-store " ;
97
99
98
100
$ this ->assertEquals ($ expectedUrl , $ this ->storeSwitcher ->switch ($ fromStore , $ toStore , $ redirectUrl ));
99
101
}
100
102
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
+
101
119
/**
102
120
* Set base url to store.
103
121
*
@@ -120,4 +138,16 @@ private function setBaseUrl(StoreInterface $targetStore): void
120
138
$ reinitibleConfig = $ this ->objectManager ->create (ReinitableConfigInterface::class);
121
139
$ reinitibleConfig ->reinit ();
122
140
}
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
+ }
123
153
}
0 commit comments