-
Notifications
You must be signed in to change notification settings - Fork 9.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
26827 Added improvements to product attribute repository (save method) #27191
Merged
magento-engcom-team
merged 2 commits into
magento:2.4-develop
from
sergiy-v:26827-product-attribute-repository-swatches-improvements
Mar 19, 2020
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
21 changes: 21 additions & 0 deletions
21
...talog/Test/Mftf/ActionGroup/AssertAdminProductAttributeByCodeOnProductFormActionGroup.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
--> | ||
|
||
<actionGroups xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="urn:magento:mftf:Test/etc/actionGroupSchema.xsd"> | ||
<actionGroup name="AssertAdminProductAttributeByCodeOnProductFormActionGroup"> | ||
<annotations> | ||
<description>Requires the navigation to the Product page. Provided dropdown attribute presents on the page.</description> | ||
</annotations> | ||
<arguments> | ||
<argument name="productAttributeCode" type="string" defaultValue="{{textSwatchProductAttribute.attribute_code}}"/> | ||
</arguments> | ||
|
||
<seeElement selector="{{AdminProductAttributesSection.attributeDropdownByCode(productAttributeCode)}}" stepKey="assertAttributeIsPresentOnForm"/> | ||
</actionGroup> | ||
</actionGroups> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
app/code/Magento/Swatches/Model/ConvertSwatchAttributeFrontendInput.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\Swatches\Model; | ||
|
||
/** | ||
* Performs the conversion of the frontend input value for attribute data | ||
*/ | ||
class ConvertSwatchAttributeFrontendInput | ||
{ | ||
/** | ||
* Performs the conversion of the frontend input value for attribute data | ||
* | ||
* @param array|null $data | ||
* | ||
* @return array|null | ||
*/ | ||
public function execute(?array $data): ?array | ||
{ | ||
if (!isset($data['frontend_input'])) { | ||
return $data; | ||
} | ||
|
||
switch ($data['frontend_input']) { | ||
case 'swatch_visual': | ||
$data[Swatch::SWATCH_INPUT_TYPE_KEY] = Swatch::SWATCH_INPUT_TYPE_VISUAL; | ||
$data['frontend_input'] = 'select'; | ||
break; | ||
case 'swatch_text': | ||
$data[Swatch::SWATCH_INPUT_TYPE_KEY] = Swatch::SWATCH_INPUT_TYPE_TEXT; | ||
$data['use_product_image_for_swatch'] = 0; | ||
$data['frontend_input'] = 'select'; | ||
break; | ||
case 'select': | ||
$data[Swatch::SWATCH_INPUT_TYPE_KEY] = Swatch::SWATCH_INPUT_TYPE_DROPDOWN; | ||
$data['frontend_input'] = 'select'; | ||
break; | ||
} | ||
|
||
return $data; | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
app/code/Magento/Swatches/Plugin/Catalog/Model/Product/Attribute/RepositoryPlugin.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\Swatches\Plugin\Catalog\Model\Product\Attribute; | ||
|
||
use Magento\Catalog\Api\Data\ProductAttributeInterface; | ||
use Magento\Catalog\Model\Product\Attribute\Repository as ProductAttributeRepository; | ||
use Magento\Swatches\Model\ConvertSwatchAttributeFrontendInput; | ||
|
||
/** | ||
* Plugin for product attribute repository | ||
*/ | ||
class RepositoryPlugin | ||
{ | ||
/** | ||
* @var ConvertSwatchAttributeFrontendInput | ||
*/ | ||
private $convertSwatchAttributeFrontendInput; | ||
|
||
/** | ||
* @param ConvertSwatchAttributeFrontendInput $convertSwatchAttributeFrontendInput | ||
*/ | ||
public function __construct( | ||
ConvertSwatchAttributeFrontendInput $convertSwatchAttributeFrontendInput | ||
) { | ||
$this->convertSwatchAttributeFrontendInput = $convertSwatchAttributeFrontendInput; | ||
} | ||
|
||
/** | ||
* Performs the conversion of the frontend input value. | ||
* | ||
* @param ProductAttributeRepository $subject | ||
* @param ProductAttributeInterface $attribute | ||
* @return array | ||
* @SuppressWarnings(PHPMD.UnusedFormalParameter) | ||
*/ | ||
public function beforeSave( | ||
ProductAttributeRepository $subject, | ||
ProductAttributeInterface $attribute | ||
): array { | ||
$data = $attribute->getData(); | ||
$data = $this->convertSwatchAttributeFrontendInput->execute($data); | ||
$attribute->setData($data); | ||
|
||
return [$attribute]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
app/code/Magento/Swatches/Test/Mftf/Test/AdminCheckTextSwatchAttributeAddedViaApiTest.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
--> | ||
|
||
<tests xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="urn:magento:mftf:Test/etc/testSchema.xsd"> | ||
<test name="AdminCheckTextSwatchAttributeAddedViaApiTest"> | ||
<annotations> | ||
<stories value="Add Swatch Text Product Attribute via API"/> | ||
<title value="Add Swatch Text Product Attribute via API"/> | ||
<description value="Login as admin, create swatch text product attribute.Go to New Product page, | ||
check the created attribute is available on the page."/> | ||
<group value="swatches"/> | ||
</annotations> | ||
<before> | ||
<!-- Login as Admin --> | ||
<actionGroup ref="LoginAsAdmin" stepKey="loginToAdminPanel"/> | ||
<!-- Create an attribute with two options to be used in the first child product --> | ||
<createData entity="textSwatchProductAttribute" stepKey="createTextSwatchConfigProductAttribute"/> | ||
<!-- Add the attribute just created to default attribute set --> | ||
<createData entity="AddToDefaultSet" stepKey="createConfigAddToAttributeSet"> | ||
<requiredEntity createDataKey="createTextSwatchConfigProductAttribute"/> | ||
</createData> | ||
</before> | ||
<after> | ||
<!-- Delete Created Data --> | ||
<deleteData createDataKey="createTextSwatchConfigProductAttribute" stepKey="deleteAttribute"/> | ||
<actionGroup ref="logout" stepKey="logout"/> | ||
<!-- Reindex invalidated indices after product attribute has been created/deleted --> | ||
<actionGroup ref="CliRunReindexUsingCronJobsActionGroup" stepKey="reindexInvalidatedIndices"/> | ||
</after> | ||
|
||
<!-- Open the new simple product page --> | ||
<actionGroup ref="AdminOpenNewProductFormPageActionGroup" stepKey="openNewProductPage"/> | ||
<!-- Check created attribute presents on the page --> | ||
<actionGroup ref="AssertAdminProductAttributeByCodeOnProductFormActionGroup" stepKey="checkTextSwatchConfigProductAttributeOnThePage"> | ||
<argument name="productAttributeCode" value="$createTextSwatchConfigProductAttribute.attribute_code$"/> | ||
</actionGroup> | ||
</test> | ||
</tests> |
70 changes: 0 additions & 70 deletions
70
...ode/Magento/Swatches/Test/Unit/Controller/Adminhtml/Product/Attribute/Plugin/SaveTest.php
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking about the implementation for some time. Must admit that the service should not receive null as the argument since it breaks the implementation consistency. We shall try to be as more specific as possible in the types of accepting/returning in order to make the logic transparent.
From my point of view, if don't have the
$data
, we should not call the service. On the other hand, the service should know what's the structure of the data and then make the decision regarding the conversion process. So, let's leave it as it is.Thank you
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your thoughts. Just to clarify, in this case this part of the logic was copied from existing logic with unit test which covers the case with NULL for some reason, that's why I decided to leave it as it was.