Skip to content

Commit d956c24

Browse files
committed
SwiftOtter-SOP-348 Add support for Catalog Search with negative page number
1 parent a5accf8 commit d956c24

File tree

3 files changed

+136
-18
lines changed

3 files changed

+136
-18
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
/**
4+
* Copyright 2025 Adobe
5+
* All Rights Reserved.
6+
*/
7+
-->
8+
<tests xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
9+
xsi:noNamespaceSchemaLocation="urn:magento:mftf:Test/etc/testSchema.xsd">
10+
<test name="StorefrontPaginationResetOnNegativePageNumberTest">
11+
<annotations>
12+
<features value="Catalog"/>
13+
<stories value="Product grid"/>
14+
<title value="Pagination should reset on storefront when negative page number requested"/>
15+
<description value="Pagination should reset on storefront when negative page number requested"/>
16+
<severity value="AVERAGE"/>
17+
<testCaseId value=""/>
18+
<useCaseId value=""/>
19+
<group value="catalog"/>
20+
</annotations>
21+
<before>
22+
<createData entity="SimpleSubCategory" stepKey="createCategory"/>
23+
<createData entity="SimpleProduct" stepKey="createSimpleProductOne">
24+
<requiredEntity createDataKey="createCategory"/>
25+
</createData>
26+
<createData entity="SimpleProduct" stepKey="createSimpleProductTwo">
27+
<requiredEntity createDataKey="createCategory"/>
28+
</createData>
29+
</before>
30+
<after>
31+
<deleteData createDataKey="createCategory" stepKey="deleteCategory"/>
32+
<deleteData createDataKey="createSimpleProductOne" stepKey="deleteProductOne"/>
33+
<deleteData createDataKey="createSimpleProductTwo" stepKey="deleteProductTwo"/>
34+
</after>
35+
<!-- Go to category page with `-1` as a Page Number -->
36+
<amOnPage url="{{StorefrontCategoryPage.url($$createCategory.custom_attributes[url_key]$$)}}?p=-1" stepKey="goToStorefrontCreatedCategoryPage"/>
37+
<!-- Expect redirect to the base URL of category, without any pagination -->
38+
<actionGroup ref="AdminGridAssertCurrentPageNumberActionGroup" stepKey="assertCurrentPageIsTwoOnProductGridFirstSearch">
39+
<argument name="expectedCurrentPageNumber" value="1"/>
40+
</actionGroup>
41+
<!-- Validate that "no products found" error message is not present -->
42+
<actionGroup ref="StorefrontDontSeeNoProductsFoundActionGroup" stepKey="dontSeeNoProdsFoundMessage"/>
43+
<!-- Verify the products are visible on the Category Page -->
44+
<actionGroup ref="AssertStorefrontProductIsPresentOnCategoryPageActionGroup" stepKey="seeProductOneOnGrid">
45+
<argument name="productName" value="$$createSimpleProductOne.name$$"/>
46+
</actionGroup>
47+
<actionGroup ref="AssertStorefrontProductIsPresentOnCategoryPageActionGroup" stepKey="seeProductTwoOnGrid">
48+
<argument name="productName" value="$$createSimpleProductTwo.name$$"/>
49+
</actionGroup>
50+
</test>
51+
</tests>

app/code/Magento/CatalogSearch/Controller/Result/Index.php

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
* Copyright 2014 Adobe
44
* All Rights Reserved.
55
*/
6+
67
namespace Magento\CatalogSearch\Controller\Result;
78

9+
use Magento\Catalog\Model\Product\ProductList\Toolbar;
810
use Magento\Framework\App\Action\HttpGetActionInterface;
911
use Magento\Catalog\Model\Layer\Resolver;
1012
use Magento\Catalog\Model\Session;
@@ -88,28 +90,37 @@ public function execute()
8890

8991
$queryText = $query->getQueryText();
9092

91-
if ($queryText != '') {
92-
$catalogSearchHelper = $this->_objectManager->get(\Magento\CatalogSearch\Helper\Data::class);
93+
if (empty($queryText)) {
94+
$this->getResponse()->setRedirect($this->_redirect->getRedirectUrl());
95+
return;
96+
}
9397

94-
$getAdditionalRequestParameters = $this->getRequest()->getParams();
95-
unset($getAdditionalRequestParameters[QueryFactory::QUERY_VAR_NAME]);
98+
// Negative ?p= value is not supported, redirect to a base version of category page.
99+
if ($this->_request->getParam(Toolbar::PAGE_PARM_NAME) < 0) {
100+
$this->getResponse()->setRedirect(
101+
$this->_url->getUrl('*/*', ['_current' => true, '_query' => [Toolbar::PAGE_PARM_NAME => null]])
102+
);
103+
return;
104+
}
96105

97-
$handles = null;
98-
if ($query->getNumResults() == 0) {
99-
$this->_view->getPage()->initLayout();
100-
$handles = $this->_view->getLayout()->getUpdate()->getHandles();
101-
$handles[] = static::DEFAULT_NO_RESULT_HANDLE;
102-
}
106+
$catalogSearchHelper = $this->_objectManager->get(\Magento\CatalogSearch\Helper\Data::class);
103107

104-
if (empty($getAdditionalRequestParameters) &&
105-
$this->_objectManager->get(PopularSearchTerms::class)->isCacheable($queryText, $storeId)
106-
) {
107-
$this->getCacheableResult($catalogSearchHelper, $query, $handles);
108-
} else {
109-
$this->getNotCacheableResult($catalogSearchHelper, $query, $handles);
110-
}
108+
$getAdditionalRequestParameters = $this->getRequest()->getParams();
109+
unset($getAdditionalRequestParameters[QueryFactory::QUERY_VAR_NAME]);
110+
111+
$handles = null;
112+
if ($query->getNumResults() == 0) {
113+
$this->_view->getPage()->initLayout();
114+
$handles = $this->_view->getLayout()->getUpdate()->getHandles();
115+
$handles[] = static::DEFAULT_NO_RESULT_HANDLE;
116+
}
117+
118+
if (empty($getAdditionalRequestParameters) &&
119+
$this->_objectManager->get(PopularSearchTerms::class)->isCacheable($queryText, $storeId)
120+
) {
121+
$this->getCacheableResult($catalogSearchHelper, $query, $handles);
111122
} else {
112-
$this->getResponse()->setRedirect($this->_redirect->getRedirectUrl());
123+
$this->getNotCacheableResult($catalogSearchHelper, $query, $handles);
113124
}
114125
}
115126

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
/**
4+
* Copyright 2019 Adobe
5+
* All Rights Reserved.
6+
*/
7+
-->
8+
9+
<tests xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
10+
xsi:noNamespaceSchemaLocation="urn:magento:mftf:Test/etc/testSchema.xsd">
11+
<test name="CatalogSearchWithNegativeQuantityTest">
12+
<annotations>
13+
<features value="CatalogSearch"/>
14+
<stories value="Catalog Search Results"/>
15+
<title value="Pagination should reset on Storefront when negative page number requested"/>
16+
<description value="Pagination should reset on Storefront when negative page number requested"/>
17+
<severity value="AVERAGE"/>
18+
<testCaseId value=""/>
19+
<useCaseId value=""/>
20+
<group value="CatalogSearch"/>
21+
<group value="SearchEngine"/>
22+
</annotations>
23+
<before>
24+
<createData entity="SimpleSubCategory" stepKey="createCategory"/>
25+
<createData entity="SimpleProduct" stepKey="createSimpleProductOne">
26+
<requiredEntity createDataKey="createCategory"/>
27+
</createData>
28+
<createData entity="SimpleProduct" stepKey="createSimpleProductTwo">
29+
<requiredEntity createDataKey="createCategory"/>
30+
</createData>
31+
</before>
32+
<after>
33+
<deleteData createDataKey="createCategory" stepKey="deleteCategory"/>
34+
<deleteData createDataKey="createSimpleProductOne" stepKey="deleteProductOne"/>
35+
<deleteData createDataKey="createSimpleProductTwo" stepKey="deleteProductTwo"/>
36+
</after>
37+
38+
<actionGroup ref="StorefrontQuickSearchWithPaginationActionGroup" stepKey="visitSearchResults">
39+
<argument name="phrase" value="simple"/>
40+
<argument name="pageNumber" value="-1"/>
41+
</actionGroup>
42+
43+
<actionGroup ref="AdminGridAssertCurrentPageNumberActionGroup" stepKey="assertCurrentPageIsTwoOnProductGridFirstSearch">
44+
<argument name="expectedCurrentPageNumber" value="1"/>
45+
</actionGroup>
46+
<!-- Validate that "no products found" error message is not present -->
47+
<actionGroup ref="StorefrontDontSeeNoProductsFoundActionGroup" stepKey="dontSeeNoProdsFoundMessage"/>
48+
<!-- Verify the products are visible on the Category Page -->
49+
<actionGroup ref="AssertStorefrontProductIsPresentOnCategoryPageActionGroup" stepKey="seeProductOneOnGrid">
50+
<argument name="productName" value="$$createSimpleProductOne.name$$"/>
51+
</actionGroup>
52+
<actionGroup ref="AssertStorefrontProductIsPresentOnCategoryPageActionGroup" stepKey="seeProductTwoOnGrid">
53+
<argument name="productName" value="$$createSimpleProductTwo.name$$"/>
54+
</actionGroup>
55+
</test>
56+
</tests>

0 commit comments

Comments
 (0)