Skip to content

Commit 1fb5666

Browse files
committed
Merge branch 'ACP2E-1413-1' of https://github.com/magento-l3/magento2ce into PR-L3-2023-01-31
2 parents bdf3746 + f27c715 commit 1fb5666

File tree

3 files changed

+85
-15
lines changed

3 files changed

+85
-15
lines changed

app/code/Magento/Catalog/Controller/Category/View.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Magento\Catalog\Model\Design;
1414
use Magento\Catalog\Model\Layer\Resolver;
1515
use Magento\Catalog\Model\Product\ProductList\ToolbarMemorizer;
16+
use Magento\Catalog\Model\Product\ProductList\Toolbar;
1617
use Magento\Catalog\Model\Session;
1718
use Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator;
1819
use Magento\Framework\App\Action\Action;
@@ -22,7 +23,7 @@
2223
use Magento\Framework\App\ActionInterface;
2324
use Magento\Framework\App\ObjectManager;
2425
use Magento\Framework\Controller\Result\ForwardFactory;
25-
use Magento\Framework\Controller\ResultInterface;
26+
use Magento\Framework\Controller\ResultFactory;
2627
use Magento\Framework\DataObject;
2728
use Magento\Framework\Exception\LocalizedException;
2829
use Magento\Framework\Exception\NoSuchEntityException;
@@ -209,6 +210,7 @@ public function execute()
209210
//phpcs:ignore Magento2.Legacy.ObsoleteResponse
210211
return $this->resultRedirectFactory->create()->setUrl($this->_redirect->getRedirectUrl());
211212
}
213+
212214
$category = $this->_initCategory();
213215
if ($category) {
214216
$this->layerResolver->create(Resolver::CATALOG_LAYER_CATEGORY);
@@ -247,6 +249,9 @@ public function execute()
247249
->addBodyClass('categorypath-' . $this->categoryUrlPathGenerator->getUrlPath($category))
248250
->addBodyClass('category-' . $category->getUrlKey());
249251

252+
if ($this->shouldRedirectOnToolbarAction()) {
253+
$this->getResponse()->setRedirect($this->_redirect->getRedirectUrl());
254+
}
250255
return $page;
251256
} elseif (!$this->getResponse()->isRedirect()) {
252257
$result = $this->resultForwardFactory->create()->forward('noroute');
@@ -294,4 +299,21 @@ private function applyLayoutUpdates(
294299
$page->addPageLayoutHandles($settings->getPageLayoutHandles());
295300
}
296301
}
302+
303+
/**
304+
* Checks for toolbar actions
305+
*
306+
* @return bool
307+
*/
308+
private function shouldRedirectOnToolbarAction(): bool
309+
{
310+
$params = $this->getRequest()->getParams();
311+
312+
return $this->toolbarMemorizer->isMemorizingAllowed() && empty(array_intersect([
313+
Toolbar::ORDER_PARAM_NAME,
314+
Toolbar::DIRECTION_PARAM_NAME,
315+
Toolbar::MODE_PARAM_NAME,
316+
Toolbar::LIMIT_PARAM_NAME
317+
], array_keys($params))) === false;
318+
}
297319
}

app/code/Magento/Catalog/Test/Mftf/Test/StorefrontRememberCategoryPaginationTest.xml

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,25 +32,18 @@
3232
<createData entity="RememberPaginationCatalogStorefrontConfig" stepKey="setRememberPaginationCatalogStorefrontConfig"/>
3333
</before>
3434

35-
<actionGroup ref="GoToStorefrontCategoryPageByParametersActionGroup" stepKey="GoToStorefrontCategory1Page">
36-
<argument name="category" value="$$defaultCategory1.custom_attributes[url_key]$$"/>
37-
<argument name="mode" value="grid"/>
38-
<argument name="numOfProductsPerPage" value="12"/>
39-
</actionGroup>
40-
41-
<actionGroup ref="VerifyCategoryPageParametersActionGroup" stepKey="verifyCategory1PageParameters">
42-
<argument name="category" value="$$defaultCategory1$$"/>
43-
<argument name="mode" value="grid"/>
44-
<argument name="numOfProductsPerPage" value="12"/>
45-
</actionGroup>
35+
<amOnPage url="{{StorefrontCategoryPage.url($$defaultCategory1.custom_attributes[url_key]$$)}}" stepKey="GoToStorefrontCategory1Page"/>
36+
<selectOption selector="{{StorefrontCategoryBottomToolbarSection.perPage}}" userInput="24" stepKey="selectPerPageCategory1" />
37+
<waitForPageLoad stepKey="waitForCategory1PageToLoad"/>
38+
<comment userInput="Comment is added to preserve the step key for backward compatibility" stepKey="verifyCategory1PageParameters"/>
4639

4740
<amOnPage url="{{StorefrontCategoryPage.url($$defaultCategory2.custom_attributes[url_key]$$)}}" stepKey="navigateToCategory2Page"/>
4841
<waitForPageLoad stepKey="waitForCategory2PageToLoad"/>
4942

5043
<actionGroup ref="VerifyCategoryPageParametersActionGroup" stepKey="verifyCategory2PageParameters">
5144
<argument name="category" value="$$defaultCategory2$$"/>
5245
<argument name="mode" value="grid"/>
53-
<argument name="numOfProductsPerPage" value="12"/>
46+
<argument name="numOfProductsPerPage" value="24"/>
5447
</actionGroup>
5548

5649
<after>

app/code/Magento/Catalog/Test/Unit/Controller/Category/ViewTest.php

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,12 @@
1212
use Magento\Catalog\Controller\Category\View;
1313
use Magento\Catalog\Helper\Category;
1414
use Magento\Catalog\Model\Design;
15+
use Magento\Catalog\Model\Product\ProductList\Toolbar;
16+
use Magento\Catalog\Model\Product\ProductList\ToolbarMemorizer;
1517
use Magento\Framework\App\Action\Action;
1618
use Magento\Framework\App\RequestInterface;
1719
use Magento\Framework\App\ResponseInterface;
20+
use Magento\Framework\App\Response\RedirectInterface;
1821
use Magento\Framework\App\ViewInterface;
1922
use Magento\Framework\Controller\ResultFactory;
2023
use Magento\Framework\DataObject;
@@ -127,13 +130,21 @@ class ViewTest extends TestCase
127130
*/
128131
protected $pageConfig;
129132

133+
/**
134+
* @var ToolbarMemorizer|MockObject
135+
*/
136+
protected ToolbarMemorizer $toolbarMemorizer;
137+
130138
/**
131139
* @inheritDoc
132140
*/
133141
protected function setUp(): void
134142
{
135143
$this->request = $this->getMockForAbstractClass(RequestInterface::class);
136-
$this->response = $this->getMockForAbstractClass(ResponseInterface::class);
144+
$this->response = $this->getMockBuilder(ResponseInterface::class)
145+
->addMethods(['setRedirect', 'isRedirect'])
146+
->onlyMethods(['sendResponse'])
147+
->getMock();
137148

138149
$this->categoryHelper = $this->createMock(Category::class);
139150
$this->objectManager = $this->getMockForAbstractClass(ObjectManagerInterface::class);
@@ -180,6 +191,8 @@ protected function setUp(): void
180191
$this->context->expects($this->any())->method('getView')->willReturn($this->view);
181192
$this->context->expects($this->any())->method('getResultFactory')
182193
->willReturn($this->resultFactory);
194+
$this->context->expects($this->once())->method('getRedirect')
195+
->willReturn($this->createMock(RedirectInterface::class));
183196

184197
$this->category = $this->createMock(\Magento\Catalog\Model\Category::class);
185198
$this->categoryRepository = $this->getMockForAbstractClass(CategoryRepositoryInterface::class);
@@ -198,6 +211,8 @@ protected function setUp(): void
198211
->method('create')
199212
->willReturn($this->page);
200213

214+
$this->toolbarMemorizer = $this->createMock(ToolbarMemorizer::class);
215+
201216
$this->action = (new ObjectManager($this))->getObject(
202217
View::class,
203218
[
@@ -206,9 +221,46 @@ protected function setUp(): void
206221
'categoryRepository' => $this->categoryRepository,
207222
'storeManager' => $this->storeManager,
208223
'resultPageFactory' => $resultPageFactory,
209-
'categoryHelper' => $this->categoryHelper
224+
'categoryHelper' => $this->categoryHelper,
225+
'toolbarMemorizer' => $this->toolbarMemorizer
226+
]
227+
);
228+
}
229+
230+
public function testRedirectOnToolbarAction()
231+
{
232+
$categoryId = 123;
233+
$this->request->expects($this->any())
234+
->method('getParams')
235+
->willReturn([Toolbar::LIMIT_PARAM_NAME => 12]);
236+
$this->request->expects($this->any())->method('getParam')->willReturnMap(
237+
[
238+
[Action::PARAM_NAME_URL_ENCODED],
239+
['id', false, $categoryId]
210240
]
211241
);
242+
$this->categoryRepository->expects($this->any())->method('get')->with($categoryId)
243+
->willReturn($this->category);
244+
$this->categoryHelper->expects($this->once())->method('canShow')->with($this->category)->willReturn(true);
245+
$this->toolbarMemorizer->expects($this->once())->method('memorizeParams');
246+
$this->toolbarMemorizer->expects($this->once())->method('isMemorizingAllowed')->willReturn(true);
247+
$this->response->expects($this->once())->method('setRedirect');
248+
$settings = $this->getMockBuilder(DataObject::class)
249+
->addMethods(['getPageLayout', 'getLayoutUpdates'])
250+
->disableOriginalConstructor()
251+
->getMock();
252+
$this->category
253+
->method('hasChildren')
254+
->willReturnOnConsecutiveCalls(true);
255+
$this->category->expects($this->any())
256+
->method('getDisplayMode')
257+
->willReturn('products');
258+
259+
$settings->expects($this->atLeastOnce())->method('getPageLayout')->willReturn('page_layout');
260+
$settings->expects($this->once())->method('getLayoutUpdates')->willReturn(['update1', 'update2']);
261+
$this->catalogDesign->expects($this->any())->method('getDesignSettings')->willReturn($settings);
262+
263+
$this->action->execute();
212264
}
213265

214266
/**
@@ -230,6 +282,9 @@ public function testApplyCustomLayoutUpdate(array $expectedData): void
230282
['id', false, $categoryId]
231283
]
232284
);
285+
$this->request->expects($this->any())
286+
->method('getParams')
287+
->willReturn([]);
233288

234289
$this->categoryRepository->expects($this->any())->method('get')->with($categoryId)
235290
->willReturn($this->category);

0 commit comments

Comments
 (0)