|
| 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\ConfigurableProduct\Block\Product\ProductList; |
| 9 | + |
| 10 | +use Magento\Catalog\Api\Data\ProductInterface; |
| 11 | +use Magento\Catalog\Block\Product\ProductList\AbstractLinksTest; |
| 12 | +use Magento\Catalog\Block\Product\ProductList\Related; |
| 13 | + |
| 14 | +/** |
| 15 | + * Check the correct behavior of related products on the configurable product view page |
| 16 | +
|
| 17 | + * @magentoDbIsolation disabled |
| 18 | + * @magentoAppArea frontend |
| 19 | + */ |
| 20 | +class RelatedTest extends AbstractLinksTest |
| 21 | +{ |
| 22 | + /** |
| 23 | + * @var Related |
| 24 | + */ |
| 25 | + protected $block; |
| 26 | + |
| 27 | + /** |
| 28 | + * @inheritdoc |
| 29 | + */ |
| 30 | + protected function setUp() |
| 31 | + { |
| 32 | + parent::setUp(); |
| 33 | + |
| 34 | + $this->block = $this->layout->createBlock(Related::class); |
| 35 | + $this->linkType = 'related'; |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * @magentoDataFixture Magento/Catalog/_files/second_product_simple.php |
| 40 | + * @magentoDataFixture Magento/ConfigurableProduct/_files/product_configurable.php |
| 41 | + * @return void |
| 42 | + */ |
| 43 | + public function testRenderConfigurableWithLinkedProduct(): void |
| 44 | + { |
| 45 | + $this->linkProducts('configurable', ['simple2' => ['position' => 1]]); |
| 46 | + $relatedProduct = $this->productRepository->get('simple2'); |
| 47 | + $this->block->setProduct($this->productRepository->get('configurable')); |
| 48 | + $this->prepareBlock(); |
| 49 | + $html = $this->block->toHtml(); |
| 50 | + $this->assertNotEmpty($html); |
| 51 | + $this->assertContains($relatedProduct->getName(), $html); |
| 52 | + $this->assertCount(1, $this->block->getItems()); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * @magentoDataFixture Magento/Catalog/_files/second_product_simple.php |
| 57 | + * @magentoDataFixture Magento/ConfigurableProduct/_files/product_configurable.php |
| 58 | + * @return void |
| 59 | + */ |
| 60 | + public function testRenderConfigurableWithLinkedProductOnChild(): void |
| 61 | + { |
| 62 | + $this->linkProducts('simple_10', ['simple2' => ['position' => 1]]); |
| 63 | + $relatedProduct = $this->productRepository->get('simple2'); |
| 64 | + $this->block->setProduct($this->productRepository->get('configurable')); |
| 65 | + $this->prepareBlock(); |
| 66 | + $html = $this->block->toHtml(); |
| 67 | + $this->assertNotEmpty($html); |
| 68 | + $this->assertNotContains($relatedProduct->getName(), $html); |
| 69 | + $this->assertEmpty($this->block->getItems()); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * @magentoDataFixture Magento/Catalog/_files/products_list.php |
| 74 | + * @magentoDataFixture Magento/ConfigurableProduct/_files/product_configurable.php |
| 75 | + * @return void |
| 76 | + */ |
| 77 | + public function testLinkedProductsPosition(): void |
| 78 | + { |
| 79 | + $this->linkProducts( |
| 80 | + 'configurable', |
| 81 | + ['wrong-simple' => ['position' => 3], 'simple-249' => ['position' => 2], 'simple-156' => ['position' => 1]] |
| 82 | + ); |
| 83 | + $this->block->setProduct($this->productRepository->get('configurable')); |
| 84 | + $this->assertEquals( |
| 85 | + ['simple-156', 'simple-249','wrong-simple'], |
| 86 | + $this->getActualLinks($this->getLinkedItems()), |
| 87 | + 'Expected linked products do not match actual linked products!' |
| 88 | + ); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * @magentoDataFixture Magento/Catalog/_files/second_product_simple.php |
| 93 | + * @magentoDataFixture Magento/ConfigurableProduct/_files/product_configurable.php |
| 94 | + * @return void |
| 95 | + */ |
| 96 | + public function testGetIdentities(): void |
| 97 | + { |
| 98 | + $this->linkProducts('configurable', ['simple2' => ['position = 1']]); |
| 99 | + $relatedProduct = $this->productRepository->get('simple2'); |
| 100 | + $this->block->setProduct($this->productRepository->get('configurable')); |
| 101 | + $this->prepareBlock(); |
| 102 | + $this->assertEquals(['cat_p_' . $relatedProduct->getId(), 'cat_p'], $this->block->getIdentities()); |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Returns linked products from block. |
| 107 | + * |
| 108 | + * @return ProductInterface[] |
| 109 | + */ |
| 110 | + protected function getLinkedItems(): array |
| 111 | + { |
| 112 | + return $this->block->getItems()->getItems(); |
| 113 | + } |
| 114 | +} |
0 commit comments