8
8
namespace Magento \UrlRewrite \Model \StoreSwitcher ;
9
9
10
10
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 ;
11
15
use Magento \Framework \App \Config \ReinitableConfigInterface ;
12
16
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 ;
13
20
use Magento \Store \Api \Data \StoreInterface ;
14
21
use Magento \Store \Api \StoreRepositoryInterface ;
15
22
use Magento \Store \Model \ScopeInterface ;
16
23
use Magento \Store \Model \StoreManagerInterface ;
17
24
use Magento \Store \Model \StoreSwitcher ;
18
- use Magento \Framework \ObjectManagerInterface as ObjectManager ;
19
25
use Magento \TestFramework \Helper \Bootstrap ;
26
+ use PHPUnit \Framework \TestCase ;
20
27
21
28
/**
22
29
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
23
30
* Test store switching
24
31
*/
25
- class RewriteUrlTest extends \ PHPUnit \ Framework \ TestCase
32
+ class RewriteUrlTest extends TestCase
26
33
{
27
34
/**
28
35
* @var StoreSwitcher
@@ -66,7 +73,7 @@ protected function setUp()
66
73
* @magentoAppIsolation enabled
67
74
* @return void
68
75
* @throws StoreSwitcher\CannotSwitchStoreException
69
- * @throws \Magento\Framework\Exception\ NoSuchEntityException
76
+ * @throws NoSuchEntityException
70
77
*/
71
78
public function testSwitchToNonExistingPage (): void
72
79
{
@@ -91,7 +98,7 @@ public function testSwitchToNonExistingPage(): void
91
98
* @magentoDbIsolation disabled
92
99
* @return void
93
100
* @throws StoreSwitcher\CannotSwitchStoreException
94
- * @throws \Magento\Framework\Exception\ NoSuchEntityException
101
+ * @throws NoSuchEntityException
95
102
*/
96
103
public function testSwitchToExistingPage (): void
97
104
{
@@ -120,6 +127,46 @@ public function testSwitchCmsPageToAnotherStore(): void
120
127
$ this ->assertEquals ($ expectedUrl , $ this ->storeSwitcher ->switch ($ fromStore , $ toStore , $ redirectUrl ));
121
128
}
122
129
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
+
123
170
/**
124
171
* Set base url to store.
125
172
*
0 commit comments