|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\LayeredNavigation\Block\Navigation\Category; |
| 9 | + |
| 10 | +use Magento\Catalog\Model\Layer\Filter\AbstractFilter; |
| 11 | +use Magento\Catalog\Model\Layer\Resolver; |
| 12 | +use Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface; |
| 13 | +use Magento\LayeredNavigation\Block\Navigation\AbstractFiltersTest; |
| 14 | +use Magento\Store\Model\StoreManagerInterface; |
| 15 | + |
| 16 | +/** |
| 17 | + * Provides tests for custom filter with different scopes in navigation block on category page. |
| 18 | + * |
| 19 | + * @magentoAppArea frontend |
| 20 | + * @magentoAppIsolation enabled |
| 21 | + * @magentoDbIsolation disabled |
| 22 | + */ |
| 23 | +class FilterScopeTest extends AbstractFiltersTest |
| 24 | +{ |
| 25 | + /** |
| 26 | + * @var StoreManagerInterface |
| 27 | + */ |
| 28 | + private $storeManager; |
| 29 | + |
| 30 | + /** |
| 31 | + * @var int |
| 32 | + */ |
| 33 | + private $oldStoreId; |
| 34 | + |
| 35 | + /** |
| 36 | + * @var int |
| 37 | + */ |
| 38 | + private $currentStoreId; |
| 39 | + |
| 40 | + /** |
| 41 | + * @inheritdoc |
| 42 | + */ |
| 43 | + protected function setUp() |
| 44 | + { |
| 45 | + parent::setUp(); |
| 46 | + $this->storeManager = $this->objectManager->create(StoreManagerInterface::class); |
| 47 | + $this->oldStoreId = (int)$this->storeManager->getStore()->getId(); |
| 48 | + $this->currentStoreId = (int)$this->storeManager->getStore('fixture_second_store')->getId(); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * @magentoDataFixture Magento/Catalog/_files/product_dropdown_attribute.php |
| 53 | + * @magentoDataFixture Magento/Catalog/_files/category_with_different_price_products_on_two_websites.php |
| 54 | + * @dataProvider filtersWithScopeDataProvider |
| 55 | + * @param int $scope |
| 56 | + * @param array $products |
| 57 | + * @param array $expectation |
| 58 | + * @return void |
| 59 | + */ |
| 60 | + public function testGetFilters(int $scope, array $products, array $expectation): void |
| 61 | + { |
| 62 | + $this->updateAttribute( |
| 63 | + [ |
| 64 | + 'is_filterable' => AbstractFilter::ATTRIBUTE_OPTIONS_ONLY_WITH_RESULTS, |
| 65 | + 'is_global' => $scope, |
| 66 | + ] |
| 67 | + ); |
| 68 | + $this->updateProductsOnStore($products); |
| 69 | + $this->clearInstanceAndReindexSearch(); |
| 70 | + $this->storeManager->setCurrentStore($this->currentStoreId); |
| 71 | + $this->navigationBlock->getLayer()->setCurrentCategory( |
| 72 | + $this->loadCategory('Category 999', $this->currentStoreId) |
| 73 | + ); |
| 74 | + $this->navigationBlock->setLayout($this->layout); |
| 75 | + $filter = $this->getFilterByCode($this->navigationBlock->getFilters(), $this->getAttributeCode()); |
| 76 | + $this->assertNotNull($filter); |
| 77 | + $this->assertEquals($expectation, $this->prepareFilterItems($filter)); |
| 78 | + $this->storeManager->setCurrentStore($this->oldStoreId); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * @return array |
| 83 | + */ |
| 84 | + public function filtersWithScopeDataProvider(): array |
| 85 | + { |
| 86 | + return [ |
| 87 | + 'with_scope_store' => [ |
| 88 | + 'scope' => ScopedAttributeInterface::SCOPE_STORE, |
| 89 | + 'products' => [ |
| 90 | + 'default' => ['simple1000' => 'Option 1', 'simple1001' => 'Option 2'], |
| 91 | + 'fixture_second_store' => ['simple1000' => 'Option 2', 'simple1001' => 'Option 3'], |
| 92 | + ], |
| 93 | + 'expectation' => [ |
| 94 | + ['label' => 'Option 2', 'count' => 1], |
| 95 | + ['label' => 'Option 3', 'count' => 1], |
| 96 | + ], |
| 97 | + ], |
| 98 | + 'with_scope_website' => [ |
| 99 | + 'scope' => ScopedAttributeInterface::SCOPE_WEBSITE, |
| 100 | + 'products' => [ |
| 101 | + 'default' => ['simple1000' => 'Option 3', 'simple1001' => 'Option 2'], |
| 102 | + 'fixture_second_store' => ['simple1000' => 'Option 1', 'simple1001' => 'Option 2'], |
| 103 | + ], |
| 104 | + 'expectation' => [ |
| 105 | + ['label' => 'Option 1', 'count' => 1], |
| 106 | + ['label' => 'Option 2', 'count' => 1], |
| 107 | + ], |
| 108 | + ], |
| 109 | + 'with_scope_global' => [ |
| 110 | + 'scope' => ScopedAttributeInterface::SCOPE_GLOBAL, |
| 111 | + 'products' => [ |
| 112 | + 'default' => ['simple1000' => 'Option 1'], |
| 113 | + 'fixture_second_store' => ['simple1001' => 'Option 2'], |
| 114 | + ], |
| 115 | + 'expectation' => [ |
| 116 | + ['label' => 'Option 1', 'count' => 1], |
| 117 | + ['label' => 'Option 2', 'count' => 1], |
| 118 | + ], |
| 119 | + ], |
| 120 | + ]; |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * @inheritdoc |
| 125 | + */ |
| 126 | + protected function getLayerType(): string |
| 127 | + { |
| 128 | + return Resolver::CATALOG_LAYER_CATEGORY; |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * @inheritdoc |
| 133 | + */ |
| 134 | + protected function getAttributeCode(): string |
| 135 | + { |
| 136 | + return 'dropdown_attribute'; |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * Updates products data for store. |
| 141 | + * |
| 142 | + * @param array $productsData |
| 143 | + * @return void |
| 144 | + */ |
| 145 | + private function updateProductsOnStore(array $productsData): void |
| 146 | + { |
| 147 | + foreach ($productsData as $storeCode => $products) { |
| 148 | + $storeId = (int)$this->storeManager->getStore($storeCode)->getId(); |
| 149 | + $this->storeManager->setCurrentStore($storeId); |
| 150 | + $this->updateProducts($products, $this->getAttributeCode(), $storeId); |
| 151 | + } |
| 152 | + $this->storeManager->setCurrentStore($this->oldStoreId); |
| 153 | + } |
| 154 | +} |
0 commit comments