Skip to content

Commit b4f2b13

Browse files
committed
MC-33493: Test render fields in composite configure block for simple and configurable product
1 parent 50404cc commit b4f2b13

File tree

8 files changed

+1123
-35
lines changed

8 files changed

+1123
-35
lines changed

dev/tests/integration/testsuite/Magento/Catalog/Block/Adminhtml/Product/Composite/Fieldset/OptionsTest.php

Lines changed: 618 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
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\Catalog\Block\Adminhtml\Product\Composite\Fieldset;
9+
10+
use Magento\Catalog\Api\Data\ProductInterface;
11+
use Magento\Catalog\Api\ProductRepositoryInterface;
12+
use Magento\Catalog\Helper\Product as HelperProduct;
13+
use Magento\Framework\DataObject;
14+
use Magento\Framework\ObjectManagerInterface;
15+
use Magento\Framework\Registry;
16+
use Magento\Framework\View\LayoutInterface;
17+
use Magento\TestFramework\Helper\Bootstrap;
18+
use PHPUnit\Framework\TestCase;
19+
20+
/**
21+
* Test Qty block in composite product configuration layout
22+
*
23+
* @see \Magento\Catalog\Block\Adminhtml\Product\Composite\Fieldset\Qty
24+
* @magentoAppArea adminhtml
25+
*/
26+
class QtyTest extends TestCase
27+
{
28+
/** @var ObjectManagerInterface */
29+
private $objectManager;
30+
31+
/** @var Qty */
32+
private $block;
33+
34+
/** @var ProductRepositoryInterface */
35+
private $productRepository;
36+
37+
/** @var Registry */
38+
private $registry;
39+
40+
/** @var HelperProduct */
41+
private $helperProduct;
42+
43+
/**
44+
* @inheritdoc
45+
*/
46+
protected function setUp(): void
47+
{
48+
$this->objectManager = Bootstrap::getObjectManager();
49+
$this->block = $this->objectManager->get(LayoutInterface::class)->createBlock(Qty::class);
50+
$this->registry = $this->objectManager->get(Registry::class);
51+
$this->productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
52+
$this->productRepository->cleanCache();
53+
$this->helperProduct = $this->objectManager->get(HelperProduct::class);
54+
}
55+
56+
/**
57+
* @inheritdoc
58+
*/
59+
protected function tearDown(): void
60+
{
61+
$this->registry->unregister('current_product');
62+
$this->registry->unregister('product');
63+
}
64+
65+
/**
66+
* @magentoDataFixture Magento/Catalog/_files/product_simple_duplicated.php
67+
* @return void
68+
*/
69+
public function testGetProduct(): void
70+
{
71+
$product = $this->productRepository->get('simple-1');
72+
$this->registerProduct($product);
73+
$this->assertSame($product, $this->block->getProduct());
74+
}
75+
76+
/**
77+
* @magentoDataFixture Magento/Catalog/_files/product_simple_duplicated.php
78+
* @dataProvider getQtyValueProvider
79+
* @param bool $isQty
80+
* @param int $qty
81+
* @return void
82+
*/
83+
public function testGetQtyValue(bool $isQty, int $qty): void
84+
{
85+
$product = $this->productRepository->get('simple-1');
86+
if ($isQty) {
87+
/** @var DataObject $request */
88+
$buyRequest = $this->objectManager->create(DataObject::class);
89+
$buyRequest->setData(['qty' => $qty]);
90+
$this->helperProduct->prepareProductOptions($product, $buyRequest);
91+
}
92+
$this->registerProduct($product);
93+
$this->assertEquals($qty, $this->block->getQtyValue(), 'Expected block qty value is incorrect!');
94+
}
95+
96+
/**
97+
* Provides test data to verify block qty value.
98+
*
99+
* @return array
100+
*/
101+
public function getQtyValueProvider(): array
102+
{
103+
return [
104+
'with_qty' => [
105+
'is_qty' => true,
106+
'qty' => 5,
107+
],
108+
'without_qty' => [
109+
'is_qty' => false,
110+
'qty' => 1,
111+
],
112+
];
113+
}
114+
115+
/**
116+
* Register the product
117+
*
118+
* @param ProductInterface $product
119+
* @return void
120+
*/
121+
private function registerProduct(ProductInterface $product): void
122+
{
123+
$this->registry->unregister('current_product');
124+
$this->registry->unregister('product');
125+
$this->registry->register('current_product', $product);
126+
$this->registry->register('product', $product);
127+
}
128+
}
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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\Catalog\Block\Adminhtml\Product\Composite;
9+
10+
use Magento\Backend\Model\View\Result\Page;
11+
use Magento\Catalog\Api\Data\ProductInterface;
12+
use Magento\Catalog\Api\ProductRepositoryInterface;
13+
use Magento\Framework\ObjectManagerInterface;
14+
use Magento\Framework\Registry;
15+
use Magento\Framework\View\Result\PageFactory;
16+
use Magento\TestFramework\Helper\Bootstrap;
17+
use Magento\TestFramework\Helper\Xpath;
18+
use PHPUnit\Framework\TestCase;
19+
20+
/**
21+
* Test Fieldset block in composite product configuration layout
22+
*
23+
* @see \Magento\Catalog\Block\Adminhtml\Product\Composite\Fieldset
24+
* @magentoAppArea adminhtml
25+
*/
26+
class FieldsetTest extends TestCase
27+
{
28+
/** @var ObjectManagerInterface */
29+
private $objectManager;
30+
31+
/** @var Page */
32+
private $page;
33+
34+
/** @var Registry */
35+
private $registry;
36+
37+
/** @var ProductRepositoryInterface */
38+
private $productRepository;
39+
40+
/** @var string */
41+
private $fieldsetXpath = "//fieldset[@id='product_composite_configure_fields_%s']";
42+
43+
/**
44+
* @inheritdoc
45+
*/
46+
protected function setUp(): void
47+
{
48+
parent::setUp();
49+
50+
$this->objectManager = Bootstrap::getObjectManager();
51+
$this->page = $this->objectManager->get(PageFactory::class)->create();
52+
$this->registry = $this->objectManager->get(Registry::class);
53+
$this->productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
54+
$this->productRepository->cleanCache();
55+
}
56+
57+
/**
58+
* @inheritdoc
59+
*/
60+
protected function tearDown(): void
61+
{
62+
$this->registry->unregister('current_product');
63+
$this->registry->unregister('product');
64+
65+
parent::tearDown();
66+
}
67+
68+
/**
69+
* @magentoDataFixture Magento/Catalog/_files/product_simple_with_options.php
70+
* @return void
71+
*/
72+
public function testRenderHtml(): void
73+
{
74+
$product = $this->productRepository->get('simple');
75+
$this->registerProduct($product);
76+
$this->preparePage($product->getTypeId());
77+
$html = $this->page->getLayout()->getBlock('product.composite.fieldset')->toHtml();
78+
79+
$this->assertEquals(
80+
1,
81+
Xpath::getElementsCountForXpath(sprintf($this->fieldsetXpath, 'options'), $html),
82+
'Expected options block is missing!'
83+
);
84+
$this->assertEquals(
85+
1,
86+
Xpath::getElementsCountForXpath(sprintf($this->fieldsetXpath, 'qty'), $html),
87+
'Expected qty block is missing!'
88+
);
89+
}
90+
91+
/**
92+
* Prepare page layout
93+
*
94+
* @param string $productType
95+
* @return void
96+
*/
97+
private function preparePage(string $productType): void
98+
{
99+
$this->page->addHandle([
100+
'default',
101+
'CATALOG_PRODUCT_COMPOSITE_CONFIGURE',
102+
'catalog_product_view_type_' . $productType,
103+
]);
104+
$this->page->getLayout()->generateXml();
105+
}
106+
107+
/**
108+
* Register the product
109+
*
110+
* @param ProductInterface $product
111+
* @return void
112+
*/
113+
private function registerProduct(ProductInterface $product): void
114+
{
115+
$this->registry->unregister('current_product');
116+
$this->registry->unregister('product');
117+
$this->registry->register('current_product', $product);
118+
$this->registry->register('product', $product);
119+
}
120+
}

0 commit comments

Comments
 (0)