8
8
namespace Magento \UrlRewrite \Model \StoreSwitcher ;
9
9
10
10
use Magento \Catalog \Api \ProductRepositoryInterface ;
11
+ use Magento \Cms \Model \Page ;
11
12
use Magento \Framework \App \Config \ReinitableConfigInterface ;
12
13
use Magento \Framework \App \Config \Value ;
13
14
use Magento \Store \Api \Data \StoreInterface ;
15
+ use Magento \Store \Api \StoreRepositoryInterface ;
14
16
use Magento \Store \Model \ScopeInterface ;
17
+ use Magento \Store \Model \StoreManagerInterface ;
15
18
use Magento \Store \Model \StoreSwitcher ;
16
19
use Magento \Framework \ObjectManagerInterface as ObjectManager ;
17
20
use Magento \TestFramework \Helper \Bootstrap ;
18
21
22
+ /**
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,43 @@ 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/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
+
101
122
/**
102
123
* Set base url to store.
103
124
*
@@ -120,4 +141,35 @@ private function setBaseUrl(StoreInterface $targetStore): void
120
141
$ reinitibleConfig = $ this ->objectManager ->create (ReinitableConfigInterface::class);
121
142
$ reinitibleConfig ->reinit ();
122
143
}
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
+ }
123
175
}
0 commit comments