Skip to content

Commit aa344a1

Browse files
committed
MC-31267: Storefront: Check bundle product regular price
1 parent ba8fae3 commit aa344a1

File tree

4 files changed

+351
-56
lines changed

4 files changed

+351
-56
lines changed
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
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\Bundle\Block\Catalog\Product\View\Type;
9+
10+
use Magento\Catalog\Api\ProductRepositoryInterface;
11+
use Magento\Framework\ObjectManagerInterface;
12+
use Magento\Framework\Registry;
13+
use Magento\Framework\Serialize\SerializerInterface;
14+
use Magento\Framework\View\LayoutInterface;
15+
use Magento\TestFramework\Helper\Bootstrap;
16+
use PHPUnit\Framework\TestCase;
17+
18+
/**
19+
* Check bundle product prices.
20+
*
21+
* @magentoDbIsolation enabled
22+
* @magentoAppIsolation disabled
23+
* @magentoAppArea frontend
24+
*/
25+
class BundleProductPriceTest extends TestCase
26+
{
27+
/** @var ObjectManagerInterface */
28+
private $objectManager;
29+
30+
/** @var Registry */
31+
private $registry;
32+
33+
/** @var ProductRepositoryInterface */
34+
private $productRepository;
35+
36+
/** @var SerializerInterface */
37+
private $json;
38+
39+
/** @var Bundle */
40+
private $block;
41+
42+
/**
43+
* @inheritdoc
44+
*/
45+
protected function setUp()
46+
{
47+
parent::setUp();
48+
49+
$this->objectManager = Bootstrap::getObjectManager();
50+
$this->productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
51+
$this->productRepository->cleanCache();
52+
$this->block = $this->objectManager->get(LayoutInterface::class)->createBlock(Bundle::class);
53+
$this->registry = $this->objectManager->get(Registry::class);
54+
$this->json = $this->objectManager->get(SerializerInterface::class);
55+
}
56+
57+
/**
58+
* @inheritdoc
59+
*/
60+
protected function tearDown()
61+
{
62+
$this->registry->unregister('product');
63+
64+
parent::tearDown();
65+
}
66+
67+
/**
68+
* @magentoDataFixture Magento/Bundle/_files/dynamic_bundle_product_multiselect_option.php
69+
*
70+
* @return void
71+
*/
72+
public function testDynamicBundleOptionPrices(): void
73+
{
74+
$expectedData = [
75+
'options_prices' => [
76+
[
77+
'oldPrice' => ['amount' => 10],
78+
'basePrice' => ['amount' => 10],
79+
'finalPrice' => ['amount' => 10],
80+
],
81+
[
82+
'oldPrice' => ['amount' => 20],
83+
'basePrice' => ['amount' => 20],
84+
'finalPrice' => ['amount' => 20],
85+
],
86+
[
87+
'oldPrice' => ['amount' => 30],
88+
'basePrice' => ['amount' => 30],
89+
'finalPrice' => ['amount' => 30],
90+
],
91+
],
92+
'bundle_prices' => [
93+
'oldPrice' => ['amount' => 0],
94+
'basePrice' => ['amount' => 0],
95+
'finalPrice' => ['amount' => 0],
96+
]
97+
];
98+
$this->processBundlePriceView('bundle_product', $expectedData);
99+
}
100+
101+
/**
102+
* @magentoDataFixture Magento/Bundle/_files/product_with_multiple_options_1.php
103+
*
104+
* @return void
105+
*/
106+
public function testFixedBundleOptionPrices(): void
107+
{
108+
$expectedData = [
109+
'options_prices' => [
110+
[
111+
'oldPrice' => ['amount' => 2.75],
112+
'basePrice' => ['amount' => 2.75],
113+
'finalPrice' => ['amount' => 2.75],
114+
],
115+
[
116+
'oldPrice' => ['amount' => 6.75],
117+
'basePrice' => ['amount' => 6.75],
118+
'finalPrice' => ['amount' => 6.75],
119+
],
120+
],
121+
'bundle_prices' => [
122+
'oldPrice' => ['amount' => 12.75],
123+
'basePrice' => ['amount' => 10],
124+
'finalPrice' => ['amount' => 10],
125+
]
126+
];
127+
$this->processBundlePriceView('bundle-product', $expectedData);
128+
}
129+
130+
/**
131+
* @param string $productSku
132+
* @param array $expectedData
133+
* @return void
134+
*/
135+
private function processBundlePriceView(string $productSku, array $expectedData): void
136+
{
137+
$this->registerProduct($productSku);
138+
$jsonConfig = $this->json->unserialize($this->block->getJsonConfig());
139+
$this->assertEquals($expectedData['bundle_prices'], $jsonConfig['prices']);
140+
$this->assertOptionsConfig($expectedData['options_prices'], $jsonConfig);
141+
}
142+
143+
/**
144+
* Assert options prices.
145+
*
146+
* @param array $expectedData
147+
* @param array $actualData
148+
* @return void
149+
*/
150+
private function assertOptionsConfig(array $expectedData, array $actualData): void
151+
{
152+
$optionConfig = $actualData['options'] ?? null;
153+
$this->assertNotNull($optionConfig);
154+
$optionConfig = reset($optionConfig);
155+
foreach (array_values($optionConfig['selections']) as $key => $selection) {
156+
$this->assertEquals($expectedData[$key], $selection['prices']);
157+
}
158+
}
159+
160+
/**
161+
* Register the product.
162+
*
163+
* @param string $productSku
164+
* @return void
165+
*/
166+
private function registerProduct(string $productSku): void
167+
{
168+
$product = $this->productRepository->get($productSku);
169+
$this->registry->unregister('product');
170+
$this->registry->register('product', $product);
171+
}
172+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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+
use Magento\Bundle\Api\Data\LinkInterfaceFactory;
9+
use Magento\Bundle\Api\Data\OptionInterfaceFactory;
10+
use Magento\Bundle\Model\Product\Price;
11+
use Magento\Catalog\Api\Data\ProductExtensionFactory;
12+
use Magento\Catalog\Model\Product\Attribute\Source\Status;
13+
use Magento\Catalog\Model\Product\Type;
14+
use Magento\Catalog\Model\Product\Type\AbstractType;
15+
use Magento\Catalog\Model\Product\Visibility;
16+
17+
require __DIR__ . '/multiple_products.php';
18+
19+
/** @var ProductExtensionFactory $extensionAttributesFactory */
20+
$extensionAttributesFactory = $objectManager->get(ProductExtensionFactory::class);
21+
/** @var OptionInterfaceFactory $optionFactory */
22+
$optionFactory = $objectManager->get(OptionInterfaceFactory::class);
23+
/** @var LinkInterfaceFactory $linkFactory */
24+
$linkFactory = $objectManager->get(LinkInterfaceFactory::class);
25+
26+
$bundleProduct = $productFactory->create();
27+
$bundleProduct->setTypeId(Type::TYPE_BUNDLE)
28+
->setAttributeSetId($bundleProduct->getDefaultAttributeSetId())
29+
->setWebsiteIds([$defaultWebsiteId])
30+
->setName('Bundle Product')
31+
->setSku('bundle_product')
32+
->setVisibility(Visibility::VISIBILITY_BOTH)
33+
->setStatus(Status::STATUS_ENABLED)
34+
->setStockData(
35+
[
36+
'use_config_manage_stock' => 1,
37+
'qty' => 100,
38+
'is_qty_decimal' => 0,
39+
'is_in_stock' => 1,
40+
]
41+
)
42+
->setSkuType(0)
43+
->setPriceView(0)
44+
->setPriceType(Price::PRICE_TYPE_DYNAMIC)
45+
->setWeightType(0)
46+
->setShipmentType(AbstractType::SHIPMENT_TOGETHER)
47+
->setBundleOptionsData(
48+
[
49+
[
50+
'title' => 'Option 1',
51+
'default_title' => 'Option 1',
52+
'type' => 'multi',
53+
'required' => 1,
54+
],
55+
]
56+
)->setBundleSelectionsData(
57+
[
58+
[
59+
[
60+
'product_id' => $product->getId(),
61+
'sku' => $product->getSku(),
62+
'selection_qty' => 1,
63+
'selection_can_change_qty' => 1,
64+
],
65+
[
66+
'product_id' => $product2->getId(),
67+
'sku' => $product2->getSku(),
68+
'selection_qty' => 1,
69+
'selection_can_change_qty' => 1,
70+
],
71+
[
72+
'product_id' => $product3->getId(),
73+
'sku' => $product3->getSku(),
74+
'selection_qty' => 1,
75+
'selection_can_change_qty' => 1,
76+
],
77+
]
78+
]
79+
);
80+
81+
$options = [];
82+
foreach ($bundleProduct->getBundleOptionsData() as $key => $optionData) {
83+
$option = $optionFactory->create(['data' => $optionData]);
84+
$option->setSku($bundleProduct->getSku());
85+
$option->setOptionId(null);
86+
$links = [];
87+
foreach ($bundleProduct->getBundleSelectionsData()[$key] as $linkData) {
88+
$link = $linkFactory->create(['data' => $linkData]);
89+
$links[] = $link;
90+
}
91+
$option->setProductLinks($links);
92+
$options[] = $option;
93+
}
94+
$extensionAttributes = $bundleProduct->getExtensionAttributes() ?: $extensionAttributesFactory->create();
95+
$extensionAttributes->setBundleProductOptions($options);
96+
$bundleProduct->setExtensionAttributes($extensionAttributes);
97+
$productRepository->save($bundleProduct);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
use Magento\Framework\Exception\NoSuchEntityException;
9+
10+
require __DIR__ . '/multiple_products_rollback.php';
11+
12+
$registry->unregister('isSecureArea');
13+
$registry->register('isSecureArea', true);
14+
15+
try {
16+
$product = $productRepository->get('bundle_product', false, null, true);
17+
$productRepository->delete($product);
18+
} catch (NoSuchEntityException $e) {
19+
}
20+
21+
$registry->unregister('isSecureArea');
22+
$registry->register('isSecureArea', false);

0 commit comments

Comments
 (0)