Skip to content
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
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,33 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

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

use Magento\Catalog\Controller\Adminhtml\Product\Attribute;
use Magento\Framework\App\RequestInterface;
use Magento\Swatches\Model\Swatch;
use Magento\Swatches\Model\ConvertSwatchAttributeFrontendInput;

/**
* Plugin for product attribute save controller.
*/
class Save
{
/**
* @var ConvertSwatchAttributeFrontendInput
*/
private $convertSwatchAttributeFrontendInput;

/**
* @param ConvertSwatchAttributeFrontendInput $convertSwatchAttributeFrontendInput
*/
public function __construct(
ConvertSwatchAttributeFrontendInput $convertSwatchAttributeFrontendInput
) {
$this->convertSwatchAttributeFrontendInput = $convertSwatchAttributeFrontendInput;
}

/**
* Performs the conversion of the frontend input value.
*
Expand All @@ -23,30 +38,12 @@ class Save
* @return array
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function beforeDispatch(Attribute\Save $subject, RequestInterface $request)
public function beforeDispatch(Attribute\Save $subject, RequestInterface $request): array
{
$data = $request->getPostValue();
$data = $this->convertSwatchAttributeFrontendInput->execute($data);
$request->setPostValue($data);

if (isset($data['frontend_input'])) {
switch ($data['frontend_input']) {
case 'swatch_visual':
$data[Swatch::SWATCH_INPUT_TYPE_KEY] = Swatch::SWATCH_INPUT_TYPE_VISUAL;
$data['frontend_input'] = 'select';
$request->setPostValue($data);
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';
$request->setPostValue($data);
break;
case 'select':
$data[Swatch::SWATCH_INPUT_TYPE_KEY] = Swatch::SWATCH_INPUT_TYPE_DROPDOWN;
$data['frontend_input'] = 'select';
$request->setPostValue($data);
break;
}
}
return [$request];
}
}
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
Copy link
Contributor

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

Copy link
Contributor Author

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.

{
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;
}
}
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];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,7 @@
<data key="default_label" unique="suffix">TextSwatchAttr</data>
<data key="attribute_code" unique="suffix">text_swatch_attr</data>
</entity>
<entity name="textSwatchProductAttribute" type="ProductAttribute" extends="productDropDownAttribute">
<data key="frontend_input">swatch_text</data>
</entity>
</entities>
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>

This file was deleted.

Loading