Skip to content

Commit 76e986c

Browse files
ENGCOM-7082: 26827 Added improvements to product attribute repository (save method) #27191
- Merge Pull Request #27191 from sergiy-v/magento2:26827-product-attribute-repository-swatches-improvements - Merged commits: 1. acfc589 2. fcf734f
2 parents ecaa3b7 + fcf734f commit 76e986c

File tree

9 files changed

+282
-92
lines changed

9 files changed

+282
-92
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
<actionGroups xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
10+
xsi:noNamespaceSchemaLocation="urn:magento:mftf:Test/etc/actionGroupSchema.xsd">
11+
<actionGroup name="AssertAdminProductAttributeByCodeOnProductFormActionGroup">
12+
<annotations>
13+
<description>Requires the navigation to the Product page. Provided dropdown attribute presents on the page.</description>
14+
</annotations>
15+
<arguments>
16+
<argument name="productAttributeCode" type="string" defaultValue="{{textSwatchProductAttribute.attribute_code}}"/>
17+
</arguments>
18+
19+
<seeElement selector="{{AdminProductAttributesSection.attributeDropdownByCode(productAttributeCode)}}" stepKey="assertAttributeIsPresentOnForm"/>
20+
</actionGroup>
21+
</actionGroups>

app/code/Magento/Swatches/Controller/Adminhtml/Product/Attribute/Plugin/Save.php

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,33 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
67

78
namespace Magento\Swatches\Controller\Adminhtml\Product\Attribute\Plugin;
89

910
use Magento\Catalog\Controller\Adminhtml\Product\Attribute;
1011
use Magento\Framework\App\RequestInterface;
11-
use Magento\Swatches\Model\Swatch;
12+
use Magento\Swatches\Model\ConvertSwatchAttributeFrontendInput;
1213

1314
/**
1415
* Plugin for product attribute save controller.
1516
*/
1617
class Save
1718
{
19+
/**
20+
* @var ConvertSwatchAttributeFrontendInput
21+
*/
22+
private $convertSwatchAttributeFrontendInput;
23+
24+
/**
25+
* @param ConvertSwatchAttributeFrontendInput $convertSwatchAttributeFrontendInput
26+
*/
27+
public function __construct(
28+
ConvertSwatchAttributeFrontendInput $convertSwatchAttributeFrontendInput
29+
) {
30+
$this->convertSwatchAttributeFrontendInput = $convertSwatchAttributeFrontendInput;
31+
}
32+
1833
/**
1934
* Performs the conversion of the frontend input value.
2035
*
@@ -23,30 +38,12 @@ class Save
2338
* @return array
2439
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
2540
*/
26-
public function beforeDispatch(Attribute\Save $subject, RequestInterface $request)
41+
public function beforeDispatch(Attribute\Save $subject, RequestInterface $request): array
2742
{
2843
$data = $request->getPostValue();
44+
$data = $this->convertSwatchAttributeFrontendInput->execute($data);
45+
$request->setPostValue($data);
2946

30-
if (isset($data['frontend_input'])) {
31-
switch ($data['frontend_input']) {
32-
case 'swatch_visual':
33-
$data[Swatch::SWATCH_INPUT_TYPE_KEY] = Swatch::SWATCH_INPUT_TYPE_VISUAL;
34-
$data['frontend_input'] = 'select';
35-
$request->setPostValue($data);
36-
break;
37-
case 'swatch_text':
38-
$data[Swatch::SWATCH_INPUT_TYPE_KEY] = Swatch::SWATCH_INPUT_TYPE_TEXT;
39-
$data['use_product_image_for_swatch'] = 0;
40-
$data['frontend_input'] = 'select';
41-
$request->setPostValue($data);
42-
break;
43-
case 'select':
44-
$data[Swatch::SWATCH_INPUT_TYPE_KEY] = Swatch::SWATCH_INPUT_TYPE_DROPDOWN;
45-
$data['frontend_input'] = 'select';
46-
$request->setPostValue($data);
47-
break;
48-
}
49-
}
5047
return [$request];
5148
}
5249
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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\Swatches\Model;
9+
10+
/**
11+
* Performs the conversion of the frontend input value for attribute data
12+
*/
13+
class ConvertSwatchAttributeFrontendInput
14+
{
15+
/**
16+
* Performs the conversion of the frontend input value for attribute data
17+
*
18+
* @param array|null $data
19+
*
20+
* @return array|null
21+
*/
22+
public function execute(?array $data): ?array
23+
{
24+
if (!isset($data['frontend_input'])) {
25+
return $data;
26+
}
27+
28+
switch ($data['frontend_input']) {
29+
case 'swatch_visual':
30+
$data[Swatch::SWATCH_INPUT_TYPE_KEY] = Swatch::SWATCH_INPUT_TYPE_VISUAL;
31+
$data['frontend_input'] = 'select';
32+
break;
33+
case 'swatch_text':
34+
$data[Swatch::SWATCH_INPUT_TYPE_KEY] = Swatch::SWATCH_INPUT_TYPE_TEXT;
35+
$data['use_product_image_for_swatch'] = 0;
36+
$data['frontend_input'] = 'select';
37+
break;
38+
case 'select':
39+
$data[Swatch::SWATCH_INPUT_TYPE_KEY] = Swatch::SWATCH_INPUT_TYPE_DROPDOWN;
40+
$data['frontend_input'] = 'select';
41+
break;
42+
}
43+
44+
return $data;
45+
}
46+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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\Swatches\Plugin\Catalog\Model\Product\Attribute;
9+
10+
use Magento\Catalog\Api\Data\ProductAttributeInterface;
11+
use Magento\Catalog\Model\Product\Attribute\Repository as ProductAttributeRepository;
12+
use Magento\Swatches\Model\ConvertSwatchAttributeFrontendInput;
13+
14+
/**
15+
* Plugin for product attribute repository
16+
*/
17+
class RepositoryPlugin
18+
{
19+
/**
20+
* @var ConvertSwatchAttributeFrontendInput
21+
*/
22+
private $convertSwatchAttributeFrontendInput;
23+
24+
/**
25+
* @param ConvertSwatchAttributeFrontendInput $convertSwatchAttributeFrontendInput
26+
*/
27+
public function __construct(
28+
ConvertSwatchAttributeFrontendInput $convertSwatchAttributeFrontendInput
29+
) {
30+
$this->convertSwatchAttributeFrontendInput = $convertSwatchAttributeFrontendInput;
31+
}
32+
33+
/**
34+
* Performs the conversion of the frontend input value.
35+
*
36+
* @param ProductAttributeRepository $subject
37+
* @param ProductAttributeInterface $attribute
38+
* @return array
39+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
40+
*/
41+
public function beforeSave(
42+
ProductAttributeRepository $subject,
43+
ProductAttributeInterface $attribute
44+
): array {
45+
$data = $attribute->getData();
46+
$data = $this->convertSwatchAttributeFrontendInput->execute($data);
47+
$attribute->setData($data);
48+
49+
return [$attribute];
50+
}
51+
}

app/code/Magento/Swatches/Test/Mftf/Data/SwatchAttributeData.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,7 @@
1818
<data key="default_label" unique="suffix">TextSwatchAttr</data>
1919
<data key="attribute_code" unique="suffix">text_swatch_attr</data>
2020
</entity>
21+
<entity name="textSwatchProductAttribute" type="ProductAttribute" extends="productDropDownAttribute">
22+
<data key="frontend_input">swatch_text</data>
23+
</entity>
2124
</entities>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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="AdminCheckTextSwatchAttributeAddedViaApiTest">
12+
<annotations>
13+
<stories value="Add Swatch Text Product Attribute via API"/>
14+
<title value="Add Swatch Text Product Attribute via API"/>
15+
<description value="Login as admin, create swatch text product attribute.Go to New Product page,
16+
check the created attribute is available on the page."/>
17+
<group value="swatches"/>
18+
</annotations>
19+
<before>
20+
<!-- Login as Admin -->
21+
<actionGroup ref="LoginAsAdmin" stepKey="loginToAdminPanel"/>
22+
<!-- Create an attribute with two options to be used in the first child product -->
23+
<createData entity="textSwatchProductAttribute" stepKey="createTextSwatchConfigProductAttribute"/>
24+
<!-- Add the attribute just created to default attribute set -->
25+
<createData entity="AddToDefaultSet" stepKey="createConfigAddToAttributeSet">
26+
<requiredEntity createDataKey="createTextSwatchConfigProductAttribute"/>
27+
</createData>
28+
</before>
29+
<after>
30+
<!-- Delete Created Data -->
31+
<deleteData createDataKey="createTextSwatchConfigProductAttribute" stepKey="deleteAttribute"/>
32+
<actionGroup ref="logout" stepKey="logout"/>
33+
<!-- Reindex invalidated indices after product attribute has been created/deleted -->
34+
<actionGroup ref="CliRunReindexUsingCronJobsActionGroup" stepKey="reindexInvalidatedIndices"/>
35+
</after>
36+
37+
<!-- Open the new simple product page -->
38+
<actionGroup ref="AdminOpenNewProductFormPageActionGroup" stepKey="openNewProductPage"/>
39+
<!-- Check created attribute presents on the page -->
40+
<actionGroup ref="AssertAdminProductAttributeByCodeOnProductFormActionGroup" stepKey="checkTextSwatchConfigProductAttributeOnThePage">
41+
<argument name="productAttributeCode" value="$createTextSwatchConfigProductAttribute.attribute_code$"/>
42+
</actionGroup>
43+
</test>
44+
</tests>

app/code/Magento/Swatches/Test/Unit/Controller/Adminhtml/Product/Attribute/Plugin/SaveTest.php

Lines changed: 0 additions & 70 deletions
This file was deleted.

0 commit comments

Comments
 (0)