ltsecurityinc安防产品在线商城代码

This commit is contained in:
2026-09-14 17:47:10 +08:00
parent 57d8444c15
commit 6a3c9c5bd8
14539 changed files with 1775310 additions and 0 deletions
@@ -0,0 +1,429 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\LayeredNavigation\Block\Navigation;
use Magento\Catalog\Api\Data\CategoryInterface;
use Magento\Catalog\Api\ProductAttributeRepositoryInterface;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Catalog\Model\Layer\Filter\AbstractFilter;
use Magento\Catalog\Model\Layer\Filter\Item;
use Magento\Catalog\Model\Layer\Resolver;
use Magento\Catalog\Model\ResourceModel\Category\Collection;
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory;
use Magento\CatalogSearch\Model\Indexer\Fulltext\Processor;
use Magento\Framework\ObjectManagerInterface;
use Magento\Framework\Search\Request\Builder;
use Magento\Framework\Search\Request\Config;
use Magento\Framework\View\LayoutInterface;
use Magento\LayeredNavigation\Block\Navigation;
use Magento\LayeredNavigation\Block\Navigation\Category as CategoryNavigationBlock;
use Magento\LayeredNavigation\Block\Navigation\Search as SearchNavigationBlock;
use Magento\Search\Model\Search;
use Magento\Store\Model\Store;
use Magento\TestFramework\Helper\Bootstrap;
use PHPUnit\Framework\TestCase;
/**
* Base class for custom filters in navigation block on category page.
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
abstract class AbstractFiltersTest extends TestCase
{
/**
* @var ObjectManagerInterface
*/
protected $objectManager;
/**
* @var CollectionFactory
*/
protected $categoryCollectionFactory;
/**
* @var Navigation
*/
protected $navigationBlock;
/**
* @var LayoutInterface
*/
protected $layout;
/**
* @var ProductAttributeRepositoryInterface
*/
protected $attributeRepository;
/**
* @var ProductRepositoryInterface
*/
protected $productRepository;
/**
* @inheritdoc
*/
protected function setUp(): void
{
parent::setUp();
$this->objectManager = Bootstrap::getObjectManager();
$this->categoryCollectionFactory = $this->objectManager->create(CollectionFactory::class);
$this->layout = $this->objectManager->get(LayoutInterface::class);
$this->attributeRepository = $this->objectManager->create(ProductAttributeRepositoryInterface::class);
$this->productRepository = $this->objectManager->create(ProductRepositoryInterface::class);
$this->createNavigationBlockInstance();
}
/**
* Returns layer type for navigation block.
*
* @return string
*/
abstract protected function getLayerType(): string;
/**
* Returns attribute code.
*
* @return string
*/
protected function getAttributeCode(): string
{
return '';
}
/**
* Tests getFilters method from navigation block on category page.
*
* @param array $products
* @param array $attributeData
* @param array $expectation
* @param string $categoryName
* @return void
*/
protected function getCategoryFiltersAndAssert(
array $products,
array $attributeData,
array $expectation,
string $categoryName
): void {
$this->updateAttribute($attributeData, $this->getAttributeCode());
$this->updateProducts($products, $this->getAttributeCode());
$this->clearInstanceAndReindexSearch();
$category = $this->loadCategory($categoryName, Store::DEFAULT_STORE_ID);
$this->navigationBlock->getLayer()->setCurrentCategory($category);
$this->navigationBlock->setLayout($this->layout);
$filter = $this->getFilterByCode($this->navigationBlock->getFilters(), $this->getAttributeCode());
if ($attributeData['is_filterable']) {
$this->assertNotNull($filter);
$preparedItems = $this->prepareFilterItems($filter);
$this->assertCount(count($expectation), $preparedItems);
foreach ($preparedItems as $key => $preparedItem) {
$this->assertEquals($expectation[$key], $preparedItem);
$item = $filter->getItems()[$key];
$this->createNavigationBlockInstance();
$this->navigationBlock->getRequest()->setParams(
$this->getRequestParams((string) $item->getValueString())
);
$this->navigationBlock->getLayer()->setCurrentCategory($category);
$this->navigationBlock->setLayout($this->layout);
$collectionSize = $this->navigationBlock->getLayer()->getProductCollection()->count();
$this->assertEquals($expectation[$key]['count'], $collectionSize);
$this->assertEquals($collectionSize, $item->getData('count'));
}
} else {
$this->assertNull($filter);
}
}
/**
* Tests getFilters method from navigation block layer state on category page.
*
* @param array $products
* @param array $expectation
* @param string $categoryName
* @param string|null $filterValue
* @param int $productsCount
* @return void
*/
protected function getCategoryActiveFiltersAndAssert(
array $products,
array $expectation,
string $categoryName,
string $filterValue,
int $productsCount
): void {
$this->updateAttribute(
['is_filterable' => AbstractFilter::ATTRIBUTE_OPTIONS_ONLY_WITH_RESULTS],
$this->getAttributeCode()
);
$this->updateProducts($products, $this->getAttributeCode());
$this->clearInstanceAndReindexSearch();
$this->navigationBlock->getRequest()->setParams($this->getRequestParams($filterValue));
$this->navigationBlock->getLayer()->setCurrentCategory(
$this->loadCategory($categoryName, Store::DEFAULT_STORE_ID)
);
$this->navigationBlock->setLayout($this->layout);
$activeFilters = $this->navigationBlock->getLayer()->getState()->getFilters();
$this->assertCount(1, $activeFilters);
$currentFilter = reset($activeFilters);
$this->assertActiveFilter($expectation, $currentFilter);
$this->assertEquals($productsCount, $this->navigationBlock->getLayer()->getProductCollection()->getSize());
}
/**
* Tests getFilters method from navigation block on search page.
*
* @param array $products
* @param array $attributeData
* @param array $expectation
* @return void
*/
protected function getSearchFiltersAndAssert(
array $products,
array $attributeData,
array $expectation
): void {
$this->updateAttribute($attributeData, $this->getAttributeCode());
$this->updateProducts($products, $this->getAttributeCode());
$this->clearInstanceAndReindexSearch();
$this->navigationBlock->getRequest()->setParams(['q' => $this->getSearchString()]);
$this->navigationBlock->setLayout($this->layout);
$filter = $this->getFilterByCode($this->navigationBlock->getFilters(), $this->getAttributeCode());
if ($attributeData['is_filterable_in_search']) {
$this->assertNotNull($filter);
$this->assertEquals($expectation, $this->prepareFilterItems($filter));
} else {
$this->assertNull($filter);
}
}
/**
* Tests getFilters method from navigation block layer state on search page.
*
* @param array $products
* @param array $expectation
* @param string $filterValue
* @param int $productsCount
* @return void
*/
protected function getSearchActiveFiltersAndAssert(
array $products,
array $expectation,
string $filterValue,
int $productsCount
): void {
$this->updateAttribute(
['is_filterable' => AbstractFilter::ATTRIBUTE_OPTIONS_ONLY_WITH_RESULTS, 'is_filterable_in_search' => 1],
$this->getAttributeCode()
);
$this->updateProducts($products, $this->getAttributeCode());
$this->clearInstanceAndReindexSearch();
$this->navigationBlock->getRequest()->setParams(
array_merge($this->getRequestParams($filterValue), ['q' => $this->getSearchString()])
);
$this->navigationBlock->setLayout($this->layout);
$activeFilters = $this->navigationBlock->getLayer()->getState()->getFilters();
$this->assertCount(1, $activeFilters);
$currentFilter = reset($activeFilters);
$this->assertActiveFilter($expectation, $currentFilter);
$this->assertEquals($productsCount, $this->navigationBlock->getLayer()->getProductCollection()->getSize());
}
/**
* Returns filter with specified attribute.
*
* @param AbstractFilter[] $filters
* @param string $code
* @return AbstractFilter|null
*/
protected function getFilterByCode(array $filters, string $code): ?AbstractFilter
{
$filter = array_filter(
$filters,
function (AbstractFilter $filter) use ($code) {
return $filter->getData('attribute_model')
&& $filter->getData('attribute_model')->getAttributeCode() === $code;
}
);
return array_shift($filter);
}
/**
* Updates attribute data.
*
* @param array $data
* @param string $attributeCode
* @return void
*/
protected function updateAttribute(
array $data,
string $attributeCode
): void {
$attribute = $this->attributeRepository->get($attributeCode);
$attribute->setDataChanges(false);
$attribute->addData($data);
if ($attribute->hasDataChanges()) {
$this->attributeRepository->save($attribute);
}
}
/**
* Returns filter items as array.
*
* @param AbstractFilter $filter
* @return array
*/
protected function prepareFilterItems(AbstractFilter $filter): array
{
$items = [];
/** @var Item $item */
foreach ($filter->getItems() as $item) {
$items[] = [
'label' => $item->getData('label'),
'count' => $item->getData('count'),
];
}
return $items;
}
/**
* Update products data by attribute.
*
* @param array $products
* @param string $attributeCode
* @param int $storeId
* @return void
*/
protected function updateProducts(
array $products,
string $attributeCode,
int $storeId = Store::DEFAULT_STORE_ID
): void {
$attribute = $this->attributeRepository->get($attributeCode);
foreach ($products as $productSku => $stringValue) {
$product = $this->productRepository->get($productSku, false, $storeId, true);
$productValue = $attribute->usesSource()
? $attribute->getSource()->getOptionId($stringValue)
: $stringValue;
$product->addData(
[$attribute->getAttributeCode() => $productValue]
);
$this->productRepository->save($product);
}
}
/**
* Clears instances and rebuilds seqrch index.
*
* @return void
*/
protected function clearInstanceAndReindexSearch(): void
{
$this->objectManager->removeSharedInstance(Config::class);
$this->objectManager->removeSharedInstance(Builder::class);
$this->objectManager->removeSharedInstance(Search::class);
$this->objectManager->create(Processor::class)->reindexAll();
}
/**
* Loads category by id.
*
* @param string $categoryName
* @param int $storeId
* @return CategoryInterface
*/
protected function loadCategory(string $categoryName, int $storeId): CategoryInterface
{
/** @var Collection $categoryCollection */
$categoryCollection = $this->categoryCollectionFactory->create();
/** @var CategoryInterface $category */
$category = $categoryCollection->setStoreId($storeId)
->addAttributeToSelect('display_mode', 'left')
->addAttributeToFilter(CategoryInterface::KEY_NAME, $categoryName)
->setPageSize(1)
->getFirstItem();
$category->setStoreId($storeId);
return $category;
}
/**
* Creates navigation block instance.
*
* @return void
*/
protected function createNavigationBlockInstance(): void
{
if ($this->getLayerType() === Resolver::CATALOG_LAYER_SEARCH) {
$class = SearchNavigationBlock::class;
$this->objectManager->removeSharedInstance('searchFilterList');
} else {
$class = CategoryNavigationBlock::class;
$this->objectManager->removeSharedInstance('categoryFilterList');
}
$layerResolver = $this->objectManager->create(Resolver::class);
$layerResolver->create($this->getLayerType());
$this->navigationBlock = $this->objectManager->create(
$class,
[
'layerResolver' => $layerResolver,
]
);
}
/**
* Returns search query for filters on search page.
*
* @return string
*/
protected function getSearchString(): string
{
return 'Simple Product';
}
/**
* Adds params for filtering.
*
* @param string $filterValue
* @return array
*/
protected function getRequestParams(string $filterValue): array
{
$attribute = $this->attributeRepository->get($this->getAttributeCode());
$filterValue = $attribute->usesSource()
? $attribute->getSource()->getOptionId($filterValue)
: $filterValue;
return [$this->getAttributeCode() => $filterValue];
}
/**
* Asserts active filter data.
*
* @param array $expectation
* @param Item $currentFilter
* @return void
*/
protected function assertActiveFilter(array $expectation, Item $currentFilter): void
{
$this->assertEquals($expectation['label'], $currentFilter->getData('label'));
$this->assertEquals($expectation['count'], $currentFilter->getData('count'));
$this->assertEquals(
$this->getAttributeCode(),
$currentFilter->getFilter()->getData('attribute_model')->getAttributeCode()
);
}
}
@@ -0,0 +1,139 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\LayeredNavigation\Block\Navigation\Category;
use Magento\Catalog\Model\Layer\Filter\AbstractFilter;
use Magento\Catalog\Model\Layer\Resolver;
use Magento\LayeredNavigation\Block\Navigation\AbstractFiltersTest;
/**
* Provides tests for custom boolean filter in navigation block on category page.
*
* @magentoAppArea frontend
* @magentoAppIsolation enabled
* @magentoDbIsolation disabled
*/
class BooleanFilterTest extends AbstractFiltersTest
{
/**
* @magentoDataFixture Magento/Catalog/_files/product_boolean_attribute.php
* @magentoDataFixture Magento/Catalog/_files/category_with_different_price_products.php
* @dataProvider getFiltersWithCustomAttributeDataProvider
* @param array $products
* @param array $attributeData
* @param array $expectation
* @return void
*/
public function testGetFiltersWithCustomAttribute(array $products, array $attributeData, array $expectation): void
{
$this->getCategoryFiltersAndAssert($products, $attributeData, $expectation, 'Category 999');
}
/**
* @return array
*/
public function getFiltersWithCustomAttributeDataProvider(): array
{
return [
'not_used_in_navigation' => [
'products_data' => [],
'attribute_data' => ['is_filterable' => 0],
'expectation' => [],
],
'used_in_navigation_with_results' => [
'products_data' => [
'simple1000' => 'Yes',
'simple1001' => 'Yes',
],
'attribute_data' => ['is_filterable' => AbstractFilter::ATTRIBUTE_OPTIONS_ONLY_WITH_RESULTS],
'expectation' => [
['label' => 'Yes', 'count' => 2],
],
],
'used_in_navigation_without_results' => [
'products_data' => [
'simple1000' => 'Yes',
'simple1001' => 'Yes',
],
'attribute_data' => ['is_filterable' => 2],
'expectation' => [
['label' => 'Yes', 'count' => 2],
['label' => 'No', 'count' => 0],
],
],
];
}
/**
* @magentoDataFixture Magento/Catalog/_files/product_boolean_attribute.php
* @magentoDataFixture Magento/Catalog/_files/category_with_different_price_products.php
* @dataProvider getActiveFiltersWithCustomAttributeDataProvider
* @param array $products
* @param array $expectation
* @param string $filterValue
* @param int $productsCount
* @return void
*/
public function testGetActiveFiltersWithCustomAttribute(
array $products,
array $expectation,
string $filterValue,
int $productsCount
): void {
$this->getCategoryActiveFiltersAndAssert($products, $expectation, 'Category 999', $filterValue, $productsCount);
}
/**
* @return array
*/
public function getActiveFiltersWithCustomAttributeDataProvider(): array
{
return [
'selected_yes_option_in_all_products' => [
'products_data' => ['simple1000' => 'Yes', 'simple1001' => 'Yes'],
'expectation' => ['label' => 'Yes', 'count' => 0],
'filter_value' => 'Yes',
'products_count' => 2,
],
'selected_yes_option_in_one_product' => [
'products_data' => ['simple1000' => 'Yes', 'simple1001' => 'No'],
'expectation' => ['label' => 'Yes', 'count' => 0],
'filter_value' => 'Yes',
'products_count' => 1,
],
'selected_no_option_in_all_products' => [
'products_data' => ['simple1000' => 'No', 'simple1001' => 'No'],
'expectation' => ['label' => 'No', 'count' => 0],
'filter_value' => 'No',
'products_count' => 2,
],
'selected_no_option_in_one_product' => [
'products_data' => ['simple1000' => 'Yes', 'simple1001' => 'No'],
'expectation' => ['label' => 'No', 'count' => 0],
'filter_value' => 'No',
'products_count' => 1,
],
];
}
/**
* @inheritdoc
*/
protected function getLayerType(): string
{
return Resolver::CATALOG_LAYER_CATEGORY;
}
/**
* @inheritdoc
*/
protected function getAttributeCode(): string
{
return 'boolean_attribute';
}
}
@@ -0,0 +1,109 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\LayeredNavigation\Block\Navigation\Category\Bundle;
use Magento\Catalog\Model\Layer\Resolver;
use Magento\Framework\Module\Manager;
use Magento\LayeredNavigation\Block\Navigation\AbstractFiltersTest;
use Magento\Catalog\Model\Layer\Filter\AbstractFilter;
/**
* Provides tests for custom multiselect filter in navigation block on category page with bundle products.
*
* @magentoAppArea frontend
* @magentoAppIsolation enabled
* @magentoDbIsolation disabled
*/
class MultiselectFilterTest extends AbstractFiltersTest
{
/**
* @var Manager
*/
private $moduleManager;
/**
* @inheritdoc
*/
protected function setUp(): void
{
parent::setUp();
$this->moduleManager = $this->objectManager->get(Manager::class);
//This check is needed because LayeredNavigation independent of Magento_Bundle
if (!$this->moduleManager->isEnabled('Magento_Bundle')) {
$this->markTestSkipped('Magento_Bundle module disabled.');
}
}
/**
* @magentoDataFixture Magento/Catalog/_files/multiselect_attribute.php
* @magentoDataFixture Magento/Bundle/_files/dynamic_and_fixed_bundle_products_in_category.php
* @dataProvider getFiltersWithCustomAttributeDataProvider
* @param array $products
* @param array $attributeData
* @param array $expectation
* @return void
*/
public function testGetFiltersWithCustomAttribute(array $products, array $attributeData, array $expectation): void
{
$this->getCategoryFiltersAndAssert($products, $attributeData, $expectation, 'Category 1');
}
/**
* @return array
*/
public function getFiltersWithCustomAttributeDataProvider(): array
{
return [
'not_used_in_navigation' => [
'products_data' => [],
'attribute_data' => ['is_filterable' => 0],
'expectation' => [],
],
'used_in_navigation_with_results' => [
'products_data' => [
'bundle-product' => 'Option 1',
'bundle-product-dropdown-options' => 'Option 2',
],
'attribute_data' => ['is_filterable' => AbstractFilter::ATTRIBUTE_OPTIONS_ONLY_WITH_RESULTS],
'expectation' => [
['label' => 'Option 1', 'count' => 1],
['label' => 'Option 2', 'count' => 1],
],
],
'used_in_navigation_without_results' => [
'products_data' => [
'bundle-product' => 'Option 1',
'bundle-product-dropdown-options' => 'Option 2',
],
'attribute_data' => ['is_filterable' => 2],
'expectation' => [
['label' => 'Option 1', 'count' => 1],
['label' => 'Option 2', 'count' => 1],
['label' => 'Option 3', 'count' => 0],
['label' => 'Option 4 "!@#$%^&*', 'count' => 0],
],
],
];
}
/**
* @inheritdoc
*/
protected function getLayerType(): string
{
return Resolver::CATALOG_LAYER_CATEGORY;
}
/**
* @inheritdoc
*/
protected function getAttributeCode(): string
{
return 'multiselect_attribute';
}
}
@@ -0,0 +1,96 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\LayeredNavigation\Block\Navigation\Category\Bundle;
use Magento\Catalog\Model\Layer\Resolver;
use Magento\Framework\Module\Manager;
use Magento\LayeredNavigation\Block\Navigation\AbstractFiltersTest;
use Magento\Catalog\Model\Layer\Filter\AbstractFilter;
use Magento\Catalog\Model\Layer\Filter\Item;
/**
* Provides price filter tests for bundle products in navigation block on category page.
*
* @magentoAppArea frontend
* @magentoAppIsolation enabled
* @magentoDbIsolation disabled
*/
class PriceFilterTest extends AbstractFiltersTest
{
/**
* @var Manager
*/
private $moduleManager;
/**
* @inheritdoc
*/
protected function setUp(): void
{
parent::setUp();
$this->moduleManager = $this->objectManager->get(Manager::class);
//This check is needed because LayeredNavigation independent of Magento_Bundle
if (!$this->moduleManager->isEnabled('Magento_Bundle')) {
$this->markTestSkipped('Magento_Bundle module disabled.');
}
}
/**
* @magentoDataFixture Magento/Bundle/_files/dynamic_and_fixed_bundle_products_in_category.php
* @magentoConfigFixture current_store catalog/layered_navigation/price_range_calculation manual
* @magentoConfigFixture current_store catalog/layered_navigation/price_range_step 10
* @return void
*/
public function testGetFilters(): void
{
$this->getCategoryFiltersAndAssert(
['bundle-product' => 20.00],
['is_filterable' => '1'],
[
['label' => '$10.00 - $19.99', 'value' => '10-20', 'count' => 1],
['label' => '$20.00 and above', 'value' => '20-30', 'count' => 1],
],
'Category 1'
);
}
/**
* @inheritdoc
*/
protected function getLayerType(): string
{
return Resolver::CATALOG_LAYER_CATEGORY;
}
/**
* @inheritdoc
*/
protected function getAttributeCode(): string
{
return 'price';
}
/**
* @inheritdoc
*/
protected function prepareFilterItems(AbstractFilter $filter): array
{
$items = [];
/** @var Item $item */
foreach ($filter->getItems() as $item) {
$item = [
'label' => strip_tags(__($item->getData('label'))->render()),
'value' => $item->getData('value'),
'count' => $item->getData('count'),
];
$items[] = $item;
}
return $items;
}
}
@@ -0,0 +1,108 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\LayeredNavigation\Block\Navigation\Category\Bundle;
use Magento\Catalog\Model\Layer\Resolver;
use Magento\Framework\Module\Manager;
use Magento\LayeredNavigation\Block\Navigation\AbstractFiltersTest;
use Magento\Catalog\Model\Layer\Filter\AbstractFilter;
/**
* Provides tests for custom select filter for bundle products in navigation block on category page.
*
* @magentoAppArea frontend
* @magentoAppIsolation enabled
* @magentoDbIsolation disabled
*/
class SelectFilterTest extends AbstractFiltersTest
{
/**
* @var Manager
*/
private $moduleManager;
/**
* @inheritdoc
*/
protected function setUp(): void
{
parent::setUp();
$this->moduleManager = $this->objectManager->get(Manager::class);
//This check is needed because LayeredNavigation independent of Magento_Bundle
if (!$this->moduleManager->isEnabled('Magento_Bundle')) {
$this->markTestSkipped('Magento_Bundle module disabled.');
}
}
/**
* @magentoDataFixture Magento/Catalog/_files/product_dropdown_attribute.php
* @magentoDataFixture Magento/Bundle/_files/dynamic_and_fixed_bundle_products_in_category.php
* @dataProvider getFiltersWithCustomAttributeDataProvider
* @param array $products
* @param array $attributeData
* @param array $expectation
* @return void
*/
public function testGetFiltersWithCustomAttribute(array $products, array $attributeData, array $expectation): void
{
$this->getCategoryFiltersAndAssert($products, $attributeData, $expectation, 'Category 1');
}
/**
* @return array
*/
public function getFiltersWithCustomAttributeDataProvider(): array
{
return [
'not_used_in_navigation' => [
'products_data' => [],
'attribute_data' => ['is_filterable' => 0],
'expectation' => [],
],
'used_in_navigation_with_results' => [
'products_data' => [
'bundle-product' => 'Option 1',
'bundle-product-dropdown-options' => 'Option 2',
],
'attribute_data' => ['is_filterable' => AbstractFilter::ATTRIBUTE_OPTIONS_ONLY_WITH_RESULTS],
'expectation' => [
['label' => 'Option 1', 'count' => 1],
['label' => 'Option 2', 'count' => 1],
],
],
'used_in_navigation_without_results' => [
'products_data' => [
'bundle-product' => 'Option 1',
'bundle-product-dropdown-options' => 'Option 2',
],
'attribute_data' => ['is_filterable' => 2],
'expectation' => [
['label' => 'Option 1', 'count' => 1],
['label' => 'Option 2', 'count' => 1],
['label' => 'Option 3', 'count' => 0],
],
],
];
}
/**
* @inheritdoc
*/
protected function getLayerType(): string
{
return Resolver::CATALOG_LAYER_CATEGORY;
}
/**
* @inheritdoc
*/
protected function getAttributeCode(): string
{
return 'dropdown_attribute';
}
}
@@ -0,0 +1,157 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\LayeredNavigation\Block\Navigation\Category\Configurable;
use Magento\Catalog\Model\Layer\Resolver;
use Magento\Catalog\Model\Product\Attribute\Source\Status;
use Magento\Framework\Module\Manager;
use Magento\LayeredNavigation\Block\Navigation\AbstractFiltersTest;
use Magento\Catalog\Model\Layer\Filter\AbstractFilter;
use Magento\Catalog\Model\Layer\Filter\Item;
use Magento\Store\Model\Store;
/**
* Provides price filter tests for configurable in navigation block on category page.
*
* @magentoAppArea frontend
* @magentoAppIsolation enabled
* @magentoDbIsolation disabled
*/
class PriceFilterTest extends AbstractFiltersTest
{
/**
* @var Manager
*/
private $moduleManager;
/**
* @inheritdoc
*/
protected function setUp(): void
{
parent::setUp();
$this->moduleManager = $this->objectManager->get(Manager::class);
//This check is needed because LayeredNavigation independent of Magento_ConfigurableProduct
if (!$this->moduleManager->isEnabled('Magento_ConfigurableProduct')) {
$this->markTestSkipped('Magento_ConfigurableProduct module disabled.');
}
}
/**
* @magentoDataFixture Magento/ConfigurableProduct/_files/configurable_product_with_category.php
* @magentoDataFixture Magento/Catalog/_files/category_product.php
* @magentoConfigFixture current_store catalog/layered_navigation/price_range_calculation manual
* @magentoConfigFixture current_store catalog/layered_navigation/price_range_step 10
* @dataProvider getFiltersDataProvider
* @param array $products
* @param array $expectation
* @return void
*/
public function testGetFilters(array $products, array $expectation): void
{
$this->updateProductData($products);
$this->getCategoryFiltersAndAssert([], ['is_filterable' => '1'], $expectation, 'Category 1');
}
/**
* @return array
*/
public function getFiltersDataProvider(): array
{
return [
'all_children_active' => [
'products_data' => [
'simple333' => ['price' => 60.00],
],
'expectation' => [
[
'label' => '<span class="price">$10.00</span> - <span class="price">$19.99</span>',
'value' => '10-20',
'count' => 1,
],
[
'label' => '<span class="price">$60.00</span> and above',
'value' => '60-70',
'count' => 1,
],
],
],
'one_child_disabled' => [
'products_data' => [
'simple333' => ['price' => 50.00],
'simple_10' => ['status' => Status::STATUS_DISABLED],
],
'expectation' => [
[
'label' => '<span class="price">$20.00</span> - <span class="price">$29.99</span>',
'value' => '20-30',
'count' => 1,
],
[
'label' => '<span class="price">$50.00</span> and above',
'value' => '50-60',
'count' => 1,
],
],
],
];
}
/**
* @inheritdoc
*/
protected function getLayerType(): string
{
return Resolver::CATALOG_LAYER_CATEGORY;
}
/**
* @inheritdoc
*/
protected function getAttributeCode(): string
{
return 'price';
}
/**
* @inheritdoc
*/
protected function prepareFilterItems(AbstractFilter $filter): array
{
$items = [];
/** @var Item $item */
foreach ($filter->getItems() as $item) {
$item = [
'label' => __($item->getData('label'))->render(),
'value' => $item->getData('value'),
'count' => $item->getData('count'),
];
$items[] = $item;
}
return $items;
}
/**
* Updates products data.
*
* @param array $products
* @param int $storeId
* @return void
*/
private function updateProductData(
array $products,
int $storeId = Store::DEFAULT_STORE_ID
): void {
foreach ($products as $productSku => $data) {
$product = $this->productRepository->get($productSku, false, $storeId, true);
$product->addData($data);
$this->productRepository->save($product);
}
}
}
@@ -0,0 +1,109 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\LayeredNavigation\Block\Navigation\Category;
use Magento\Catalog\Model\Layer\Resolver;
use Magento\LayeredNavigation\Block\Navigation\AbstractFiltersTest;
use Magento\Catalog\Model\Layer\Filter\AbstractFilter;
use Magento\Catalog\Model\Layer\Filter\Item;
use Magento\Store\Model\Store;
/**
* Provides tests for custom price filter in navigation block on category page.
*
* @magentoAppArea frontend
* @magentoAppIsolation enabled
* @magentoDbIsolation disabled
*/
class DecimalFilterTest extends AbstractFiltersTest
{
/**
* @magentoDataFixture Magento/Catalog/_files/product_decimal_attribute.php
* @magentoDataFixture Magento/Catalog/_files/category_with_different_price_products.php
* @dataProvider getFiltersWithCustomAttributeDataProvider
* @param array $products
* @param array $attributeData
* @param array $expectation
* @return void
*/
public function testGetFiltersWithCustomAttribute(array $products, array $attributeData, array $expectation): void
{
$this->getCategoryFiltersAndAssert($products, $attributeData, $expectation, 'Category 999');
}
/**
* @inheritdoc
*/
protected function getLayerType(): string
{
return Resolver::CATALOG_LAYER_CATEGORY;
}
/**
* @inheritdoc
*/
protected function getAttributeCode(): string
{
return 'decimal_attribute';
}
/**
* @inheritdoc
*/
protected function prepareFilterItems(AbstractFilter $filter): array
{
$items = [];
/** @var Item $item */
foreach ($filter->getItems() as $item) {
$item = [
'label' => __($item->getData('label'))->render(),
'value' => $item->getData('value'),
'count' => $item->getData('count'),
];
$items[] = $item;
}
return $items;
}
/**
* @return array
*/
public function getFiltersWithCustomAttributeDataProvider(): array
{
return [
'not_used_in_navigation' => [
'products_data' => [],
'attribute_data' => ['is_filterable' => 0],
'expectation' => [],
],
/* @TODO: Should be uncommented in MC-16650 */
/*'used_in_navigation_with_results' => [
'products_data' => [
'simple1000' => 10.00,
'simple1001' => 20.00,
],
'attribute_data' => ['is_filterable' => AbstractFilter::ATTRIBUTE_OPTIONS_ONLY_WITH_RESULTS],
'expectation' => [
[
'label' => '<span class="price">$10.00</span> - <span class="price">$19.99</span>',
'value' => '10-20',
'count' => 1,
],
[
'label' => '<span class="price">$20.00</span> and above',
'value' => '20-',
'count' => 1,
],
],
],*/
];
}
}
@@ -0,0 +1,161 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\LayeredNavigation\Block\Navigation\Category;
use Magento\Catalog\Model\Layer\Filter\AbstractFilter;
use Magento\Catalog\Model\Layer\Resolver;
use Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface;
use Magento\LayeredNavigation\Block\Navigation\AbstractFiltersTest;
use Magento\Store\Model\StoreManagerInterface;
/**
* Provides tests for custom filter with different scopes in navigation block on category page.
*
* @magentoAppArea frontend
* @magentoAppIsolation enabled
* @magentoDbIsolation disabled
*/
class FilterScopeTest extends AbstractFiltersTest
{
/**
* @var StoreManagerInterface
*/
private $storeManager;
/**
* @var int
*/
private $oldStoreId;
/**
* @var int
*/
private $currentStoreId;
/**
* @inheritdoc
*/
protected function setUp(): void
{
parent::setUp();
$this->storeManager = $this->objectManager->get(StoreManagerInterface::class);
$this->oldStoreId = (int)$this->storeManager->getStore()->getId();
$this->currentStoreId = (int)$this->storeManager->getStore('fixture_second_store')->getId();
}
/**
* @magentoDataFixture Magento/Catalog/_files/product_dropdown_attribute.php
* @magentoDataFixture Magento/Catalog/_files/category_with_different_price_products_on_two_websites.php
* @dataProvider filtersWithScopeDataProvider
* @param int $scope
* @param array $products
* @param array $expectation
* @return void
*/
public function testGetFilters(int $scope, array $products, array $expectation): void
{
$this->updateAttribute(
[
'is_filterable' => AbstractFilter::ATTRIBUTE_OPTIONS_ONLY_WITH_RESULTS,
'is_global' => $scope,
],
$this->getAttributeCode()
);
$this->updateProductsOnStore($products);
$this->clearInstanceAndReindexSearch();
try {
$this->storeManager->setCurrentStore($this->currentStoreId);
$this->navigationBlock->getLayer()->setCurrentCategory(
$this->loadCategory('Category 999', $this->currentStoreId)
);
$this->navigationBlock->setLayout($this->layout);
$filter = $this->getFilterByCode($this->navigationBlock->getFilters(), $this->getAttributeCode());
$this->assertNotNull($filter);
$this->assertEquals($expectation, $this->prepareFilterItems($filter));
} finally {
$this->storeManager->setCurrentStore($this->oldStoreId);
}
}
/**
* @return array
*/
public function filtersWithScopeDataProvider(): array
{
return [
'with_scope_store' => [
'scope' => ScopedAttributeInterface::SCOPE_STORE,
'products' => [
'default' => ['simple1000' => 'Option 1', 'simple1001' => 'Option 2'],
'fixture_second_store' => ['simple1000' => 'Option 2', 'simple1001' => 'Option 3'],
],
'expectation' => [
['label' => 'Option 2', 'count' => 1],
['label' => 'Option 3', 'count' => 1],
],
],
'with_scope_website' => [
'scope' => ScopedAttributeInterface::SCOPE_WEBSITE,
'products' => [
'default' => ['simple1000' => 'Option 3', 'simple1001' => 'Option 2'],
'fixture_second_store' => ['simple1000' => 'Option 1', 'simple1001' => 'Option 2'],
],
'expectation' => [
['label' => 'Option 1', 'count' => 1],
['label' => 'Option 2', 'count' => 1],
],
],
'with_scope_global' => [
'scope' => ScopedAttributeInterface::SCOPE_GLOBAL,
'products' => [
'default' => ['simple1000' => 'Option 1'],
'fixture_second_store' => ['simple1001' => 'Option 2'],
],
'expectation' => [
['label' => 'Option 1', 'count' => 1],
['label' => 'Option 2', 'count' => 1],
],
],
];
}
/**
* @inheritdoc
*/
protected function getLayerType(): string
{
return Resolver::CATALOG_LAYER_CATEGORY;
}
/**
* @inheritdoc
*/
protected function getAttributeCode(): string
{
return 'dropdown_attribute';
}
/**
* Updates products data for store.
*
* @param array $productsData
* @return void
*/
private function updateProductsOnStore(array $productsData): void
{
try {
foreach ($productsData as $storeCode => $products) {
$storeId = (int)$this->storeManager->getStore($storeCode)->getId();
$this->storeManager->setCurrentStore($storeId);
$this->updateProducts($products, $this->getAttributeCode(), $storeId);
}
} finally {
$this->storeManager->setCurrentStore($this->oldStoreId);
}
}
}
@@ -0,0 +1,187 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\LayeredNavigation\Block\Navigation\Category;
use Magento\Catalog\Model\Layer\Resolver;
use Magento\Catalog\Model\ResourceModel\Product\Collection;
use Magento\LayeredNavigation\Block\Navigation\AbstractFiltersTest;
use Magento\Catalog\Model\Layer\Filter\AbstractFilter;
use Magento\Store\Model\Store;
/**
* Provides tests for multiple custom select filters in navigation block on category page.
*
* @magentoAppArea frontend
* @magentoAppIsolation enabled
* @magentoDbIsolation disabled
*/
class MultipleFiltersTest extends AbstractFiltersTest
{
/**
* @magentoDataFixture Magento/Catalog/_files/product_dropdown_attribute.php
* @magentoDataFixture Magento/Catalog/_files/configurable_attribute.php
* @magentoDataFixture Magento/Catalog/_files/category_with_three_products.php
* @dataProvider getMultipleActiveFiltersDataProvider
* @param array $products
* @param array $filters
* @param array $expectedProducts
* @return void
*/
public function testGetMultipleActiveFilters(
array $products,
array $filters,
array $expectedProducts
): void {
$this->updateAttributesAndProducts(
$products,
['is_filterable' => AbstractFilter::ATTRIBUTE_OPTIONS_ONLY_WITH_RESULTS]
);
$this->clearInstanceAndReindexSearch();
$this->navigationBlock->getRequest()->setParams($this->getMultipleRequestParams($filters));
$this->navigationBlock->getLayer()->setCurrentCategory(
$this->loadCategory('Category 999', Store::DEFAULT_STORE_ID)
);
$this->navigationBlock->setLayout($this->layout);
$resultProducts = $this->getProductSkus($this->navigationBlock->getLayer()->getProductCollection());
self::assertEqualsCanonicalizing($expectedProducts, $resultProducts);
}
/**
* @return array
*/
public function getMultipleActiveFiltersDataProvider(): array
{
return [
'without_filters' => [
'products_data' => [
'test_configurable' => [
'simple1000' => 'Option 1',
'simple1001' => 'Option 2',
'simple1002' => 'Option 2',
],
'dropdown_attribute' => [
'simple1000' => 'Option 1',
'simple1001' => 'Option 2',
'simple1002' => 'Option 3',
],
],
'filters' => [],
'expected_products' => ['simple1000', 'simple1001', 'simple1002'],
],
'applied_first_option_in_both_filters' => [
'products_data' => [
'test_configurable' => [
'simple1000' => 'Option 1',
'simple1001' => 'Option 1',
'simple1002' => 'Option 2',
],
'dropdown_attribute' => [
'simple1000' => 'Option 1',
'simple1001' => 'Option 1',
'simple1002' => 'Option 3',
],
],
'filters' => ['test_configurable' => 'Option 1', 'dropdown_attribute' => 'Option 1'],
'expected_products' => ['simple1000', 'simple1001'],
],
'applied_mixed_options_in_filters' => [
'products_data' => [
'test_configurable' => [
'simple1000' => 'Option 1',
'simple1001' => 'Option 2',
'simple1002' => 'Option 2',
],
'dropdown_attribute' => [
'simple1000' => 'Option 1',
'simple1001' => 'Option 2',
'simple1002' => 'Option 3',
],
],
'filters' => ['test_configurable' => 'Option 2', 'dropdown_attribute' => 'Option 3'],
'expected_products' => ['simple1002'],
],
];
}
/**
* @inheritdoc
*/
protected function getLayerType(): string
{
return Resolver::CATALOG_LAYER_CATEGORY;
}
/**
* Updates products and product attribute.
*
* @param array $productsData
* @param array $attributesData
* @return void
*/
protected function updateAttributesAndProducts(array $productsData, array $attributesData): void
{
$products = [];
foreach ($productsData as $attributeCode => $data) {
$this->updateAttribute($attributesData, $attributeCode);
$attribute = $this->attributeRepository->get($attributeCode);
foreach ($data as $productSku => $stringValue) {
if (empty($products[$productSku])) {
$product = $this->productRepository->get($productSku, false, Store::DEFAULT_STORE_ID, true);
$products[$productSku] = $product;
} else {
$product = $products[$productSku];
}
$productValue = $attribute->usesSource()
? $attribute->getSource()->getOptionId($stringValue)
: $stringValue;
$product->addData([$attribute->getAttributeCode() => $productValue]);
}
}
foreach ($products as $product) {
$this->productRepository->save($product);
}
}
/**
* Returns array with multiple filters.
*
* @param array $filters
* @return array
*/
protected function getMultipleRequestParams(array $filters): array
{
$params = [];
foreach ($filters as $attributeCode => $filterValue) {
$attribute = $this->attributeRepository->get($attributeCode);
$filterValue = $attribute->usesSource()
? $attribute->getSource()->getOptionId($filterValue)
: $filterValue;
$params[$attributeCode] = $filterValue;
}
return $params;
}
/**
* Returns list of product skus from given collection.
*
* @param Collection $getProductCollection
* @return array
*/
protected function getProductSkus(Collection $getProductCollection): array
{
$skus = [];
foreach ($getProductCollection as $product) {
$skus[] = $product->getSku();
}
return $skus;
}
}
@@ -0,0 +1,165 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\LayeredNavigation\Block\Navigation\Category;
use Magento\Catalog\Model\Layer\Filter\AbstractFilter;
use Magento\Catalog\Model\Layer\Resolver;
use Magento\LayeredNavigation\Block\Navigation\AbstractFiltersTest;
use Magento\Store\Model\Store;
/**
* Provides tests for custom multiselect filter in navigation block on category page.
*
* @magentoAppArea frontend
* @magentoAppIsolation enabled
* @magentoDbIsolation disabled
*/
class MultiselectFilterTest extends AbstractFiltersTest
{
/**
* @magentoDataFixture Magento/Catalog/_files/multiselect_attribute.php
* @magentoDataFixture Magento/Catalog/_files/category_with_different_price_products.php
* @dataProvider getFiltersWithCustomAttributeDataProvider
* @param array $products
* @param array $attributeData
* @param array $expectation
* @return void
*/
public function testGetFiltersWithCustomAttribute(array $products, array $attributeData, array $expectation): void
{
$this->getCategoryFiltersAndAssert($products, $attributeData, $expectation, 'Category 999');
}
/**
* @return array
*/
public function getFiltersWithCustomAttributeDataProvider(): array
{
return [
'not_used_in_navigation' => [
'products_data' => [],
'attribute_data' => ['is_filterable' => 0],
'expectation' => [],
],
'used_in_navigation_with_results' => [
'products_data' => [
'simple1000' => 'Option 1',
'simple1001' => 'Option 2',
],
'attribute_data' => ['is_filterable' => AbstractFilter::ATTRIBUTE_OPTIONS_ONLY_WITH_RESULTS],
'expectation' => [
['label' => 'Option 1', 'count' => 1],
['label' => 'Option 2', 'count' => 1],
],
],
'used_in_navigation_without_results' => [
'products_data' => [
'simple1000' => 'Option 1',
'simple1001' => 'Option 2',
],
'attribute_data' => ['is_filterable' => 2],
'expectation' => [
['label' => 'Option 1', 'count' => 1],
['label' => 'Option 2', 'count' => 1],
['label' => 'Option 3', 'count' => 0],
['label' => 'Option 4 "!@#$%^&*', 'count' => 0],
],
],
];
}
/**
* @magentoDataFixture Magento/Catalog/_files/multiselect_attribute.php
* @magentoDataFixture Magento/Catalog/_files/category_with_different_price_products.php
* @dataProvider getActiveFiltersWithCustomAttributeDataProvider
* @param array $products
* @param array $expectation
* @param string $filterValue
* @param int $productsCount
* @return void
*/
public function testGetActiveFiltersWithCustomAttribute(
array $products,
array $expectation,
string $filterValue,
int $productsCount
): void {
$this->getCategoryActiveFiltersAndAssert($products, $expectation, 'Category 999', $filterValue, $productsCount);
}
/**
* @return array
*/
public function getActiveFiltersWithCustomAttributeDataProvider(): array
{
return [
'filter_by_first_option_in_products_with_first_option' => [
'products_data' => ['simple1000' => 'Option 1', 'simple1001' => 'Option 1'],
'expectation' => ['label' => 'Option 1', 'count' => 0],
'filter_value' => 'Option 1',
'products_count' => 2,
],
'filter_by_first_option_in_products_with_different_options' => [
'products_data' => ['simple1000' => 'Option 1', 'simple1001' => 'Option 2'],
'expectation' => ['label' => 'Option 1', 'count' => 0],
'filter_value' => 'Option 1',
'products_count' => 1,
],
'filter_by_second_option_in_products_with_two_options' => [
'products_data' => ['simple1000' => 'Option 1,Option 2', 'simple1001' => 'Option 1,Option 2'],
'expectation' => ['label' => 'Option 2', 'count' => 0],
'filter_value' => 'Option 2',
'products_count' => 2,
],
'filter_by_second_option_in_products_with_hybrid_options' => [
'products_data' => ['simple1000' => 'Option 1,Option 2', 'simple1001' => 'Option 2'],
'expectation' => ['label' => 'Option 2', 'count' => 0],
'filter_value' => 'Option 2',
'products_count' => 2,
],
];
}
/**
* @inheritdoc
*/
protected function getLayerType(): string
{
return Resolver::CATALOG_LAYER_CATEGORY;
}
/**
* @inheritdoc
*/
protected function getAttributeCode(): string
{
return 'multiselect_attribute';
}
/**
* @inheritdoc
*/
protected function updateProducts(
array $products,
string $attributeCode,
int $storeId = Store::DEFAULT_STORE_ID
): void {
$attribute = $this->attributeRepository->get($attributeCode);
foreach ($products as $productSku => $stringValue) {
$product = $this->productRepository->get($productSku, false, $storeId, true);
$values = explode(',', $stringValue);
$productValue = [];
foreach ($values as $value) {
$productValue[] = $attribute->usesSource() ? $attribute->getSource()->getOptionId($value) : $value;
}
$product->addData([$attribute->getAttributeCode() => implode(',', $productValue)]);
$this->productRepository->save($product);
}
}
}
@@ -0,0 +1,110 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\LayeredNavigation\Block\Navigation\Category;
use Magento\Catalog\Model\Layer\Resolver;
use Magento\CatalogInventory\Model\Configuration;
use Magento\Framework\App\Config\MutableScopeConfigInterface;
use Magento\Framework\App\ScopeInterface;
use Magento\LayeredNavigation\Block\Navigation\AbstractFiltersTest;
use Magento\Catalog\Model\Layer\Filter\AbstractFilter;
use Magento\Store\Model\ScopeInterface as StoreScope;
/**
* Provides tests for select filter in navigation block on category page with out of stock products
* and enabled out of stock products displaying.
*
* @magentoAppArea frontend
* @magentoAppIsolation enabled
* @magentoDbIsolation disabled
*/
class OutOfStockProductsFilterTest extends AbstractFiltersTest
{
/**
* @var MutableScopeConfigInterface
*/
private $scopeConfig;
/**
* @inheritdoc
*/
protected function setUp(): void
{
parent::setUp();
$this->scopeConfig = $this->objectManager->get(MutableScopeConfigInterface::class);
}
/**
* @magentoDataFixture Magento/Catalog/_files/product_dropdown_attribute.php
* @magentoDataFixture Magento/Catalog/_files/out_of_stock_product_with_category.php
* @magentoDataFixture Magento/Catalog/_files/product_with_category.php
* @dataProvider getFiltersWithOutOfStockProduct
* @param int $showOutOfStock
* @param array $expectation
* @return void
*/
public function testGetFiltersWithOutOfStockProduct(int $showOutOfStock, array $expectation): void
{
$this->updateConfigShowOutOfStockFlag($showOutOfStock);
$this->getCategoryFiltersAndAssert(
['out-of-stock-product' => 'Option 1', 'in-stock-product' => 'Option 2'],
['is_filterable' => AbstractFilter::ATTRIBUTE_OPTIONS_ONLY_WITH_RESULTS],
$expectation,
'Category 1'
);
}
/**
* @return array
*/
public function getFiltersWithOutOfStockProduct(): array
{
return [
'show_out_of_stock' => [
'show_out_of_stock' => 1,
'expectation' => [['label' => 'Option 1', 'count' => 1], ['label' => 'Option 2', 'count' => 1]],
],
'not_show_out_of_stock' => [
'show_out_of_stock' => 0,
'expectation' => [['label' => 'Option 2', 'count' => 1]],
],
];
}
/**
* @inheritdoc
*/
protected function getLayerType(): string
{
return Resolver::CATALOG_LAYER_CATEGORY;
}
/**
* @inheritdoc
*/
protected function getAttributeCode(): string
{
return 'dropdown_attribute';
}
/**
* Updates store config 'cataloginventory/options/show_out_of_stock' flag.
*
* @param int $showOutOfStock
* @return void
*/
protected function updateConfigShowOutOfStockFlag(int $showOutOfStock): void
{
$this->scopeConfig->setValue(
Configuration::XML_PATH_SHOW_OUT_OF_STOCK,
$showOutOfStock,
StoreScope::SCOPE_STORE,
ScopeInterface::SCOPE_DEFAULT
);
}
}
@@ -0,0 +1,274 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\LayeredNavigation\Block\Navigation\Category;
use Magento\Catalog\Model\Layer\Resolver;
use Magento\Framework\App\Config\MutableScopeConfigInterface;
use Magento\Framework\App\ScopeInterface;
use Magento\LayeredNavigation\Block\Navigation\AbstractFiltersTest;
use Magento\Catalog\Model\Layer\Filter\AbstractFilter;
use Magento\Catalog\Model\Layer\Filter\Item;
use Magento\Store\Model\ScopeInterface as StoreScope;
/**
* Provides price filter tests with different price ranges calculation in navigation block on category page.
*
* @magentoAppArea frontend
* @magentoAppIsolation enabled
* @magentoDbIsolation disabled
*/
class PriceFilterTest extends AbstractFiltersTest
{
/**
* @var MutableScopeConfigInterface
*/
private $scopeConfig;
/**
* @inheritdoc
*/
protected function setUp(): void
{
parent::setUp();
$this->scopeConfig = $this->objectManager->get(MutableScopeConfigInterface::class);
}
/**
* @magentoDataFixture Magento/Catalog/_files/category_with_three_products.php
* @dataProvider getFiltersDataProvider
* @param array $config
* @param array $products
* @param array $expectation
* @return void
*/
public function testGetFilters(array $config, array $products, array $expectation): void
{
$this->applyCatalogConfig($config);
$this->getCategoryFiltersAndAssert(
$products,
['is_filterable' => AbstractFilter::ATTRIBUTE_OPTIONS_ONLY_WITH_RESULTS],
$expectation,
'Category 999'
);
}
/**
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
* @return array
*/
public function getFiltersDataProvider(): array
{
return [
'auto_calculation_variation_with_small_price_difference' => [
'config' => ['catalog/layered_navigation/price_range_calculation' => 'auto'],
'products_data' => ['simple1000' => 10.00, 'simple1001' => 20.00, 'simple1002' => 50.00],
'expectation' => [
['label' => '$10.00 - $19.99', 'value' => '10-20', 'count' => 1],
['label' => '$20.00 - $29.99', 'value' => '20-30', 'count' => 1],
['label' => '$50.00 and above', 'value' => '50-60', 'count' => 1],
],
],
'auto_calculation_variation_with_big_price_difference' => [
'config' => ['catalog/layered_navigation/price_range_calculation' => 'auto'],
'products_data' => ['simple1000' => 10.00, 'simple1001' => 20.00, 'simple1002' => 300.00],
'expectation' => [
['label' => '$0.00 - $99.99', 'value' => '0-100', 'count' => 2],
['label' => '$300.00 and above', 'value' => '300-400', 'count' => 1],
],
],
'auto_calculation_variation_with_fixed_price_step' => [
'config' => ['catalog/layered_navigation/price_range_calculation' => 'auto'],
'products_data' => ['simple1000' => 300.00, 'simple1001' => 400.00, 'simple1002' => 500.00],
'expectation' => [
['label' => '$300.00 - $399.99', 'value' => '300-400', 'count' => 1],
['label' => '$400.00 - $499.99', 'value' => '400-500', 'count' => 1],
['label' => '$500.00 and above', 'value' => '500-600', 'count' => 1],
],
],
'improved_calculation_variation_with_small_price_difference' => [
'config' => [
'catalog/layered_navigation/price_range_calculation' => 'improved',
'catalog/layered_navigation/interval_division_limit' => 3,
],
'products_data' => ['simple1000' => 10.00, 'simple1001' => 20.00, 'simple1002' => 50.00],
'expectation' => [
['label' => '$0.00 - $19.99', 'value' => '0-20', 'count' => 1],
['label' => '$20.00 and above', 'value' => '20-50.01', 'count' => 2],
],
],
'improved_calculation_variation_with_big_price_difference' => [
'config' => [
'catalog/layered_navigation/price_range_calculation' => 'improved',
'catalog/layered_navigation/interval_division_limit' => 3,
],
'products_data' => ['simple1000' => 10.00, 'simple1001' => 20.00, 'simple1002' => 300.00],
'expectation' => [
['label' => '$0.00 - $19.99', 'value' => '0-20', 'count' => 1],
['label' => '$20.00 and above', 'value' => '20-300.01', 'count' => 2],
],
],
'manual_calculation_with_price_step_200' => [
'config' => [
'catalog/layered_navigation/price_range_calculation' => 'manual',
'catalog/layered_navigation/price_range_step' => 200,
],
'products_data' => ['simple1000' => 300.00, 'simple1001' => 300.00, 'simple1002' => 500.00],
'expectation' => [
['label' => '$200.00 - $399.99', 'value' => '200-400', 'count' => 2],
['label' => '$400.00 and above', 'value' => '400-600', 'count' => 1],
],
],
'manual_calculation_with_price_step_10' => [
'config' => [
'catalog/layered_navigation/price_range_calculation' => 'manual',
'catalog/layered_navigation/price_range_step' => 10,
],
'products_data' => ['simple1000' => 300.00, 'simple1001' => 300.00, 'simple1002' => 500.00],
'expectation' => [
['label' => '$300.00 - $309.99', 'value' => '300-310', 'count' => 2],
['label' => '$500.00 and above', 'value' => '500-510', 'count' => 1],
],
],
'manual_calculation_with_number_of_intervals_10' => [
'config' => [
'catalog/layered_navigation/price_range_calculation' => 'manual',
'catalog/layered_navigation/price_range_step' => 10,
'catalog/layered_navigation/price_range_max_intervals' => 10,
],
'products_data' => ['simple1000' => 10.00, 'simple1001' => 20.00, 'simple1002' => 30.00],
'expectation' => [
['label' => '$10.00 - $19.99', 'value' => '10-20', 'count' => 1],
['label' => '$20.00 - $29.99', 'value' => '20-30', 'count' => 1],
['label' => '$30.00 and above', 'value' => '30-40', 'count' => 1],
],
],
'manual_calculation_with_number_of_intervals_2' => [
'config' => [
'catalog/layered_navigation/price_range_calculation' => 'manual',
'catalog/layered_navigation/price_range_step' => 10,
'catalog/layered_navigation/price_range_max_intervals' => 2,
],
'products_data' => ['simple1000' => 10.00, 'simple1001' => 20.00, 'simple1002' => 30.00],
'expectation' => [
['label' => '$10.00 - $19.99', 'value' => '10-20', 'count' => 1],
['label' => '$20.00 and above', 'value' => '20-30.01', 'count' => 2],
],
],
];
}
/**
* @magentoDataFixture Magento/Catalog/_files/category_with_three_products.php
* @dataProvider getActiveFiltersDataProvider
* @param array $config
* @param array $products
* @param array $expectation
* @param string $filterValue
* @return void
*/
public function testGetActiveFilters(array $config, array $products, array $expectation, string $filterValue): void
{
$this->applyCatalogConfig($config);
$this->getCategoryActiveFiltersAndAssert($products, $expectation, 'Category 999', $filterValue, 1);
}
/**
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
* @return array
*/
public function getActiveFiltersDataProvider(): array
{
return [
'auto_calculation' => [
'config' => ['catalog/layered_navigation/price_range_calculation' => 'auto'],
'products_data' => ['simple1000' => 10.00, 'simple1001' => 20.00, 'simple1002' => 50.00],
'expectation' => ['label' => '$10.00 - $19.99', 'count' => 0],
'filter_value' => '10-20',
],
'improved_calculation' => [
'config' => [
'catalog/layered_navigation/price_range_calculation' => 'improved',
'catalog/layered_navigation/interval_division_limit' => 3,
],
'products_data' => ['simple1000' => 10.00, 'simple1001' => 20.00, 'simple1002' => 50.00],
'expectation' => ['label' => '$0.00 - $19.99', 'count' => 0],
'filter_value' => '0-20',
],
'manual_calculation' => [
'config' => [
'catalog/layered_navigation/price_range_calculation' => 'manual',
'catalog/layered_navigation/price_range_step' => 10,
'catalog/layered_navigation/price_range_max_intervals' => 10,
],
'products_data' => ['simple1000' => 10.00, 'simple1001' => 20.00, 'simple1002' => 30.00],
'expectation' => ['label' => '$10.00 - $19.99', 'count' => 0],
'filter_value' => '10-20',
],
];
}
/**
* @inheritdoc
*/
protected function getLayerType(): string
{
return Resolver::CATALOG_LAYER_CATEGORY;
}
/**
* @inheritdoc
*/
protected function getAttributeCode(): string
{
return 'price';
}
/**
* @inheritdoc
*/
protected function prepareFilterItems(AbstractFilter $filter): array
{
$items = [];
/** @var Item $item */
foreach ($filter->getItems() as $item) {
$items[] = [
'label' => strip_tags(__($item->getData('label'))->render()),
'value' => $item->getData('value'),
'count' => $item->getData('count'),
];
}
return $items;
}
/**
* Updates price filter store configuration.
*
* @param array $config
* @return void
*/
protected function applyCatalogConfig(array $config): void
{
foreach ($config as $path => $value) {
$this->scopeConfig->setValue($path, $value, StoreScope::SCOPE_STORE, ScopeInterface::SCOPE_DEFAULT);
}
}
/**
* @inheritdoc
*/
protected function assertActiveFilter(array $expectation, Item $currentFilter): void
{
$this->assertEquals($expectation['label'], strip_tags((string)$currentFilter->getData('label')));
$this->assertEquals($expectation['count'], $currentFilter->getData('count'));
$this->assertEquals(
$this->getAttributeCode(),
$currentFilter->getFilter()->getData('attribute_model')->getAttributeCode()
);
}
}
@@ -0,0 +1,135 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\LayeredNavigation\Block\Navigation\Category;
use Magento\Catalog\Model\Layer\Resolver;
use Magento\LayeredNavigation\Block\Navigation\AbstractFiltersTest;
use Magento\Catalog\Model\Layer\Filter\AbstractFilter;
/**
* Provides tests for custom select filter in navigation block on category page.
*
* @magentoAppArea frontend
* @magentoAppIsolation enabled
* @magentoDbIsolation disabled
*/
class SelectFilterTest extends AbstractFiltersTest
{
/**
* @magentoDataFixture Magento/Catalog/_files/product_dropdown_attribute.php
* @magentoDataFixture Magento/Catalog/_files/category_with_different_price_products.php
* @dataProvider getFiltersWithCustomAttributeDataProvider
* @param array $products
* @param array $attributeData
* @param array $expectation
* @return void
*/
public function testGetFiltersWithCustomAttribute(array $products, array $attributeData, array $expectation): void
{
$this->getCategoryFiltersAndAssert($products, $attributeData, $expectation, 'Category 999');
}
/**
* @return array
*/
public function getFiltersWithCustomAttributeDataProvider(): array
{
return [
'not_used_in_navigation' => [
'products_data' => [],
'attribute_data' => ['is_filterable' => 0],
'expectation' => [],
],
'used_in_navigation_with_results' => [
'products_data' => [
'simple1000' => 'Option 1',
'simple1001' => 'Option 2',
],
'attribute_data' => ['is_filterable' => AbstractFilter::ATTRIBUTE_OPTIONS_ONLY_WITH_RESULTS],
'expectation' => [
['label' => 'Option 1', 'count' => 1],
['label' => 'Option 2', 'count' => 1],
],
],
'used_in_navigation_without_results' => [
'products_data' => [
'simple1000' => 'Option 1',
'simple1001' => 'Option 2',
],
'attribute_data' => ['is_filterable' => 2],
'expectation' => [
['label' => 'Option 1', 'count' => 1],
['label' => 'Option 2', 'count' => 1],
['label' => 'Option 3', 'count' => 0],
],
],
];
}
/**
* @magentoDataFixture Magento/Catalog/_files/product_dropdown_attribute.php
* @magentoDataFixture Magento/Catalog/_files/category_with_different_price_products.php
* @dataProvider getActiveFiltersWithCustomAttributeDataProvider
* @param array $products
* @param array $expectation
* @param string $filterValue
* @param int $productsCount
* @return void
*/
public function testGetActiveFiltersWithCustomAttribute(
array $products,
array $expectation,
string $filterValue,
int $productsCount
): void {
$this->getCategoryActiveFiltersAndAssert($products, $expectation, 'Category 999', $filterValue, $productsCount);
}
/**
* @return array
*/
public function getActiveFiltersWithCustomAttributeDataProvider(): array
{
return [
'filter_by_first_option_in_products_with_first_option' => [
'products_data' => ['simple1000' => 'Option 1', 'simple1001' => 'Option 1'],
'expectation' => ['label' => 'Option 1', 'count' => 0],
'filter_value' => 'Option 1',
'products_count' => 2,
],
'filter_by_first_option_in_products_with_different_options' => [
'products_data' => ['simple1000' => 'Option 1', 'simple1001' => 'Option 2'],
'expectation' => ['label' => 'Option 1', 'count' => 0],
'filter_value' => 'Option 1',
'products_count' => 1,
],
'filter_by_second_option_in_products_with_different_options' => [
'products_data' => ['simple1000' => 'Option 1', 'simple1001' => 'Option 2'],
'expectation' => ['label' => 'Option 2', 'count' => 0],
'filter_value' => 'Option 2',
'products_count' => 1,
],
];
}
/**
* @inheritdoc
*/
protected function getLayerType(): string
{
return Resolver::CATALOG_LAYER_CATEGORY;
}
/**
* @inheritdoc
*/
protected function getAttributeCode(): string
{
return 'dropdown_attribute';
}
}
@@ -0,0 +1,228 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\LayeredNavigation\Block\Navigation;
use Magento\Catalog\Api\Data\CategoryInterface;
use Magento\Catalog\Api\Data\CategoryInterfaceFactory;
use Magento\Catalog\Model\Category as CategoryModel;
use Magento\Catalog\Model\ResourceModel\Category as CategoryResource;
use Magento\Catalog\Model\ResourceModel\Category\Collection;
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory;
use Magento\Framework\ObjectManagerInterface;
use Magento\Framework\View\LayoutInterface;
use Magento\LayeredNavigation\Block\Navigation;
use Magento\Store\Model\Store;
use Magento\Store\Model\StoreManagerInterface;
use Magento\TestFramework\Helper\Bootstrap;
use PHPUnit\Framework\TestCase;
/**
* Provides tests for filters block on category page.
*
* @magentoAppArea frontend
* @magentoAppIsolation enabled
* @magentoDbIsolation disabled
*/
class CategoryTest extends TestCase
{
/**
* @var ObjectManagerInterface
*/
protected $objectManager;
/**
* @var CollectionFactory
*/
protected $categoryCollectionFactory;
/**
* @var CategoryResource
*/
protected $categoryResource;
/**
* @var Navigation
*/
protected $navigationBlock;
/**
* @var LayoutInterface
*/
protected $layout;
/**
* @var StoreManagerInterface
*/
private $storeManager;
/**
* @inheritdoc
*/
protected function setUp(): void
{
$this->objectManager = Bootstrap::getObjectManager();
$this->categoryCollectionFactory = $this->objectManager->create(CollectionFactory::class);
$this->categoryResource = $this->objectManager->get(CategoryResource::class);
$this->layout = $this->objectManager->get(LayoutInterface::class);
$this->navigationBlock = $this->objectManager->create(Category::class);
$this->storeManager = $this->objectManager->get(StoreManagerInterface::class);
parent::setUp();
}
/**
* @magentoDataFixture Magento/Catalog/_files/category.php
* @return void
*/
public function testCanShowBlockWithoutProducts(): void
{
$this->prepareNavigationBlock('Category 1');
$this->assertFalse($this->navigationBlock->canShowBlock());
$this->assertCount(0, $this->navigationBlock->getLayer()->getProductCollection()->getItems());
}
/**
* @magentoDataFixture Magento/Catalog/_files/category_product.php
* @return void
*/
public function testCanShowBlockWithoutFilterOptions(): void
{
$this->prepareNavigationBlock('Category 1');
$this->assertFalse($this->navigationBlock->canShowBlock());
$this->assertCount(1, $this->navigationBlock->getLayer()->getProductCollection()->getItems());
}
/**
* @magentoDataFixture Magento/Catalog/_files/category_with_different_price_products.php
* @dataProvider canShowBlockWithDisplayModeDataProvider
* @param string $displayMode
* @param bool $canShow
* @return void
*/
public function testCanShowBlockWithDisplayMode(string $displayMode, bool $canShow): void
{
$this->updateCategoryDisplayMode('Category 999', $displayMode);
$this->prepareNavigationBlock('Category 999');
$this->assertEquals($canShow, $this->navigationBlock->canShowBlock());
}
/**
* @return array
*/
public function canShowBlockWithDisplayModeDataProvider(): array
{
return [
'with_mode_products' => ['mode' => CategoryModel::DM_PRODUCT, 'can_show' => true],
'with_mode_cms_block' => ['mode' => CategoryModel::DM_PAGE, 'can_show' => false],
'with_mode_cms_block_and_products' => ['mode' => CategoryModel::DM_MIXED, 'can_show' => true],
];
}
/**
* @magentoDataFixture Magento/Store/_files/second_store.php
* @magentoDataFixture Magento/Catalog/_files/category_with_different_price_products.php
* @dataProvider canShowBlockWithDisplayModeDataProviderOnStoreView
* @param string $defaultMode
* @param string $storeMode
* @param bool $canShow
* @return void
*/
public function testCanShowBlockWithDisplayModeOnStoreView(
string $defaultMode,
string $storeMode,
bool $canShow
): void {
$secondStoreId = (int)$this->storeManager->getStore('fixture_second_store')->getId();
$this->updateCategoryDisplayMode('Category 999', $defaultMode);
$this->updateCategoryDisplayMode('Category 999', $storeMode, $secondStoreId);
$this->prepareNavigationBlock('Category 999', $secondStoreId);
$this->assertEquals($canShow, $this->navigationBlock->canShowBlock());
}
/**
* @return array
*/
public function canShowBlockWithDisplayModeDataProviderOnStoreView(): array
{
return [
'with_mode_products' => [
'default_mode' => CategoryModel::DM_PAGE,
'store_mode' => CategoryModel::DM_PRODUCT,
'can_show' => true,
],
'with_mode_cms_block' => [
'default_mode' => CategoryModel::DM_PRODUCT,
'store_mode' => CategoryModel::DM_PAGE,
'can_show' => false
],
'with_mode_cms_block_and_products' => [
'default_mode' => CategoryModel::DM_PAGE,
'store_mode' => CategoryModel::DM_MIXED,
'can_show' => true
],
];
}
/**
* Inits navigation block.
*
* @param string $categoryName
* @param int $storeId
* @return void
*/
private function prepareNavigationBlock(
string $categoryName,
int $storeId = Store::DEFAULT_STORE_ID
): void {
$category = $this->loadCategory($categoryName, $storeId);
$this->navigationBlock->getLayer()->setCurrentCategory($category);
$this->navigationBlock->setLayout($this->layout);
}
/**
* Loads category by id.
*
* @param string $categoryName
* @param int $storeId
* @return CategoryInterface
*/
private function loadCategory(string $categoryName, int $storeId): CategoryInterface
{
/** @var Collection $categoryCollection */
$categoryCollection = $this->categoryCollectionFactory->create();
/** @var CategoryInterface $category */
$category = $categoryCollection->setStoreId($storeId)
->addAttributeToSelect('display_mode', 'left')
->addAttributeToFilter(CategoryInterface::KEY_NAME, $categoryName)
->setPageSize(1)
->getFirstItem();
$category->setStoreId($storeId);
return $category;
}
/**
* Updates category display mode.
*
* @param string $categoryName
* @param string $displayMode
* @param int $storeId
* @return void
*/
private function updateCategoryDisplayMode(
string $categoryName,
string $displayMode,
int $storeId = Store::DEFAULT_STORE_ID
): void {
$category = $this->loadCategory($categoryName, $storeId);
$category->setData('display_mode', $displayMode);
if ($category->dataHasChangedFor('display_mode')) {
$this->categoryResource->save($category);
}
}
}
@@ -0,0 +1,100 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\LayeredNavigation\Block\Navigation\Search;
use Magento\Catalog\Model\Layer\Filter\AbstractFilter;
use Magento\Catalog\Model\Layer\Resolver;
use Magento\LayeredNavigation\Block\Navigation\Category\BooleanFilterTest as CategoryBooleanFilterTest;
/**
* Provides tests for custom boolean filter in navigation block on search page.
*
* @magentoAppArea frontend
* @magentoAppIsolation enabled
* @magentoDbIsolation disabled
*/
class BooleanFilterTest extends CategoryBooleanFilterTest
{
/**
* @magentoDataFixture Magento/Catalog/_files/product_boolean_attribute.php
* @magentoDataFixture Magento/Catalog/_files/category_with_different_price_products.php
* @dataProvider getFiltersWithCustomAttributeDataProvider
* @param array $products
* @param array $attributeData
* @param array $expectation
* @return void
*/
public function testGetFiltersWithCustomAttribute(
array $products,
array $attributeData,
array $expectation
): void {
$this->getSearchFiltersAndAssert($products, $attributeData, $expectation);
}
/**
* @return array
*/
public function getFiltersWithCustomAttributeDataProvider(): array
{
$dataProvider = parent::getFiltersWithCustomAttributeDataProvider();
$dataProvider = array_replace_recursive(
$dataProvider,
[
'not_used_in_navigation' => [
'attribute_data' => [
'is_filterable_in_search' => 0,
],
],
'used_in_navigation_with_results' => [
'attribute_data' => [
'is_filterable_in_search' => 1,
],
],
'used_in_navigation_without_results' => [
'attribute_data' => [
'is_filterable' => 0,
'is_filterable_in_search' => 1,
],
],
]
);
//TODO uncomment after fix MC-29227
//unset($dataProvider['used_in_navigation_without_results']['expectation'][1]);
return $dataProvider;
}
/**
* @magentoDataFixture Magento/Catalog/_files/product_boolean_attribute.php
* @magentoDataFixture Magento/Catalog/_files/category_with_different_price_products.php
* @dataProvider getActiveFiltersWithCustomAttributeDataProvider
* @param array $products
* @param array $expectation
* @param string $filterValue
* @param int $productsCount
* @return void
*/
public function testGetActiveFiltersWithCustomAttribute(
array $products,
array $expectation,
string $filterValue,
int $productsCount
): void {
$this->getSearchActiveFiltersAndAssert($products, $expectation, $filterValue, $productsCount);
}
/**
* @inheritdoc
*/
protected function getLayerType(): string
{
return Resolver::CATALOG_LAYER_SEARCH;
}
}
@@ -0,0 +1,55 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\LayeredNavigation\Block\Navigation\Search\Bundle;
use Magento\Catalog\Model\Layer\Resolver;
use Magento\LayeredNavigation\Block\Navigation\Category\Bundle\PriceFilterTest as CategoryFilterTest;
/**
* Provides price filter tests for bundle product in navigation block on search page.
*
* @magentoAppArea frontend
* @magentoAppIsolation enabled
* @magentoDbIsolation disabled
*/
class PriceFilterTest extends CategoryFilterTest
{
/**
* @magentoDataFixture Magento/Bundle/_files/dynamic_and_fixed_bundle_products_in_category.php
* @magentoConfigFixture current_store catalog/layered_navigation/price_range_calculation manual
* @magentoConfigFixture current_store catalog/layered_navigation/price_range_step 10
* @return void
*/
public function testGetFilters(): void
{
$this->getSearchFiltersAndAssert(
['bundle-product' => 20.00],
['is_filterable_in_search' => 1],
[
['label' => '$10.00 - $19.99', 'value' => '10-20', 'count' => 1],
['label' => '$20.00 and above', 'value' => '20-30', 'count' => 1],
]
);
}
/**
* @inheritdoc
*/
protected function getLayerType(): string
{
return Resolver::CATALOG_LAYER_SEARCH;
}
/**
* @inheritdoc
*/
protected function getSearchString(): string
{
return 'Bundle';
}
}
@@ -0,0 +1,75 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\LayeredNavigation\Block\Navigation\Search;
use Magento\Catalog\Model\Layer\Filter\AbstractFilter;
use Magento\Catalog\Model\Layer\Resolver;
use Magento\LayeredNavigation\Block\Navigation\Category\DecimalFilterTest as CategoryDecimalFilterTest;
/**
* Provides tests for custom price filter in navigation block on search page.
*
* @magentoAppArea frontend
* @magentoAppIsolation enabled
* @magentoDbIsolation disabled
*/
class DecimalFilterTest extends CategoryDecimalFilterTest
{
/**
* @magentoDataFixture Magento/Catalog/_files/product_decimal_attribute.php
* @magentoDataFixture Magento/Catalog/_files/category_with_different_price_products.php
* @dataProvider getFiltersWithCustomAttributeDataProvider
* @param array $products
* @param array $attributeData
* @param array $expectation
* @return void
*/
public function testGetFiltersWithCustomAttribute(
array $products,
array $attributeData,
array $expectation
): void {
$this->getSearchFiltersAndAssert($products, $attributeData, $expectation);
}
/**
* @return array
*/
public function getFiltersWithCustomAttributeDataProvider(): array
{
$dataProvider = parent::getFiltersWithCustomAttributeDataProvider();
$dataProvider = array_replace_recursive(
$dataProvider,
[
'not_used_in_navigation' => [
'attribute_data' => ['is_filterable' => 0, 'is_filterable_in_search' => 0],
],
/* @TODO: Should be uncommented in MC-16650 */
/*'used_in_navigation_with_results' => [
'attribute_data' => [
'is_filterable' => AbstractFilter::ATTRIBUTE_OPTIONS_ONLY_WITH_RESULTS,
'is_filterable_in_search' => 1,
],
],*/
]
);
return $dataProvider;
}
/**
* @inheritdoc
*/
protected function getLayerType(): string
{
return Resolver::CATALOG_LAYER_SEARCH;
}
}
@@ -0,0 +1,58 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\LayeredNavigation\Block\Navigation\Search;
use Magento\Catalog\Model\Layer\Resolver;
use Magento\LayeredNavigation\Block\Navigation\Category\MultipleFiltersTest as CategoryFilterTest;
use Magento\Catalog\Model\Layer\Filter\AbstractFilter;
/**
* Provides tests for multiple custom select filters in navigation block on search page.
*
* @magentoAppArea frontend
* @magentoAppIsolation enabled
* @magentoDbIsolation disabled
*/
class MultipleFiltersTest extends CategoryFilterTest
{
/**
* @magentoDataFixture Magento/Catalog/_files/product_dropdown_attribute.php
* @magentoDataFixture Magento/Catalog/_files/configurable_attribute.php
* @magentoDataFixture Magento/Catalog/_files/category_with_three_products.php
* @dataProvider getMultipleActiveFiltersDataProvider
* @param array $products
* @param array $filters
* @param array $expectedProducts
* @return void
*/
public function testGetMultipleActiveFilters(
array $products,
array $filters,
array $expectedProducts
): void {
$this->updateAttributesAndProducts(
$products,
['is_filterable' => AbstractFilter::ATTRIBUTE_OPTIONS_ONLY_WITH_RESULTS, 'is_filterable_in_search' => 1]
);
$this->clearInstanceAndReindexSearch();
$this->navigationBlock->getRequest()->setParams(
array_merge($this->getMultipleRequestParams($filters), ['q' => $this->getSearchString()])
);
$this->navigationBlock->setLayout($this->layout);
$resultProducts = $this->getProductSkus($this->navigationBlock->getLayer()->getProductCollection());
self::assertEqualsCanonicalizing($expectedProducts, $resultProducts);
}
/**
* @inheritdoc
*/
protected function getLayerType(): string
{
return Resolver::CATALOG_LAYER_SEARCH;
}
}
@@ -0,0 +1,103 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\LayeredNavigation\Block\Navigation\Search;
use Magento\Catalog\Model\Layer\Filter\AbstractFilter;
use Magento\Catalog\Model\Layer\Resolver;
use Magento\LayeredNavigation\Block\Navigation\Category\MultiselectFilterTest as CategoryMultiselectFilterTest;
/**
* Provides tests for custom multiselect filter in navigation block on search page.
*
* @magentoAppArea frontend
* @magentoAppIsolation enabled
* @magentoDbIsolation disabled
*/
class MultiselectFilterTest extends CategoryMultiselectFilterTest
{
/**
* @magentoDataFixture Magento/Catalog/_files/multiselect_attribute.php
* @magentoDataFixture Magento/Catalog/_files/category_with_different_price_products.php
* @dataProvider getFiltersWithCustomAttributeDataProvider
* @param array $products
* @param array $attributeData
* @param array $expectation
* @return void
*/
public function testGetFiltersWithCustomAttribute(
array $products,
array $attributeData,
array $expectation
): void {
$this->getSearchFiltersAndAssert($products, $attributeData, $expectation);
}
/**
* @return array
*/
public function getFiltersWithCustomAttributeDataProvider(): array
{
$dataProvider = parent::getFiltersWithCustomAttributeDataProvider();
$dataProvider = array_replace_recursive(
$dataProvider,
[
'not_used_in_navigation' => [
'attribute_data' => [
'is_filterable_in_search' => 0,
],
],
'used_in_navigation_with_results' => [
'attribute_data' => [
'is_filterable' => AbstractFilter::ATTRIBUTE_OPTIONS_ONLY_WITH_RESULTS,
'is_filterable_in_search' => 1,
],
],
'used_in_navigation_without_results' => [
'attribute_data' => [
'is_filterable' => 0,
'is_filterable_in_search' => 1,
],
],
]
);
//TODO uncomment after fix MC-29227
//unset($dataProvider['used_in_navigation_without_results']['expectation'][1]);
//unset($dataProvider['used_in_navigation_without_results']['expectation'][2]);
//unset($dataProvider['used_in_navigation_without_results']['expectation'][3]);
return $dataProvider;
}
/**
* @magentoDataFixture Magento/Catalog/_files/multiselect_attribute.php
* @magentoDataFixture Magento/Catalog/_files/category_with_different_price_products.php
* @dataProvider getActiveFiltersWithCustomAttributeDataProvider
* @param array $products
* @param array $expectation
* @param string $filterValue
* @param int $productsCount
* @return void
*/
public function testGetActiveFiltersWithCustomAttribute(
array $products,
array $expectation,
string $filterValue,
int $productsCount
): void {
$this->getSearchActiveFiltersAndAssert($products, $expectation, $filterValue, $productsCount);
}
/**
* @inheritdoc
*/
protected function getLayerType(): string
{
return Resolver::CATALOG_LAYER_SEARCH;
}
}
@@ -0,0 +1,53 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\LayeredNavigation\Block\Navigation\Search;
use Magento\Catalog\Model\Layer\Filter\AbstractFilter;
use Magento\Catalog\Model\Layer\Resolver;
use Magento\LayeredNavigation\Block\Navigation\Category\OutOfStockProductsFilterTest as CategoryFilterTest;
/**
* Provides tests for select filter in navigation block on search page with out of stock products
* and enabled out of stock products displaying.
*
* @magentoAppArea frontend
* @magentoAppIsolation enabled
* @magentoDbIsolation disabled
*/
class OutOfStockProductsFilterTest extends CategoryFilterTest
{
/**
* @magentoDataFixture Magento/Catalog/_files/product_dropdown_attribute.php
* @magentoDataFixture Magento/Catalog/_files/out_of_stock_product_with_category.php
* @magentoDataFixture Magento/Catalog/_files/product_with_category.php
* @dataProvider getFiltersWithOutOfStockProduct
* @param int $showOutOfStock
* @param array $expectation
* @return void
*/
public function testGetFiltersWithOutOfStockProduct(int $showOutOfStock, array $expectation): void
{
$this->updateConfigShowOutOfStockFlag($showOutOfStock);
$this->getSearchFiltersAndAssert(
['out-of-stock-product' => 'Option 1', 'in-stock-product' => 'Option 2'],
[
'is_filterable' => AbstractFilter::ATTRIBUTE_OPTIONS_ONLY_WITH_RESULTS,
'is_filterable_in_search' => 1,
],
$expectation
);
}
/**
* @inheritdoc
*/
protected function getLayerType(): string
{
return Resolver::CATALOG_LAYER_SEARCH;
}
}
@@ -0,0 +1,66 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\LayeredNavigation\Block\Navigation\Search;
use Magento\Catalog\Model\Layer\Filter\AbstractFilter;
use Magento\Catalog\Model\Layer\Resolver;
use Magento\LayeredNavigation\Block\Navigation\Category\PriceFilterTest as CategoryPriceFilterTest;
/**
* Provides price filter tests with different price ranges calculation in navigation block on search page.
*
* @magentoAppArea frontend
* @magentoAppIsolation enabled
* @magentoDbIsolation disabled
*/
class PriceFilterTest extends CategoryPriceFilterTest
{
/**
* @magentoDataFixture Magento/Catalog/_files/category_with_three_products.php
* @dataProvider getFiltersDataProvider
* @param array $config
* @param array $products
* @param array $expectation
* @return void
*/
public function testGetFilters(array $config, array $products, array $expectation): void
{
$this->applyCatalogConfig($config);
$this->getSearchFiltersAndAssert(
$products,
[
'is_filterable' => AbstractFilter::ATTRIBUTE_OPTIONS_ONLY_WITH_RESULTS,
'is_filterable_in_search' => 1,
],
$expectation
);
}
/**
* @magentoDataFixture Magento/Catalog/_files/category_with_three_products.php
* @dataProvider getActiveFiltersDataProvider
* @param array $config
* @param array $products
* @param array $expectation
* @param string $filterValue
* @return void
*/
public function testGetActiveFilters(array $config, array $products, array $expectation, string $filterValue): void
{
$this->applyCatalogConfig($config);
$this->getSearchActiveFiltersAndAssert($products, $expectation, $filterValue, 1);
}
/**
* @inheritdoc
*/
protected function getLayerType(): string
{
return Resolver::CATALOG_LAYER_SEARCH;
}
}
@@ -0,0 +1,101 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\LayeredNavigation\Block\Navigation\Search;
use Magento\Catalog\Model\Layer\Filter\AbstractFilter;
use Magento\Catalog\Model\Layer\Resolver;
use Magento\LayeredNavigation\Block\Navigation\Category\SelectFilterTest as CategorySelectFilterTest;
/**
* Provides tests for custom select filter in navigation block on search page.
*
* @magentoAppArea frontend
* @magentoAppIsolation enabled
* @magentoDbIsolation disabled
*/
class SelectFilterTest extends CategorySelectFilterTest
{
/**
* @magentoDataFixture Magento/Catalog/_files/product_dropdown_attribute.php
* @magentoDataFixture Magento/Catalog/_files/category_with_different_price_products.php
* @dataProvider getFiltersWithCustomAttributeDataProvider
* @param array $products
* @param array $attributeData
* @param array $expectation
* @return void
*/
public function testGetFiltersWithCustomAttribute(
array $products,
array $attributeData,
array $expectation
): void {
$this->getSearchFiltersAndAssert($products, $attributeData, $expectation);
}
/**
* @return array
*/
public function getFiltersWithCustomAttributeDataProvider(): array
{
$dataProvider = parent::getFiltersWithCustomAttributeDataProvider();
$dataProvider = array_replace_recursive(
$dataProvider,
[
'not_used_in_navigation' => [
'attribute_data' => [
'is_filterable_in_search' => 0,
],
],
'used_in_navigation_with_results' => [
'attribute_data' => [
'is_filterable' => AbstractFilter::ATTRIBUTE_OPTIONS_ONLY_WITH_RESULTS,
'is_filterable_in_search' => 1,
],
],
'used_in_navigation_without_results' => [
'attribute_data' => [
'is_filterable' => 0,
'is_filterable_in_search' => 1,
],
],
]
);
//TODO uncomment after fix MC-29227
//unset($dataProvider['used_in_navigation_without_results']['expectation'][2]);
return $dataProvider;
}
/**
* @magentoDataFixture Magento/Catalog/_files/product_dropdown_attribute.php
* @magentoDataFixture Magento/Catalog/_files/category_with_different_price_products.php
* @dataProvider getActiveFiltersWithCustomAttributeDataProvider
* @param array $products
* @param array $expectation
* @param string $filterValue
* @param int $productsCount
* @return void
*/
public function testGetActiveFiltersWithCustomAttribute(
array $products,
array $expectation,
string $filterValue,
int $productsCount
): void {
$this->getSearchActiveFiltersAndAssert($products, $expectation, $filterValue, $productsCount);
}
/**
* @inheritdoc
*/
protected function getLayerType(): string
{
return Resolver::CATALOG_LAYER_SEARCH;
}
}