diff --git a/src/Definition/Availability.php b/src/Definition/Availability.php new file mode 100644 index 000000000..558a22387 --- /dev/null +++ b/src/Definition/Availability.php @@ -0,0 +1,28 @@ + + * @copyright Since 2007 PrestaShop SA and Contributors + * @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0) + */ + +namespace PrestaShop\Module\FacetedSearch\Definition; + +class Availability +{ + const IN_STOCK = 2; + const AVAILABLE = 1; + const NOT_AVAILABLE = 0; +} diff --git a/src/Filters/Block.php b/src/Filters/Block.php index 2f31dc2db..bd411ff84 100644 --- a/src/Filters/Block.php +++ b/src/Filters/Block.php @@ -28,6 +28,7 @@ use Group; use Manufacturer; use PrestaShop\Module\FacetedSearch\Adapter\InterfaceAdapter; +use PrestaShop\Module\FacetedSearch\Definition\Availability; use PrestaShop\Module\FacetedSearch\Product\Search; use PrestaShop\PrestaShop\Core\Localization\Locale; use PrestaShop\PrestaShop\Core\Localization\Specification\NumberSymbolList; @@ -435,7 +436,7 @@ private function getAvailabilitiesBlock($filter, $selectedFilters) $availabilityOptions = []; if ($this->psStockManagement) { $availabilityOptions = [ - 2 => [ + Availability::IN_STOCK => [ 'name' => $this->context->getTranslator()->trans( 'In stock', [], @@ -443,7 +444,7 @@ private function getAvailabilitiesBlock($filter, $selectedFilters) ), 'nbr' => 0, ], - 1 => [ + Availability::AVAILABLE => [ 'name' => $this->context->getTranslator()->trans( 'Available', [], @@ -451,7 +452,7 @@ private function getAvailabilitiesBlock($filter, $selectedFilters) ), 'nbr' => 0, ], - 0 => [ + Availability::NOT_AVAILABLE => [ 'name' => $this->context->getTranslator()->trans( 'Not available', [], @@ -473,7 +474,7 @@ private function getAvailabilitiesBlock($filter, $selectedFilters) ], ] ); - $availabilityOptions[0]['nbr'] = $filteredSearchAdapter->count(); + $availabilityOptions[Availability::NOT_AVAILABLE]['nbr'] = $filteredSearchAdapter->count(); // Products in stock, or with out-of-stock ordering enabled $filteredSearchAdapter->addOperationsFilter( @@ -487,7 +488,7 @@ private function getAvailabilitiesBlock($filter, $selectedFilters) ], ] ); - $availabilityOptions[1]['nbr'] = $filteredSearchAdapter->count(); + $availabilityOptions[Availability::AVAILABLE]['nbr'] = $filteredSearchAdapter->count(); // Products in stock $filteredSearchAdapter->addOperationsFilter( @@ -498,7 +499,7 @@ private function getAvailabilitiesBlock($filter, $selectedFilters) ], ] ); - $availabilityOptions[2]['nbr'] = $filteredSearchAdapter->count(); + $availabilityOptions[Availability::IN_STOCK]['nbr'] = $filteredSearchAdapter->count(); // If some filter was selected, we want to show only this single filter, it does not make sense to show others if (isset($selectedFilters['availability'])) { @@ -513,8 +514,8 @@ private function getAvailabilitiesBlock($filter, $selectedFilters) // Hide Available option if the count is the same as In stock, it doesn't make no sense // Product count is a reliable indicator here, because there can never be product IN STOCK that is not AVAILABLE // So if the counts match, it MUST BE the same products - if ($availabilityOptions[1]['nbr'] == $availabilityOptions[2]['nbr']) { - unset($availabilityOptions[1]); + if ($availabilityOptions[Availability::AVAILABLE]['nbr'] == $availabilityOptions[Availability::IN_STOCK]['nbr']) { + unset($availabilityOptions[Availability::AVAILABLE]); } } diff --git a/src/Filters/Converter.php b/src/Filters/Converter.php index 3650b6a70..abe255c03 100644 --- a/src/Filters/Converter.php +++ b/src/Filters/Converter.php @@ -25,6 +25,7 @@ use Context; use Db; use Manufacturer; +use PrestaShop\Module\FacetedSearch\Definition\Availability; use PrestaShop\Module\FacetedSearch\Filters; use PrestaShop\Module\FacetedSearch\URLSerializer; use PrestaShop\PrestaShop\Core\Product\Search\Facet; @@ -257,7 +258,7 @@ public function createFacetedSearchFiltersFromQuery(ProductSearchQuery $query) $searchFilters = []; // Get filters configured in module settings for the current query - $filters = $this->provider->getFiltersForQuery($query, $idShop); + $configuredFilters = $this->provider->getFiltersForQuery($query, $idShop); /* * Parses submitted encoded facets from (URL) string into a nice array. @@ -266,16 +267,16 @@ public function createFacetedSearchFiltersFromQuery(ProductSearchQuery $query) * work very well, because there could be duplicate values for both facet and filter. * For example, if there are two features, feature values or categories with the same name. */ - $facetAndFiltersLabels = $this->urlSerializer->unserialize($query->getEncodedFacets()); + $receivedFilters = $this->urlSerializer->unserialize($query->getEncodedFacets()); // Go through filters that are configured and find out which should be activated, // depending on what was provided in the encodedFacets. - foreach ($filters as $filter) { + foreach ($configuredFilters as $filter) { $filterLabel = $this->convertFilterTypeToLabel($filter['type']); switch ($filter['type']) { case self::TYPE_MANUFACTURER: - if (!isset($facetAndFiltersLabels[$filterLabel])) { + if (!isset($receivedFilters[$filterLabel])) { // No need to filter if no information continue 2; } @@ -283,13 +284,13 @@ public function createFacetedSearchFiltersFromQuery(ProductSearchQuery $query) $manufacturers = Manufacturer::getManufacturers(false, $idLang); $searchFilters[$filter['type']] = []; foreach ($manufacturers as $manufacturer) { - if (in_array($manufacturer['name'], $facetAndFiltersLabels[$filterLabel])) { + if (in_array($manufacturer['name'], $receivedFilters[$filterLabel])) { $searchFilters[$filter['type']][$manufacturer['name']] = $manufacturer['id_manufacturer']; } } break; case self::TYPE_AVAILABILITY: - if (!isset($facetAndFiltersLabels[$filterLabel])) { + if (!isset($receivedFilters[$filterLabel])) { // No need to filter if no information continue 2; } @@ -299,28 +300,28 @@ public function createFacetedSearchFiltersFromQuery(ProductSearchQuery $query) 'Not available', [], 'Modules.Facetedsearch.Shop' - ) => 0, + ) => Availability::NOT_AVAILABLE, $this->context->getTranslator()->trans( 'Available', [], 'Modules.Facetedsearch.Shop' - ) => 1, + ) => Availability::AVAILABLE, $this->context->getTranslator()->trans( 'In stock', [], 'Modules.Facetedsearch.Shop' - ) => 2, + ) => Availability::IN_STOCK, ]; $searchFilters[$filter['type']] = []; foreach ($quantityArray as $quantityName => $quantityId) { - if (isset($facetAndFiltersLabels[$filterLabel]) && in_array($quantityName, $facetAndFiltersLabels[$filterLabel])) { + if (isset($receivedFilters[$filterLabel]) && in_array($quantityName, $receivedFilters[$filterLabel])) { $searchFilters[$filter['type']][] = $quantityId; } } break; case self::TYPE_CONDITION: - if (!isset($facetAndFiltersLabels[$filterLabel])) { + if (!isset($receivedFilters[$filterLabel])) { // No need to filter if no information continue 2; } @@ -345,7 +346,7 @@ public function createFacetedSearchFiltersFromQuery(ProductSearchQuery $query) $searchFilters[$filter['type']] = []; foreach ($conditionArray as $conditionName => $conditionId) { - if (isset($facetAndFiltersLabels[$filterLabel]) && in_array($conditionName, $facetAndFiltersLabels[$filterLabel])) { + if (isset($receivedFilters[$filterLabel]) && in_array($conditionName, $receivedFilters[$filterLabel])) { $searchFilters[$filter['type']][] = $conditionId; } } @@ -357,10 +358,10 @@ public function createFacetedSearchFiltersFromQuery(ProductSearchQuery $query) continue; } - if (isset($facetAndFiltersLabels[$feature['url_name']])) { - $featureValueLabels = $facetAndFiltersLabels[$feature['url_name']]; - } elseif (isset($facetAndFiltersLabels[$feature['name']])) { - $featureValueLabels = $facetAndFiltersLabels[$feature['name']]; + if (isset($receivedFilters[$feature['url_name']])) { + $featureValueLabels = $receivedFilters[$feature['url_name']]; + } elseif (isset($receivedFilters[$feature['name']])) { + $featureValueLabels = $receivedFilters[$feature['name']]; } else { continue; } @@ -382,10 +383,10 @@ public function createFacetedSearchFiltersFromQuery(ProductSearchQuery $query) continue; } - if (isset($facetAndFiltersLabels[$attributeGroup['url_name']])) { - $attributeLabels = $facetAndFiltersLabels[$attributeGroup['url_name']]; - } elseif (isset($facetAndFiltersLabels[$attributeGroup['attribute_group_name']])) { - $attributeLabels = $facetAndFiltersLabels[$attributeGroup['attribute_group_name']]; + if (isset($receivedFilters[$attributeGroup['url_name']])) { + $attributeLabels = $receivedFilters[$attributeGroup['url_name']]; + } elseif (isset($receivedFilters[$attributeGroup['attribute_group_name']])) { + $attributeLabels = $receivedFilters[$attributeGroup['attribute_group_name']]; } else { continue; } @@ -402,8 +403,8 @@ public function createFacetedSearchFiltersFromQuery(ProductSearchQuery $query) break; case self::TYPE_PRICE: case self::TYPE_WEIGHT: - if (isset($facetAndFiltersLabels[$filterLabel])) { - $filters = $facetAndFiltersLabels[$filterLabel]; + if (isset($receivedFilters[$filterLabel])) { + $filters = $receivedFilters[$filterLabel]; if (isset($filters[1]) && isset($filters[2])) { $from = $filters[1]; $to = $filters[2]; @@ -413,8 +414,8 @@ public function createFacetedSearchFiltersFromQuery(ProductSearchQuery $query) } break; case self::TYPE_CATEGORY: - if (isset($facetAndFiltersLabels[$filterLabel])) { - foreach ($facetAndFiltersLabels[$filterLabel] as $queryFilter) { + if (isset($receivedFilters[$filterLabel])) { + foreach ($receivedFilters[$filterLabel] as $queryFilter) { /* * This works only for categories that are child of the category we are browsing (or home category). * Categories deeper in the tree will never be found. This could be fixed by providing a unique ID @@ -428,8 +429,8 @@ public function createFacetedSearchFiltersFromQuery(ProductSearchQuery $query) } break; default: - if (isset($facetAndFiltersLabels[$filterLabel])) { - foreach ($facetAndFiltersLabels[$filterLabel] as $queryFilter) { + if (isset($receivedFilters[$filterLabel])) { + foreach ($receivedFilters[$filterLabel] as $queryFilter) { $searchFilters[$filter['type']][] = $queryFilter; } } diff --git a/src/Product/Search.php b/src/Product/Search.php index 79fc538d0..c61013bba 100644 --- a/src/Product/Search.php +++ b/src/Product/Search.php @@ -27,6 +27,7 @@ use Group; use PrestaShop\Module\FacetedSearch\Adapter\AbstractAdapter; use PrestaShop\Module\FacetedSearch\Adapter\MySQL as MySQLAdapter; +use PrestaShop\Module\FacetedSearch\Definition\Availability; use PrestaShop\PrestaShop\Core\Product\Search\ProductSearchQuery; class Search @@ -192,13 +193,13 @@ private function addSearchFilters($selectedFilters) // Simple cases with 1 option selected if (count($filterValues) == 1) { // Not available - if ($filterValues[0] == 0) { + if ($filterValues[0] == Availability::NOT_AVAILABLE) { $operationsFilter[] = [ ['quantity', [0], '<='], ['out_of_stock', $this->psOrderOutOfStock ? [0] : [0, 2], '='], ]; // Available - } elseif ($filterValues[0] == 1) { + } elseif ($filterValues[0] == Availability::AVAILABLE) { $operationsFilter[] = [ ['out_of_stock', $this->psOrderOutOfStock ? [1, 2] : [1], '='], ]; @@ -206,7 +207,7 @@ private function addSearchFilters($selectedFilters) ['quantity', [0], '>'], ]; // In stock - } elseif ($filterValues[0] == 2) { + } elseif ($filterValues[0] == Availability::IN_STOCK) { $operationsFilter[] = [ ['quantity', [0], '>'], ]; @@ -214,10 +215,10 @@ private function addSearchFilters($selectedFilters) // Cases with 2 options selected } elseif (count($filterValues) == 2) { // Not available and available, we show everything - if (in_array(0, $filterValues) && in_array(1, $filterValues)) { + if (in_array(Availability::NOT_AVAILABLE, $filterValues) && in_array(Availability::AVAILABLE, $filterValues)) { break; // Not available or in stock - } elseif (in_array(0, $filterValues) && in_array(2, $filterValues)) { + } elseif (in_array(Availability::NOT_AVAILABLE, $filterValues) && in_array(Availability::IN_STOCK, $filterValues)) { $operationsFilter[] = [ ['quantity', [0], '<='], ['out_of_stock', $this->psOrderOutOfStock ? [0] : [0, 2], '='], @@ -226,7 +227,7 @@ private function addSearchFilters($selectedFilters) ['quantity', [0], '>'], ]; // Available or in stock - } elseif (in_array(1, $filterValues) && in_array(2, $filterValues)) { + } elseif (in_array(Availability::AVAILABLE, $filterValues) && in_array(Availability::IN_STOCK, $filterValues)) { $operationsFilter[] = [ ['out_of_stock', $this->psOrderOutOfStock ? [1, 2] : [1], '='], ]; diff --git a/tests/php/FacetedSearch/Filters/BlockTest.php b/tests/php/FacetedSearch/Filters/BlockTest.php index e803b950c..4cc5f382c 100644 --- a/tests/php/FacetedSearch/Filters/BlockTest.php +++ b/tests/php/FacetedSearch/Filters/BlockTest.php @@ -29,6 +29,7 @@ use Mockery; use Mockery\Adapter\Phpunit\MockeryTestCase; use PrestaShop\Module\FacetedSearch\Adapter\MySQL; +use PrestaShop\Module\FacetedSearch\Definition\Availability; use PrestaShop\Module\FacetedSearch\Filters\Block; use PrestaShop\Module\FacetedSearch\Filters\DataAccessor; use PrestaShop\Module\FacetedSearch\Filters\Provider; @@ -380,16 +381,16 @@ public function testGetFiltersBlockWithQuantities() 'id_key' => 0, 'name' => 'Availability', 'values' => [ - 2 => [ + Availability::IN_STOCK => [ 'name' => 'In stock', 'nbr' => 50, ], - 1 => [ + Availability::AVAILABLE => [ 'name' => 'Available', 'nbr' => 100, 'checked' => true, ], - 0 => [ + Availability::NOT_AVAILABLE => [ 'name' => 'Not available', 'nbr' => 1000, ], @@ -403,7 +404,7 @@ public function testGetFiltersBlockWithQuantities() 10, [ 'availability' => [ - 1, + Availability::AVAILABLE, ], ] ) diff --git a/tests/php/FacetedSearch/Product/SearchTest.php b/tests/php/FacetedSearch/Product/SearchTest.php index 65a7e200f..ac6ad153a 100644 --- a/tests/php/FacetedSearch/Product/SearchTest.php +++ b/tests/php/FacetedSearch/Product/SearchTest.php @@ -27,6 +27,7 @@ use Mockery; use Mockery\Adapter\Phpunit\MockeryTestCase; use PrestaShop\Module\FacetedSearch\Adapter\MySQL; +use PrestaShop\Module\FacetedSearch\Definition\Availability; use PrestaShop\Module\FacetedSearch\Product\Search; use PrestaShop\PrestaShop\Core\Product\Search\ProductSearchQuery; use stdClass; @@ -291,7 +292,7 @@ public function testInitSearchWithAllFilters() [6], ], 'availability' => [ - 0, + Availability::NOT_AVAILABLE, ], 'weight' => [ '10', @@ -636,7 +637,7 @@ public function testInitSearchWithQuantityFiltersWithoutStockManagement() ->andReturn('category'); $this->search->setQuery($query); - $this->search->initSearch(['availability' => [0]]); + $this->search->initSearch(['availability' => [Availability::NOT_AVAILABLE]]); $this->assertEquals([], $this->search->getSearchAdapter()->getFilters()->toArray()); $this->assertEquals([], $this->search->getSearchAdapter()->getOperationsFilters()->toArray()); @@ -686,7 +687,7 @@ public function testInitSearchWithQuantityFiltersWithoutStockManagement() public function testInitSearchWithFirstQuantityFilters() { - $this->search->initSearch(['availability' => [1]]); + $this->search->initSearch(['availability' => [Availability::AVAILABLE]]); $this->assertEquals([], $this->search->getSearchAdapter()->getFilters()->toArray()); $this->assertEquals([], $this->search->getSearchAdapter()->getOperationsFilters()->toArray()); @@ -720,7 +721,7 @@ public function testInitSearchWithFirstQuantityFilters() public function testInitSearchWithSecondQuantityFilters() { - $this->search->initSearch(['availability' => [2]]); + $this->search->initSearch(['availability' => [Availability::IN_STOCK]]); $this->assertEquals([], $this->search->getSearchAdapter()->getFilters()->toArray()); $this->assertEquals([], $this->search->getSearchAdapter()->getOperationsFilters()->toArray()); @@ -750,7 +751,7 @@ public function testInitSearchWithoutGroupFeature() Group::setStaticExpectations($groupMock); - $this->search->initSearch(['availability' => [2]]); + $this->search->initSearch(['availability' => [Availability::IN_STOCK]]); $this->assertEquals([], $this->search->getSearchAdapter()->getFilters()->toArray()); $this->assertEquals([], $this->search->getSearchAdapter()->getOperationsFilters()->toArray()); @@ -798,7 +799,7 @@ public function testInitSearchWithUserBelongingToGroups() FrontController::setStaticExpectations($frontControllerMock); - $this->search->initSearch(['availability' => [2]]); + $this->search->initSearch(['availability' => [Availability::IN_STOCK]]); $this->assertEquals([], $this->search->getSearchAdapter()->getFilters()->toArray()); $this->assertEquals([], $this->search->getSearchAdapter()->getOperationsFilters()->toArray());