Skip to content

Commit aee5a50

Browse files
committed
MC-21456: Storefront: Product view in a category
1 parent e58292e commit aee5a50

File tree

2 files changed

+85
-81
lines changed

2 files changed

+85
-81
lines changed

dev/tests/integration/testsuite/Magento/Catalog/Block/Product/ListProduct/ProductInCategoriesViewTest.php

Lines changed: 76 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,13 @@
77

88
namespace Magento\Catalog\Block\Product\ListProduct;
99

10-
use Magento\Catalog\Api\CategoryLinkManagementInterface;
1110
use Magento\Catalog\Api\CategoryRepositoryInterface;
12-
use Magento\Catalog\Api\Data\CategoryInterface;
1311
use Magento\Catalog\Api\ProductRepositoryInterface;
1412
use Magento\Catalog\Block\Product\ListProduct;
15-
use Magento\Catalog\Model\Layer;
16-
use Magento\Catalog\Model\Layer\Resolver;
1713
use Magento\Catalog\Model\Product\Visibility;
1814
use Magento\Eav\Model\Entity\Collection\AbstractCollection;
1915
use Magento\Framework\ObjectManagerInterface;
20-
use Magento\Framework\Registry;
2116
use Magento\Framework\View\LayoutInterface;
22-
use Magento\Store\Api\StoreRepositoryInterface;
2317
use Magento\Store\Model\StoreManagerInterface;
2418
use Magento\TestFramework\Helper\Bootstrap;
2519
use PHPUnit\Framework\TestCase;
@@ -28,9 +22,9 @@
2822
* Checks products displaying on category page
2923
*
3024
* @magentoDbIsolation disabled
31-
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
25+
* @magentoAppIsolation enabled
3226
*/
33-
class AbstractTest extends TestCase
27+
class ProductInCategoriesViewTest extends TestCase
3428
{
3529
/** @var ObjectManagerInterface */
3630
private $objectManager;
@@ -41,21 +35,12 @@ class AbstractTest extends TestCase
4135
/** @var CategoryRepositoryInterface */
4236
private $categoryRepository;
4337

44-
/** @var Registry */
45-
private $registry;
46-
4738
/** @var ProductRepositoryInterface */
4839
private $productRepository;
4940

50-
/** @var CategoryLinkManagementInterface */
51-
private $categoryLinkManagement;
52-
5341
/** @var StoreManagerInterface */
5442
private $storeManager;
5543

56-
/** @var StoreRepositoryInterface */
57-
private $storeRepository;
58-
5944
/** @var LayoutInterface */
6045
private $layout;
6146

@@ -68,25 +53,22 @@ protected function setUp()
6853

6954
$this->objectManager = Bootstrap::getObjectManager();
7055
$this->categoryRepository = $this->objectManager->create(CategoryRepositoryInterface::class);
71-
$this->block = $this->objectManager->get(LayoutInterface::class)->createBlock(ListProduct::class);
72-
$this->registry = $this->objectManager->get(Registry::class);
7356
$this->productRepository = $this->objectManager->create(ProductRepositoryInterface::class);
74-
$this->categoryLinkManagement = $this->objectManager->create(CategoryLinkManagementInterface::class);
7557
$this->storeManager = $this->objectManager->get(StoreManagerInterface::class);
76-
$this->storeRepository = $this->objectManager->create(StoreRepositoryInterface::class);
7758
$this->layout = $this->objectManager->get(LayoutInterface::class);
59+
$this->block = $this->layout->createBlock(ListProduct::class);
7860
}
7961

8062
/**
8163
* @magentoDataFixture Magento/Catalog/_files/category_with_two_products.php
82-
* @magentoAppIsolation enabled
8364
* @dataProvider productDataProvider
8465
* @param array $data
8566
* @return void
8667
*/
8768
public function testCategoryProductView(array $data): void
8869
{
89-
$collection = $this->processCategoryViewTest($data['sku'], $data);
70+
$this->updateProduct($data['sku'], $data);
71+
$collection = $this->getCategoryProductCollection(333);
9072

9173
$this->assertEquals(1, $collection->getSize());
9274
$this->assertEquals('simple333', $collection->getFirstItem()->getSku());
@@ -120,14 +102,14 @@ public function productDataProvider(): array
120102

121103
/**
122104
* @magentoDataFixture Magento/Catalog/_files/category_product.php
123-
* @magentoAppIsolation enabled
124105
* @dataProvider productVisibilityProvider
125106
* @param array $data
126107
* @return void
127108
*/
128109
public function testCategoryProductVisibility(array $data): void
129110
{
130-
$collection = $this->processCategoryViewTest($data['data']['sku'], $data['data']);
111+
$this->updateProduct($data['data']['sku'], $data['data']);
112+
$collection = $this->getCategoryProductCollection(333);
131113

132114
$this->assertEquals($data['expected_count'], $collection->getSize());
133115
}
@@ -180,34 +162,38 @@ public function productVisibilityProvider(): array
180162
}
181163

182164
/**
183-
* @magentoAppIsolation enabled
184165
* @magentoDataFixture Magento/Catalog/_files/category_tree.php
185166
* @magentoDataFixture Magento/Catalog/_files/second_product_simple.php
186167
* @return void
187168
*/
188169
public function testAnchorCategoryProductVisibility(): void
189170
{
190-
$collections = $this->processAnchorTest(true);
171+
$this->updateCategoryIsAnchor(400, true);
172+
$this->assignProductCategories(['simple2'], [402]);
173+
$parentCategoryCollection = $this->getCategoryProductCollection(400);
174+
$childCategoryCollection = $this->getCategoryProductCollection(402, true);
191175

192-
$this->assertEquals(1, $collections['parent_collection']->getSize());
176+
$this->assertEquals(1, $parentCategoryCollection->getSize());
193177
$this->assertEquals(
194-
$collections['child_collection']->getAllIds(),
195-
$collections['parent_collection']->getAllIds()
178+
$childCategoryCollection->getAllIds(),
179+
$parentCategoryCollection->getAllIds()
196180
);
197181
}
198182

199183
/**
200-
* @magentoAppIsolation enabled
201184
* @magentoDataFixture Magento/Catalog/_files/category_tree.php
202185
* @magentoDataFixture Magento/Catalog/_files/second_product_simple.php
203186
* @return void
204187
*/
205188
public function testNonAnchorCategoryProductVisibility(): void
206189
{
207-
$collections = $this->processAnchorTest(false);
190+
$this->updateCategoryIsAnchor(400, false);
191+
$this->assignProductCategories(['simple2'], [402]);
192+
$parentCategoryCollection = $this->getCategoryProductCollection(400);
193+
$childCategoryCollection = $this->getCategoryProductCollection(402, true);
208194

209-
$this->assertCount(0, $collections['parent_collection']);
210-
$this->assertCount(1, $collections['child_collection']);
195+
$this->assertCount(0, $parentCategoryCollection);
196+
$this->assertCount(1, $childCategoryCollection);
211197
}
212198

213199
/**
@@ -217,85 +203,94 @@ public function testNonAnchorCategoryProductVisibility(): void
217203
*/
218204
public function testCategoryProductViewOnMultiWebsite(): void
219205
{
220-
$this->setCategoriesToProducts(['simple-1', 'simple-2']);
221-
$store = $this->storeRepository->get('fixture_second_store');
222-
$this->storeManager->setCurrentStore($store->getId());
223-
$category = $this->categoryRepository->get(333);
224-
$this->registerCategory($category);
225-
$collection = $this->block->getLoadedProductCollection();
226-
$this->assertEquals(1, $collection->getSize());
227-
$this->assertEquals('simple-2', $collection->getFirstItem()->getSku());
206+
$this->assignProductCategories(['simple-1', 'simple-2'], [3, 333]);
207+
$store = $this->storeManager->getStore('fixture_second_store');
208+
$currentStore = $this->storeManager->getStore();
209+
210+
try {
211+
$this->storeManager->setCurrentStore($store->getId());
212+
$collection = $this->block->getLoadedProductCollection();
213+
$collectionSize = $collection->getSize();
214+
} finally {
215+
$this->storeManager->setCurrentStore($currentStore);
216+
}
217+
218+
$this->assertEquals(1, $collectionSize);
219+
$this->assertNull($collection->getItemByColumnValue('sku', 'simple-1'));
220+
$this->assertNotNull($collection->getItemByColumnValue('sku', 'simple-2'));
228221
}
229222

230223
/**
231224
* Set categories to the products
232225
*
233226
* @param array $skus
227+
* @param $categoryIds
234228
* @return void
235229
*/
236-
private function setCategoriesToProducts(array $skus): void
230+
private function assignProductCategories(array $skus, array $categoryIds): void
237231
{
238232
foreach ($skus as $sku) {
239233
$product = $this->productRepository->get($sku);
240-
$product->setCategoryIds([2, 333]);
234+
$product->setCategoryIds($categoryIds);
241235
$this->productRepository->save($product);
242236
}
243237
}
244238

245239
/**
246-
* Proccess for anchor and non anchor category test
240+
* Update product
247241
*
248-
* @param bool $isAnchor
249-
* @return array
242+
* @param string $sku
243+
* @param array $data
244+
* @return void
250245
*/
251-
private function processAnchorTest(bool $isAnchor): array
246+
private function updateProduct(string $sku, array $data): void
252247
{
253-
$category = $this->categoryRepository->get(400);
254-
$category->setIsAnchor($isAnchor);
255-
$this->categoryRepository->save($category);
256-
$childCategory = $this->categoryRepository->get(402);
257-
$this->categoryLinkManagement->assignProductToCategories('simple2', [$childCategory->getId()]);
258-
$this->registerCategory($category);
259-
$parentCategoryCollection = $this->block->getLoadedProductCollection();
260-
$this->objectManager->removeSharedInstance(Resolver::class);
261-
$this->objectManager->removeSharedInstance(Layer::class);
262-
$this->registerCategory($childCategory);
263-
$newBlock = $this->layout->createBlock(ListProduct::class);
264-
$childCategoryCollection = $newBlock->getLoadedProductCollection();
265-
266-
return [
267-
'parent_collection' => $parentCategoryCollection,
268-
'child_collection' => $childCategoryCollection,
269-
];
248+
$product = $this->productRepository->get($sku);
249+
$product->addData($data);
250+
$this->productRepository->save($product);
270251
}
271252

272253
/**
273-
* Proccess category view test
254+
* Returns category collection by category id
274255
*
275-
* @param string $sku
276-
* @param array $data
256+
* @param int $categoryId
257+
* @param bool $refreshBlock
277258
* @return AbstractCollection
278259
*/
279-
private function processCategoryViewTest(string $sku, array $data): AbstractCollection
260+
private function getCategoryProductCollection(int $categoryId, bool $refreshBlock = false): AbstractCollection
280261
{
281-
$product = $this->productRepository->get($sku);
282-
$product->addData($data);
283-
$this->productRepository->save($product);
284-
$category = $this->categoryRepository->get(333);
285-
$this->registerCategory($category);
262+
$block = $this->getListingBlock($refreshBlock);
263+
$block->getLayer()->setCurrentCategory($categoryId);
264+
265+
return $block->getLoadedProductCollection();
266+
}
286267

287-
return $this->block->getLoadedProductCollection();
268+
/**
269+
* Update is_anchor attribute of the category
270+
*
271+
* @param int $categoryId
272+
* @param bool $isAnchor
273+
* @return void
274+
*/
275+
private function updateCategoryIsAnchor(int $categoryId, bool $isAnchor): void
276+
{
277+
$category = $this->categoryRepository->get($categoryId);
278+
$category->setIsAnchor($isAnchor);
279+
$this->categoryRepository->save($category);
288280
}
289281

290282
/**
291-
* Register current category
283+
* Get product listing block
292284
*
293-
* @param CategoryInterface $category
294-
* @retun void
285+
* @param bool $refresh
286+
* @return ListProduct
295287
*/
296-
private function registerCategory(CategoryInterface $category): void
288+
private function getListingBlock(bool $refresh): ListProduct
297289
{
298-
$this->registry->unregister('current_category');
299-
$this->registry->register('current_category', $category);
290+
if ($refresh) {
291+
$this->block = $this->layout->createBlock(ListProduct::class);
292+
}
293+
294+
return $this->block;
300295
}
301296
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
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+
require __DIR__ . '/category_product_rollback.php';
9+
require __DIR__ . '/second_product_simple_rollback.php';

0 commit comments

Comments
 (0)