Skip to content

Commit 5c440b9

Browse files
authored
ENGCOM-7453: #27089 Fix issue with returning non-available default limit #27093
2 parents d4a9098 + c4ec8e4 commit 5c440b9

File tree

2 files changed

+119
-63
lines changed

2 files changed

+119
-63
lines changed

app/code/Magento/Catalog/Helper/Product/ProductList.php

Lines changed: 46 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,37 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
67

78
namespace Magento\Catalog\Helper\Product;
89

10+
use Magento\Catalog\Model\Config;
11+
use Magento\Framework\App\Config\ScopeConfigInterface;
12+
use Magento\Framework\App\ObjectManager;
13+
use Magento\Framework\Registry;
14+
use Magento\Store\Model\ScopeInterface;
15+
916
/**
10-
* Class ProductList
17+
* Returns data for toolbars of Sorting and Pagination
1118
*
1219
* @api
1320
* @since 100.0.2
1421
*/
1522
class ProductList
1623
{
17-
/**
18-
* List mode configuration path
19-
*/
20-
const XML_PATH_LIST_MODE = 'catalog/frontend/list_mode';
24+
public const XML_PATH_LIST_MODE = 'catalog/frontend/list_mode';
25+
public const DEFAULT_SORT_DIRECTION = 'asc';
2126

2227
const VIEW_MODE_LIST = 'list';
2328
const VIEW_MODE_GRID = 'grid';
2429

25-
const DEFAULT_SORT_DIRECTION = 'asc';
2630
/**
27-
* @var \Magento\Framework\App\Config\ScopeConfigInterface
31+
* @var ScopeConfigInterface
2832
*/
2933
protected $scopeConfig;
3034

3135
/**
32-
* @var \Magento\Framework\Registry
36+
* @var Registry
3337
*/
3438
private $coreRegistry;
3539

@@ -38,20 +42,18 @@ class ProductList
3842
*
3943
* @var array
4044
*/
41-
protected $_defaultAvailableLimit = [10 => 10,20 => 20,50 => 50];
45+
protected $_defaultAvailableLimit = [10 => 10, 20 => 20, 50 => 50];
4246

4347
/**
44-
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
45-
* @param \Magento\Framework\Registry $coreRegistry
48+
* @param ScopeConfigInterface $scopeConfig
49+
* @param Registry $coreRegistry
4650
*/
4751
public function __construct(
48-
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
49-
\Magento\Framework\Registry $coreRegistry = null
52+
ScopeConfigInterface $scopeConfig,
53+
Registry $coreRegistry = null
5054
) {
5155
$this->scopeConfig = $scopeConfig;
52-
$this->coreRegistry = $coreRegistry ?: \Magento\Framework\App\ObjectManager::getInstance()->get(
53-
\Magento\Framework\Registry::class
54-
);
56+
$this->coreRegistry = $coreRegistry ?? ObjectManager::getInstance()->get(Registry::class);
5557
}
5658

5759
/**
@@ -61,31 +63,23 @@ public function __construct(
6163
*/
6264
public function getAvailableViewMode()
6365
{
64-
$value = $this->scopeConfig->getValue(
65-
self::XML_PATH_LIST_MODE,
66-
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
67-
);
66+
$value = $this->scopeConfig->getValue(self::XML_PATH_LIST_MODE, ScopeInterface::SCOPE_STORE);
67+
6868
switch ($value) {
6969
case 'grid':
70-
$availableMode = ['grid' => __('Grid')];
71-
break;
70+
return ['grid' => __('Grid')];
7271

7372
case 'list':
74-
$availableMode = ['list' => __('List')];
75-
break;
73+
return ['list' => __('List')];
7674

7775
case 'grid-list':
78-
$availableMode = ['grid' => __('Grid'), 'list' => __('List')];
79-
break;
76+
return ['grid' => __('Grid'), 'list' => __('List')];
8077

8178
case 'list-grid':
82-
$availableMode = ['list' => __('List'), 'grid' => __('Grid')];
83-
break;
84-
default:
85-
$availableMode = null;
86-
break;
79+
return ['list' => __('List'), 'grid' => __('Grid')];
8780
}
88-
return $availableMode;
81+
82+
return null;
8983
}
9084

9185
/**
@@ -99,12 +93,14 @@ public function getDefaultViewMode($options = [])
9993
if (empty($options)) {
10094
$options = $this->getAvailableViewMode();
10195
}
96+
10297
return current(array_keys($options));
10398
}
10499

105100
/**
106101
* Get default sort field
107102
*
103+
* @FIXME Helper should be context-independent
108104
* @return null|string
109105
*/
110106
public function getDefaultSortField()
@@ -114,59 +110,46 @@ public function getDefaultSortField()
114110
return $currentCategory->getDefaultSortBy();
115111
}
116112

117-
return $this->scopeConfig->getValue(
118-
\Magento\Catalog\Model\Config::XML_PATH_LIST_DEFAULT_SORT_BY,
119-
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
120-
);
113+
return $this->scopeConfig->getValue(Config::XML_PATH_LIST_DEFAULT_SORT_BY, ScopeInterface::SCOPE_STORE);
121114
}
122115

123116
/**
124117
* Retrieve available limits for specified view mode
125118
*
126-
* @param string $mode
119+
* @param string $viewMode
127120
* @return array
128121
*/
129-
public function getAvailableLimit($mode)
122+
public function getAvailableLimit($viewMode): array
130123
{
131-
if (!in_array($mode, [self::VIEW_MODE_GRID, self::VIEW_MODE_LIST])) {
124+
$availableViewModes = $this->getAvailableViewMode();
125+
126+
if (!isset($availableViewModes[$viewMode])) {
132127
return $this->_defaultAvailableLimit;
133128
}
134-
$perPageConfigKey = 'catalog/frontend/' . $mode . '_per_page_values';
135-
$perPageValues = (string)$this->scopeConfig->getValue(
136-
$perPageConfigKey,
137-
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
138-
);
129+
130+
$perPageConfigPath = 'catalog/frontend/' . $viewMode . '_per_page_values';
131+
$perPageValues = (string)$this->scopeConfig->getValue($perPageConfigPath, ScopeInterface::SCOPE_STORE);
139132
$perPageValues = explode(',', $perPageValues);
140133
$perPageValues = array_combine($perPageValues, $perPageValues);
141-
if ($this->scopeConfig->isSetFlag(
142-
'catalog/frontend/list_allow_all',
143-
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
144-
)) {
134+
if ($this->scopeConfig->isSetFlag('catalog/frontend/list_allow_all', ScopeInterface::SCOPE_STORE)) {
145135
return ($perPageValues + ['all' => __('All')]);
146136
} else {
147137
return $perPageValues;
148138
}
149139
}
150140

151141
/**
152-
* Retrieve default per page values
142+
* Returns default value of `per_page` for view mode provided
153143
*
154144
* @param string $viewMode
155-
* @return string (comma separated)
145+
* @return int
156146
*/
157-
public function getDefaultLimitPerPageValue($viewMode)
147+
public function getDefaultLimitPerPageValue($viewMode): int
158148
{
159-
if ($viewMode == self::VIEW_MODE_LIST) {
160-
return $this->scopeConfig->getValue(
161-
'catalog/frontend/list_per_page',
162-
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
163-
);
164-
} elseif ($viewMode == self::VIEW_MODE_GRID) {
165-
return $this->scopeConfig->getValue(
166-
'catalog/frontend/grid_per_page',
167-
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
168-
);
169-
}
170-
return 0;
149+
$xmlConfigPath = sprintf('catalog/frontend/%s_per_page', $viewMode);
150+
$defaultLimit = $this->scopeConfig->getValue($xmlConfigPath, ScopeInterface::SCOPE_STORE);
151+
152+
$availableLimits = $this->getAvailableLimit($viewMode);
153+
return (int)($availableLimits[$defaultLimit] ?? current($availableLimits));
171154
}
172155
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Catalog\Test\Unit\Helper\Product;
9+
10+
use Magento\Catalog\Helper\Product\ProductList;
11+
use Magento\Framework\App\Config\ScopeConfigInterface;
12+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
13+
use PHPUnit\Framework\MockObject\MockObject;
14+
use PHPUnit\Framework\TestCase;
15+
16+
class ProductListTest extends TestCase
17+
{
18+
const STUB_VIEW_MODE = 'grid';
19+
/**
20+
* @var ScopeConfigInterface|MockObject
21+
*/
22+
private $scopeConfigMock;
23+
24+
/**
25+
* @var ProductList
26+
*/
27+
private $productListHelper;
28+
29+
protected function setUp()
30+
{
31+
$objectManager = new ObjectManager($this);
32+
33+
$this->scopeConfigMock = $this->getMockForAbstractClass(ScopeConfigInterface::class);
34+
$this->productListHelper = $objectManager->getObject(ProductList::class, [
35+
'scopeConfig' => $this->scopeConfigMock
36+
]);
37+
}
38+
39+
/**
40+
* @dataProvider defaultAvailableLimitsDataProvider
41+
*/
42+
public function testGetDefaultLimitPerPageValueReturnsOneOfAvailableLimits(
43+
string $availableValues,
44+
int $defaultValue,
45+
int $expectedReturn
46+
) {
47+
$this->scopeConfigMock->method('getValue')
48+
->willReturnMap([
49+
[sprintf('catalog/frontend/%s_per_page_values', self::STUB_VIEW_MODE), $availableValues],
50+
[sprintf('catalog/frontend/%s_per_page', self::STUB_VIEW_MODE), $defaultValue]
51+
]);
52+
53+
$returnedValue = $this->productListHelper->getDefaultLimitPerPageValue(self::STUB_VIEW_MODE);
54+
55+
$this->assertSame($expectedReturn, $returnedValue);
56+
}
57+
58+
public function defaultAvailableLimitsDataProvider(): array
59+
{
60+
return [
61+
'limit-available' => [
62+
'values' => '10,20,30',
63+
'default' => 10,
64+
'expected' => 10
65+
],
66+
'limit-not-available' => [
67+
'values' => '10,20,30',
68+
'default' => 1,
69+
'expected' => 10
70+
]
71+
];
72+
}
73+
}

0 commit comments

Comments
 (0)