Skip to content

Commit 64db19a

Browse files
committed
refactoring SaveAction.php, add MFTF test
1 parent 9f622ee commit 64db19a

File tree

3 files changed

+117
-10
lines changed

3 files changed

+117
-10
lines changed

app/code/Magento/Bundle/Model/Option/SaveAction.php

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77

88
namespace Magento\Bundle\Model\Option;
99

10+
use Magento\Bundle\Api\Data\LinkInterface;
1011
use Magento\Bundle\Api\Data\OptionInterface;
1112
use Magento\Bundle\Model\ResourceModel\Option;
1213
use Magento\Catalog\Api\Data\ProductInterface;
14+
use Magento\Framework\App\ObjectManager;
1315
use Magento\Framework\EntityManager\MetadataPool;
1416
use Magento\Framework\Exception\CouldNotSaveException;
1517
use Magento\Bundle\Model\Product\Type;
@@ -51,20 +53,21 @@ class SaveAction
5153
* @param MetadataPool $metadataPool
5254
* @param Type $type
5355
* @param ProductLinkManagementInterface $linkManagement
54-
* @param StoreManagerInterface $storeManager
56+
* @param StoreManagerInterface|null $storeManager
5557
*/
5658
public function __construct(
5759
Option $optionResource,
5860
MetadataPool $metadataPool,
5961
Type $type,
6062
ProductLinkManagementInterface $linkManagement,
61-
StoreManagerInterface $storeManager
63+
?StoreManagerInterface $storeManager = null
6264
) {
6365
$this->optionResource = $optionResource;
6466
$this->metadataPool = $metadataPool;
6567
$this->type = $type;
6668
$this->linkManagement = $linkManagement;
67-
$this->storeManager = $storeManager;
69+
$this->storeManager = $storeManager
70+
?: ObjectManager::getInstance()->get(StoreManagerInterface::class);
6871
}
6972

7073
/**
@@ -78,7 +81,7 @@ public function __construct(
7881
*/
7982
public function save(ProductInterface $bundleProduct, OptionInterface $option)
8083
{
81-
$metadata = $this->metadataPool->getMetadata(\Magento\Catalog\Api\Data\ProductInterface::class);
84+
$metadata = $this->metadataPool->getMetadata(ProductInterface::class);
8285

8386
$option->setStoreId($bundleProduct->getStoreId());
8487
$parentId = $bundleProduct->getData($metadata->getLinkField());
@@ -117,7 +120,7 @@ public function save(ProductInterface $bundleProduct, OptionInterface $option)
117120
throw new CouldNotSaveException(__("The option couldn't be saved."), $e);
118121
}
119122

120-
/** @var \Magento\Bundle\Api\Data\LinkInterface $linkedProduct */
123+
/** @var LinkInterface $linkedProduct */
121124
foreach ($linksToAdd as $linkedProduct) {
122125
$this->linkManagement->addChild($bundleProduct, $option->getOptionId(), $linkedProduct);
123126
}
@@ -130,8 +133,8 @@ public function save(ProductInterface $bundleProduct, OptionInterface $option)
130133
/**
131134
* Update option selections
132135
*
133-
* @param \Magento\Catalog\Api\Data\ProductInterface $product
134-
* @param \Magento\Bundle\Api\Data\OptionInterface $option
136+
* @param ProductInterface $product
137+
* @param OptionInterface $option
135138
* @return void
136139
*/
137140
private function updateOptionSelection(ProductInterface $product, OptionInterface $option)
@@ -151,7 +154,7 @@ private function updateOptionSelection(ProductInterface $product, OptionInterfac
151154
$linksToUpdate[] = $productLink;
152155
}
153156
}
154-
/** @var \Magento\Bundle\Api\Data\LinkInterface[] $linksToDelete */
157+
/** @var LinkInterface[] $linksToDelete */
155158
$linksToDelete = $this->compareLinks($existingLinks, $linksToUpdate);
156159
}
157160
foreach ($linksToUpdate as $linkedProduct) {
@@ -172,8 +175,8 @@ private function updateOptionSelection(ProductInterface $product, OptionInterfac
172175
/**
173176
* Compute the difference between given arrays.
174177
*
175-
* @param \Magento\Bundle\Api\Data\LinkInterface[] $firstArray
176-
* @param \Magento\Bundle\Api\Data\LinkInterface[] $secondArray
178+
* @param LinkInterface[] $firstArray
179+
* @param LinkInterface[] $secondArray
177180
*
178181
* @return array
179182
*/

app/code/Magento/Bundle/Test/Mftf/Data/BundleLinkData.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,13 @@
1717
<data key="price_type">1</data>
1818
<data key="can_change_quantity">1</data>
1919
</entity>
20+
<entity name="ApiBundleLinkFixed" type="bundle_link">
21+
<var key="option_id" entityKey="return" entityType="bundle_option"/>
22+
<var key="sku" entityKey="sku" entityType="product"/>
23+
<data key="qty">1</data>
24+
<data key="is_default">1</data>
25+
<data key="price">30</data>
26+
<data key="price_type">0</data>
27+
<data key="can_change_quantity">1</data>
28+
</entity>
2029
</entities>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
9+
<tests xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
10+
xsi:noNamespaceSchemaLocation="urn:magento:mftf:Test/etc/testSchema.xsd">
11+
<test name="StorefrontCheckBundleProductTwoWebsiteDifferentPriceOptionTest">
12+
<annotations>
13+
<title value="Verify bundle item price different websites."/>
14+
<stories value="Github issue: #12584 Bundle Item price cannot differ per website"/>
15+
<description value="Verify bundle item price different websites. Change bundle item price on second website."/>
16+
<features value="Bundle"/>
17+
<group value="bundle"/>
18+
</annotations>
19+
<before>
20+
<magentoCLI command="config:set {{WebsiteCatalogPriceScopeConfigData.path}} {{WebsiteCatalogPriceScopeConfigData.value}}" stepKey="setPriceScopeWebsite"/>
21+
<actionGroup ref="AdminLoginActionGroup" stepKey="logInAsAdmin"/>
22+
23+
<actionGroup ref="AdminCreateWebsiteActionGroup" stepKey="createWebsite">
24+
<argument name="newWebsiteName" value="{{customWebsite.name}}"/>
25+
<argument name="websiteCode" value="{{customWebsite.code}}"/>
26+
</actionGroup>
27+
<actionGroup ref="CreateCustomStoreActionGroup" stepKey="createCustomStoreGroup">
28+
<argument name="website" value="{{customWebsite.name}}"/>
29+
<argument name="store" value="{{customStoreGroup.name}}"/>
30+
<argument name="rootCategory" value="Default Category"/>
31+
</actionGroup>
32+
<actionGroup ref="AdminCreateStoreViewActionGroup" stepKey="createCustomStoreView">
33+
<argument name="StoreGroup" value="customStoreGroup"/>
34+
<argument name="customStore" value="customStore"/>
35+
</actionGroup>
36+
37+
<createData entity="SimpleProduct2" stepKey="simpleProduct"/>
38+
<createData entity="ApiFixedBundleProduct" stepKey="createBundleProduct" />
39+
<createData entity="CheckboxOption" stepKey="createBundleOption">
40+
<requiredEntity createDataKey="createBundleProduct"/>
41+
</createData>
42+
<createData entity="ApiBundleLinkFixed" stepKey="linkOptionToProduct">
43+
<requiredEntity createDataKey="createBundleProduct"/>
44+
<requiredEntity createDataKey="createBundleOption"/>
45+
<requiredEntity createDataKey="simpleProduct"/>
46+
</createData>
47+
</before>
48+
<after>
49+
<magentoCLI command="config:set {{GlobalCatalogPriceScopeConfigData.path}} {{GlobalCatalogPriceScopeConfigData.value}}" stepKey="setPriceScopeGlobal"/>
50+
51+
<deleteData createDataKey="simpleProduct" stepKey="deleteSimpleProduct"/>
52+
<deleteData createDataKey="createBundleProduct" stepKey="deleteBundleProduct"/>
53+
54+
<actionGroup ref="AdminDeleteWebsiteActionGroup" stepKey="deleteWebsite">
55+
<argument name="websiteName" value="{{customWebsite.name}}"/>
56+
</actionGroup>
57+
<actionGroup ref="AdminLogoutActionGroup" stepKey="logout"/>
58+
59+
<actionGroup ref="CliIndexerReindexActionGroup" stepKey="reindex">
60+
<argument name="indices" value=""/>
61+
</actionGroup>
62+
<actionGroup ref="CliCacheCleanActionGroup" stepKey="cleanFullPageCache">
63+
<argument name="tags" value="config full_page"/>
64+
</actionGroup>
65+
</after>
66+
67+
<actionGroup ref="NavigateToCreatedProductEditPageActionGroup" stepKey="openEditBundleProduct">
68+
<argument name="product" value="$$createBundleProduct$$"/>
69+
</actionGroup>
70+
71+
<actionGroup ref="AdminAssignProductInWebsiteActionGroup" stepKey="selectProductInWebsites">
72+
<argument name="website" value="{{customWebsite.name}}"/>
73+
</actionGroup>
74+
<actionGroup ref="SaveProductFormActionGroup" stepKey="clickSaveButton"/>
75+
<actionGroup ref="SwitchToTheNewStoreViewActionGroup" stepKey="SwitchNewStoreView">
76+
<argument name="storeViewName" value="{{customStore.name}}"/>
77+
</actionGroup>
78+
79+
<fillField selector="{{AdminProductFormBundleSection.bundleOptionXProductYPrice('0', '0')}}" userInput="100" stepKey="fillBundleOption1Price"/>
80+
81+
<actionGroup ref="SaveProductFormActionGroup" stepKey="saveNewPrice"/>
82+
<actionGroup ref="StorefrontOpenProductPageActionGroup" stepKey="openProductPage">
83+
<argument name="productUrl" value="$$createBundleProduct.custom_attributes[url_key]$$"/>
84+
</actionGroup>
85+
86+
<click selector="{{StorefrontBundledSection.addToCart}}" stepKey="clickCustomizeAndAddToCart"/>
87+
88+
<grabTextFrom selector="{{StorefrontBundledSection.bundleProductsPrice}}" stepKey="grabPriceText"/>
89+
<assertEquals stepKey="assertPriceText">
90+
<expectedResult type="string">$31.23</expectedResult>
91+
<actualResult type="variable">$grabPriceText</actualResult>
92+
</assertEquals>
93+
94+
</test>
95+
</tests>

0 commit comments

Comments
 (0)