Skip to content

#40079 SwiftOtter-SOP-348 Resolve issues with Product listing with negative … #40080

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: 2.4-develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions app/code/Magento/Catalog/Controller/Category/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,13 @@ public function execute()

$category = $this->_initCategory();
if ($category) {
// Negative ?p= value is not supported, redirect to a base version of category page.
if ($this->_request->getParam(Toolbar::PAGE_PARM_NAME) < 0) {
return $this->resultRedirectFactory->create()
->setHttpResponseCode(301)
->setUrl($category->getUrl());
}

$this->layerResolver->create(Resolver::CATALOG_LAYER_CATEGORY);
$settings = $this->_catalogDesign->getDesignSettings($category);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
/**
* Copyright 2025 Adobe
* All Rights Reserved.
*/
-->
<tests xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:mftf:Test/etc/testSchema.xsd">
<test name="StorefrontPaginationResetOnNegativePageNumberTest">
<annotations>
<features value="Catalog"/>
<stories value="Product grid"/>
<title value="Pagination should reset on storefront when negative page number requested"/>
<description value="Pagination should reset on storefront when negative page number requested"/>
<severity value="AVERAGE"/>
<testCaseId value=""/>
<useCaseId value=""/>
<group value="catalog"/>
</annotations>
<before>
<createData entity="SimpleSubCategory" stepKey="createCategory"/>
<createData entity="SimpleProduct" stepKey="createSimpleProductOne">
<requiredEntity createDataKey="createCategory"/>
</createData>
<createData entity="SimpleProduct" stepKey="createSimpleProductTwo">
<requiredEntity createDataKey="createCategory"/>
</createData>
</before>
<after>
<deleteData createDataKey="createCategory" stepKey="deleteCategory"/>
<deleteData createDataKey="createSimpleProductOne" stepKey="deleteProductOne"/>
<deleteData createDataKey="createSimpleProductTwo" stepKey="deleteProductTwo"/>
</after>
<!-- Go to category page with `-1` as a Page Number -->
<amOnPage url="{{StorefrontCategoryPage.url($$createCategory.custom_attributes[url_key]$$)}}?p=-1" stepKey="goToStorefrontCreatedCategoryPage"/>
<!-- Expect redirect to the base URL of category, without any pagination -->
<actionGroup ref="AssertStorefrontCategoryCurrentPageIsNthActionGroup" stepKey="assertCurrentPageIsTwoOnProductGridFirstSearch">
<argument name="expectedPage" value="1"/>
</actionGroup>
<!-- Validate that "no products found" error message is not present -->
<actionGroup ref="StorefrontDontSeeNoProductsFoundActionGroup" stepKey="dontSeeNoProdsFoundMessage"/>
<!-- Verify the products are visible on the Category Page -->
<actionGroup ref="AssertStorefrontProductIsPresentOnCategoryPageActionGroup" stepKey="seeProductOneOnGrid">
<argument name="productName" value="$$createSimpleProductOne.name$$"/>
</actionGroup>
<actionGroup ref="AssertStorefrontProductIsPresentOnCategoryPageActionGroup" stepKey="seeProductTwoOnGrid">
<argument name="productName" value="$$createSimpleProductTwo.name$$"/>
</actionGroup>
</test>
</tests>
47 changes: 29 additions & 18 deletions app/code/Magento/CatalogSearch/Controller/Result/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
* Copyright 2014 Adobe
* All Rights Reserved.
*/

namespace Magento\CatalogSearch\Controller\Result;

use Magento\Catalog\Model\Product\ProductList\Toolbar;
use Magento\Framework\App\Action\HttpGetActionInterface;
use Magento\Catalog\Model\Layer\Resolver;
use Magento\Catalog\Model\Session;
Expand Down Expand Up @@ -88,28 +90,37 @@ public function execute()

$queryText = $query->getQueryText();

if ($queryText != '') {
$catalogSearchHelper = $this->_objectManager->get(\Magento\CatalogSearch\Helper\Data::class);
if (empty($queryText)) {
$this->getResponse()->setRedirect($this->_redirect->getRedirectUrl());
return;
}

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

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

if (empty($getAdditionalRequestParameters) &&
$this->_objectManager->get(PopularSearchTerms::class)->isCacheable($queryText, $storeId)
) {
$this->getCacheableResult($catalogSearchHelper, $query, $handles);
} else {
$this->getNotCacheableResult($catalogSearchHelper, $query, $handles);
}
$getAdditionalRequestParameters = $this->getRequest()->getParams();
unset($getAdditionalRequestParameters[QueryFactory::QUERY_VAR_NAME]);

$handles = null;
if ($query->getNumResults() == 0) {
$this->_view->getPage()->initLayout();
$handles = $this->_view->getLayout()->getUpdate()->getHandles();
$handles[] = static::DEFAULT_NO_RESULT_HANDLE;
}

if (empty($getAdditionalRequestParameters) &&
$this->_objectManager->get(PopularSearchTerms::class)->isCacheable($queryText, $storeId)
) {
$this->getCacheableResult($catalogSearchHelper, $query, $handles);
} else {
$this->getResponse()->setRedirect($this->_redirect->getRedirectUrl());
$this->getNotCacheableResult($catalogSearchHelper, $query, $handles);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
/**
* Copyright 2019 Adobe
* All Rights Reserved.
*/
-->

<tests xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:mftf:Test/etc/testSchema.xsd">
<test name="CatalogSearchWithNegativeQuantityTest">
<annotations>
<features value="CatalogSearch"/>
<stories value="Catalog Search Results"/>
<title value="Pagination should reset on Storefront when negative page number requested"/>
<description value="Pagination should reset on Storefront when negative page number requested"/>
<severity value="AVERAGE"/>
<testCaseId value=""/>
<useCaseId value=""/>
<group value="CatalogSearch"/>
<group value="SearchEngine"/>
</annotations>
<before>
<createData entity="SimpleSubCategory" stepKey="createCategory"/>
<createData entity="SimpleProduct" stepKey="createSimpleProductOne">
<requiredEntity createDataKey="createCategory"/>
</createData>
<createData entity="SimpleProduct" stepKey="createSimpleProductTwo">
<requiredEntity createDataKey="createCategory"/>
</createData>
</before>
<after>
<deleteData createDataKey="createCategory" stepKey="deleteCategory"/>
<deleteData createDataKey="createSimpleProductOne" stepKey="deleteProductOne"/>
<deleteData createDataKey="createSimpleProductTwo" stepKey="deleteProductTwo"/>
</after>

<actionGroup ref="StorefrontQuickSearchWithPaginationActionGroup" stepKey="visitSearchResults">
<argument name="phrase" value="simple"/>
<argument name="pageNumber" value="-1"/>
</actionGroup>

<actionGroup ref="AssertStorefrontCategoryCurrentPageIsNthActionGroup" stepKey="assertCurrentPageIsTwoOnProductGridFirstSearch">
<argument name="expectedPage" value="1"/>
</actionGroup>
<!-- Validate that "no products found" error message is not present -->
<actionGroup ref="StorefrontDontSeeNoProductsFoundActionGroup" stepKey="dontSeeNoProdsFoundMessage"/>
<!-- Verify the products are visible on the Category Page -->
<actionGroup ref="AssertStorefrontProductIsPresentOnCategoryPageActionGroup" stepKey="seeProductOneOnGrid">
<argument name="productName" value="$$createSimpleProductOne.name$$"/>
</actionGroup>
<actionGroup ref="AssertStorefrontProductIsPresentOnCategoryPageActionGroup" stepKey="seeProductTwoOnGrid">
<argument name="productName" value="$$createSimpleProductTwo.name$$"/>
</actionGroup>
</test>
</tests>