forked from Hevelop/magento2-patches
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Patch-Magento_Url_Rewrite-2.2-language-switcher.patch
232 lines (219 loc) · 9.98 KB
/
Patch-Magento_Url_Rewrite-2.2-language-switcher.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
--- a/Controller/Router.php
+++ b/Controller/Router.php
@@ -5,10 +5,16 @@
*/
namespace Magento\UrlRewrite\Controller;
+use Magento\Framework\App\RequestInterface;
use Magento\UrlRewrite\Controller\Adminhtml\Url\Rewrite;
use Magento\UrlRewrite\Model\OptionProvider;
use Magento\UrlRewrite\Model\UrlFinderInterface;
use Magento\UrlRewrite\Service\V1\Data\UrlRewrite;
+use Magento\Framework\App\Request\Http as HttpRequest;
+use Magento\Framework\App\Response\Http as HttpResponse;
+use Magento\Framework\UrlInterface;
+use Magento\Framework\App\Action\Redirect;
+use Magento\Framework\App\ActionInterface;
/**
* UrlRewrite Controller Router
@@ -23,7 +29,7 @@ class Router implements \Magento\Framework\App\RouterInterface
protected $actionFactory;
/**
- * @var \Magento\Framework\UrlInterface
+ * @var UrlInterface
*/
protected $url;
@@ -33,7 +39,7 @@ class Router implements \Magento\Framework\App\RouterInterface
protected $storeManager;
/**
- * @var \Magento\Framework\App\ResponseInterface
+ * @var HttpResponse
*/
protected $response;
@@ -44,14 +50,14 @@ class Router implements \Magento\Framework\App\RouterInterface
/**
* @param \Magento\Framework\App\ActionFactory $actionFactory
- * @param \Magento\Framework\UrlInterface $url
+ * @param UrlInterface $url
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
* @param \Magento\Framework\App\ResponseInterface $response
* @param UrlFinderInterface $urlFinder
*/
public function __construct(
\Magento\Framework\App\ActionFactory $actionFactory,
- \Magento\Framework\UrlInterface $url,
+ UrlInterface $url,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Framework\App\ResponseInterface $response,
UrlFinderInterface $urlFinder
@@ -64,48 +70,83 @@ public function __construct(
}
/**
- * Match corresponding URL Rewrite and modify request
+ * Match corresponding URL Rewrite and modify request.
*
- * @param \Magento\Framework\App\RequestInterface $request
- * @return \Magento\Framework\App\ActionInterface|null
+ * @param RequestInterface|HttpRequest $request
+ *
+ * @return ActionInterface|null
*/
- public function match(\Magento\Framework\App\RequestInterface $request)
+ public function match(RequestInterface $request)
{
if ($fromStore = $request->getParam('___from_store')) {
+ //If we're in the process of switching stores then matching rewrite
+ //rule from previous store because the URL was not changed yet from
+ //old store's format.
$oldStoreId = $this->storeManager->getStore($fromStore)->getId();
- $oldRewrite = $this->getRewrite($request->getPathInfo(), $oldStoreId);
- if ($oldRewrite) {
- $rewrite = $this->urlFinder->findOneByData(
+ $oldRewrite = $this->getRewrite(
+ $request->getPathInfo(),
+ $oldStoreId
+ );
+ if ($oldRewrite && $oldRewrite->getRedirectType() === 0) {
+ //If there is a match and it's a correct URL then just
+ //redirecting to current store's URL equivalent,
+ //otherwise just continuing finding a rule within current store.
+ $currentRewrite = $this->urlFinder->findOneByData(
[
UrlRewrite::ENTITY_TYPE => $oldRewrite->getEntityType(),
UrlRewrite::ENTITY_ID => $oldRewrite->getEntityId(),
- UrlRewrite::STORE_ID => $this->storeManager->getStore()->getId(),
- UrlRewrite::IS_AUTOGENERATED => 1,
+ UrlRewrite::STORE_ID =>
+ $this->storeManager->getStore()->getId(),
+ UrlRewrite::REDIRECT_TYPE => 0,
]
);
- if ($rewrite && $rewrite->getRequestPath() !== $oldRewrite->getRequestPath()) {
- return $this->redirect($request, $rewrite->getRequestPath(), OptionProvider::TEMPORARY);
+ if ($currentRewrite
+ && $currentRewrite->getRequestPath()
+ !== $oldRewrite->getRequestPath()
+ ) {
+ return $this->redirect(
+ $request,
+ $this->url->getUrl(
+ '',
+ ['_direct' => $currentRewrite->getRequestPath()]
+ ),
+ OptionProvider::TEMPORARY
+ );
}
}
}
- $rewrite = $this->getRewrite($request->getPathInfo(), $this->storeManager->getStore()->getId());
+
+ $rewrite = $this->getRewrite(
+ $request->getPathInfo(),
+ $this->storeManager->getStore()->getId()
+ );
+
if ($rewrite === null) {
+ //No rewrite rule matching current URl found, continuing with
+ //processing of this URL.
return null;
}
-
if ($rewrite->getRedirectType()) {
+ //Rule requires the request to be redirected to another URL
+ //and cannot be processed further.
return $this->processRedirect($request, $rewrite);
}
-
- $request->setAlias(\Magento\Framework\UrlInterface::REWRITE_REQUEST_PATH_ALIAS, $rewrite->getRequestPath());
+ //Rule provides actual URL that can be processed by a controller.
+ $request->setAlias(
+ UrlInterface::REWRITE_REQUEST_PATH_ALIAS,
+ $rewrite->getRequestPath()
+ );
$request->setPathInfo('/' . $rewrite->getTargetPath());
- return $this->actionFactory->create(\Magento\Framework\App\Action\Forward::class);
+ return $this->actionFactory->create(
+ \Magento\Framework\App\Action\Forward::class
+ );
}
/**
- * @param \Magento\Framework\App\RequestInterface $request
+ * @param RequestInterface $request
* @param UrlRewrite $rewrite
- * @return \Magento\Framework\App\ActionInterface|null
+ *
+ * @return ActionInterface|null
*/
protected function processRedirect($request, $rewrite)
{
@@ -119,16 +160,17 @@ protected function processRedirect($request, $rewrite)
}
/**
- * @param \Magento\Framework\App\RequestInterface $request
+ * @param RequestInterface|HttpRequest $request
* @param string $url
* @param int $code
- * @return \Magento\Framework\App\ActionInterface
+ * @return ActionInterface
*/
protected function redirect($request, $url, $code)
{
$this->response->setRedirect($url, $code);
$request->setDispatched(true);
- return $this->actionFactory->create(\Magento\Framework\App\Action\Redirect::class);
+
+ return $this->actionFactory->create(Redirect::class);
}
/**
--- a/Test/Unit/Controller/RouterTest.php
+++ b/Test/Unit/Controller/RouterTest.php
@@ -76,51 +76,6 @@ public function testNoRewriteExist()
$this->assertNull($this->router->match($this->request));
}
- public function testRewriteAfterStoreSwitcher()
- {
- $this->request->expects($this->any())->method('getPathInfo')->will($this->returnValue('request-path'));
- $this->request->expects($this->any())->method('getParam')->with('___from_store')
- ->will($this->returnValue('old-store'));
- $oldStore = $this->getMockBuilder(\Magento\Store\Model\Store::class)->disableOriginalConstructor()->getMock();
- $this->storeManager->expects($this->any())->method('getStore')
- ->will($this->returnValueMap([['old-store', $oldStore], [null, $this->store]]));
- $oldStore->expects($this->any())->method('getId')->will($this->returnValue('old-store-id'));
- $this->store->expects($this->any())->method('getId')->will($this->returnValue('current-store-id'));
- $oldUrlRewrite = $this->getMockBuilder(\Magento\UrlRewrite\Service\V1\Data\UrlRewrite::class)
- ->disableOriginalConstructor()->getMock();
- $oldUrlRewrite->expects($this->any())->method('getEntityType')->will($this->returnValue('entity-type'));
- $oldUrlRewrite->expects($this->any())->method('getEntityId')->will($this->returnValue('entity-id'));
- $oldUrlRewrite->expects($this->any())->method('getRequestPath')->will($this->returnValue('old-request-path'));
- $urlRewrite = $this->getMockBuilder(\Magento\UrlRewrite\Service\V1\Data\UrlRewrite::class)
- ->disableOriginalConstructor()->getMock();
- $urlRewrite->expects($this->any())->method('getRequestPath')->will($this->returnValue('new-request-path'));
-
- $this->urlFinder->expects($this->any())->method('findOneByData')->will(
- $this->returnValueMap([
- [
- [UrlRewrite::REQUEST_PATH => 'request-path', UrlRewrite::STORE_ID => 'old-store-id'],
- $oldUrlRewrite,
- ],
- [
- [
- UrlRewrite::ENTITY_TYPE => 'entity-type',
- UrlRewrite::ENTITY_ID => 'entity-id',
- UrlRewrite::STORE_ID => 'current-store-id',
- UrlRewrite::IS_AUTOGENERATED => 1,
- ],
- $urlRewrite
- ],
- ])
- );
- $this->response->expects($this->once())->method('setRedirect')
- ->with('new-request-path', OptionProvider::TEMPORARY);
- $this->request->expects($this->once())->method('setDispatched')->with(true);
- $this->actionFactory->expects($this->once())->method('create')
- ->with(\Magento\Framework\App\Action\Redirect::class);
-
- $this->router->match($this->request);
- }
-
public function testNoRewriteAfterStoreSwitcherWhenNoOldRewrite()
{
$this->request->expects($this->any())->method('getPathInfo')->will($this->returnValue('request-path'));