Skip to content

Commit c5f460e

Browse files
Merge pull request #2270 from magento-qwerty/2.3-bugfixes-220318
Fixed issues: - MAGETWO-88209: [github] Pages are cached in browser and not updated - MAGETWO-88091: [GITHUB] [2.1] Store View Language switch leads to 404 on some cases #5416
2 parents 068a97e + 2c3ff92 commit c5f460e

File tree

9 files changed

+199
-107
lines changed

9 files changed

+199
-107
lines changed

app/code/Magento/CatalogUrlRewrite/Model/Storage/DbStorage.php

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,37 @@
1212
class DbStorage extends BaseDbStorage
1313
{
1414
/**
15-
* @param array $data
16-
* @return \Magento\Framework\DB\Select
15+
* {@inheritDoc}
1716
*/
1817
protected function prepareSelect(array $data)
1918
{
19+
$metadata = [];
20+
if (array_key_exists(UrlRewrite::METADATA, $data)) {
21+
$metadata = $data[UrlRewrite::METADATA];
22+
unset($data[UrlRewrite::METADATA]);
23+
}
24+
2025
$select = $this->connection->select();
21-
$select->from(['url_rewrite' => $this->resource->getTableName('url_rewrite')])
22-
->joinLeft(
23-
['relation' => $this->resource->getTableName(Product::TABLE_NAME)],
24-
'url_rewrite.url_rewrite_id = relation.url_rewrite_id'
25-
)
26-
->where('url_rewrite.entity_id IN (?)', $data['entity_id'])
27-
->where('url_rewrite.entity_type = ?', $data['entity_type'])
28-
->where('url_rewrite.store_id IN (?)', $data['store_id']);
29-
if (empty($data[UrlRewrite::METADATA]['category_id'])) {
26+
$select->from([
27+
'url_rewrite' => $this->resource->getTableName(self::TABLE_NAME)
28+
]);
29+
$select->joinLeft(
30+
['relation' => $this->resource->getTableName(Product::TABLE_NAME)],
31+
'url_rewrite.url_rewrite_id = relation.url_rewrite_id'
32+
);
33+
34+
foreach ($data as $column => $value) {
35+
$select->where('url_rewrite.' . $column . ' IN (?)', $value);
36+
}
37+
if (empty($metadata['category_id'])) {
3038
$select->where('relation.category_id IS NULL');
3139
} else {
32-
$select->where('relation.category_id = ?', $data[UrlRewrite::METADATA]['category_id']);
40+
$select->where(
41+
'relation.category_id = ?',
42+
$metadata['category_id']
43+
);
3344
}
45+
3446
return $select;
3547
}
3648
}

app/code/Magento/PageCache/etc/varnish4.vcl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,13 @@ sub vcl_deliver {
187187
unset resp.http.Age;
188188
}
189189

190+
# Not letting browser to cache non-static files.
191+
if (resp.http.Cache-Control !~ "private" && req.url !~ "^/(pub/)?(media|static)/") {
192+
set resp.http.Pragma = "no-cache";
193+
set resp.http.Expires = "-1";
194+
set resp.http.Cache-Control = "no-store, no-cache, must-revalidate, max-age=0";
195+
}
196+
190197
unset resp.http.X-Magento-Debug;
191198
unset resp.http.X-Magento-Tags;
192199
unset resp.http.X-Powered-By;

app/code/Magento/PageCache/etc/varnish5.vcl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,13 @@ sub vcl_deliver {
188188
unset resp.http.Age;
189189
}
190190

191+
# Not letting browser to cache non-static files.
192+
if (resp.http.Cache-Control !~ "private" && req.url !~ "^/(pub/)?(media|static)/") {
193+
set resp.http.Pragma = "no-cache";
194+
set resp.http.Expires = "-1";
195+
set resp.http.Cache-Control = "no-store, no-cache, must-revalidate, max-age=0";
196+
}
197+
191198
unset resp.http.X-Magento-Debug;
192199
unset resp.http.X-Magento-Tags;
193200
unset resp.http.X-Powered-By;

app/code/Magento/Store/Block/Switcher.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
namespace Magento\Store\Block;
1111

1212
use Magento\Directory\Helper\Data;
13+
use Magento\Store\Api\StoreResolverInterface;
1314
use Magento\Store\Model\Group;
15+
use Magento\Store\Model\Store;
1416

1517
/**
1618
* @api
@@ -217,15 +219,18 @@ public function getStoreName()
217219
/**
218220
* Returns target store post data
219221
*
220-
* @param \Magento\Store\Model\Store $store
222+
* @param Store $store
221223
* @param array $data
222224
* @return string
223225
*/
224-
public function getTargetStorePostData(\Magento\Store\Model\Store $store, $data = [])
226+
public function getTargetStorePostData(Store $store, $data = [])
225227
{
226-
$data[\Magento\Store\Api\StoreResolverInterface::PARAM_NAME] = $store->getCode();
228+
$data[StoreResolverInterface::PARAM_NAME] = $store->getCode();
229+
230+
//We need to set fromStore argument as true because
231+
//it will enable proper URL rewriting during store switching.
227232
return $this->_postDataHelper->getPostData(
228-
$store->getCurrentUrl(false),
233+
$store->getCurrentUrl(true),
229234
$data
230235
);
231236
}

app/code/Magento/Store/Test/Unit/Block/SwitcherTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function testGetTargetStorePostData()
5353
$storeSwitchUrl = 'http://domain.com/stores/store/switch';
5454
$store->expects($this->atLeastOnce())
5555
->method('getCurrentUrl')
56-
->with(false)
56+
->with(true)
5757
->willReturn($storeSwitchUrl);
5858
$this->corePostDataHelper->expects($this->any())
5959
->method('getPostData')

app/code/Magento/UrlRewrite/Controller/Router.php

Lines changed: 67 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,16 @@
55
*/
66
namespace Magento\UrlRewrite\Controller;
77

8+
use Magento\Framework\App\RequestInterface;
89
use Magento\UrlRewrite\Controller\Adminhtml\Url\Rewrite;
910
use Magento\UrlRewrite\Model\OptionProvider;
1011
use Magento\UrlRewrite\Model\UrlFinderInterface;
1112
use Magento\UrlRewrite\Service\V1\Data\UrlRewrite;
13+
use Magento\Framework\App\Request\Http as HttpRequest;
14+
use Magento\Framework\App\Response\Http as HttpResponse;
15+
use Magento\Framework\UrlInterface;
16+
use Magento\Framework\App\Action\Redirect;
17+
use Magento\Framework\App\ActionInterface;
1218

1319
/**
1420
* UrlRewrite Controller Router
@@ -23,7 +29,7 @@ class Router implements \Magento\Framework\App\RouterInterface
2329
protected $actionFactory;
2430

2531
/**
26-
* @var \Magento\Framework\UrlInterface
32+
* @var UrlInterface
2733
*/
2834
protected $url;
2935

@@ -33,7 +39,7 @@ class Router implements \Magento\Framework\App\RouterInterface
3339
protected $storeManager;
3440

3541
/**
36-
* @var \Magento\Framework\App\ResponseInterface
42+
* @var HttpResponse
3743
*/
3844
protected $response;
3945

@@ -44,14 +50,14 @@ class Router implements \Magento\Framework\App\RouterInterface
4450

4551
/**
4652
* @param \Magento\Framework\App\ActionFactory $actionFactory
47-
* @param \Magento\Framework\UrlInterface $url
53+
* @param UrlInterface $url
4854
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
4955
* @param \Magento\Framework\App\ResponseInterface $response
5056
* @param UrlFinderInterface $urlFinder
5157
*/
5258
public function __construct(
5359
\Magento\Framework\App\ActionFactory $actionFactory,
54-
\Magento\Framework\UrlInterface $url,
60+
UrlInterface $url,
5561
\Magento\Store\Model\StoreManagerInterface $storeManager,
5662
\Magento\Framework\App\ResponseInterface $response,
5763
UrlFinderInterface $urlFinder
@@ -64,48 +70,83 @@ public function __construct(
6470
}
6571

6672
/**
67-
* Match corresponding URL Rewrite and modify request
73+
* Match corresponding URL Rewrite and modify request.
6874
*
69-
* @param \Magento\Framework\App\RequestInterface $request
70-
* @return \Magento\Framework\App\ActionInterface|null
75+
* @param RequestInterface|HttpRequest $request
76+
*
77+
* @return ActionInterface|null
7178
*/
72-
public function match(\Magento\Framework\App\RequestInterface $request)
79+
public function match(RequestInterface $request)
7380
{
7481
if ($fromStore = $request->getParam('___from_store')) {
82+
//If we're in the process of switching stores then matching rewrite
83+
//rule from previous store because the URL was not changed yet from
84+
//old store's format.
7585
$oldStoreId = $this->storeManager->getStore($fromStore)->getId();
76-
$oldRewrite = $this->getRewrite($request->getPathInfo(), $oldStoreId);
77-
if ($oldRewrite) {
78-
$rewrite = $this->urlFinder->findOneByData(
86+
$oldRewrite = $this->getRewrite(
87+
$request->getPathInfo(),
88+
$oldStoreId
89+
);
90+
if ($oldRewrite && $oldRewrite->getRedirectType() === 0) {
91+
//If there is a match and it's a correct URL then just
92+
//redirecting to current store's URL equivalent,
93+
//otherwise just continuing finding a rule within current store.
94+
$currentRewrite = $this->urlFinder->findOneByData(
7995
[
8096
UrlRewrite::ENTITY_TYPE => $oldRewrite->getEntityType(),
8197
UrlRewrite::ENTITY_ID => $oldRewrite->getEntityId(),
82-
UrlRewrite::STORE_ID => $this->storeManager->getStore()->getId(),
83-
UrlRewrite::IS_AUTOGENERATED => 1,
98+
UrlRewrite::STORE_ID =>
99+
$this->storeManager->getStore()->getId(),
100+
UrlRewrite::REDIRECT_TYPE => 0,
84101
]
85102
);
86-
if ($rewrite && $rewrite->getRequestPath() !== $oldRewrite->getRequestPath()) {
87-
return $this->redirect($request, $rewrite->getRequestPath(), OptionProvider::TEMPORARY);
103+
if ($currentRewrite
104+
&& $currentRewrite->getRequestPath()
105+
!== $oldRewrite->getRequestPath()
106+
) {
107+
return $this->redirect(
108+
$request,
109+
$this->url->getUrl(
110+
'',
111+
['_direct' => $currentRewrite->getRequestPath()]
112+
),
113+
OptionProvider::TEMPORARY
114+
);
88115
}
89116
}
90117
}
91-
$rewrite = $this->getRewrite($request->getPathInfo(), $this->storeManager->getStore()->getId());
118+
119+
$rewrite = $this->getRewrite(
120+
$request->getPathInfo(),
121+
$this->storeManager->getStore()->getId()
122+
);
123+
92124
if ($rewrite === null) {
125+
//No rewrite rule matching current URl found, continuing with
126+
//processing of this URL.
93127
return null;
94128
}
95-
96129
if ($rewrite->getRedirectType()) {
130+
//Rule requires the request to be redirected to another URL
131+
//and cannot be processed further.
97132
return $this->processRedirect($request, $rewrite);
98133
}
99-
100-
$request->setAlias(\Magento\Framework\UrlInterface::REWRITE_REQUEST_PATH_ALIAS, $rewrite->getRequestPath());
134+
//Rule provides actual URL that can be processed by a controller.
135+
$request->setAlias(
136+
UrlInterface::REWRITE_REQUEST_PATH_ALIAS,
137+
$rewrite->getRequestPath()
138+
);
101139
$request->setPathInfo('/' . $rewrite->getTargetPath());
102-
return $this->actionFactory->create(\Magento\Framework\App\Action\Forward::class);
140+
return $this->actionFactory->create(
141+
\Magento\Framework\App\Action\Forward::class
142+
);
103143
}
104144

105145
/**
106-
* @param \Magento\Framework\App\RequestInterface $request
146+
* @param RequestInterface $request
107147
* @param UrlRewrite $rewrite
108-
* @return \Magento\Framework\App\ActionInterface|null
148+
*
149+
* @return ActionInterface|null
109150
*/
110151
protected function processRedirect($request, $rewrite)
111152
{
@@ -119,16 +160,17 @@ protected function processRedirect($request, $rewrite)
119160
}
120161

121162
/**
122-
* @param \Magento\Framework\App\RequestInterface $request
163+
* @param RequestInterface|HttpRequest $request
123164
* @param string $url
124165
* @param int $code
125-
* @return \Magento\Framework\App\ActionInterface
166+
* @return ActionInterface
126167
*/
127168
protected function redirect($request, $url, $code)
128169
{
129170
$this->response->setRedirect($url, $code);
130171
$request->setDispatched(true);
131-
return $this->actionFactory->create(\Magento\Framework\App\Action\Redirect::class);
172+
173+
return $this->actionFactory->create(Redirect::class);
132174
}
133175

134176
/**

app/code/Magento/UrlRewrite/Test/Unit/Controller/RouterTest.php

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -76,51 +76,6 @@ public function testNoRewriteExist()
7676
$this->assertNull($this->router->match($this->request));
7777
}
7878

79-
public function testRewriteAfterStoreSwitcher()
80-
{
81-
$this->request->expects($this->any())->method('getPathInfo')->will($this->returnValue('request-path'));
82-
$this->request->expects($this->any())->method('getParam')->with('___from_store')
83-
->will($this->returnValue('old-store'));
84-
$oldStore = $this->getMockBuilder(\Magento\Store\Model\Store::class)->disableOriginalConstructor()->getMock();
85-
$this->storeManager->expects($this->any())->method('getStore')
86-
->will($this->returnValueMap([['old-store', $oldStore], [null, $this->store]]));
87-
$oldStore->expects($this->any())->method('getId')->will($this->returnValue('old-store-id'));
88-
$this->store->expects($this->any())->method('getId')->will($this->returnValue('current-store-id'));
89-
$oldUrlRewrite = $this->getMockBuilder(\Magento\UrlRewrite\Service\V1\Data\UrlRewrite::class)
90-
->disableOriginalConstructor()->getMock();
91-
$oldUrlRewrite->expects($this->any())->method('getEntityType')->will($this->returnValue('entity-type'));
92-
$oldUrlRewrite->expects($this->any())->method('getEntityId')->will($this->returnValue('entity-id'));
93-
$oldUrlRewrite->expects($this->any())->method('getRequestPath')->will($this->returnValue('old-request-path'));
94-
$urlRewrite = $this->getMockBuilder(\Magento\UrlRewrite\Service\V1\Data\UrlRewrite::class)
95-
->disableOriginalConstructor()->getMock();
96-
$urlRewrite->expects($this->any())->method('getRequestPath')->will($this->returnValue('new-request-path'));
97-
98-
$this->urlFinder->expects($this->any())->method('findOneByData')->will(
99-
$this->returnValueMap([
100-
[
101-
[UrlRewrite::REQUEST_PATH => 'request-path', UrlRewrite::STORE_ID => 'old-store-id'],
102-
$oldUrlRewrite,
103-
],
104-
[
105-
[
106-
UrlRewrite::ENTITY_TYPE => 'entity-type',
107-
UrlRewrite::ENTITY_ID => 'entity-id',
108-
UrlRewrite::STORE_ID => 'current-store-id',
109-
UrlRewrite::IS_AUTOGENERATED => 1,
110-
],
111-
$urlRewrite
112-
],
113-
])
114-
);
115-
$this->response->expects($this->once())->method('setRedirect')
116-
->with('new-request-path', OptionProvider::TEMPORARY);
117-
$this->request->expects($this->once())->method('setDispatched')->with(true);
118-
$this->actionFactory->expects($this->once())->method('create')
119-
->with(\Magento\Framework\App\Action\Redirect::class);
120-
121-
$this->router->match($this->request);
122-
}
123-
12479
public function testNoRewriteAfterStoreSwitcherWhenNoOldRewrite()
12580
{
12681
$this->request->expects($this->any())->method('getPathInfo')->will($this->returnValue('request-path'));

0 commit comments

Comments
 (0)