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

Fix #21025 - Missing "Apply To" when editing Product attributes #27625

Open
wants to merge 12 commits into
base: 2.4-develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
Expand Up @@ -6,13 +6,18 @@

namespace Magento\Catalog\Block\Adminhtml\Product\Attribute\Edit\Tab;

use Magento\Backend\Block\Template\Context;
use Magento\Backend\Block\Widget\Form\Generic;
use Magento\Catalog\Model\Attribute\Source\ApplyTo;
use Magento\Catalog\Model\ResourceModel\Eav\Attribute;
use Magento\Config\Model\Config\Source\Yesno;
use Magento\Eav\Block\Adminhtml\Attribute\PropertyLocker;
use Magento\Eav\Helper\Data;
use Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Data\FormFactory;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Registry;
use Magento\Framework\Stdlib\DateTime;

/**
Expand All @@ -25,11 +30,10 @@
class Advanced extends Generic
{
/**
* Eav data
*
* @var Data
*/
protected $_eavData = null;
protected $_eavData;

/**
* @var Yesno
Expand All @@ -47,27 +51,38 @@ class Advanced extends Generic
private $propertyLocker;

/**
* @param \Magento\Backend\Block\Template\Context $context
* @param \Magento\Framework\Registry $registry
* @param \Magento\Framework\Data\FormFactory $formFactory
* @var ApplyTo
*/
private $applyTo;

/**
* @param Context $context
* @param Registry $registry
* @param FormFactory $formFactory
* @param Yesno $yesNo
* @param Data $eavData
* @param array $disableScopeChangeList
* @param array $data
* @param PropertyLocker|null $propertyLocker
* @param ApplyTo|null $applyTo
*/
public function __construct(
\Magento\Backend\Block\Template\Context $context,
\Magento\Framework\Registry $registry,
\Magento\Framework\Data\FormFactory $formFactory,
Context $context,
Registry $registry,
FormFactory $formFactory,
Yesno $yesNo,
Data $eavData,
array $disableScopeChangeList = [],
array $data = []
array $data = [],
?PropertyLocker $propertyLocker = null,
?ApplyTo $applyTo = null
) {
$this->_yesNo = $yesNo;
$this->_eavData = $eavData;
$this->disableScopeChangeList = $disableScopeChangeList;
parent::__construct($context, $registry, $formFactory, $data);
$this->propertyLocker = $propertyLocker ?? ObjectManager::getInstance()->get(PropertyLocker::class);
$this->applyTo = $applyTo ?? ObjectManager::getInstance()->get(ApplyTo::class);
}

/**
Expand Down Expand Up @@ -230,22 +245,35 @@ protected function _prepareForm()
]
);

$fieldset->addField(
'apply_to',
'multiselect',
[
'name' => 'apply_to',
'label' => __('Apply To'),
'title' => __('Apply To'),
'values' => $this->applyTo->toOptionArray(),
'value' => $attributeObject->getApplyTo()
]
);

if ($attributeObject->getId()) {
$form->getElement('attribute_code')->setDisabled(1);
if (!$attributeObject->getIsUserDefined()) {
$form->getElement('is_unique')->setDisabled(1);
$form->getElement('apply_to')->setDisabled(1);
}
}

$scopes = [
\Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE => __('Store View'),
\Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_WEBSITE => __('Website'),
\Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL => __('Global'),
ScopedAttributeInterface::SCOPE_STORE => __('Store View'),
ScopedAttributeInterface::SCOPE_WEBSITE => __('Website'),
ScopedAttributeInterface::SCOPE_GLOBAL => __('Global'),
];

if ($attributeObject->getAttributeCode() == 'status' || $attributeObject->getAttributeCode() == 'tax_class_id'
) {
unset($scopes[\Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE]);
unset($scopes[ScopedAttributeInterface::SCOPE_STORE]);
}

$fieldset->addField(
Expand All @@ -266,7 +294,7 @@ protected function _prepareForm()
$form->getElement('is_global')->setDisabled(1);
}
$this->setForm($form);
$this->getPropertyLocker()->lock($form);
$this->propertyLocker->lock($form);
return $this;
}

Expand All @@ -291,19 +319,6 @@ private function getAttributeObject()
return $this->_coreRegistry->registry('entity_attribute');
}

/**
* Get property locker
*
* @return PropertyLocker
*/
private function getPropertyLocker()
{
if (null === $this->propertyLocker) {
$this->propertyLocker = ObjectManager::getInstance()->get(PropertyLocker::class);
}
return $this->propertyLocker;
}

/**
* Get localized date default value
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
<?php
/**
*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Catalog\Controller\Adminhtml\Product\Attribute;

use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
use Magento\Backend\App\Action\Context;
use Magento\Backend\Model\View\Result\Redirect;
use Magento\Catalog\Api\Data\ProductAttributeInterface;
use Magento\Catalog\Controller\Adminhtml\Product\Attribute;
use Magento\Catalog\Helper\Product;
use Magento\Catalog\Model\Product\Attribute\Frontend\Inputtype\Presentation;
use Magento\Framework\Serialize\Serializer\FormData;
use Magento\Catalog\Model\Product\AttributeSet\BuildFactory;
use Magento\Catalog\Model\ResourceModel\Eav\AttributeFactory;
use Magento\Eav\Model\Adminhtml\System\Config\Source\Inputtype\Validator;
use Magento\Eav\Model\Adminhtml\System\Config\Source\Inputtype\ValidatorFactory;
use Magento\Eav\Model\Entity\Attribute\Set;
use Magento\Eav\Model\ResourceModel\Entity\Attribute\Group\CollectionFactory;
use Magento\Framework\App\Action\HttpPostActionInterface;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Cache\FrontendInterface;
use Magento\Framework\Controller\Result\Json;
Expand All @@ -29,6 +28,7 @@
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Filter\FilterManager;
use Magento\Framework\Registry;
use Magento\Framework\Serialize\Serializer\FormData;
use Magento\Framework\View\LayoutFactory;
use Magento\Framework\View\Result\PageFactory;

Expand Down Expand Up @@ -261,6 +261,8 @@ public function execute()
if (!$model->getIsUserDefined() && $model->getId()) {
// Unset attribute field for system attributes
unset($data['apply_to']);
} elseif (!isset($data['apply_to'])) {
$data['apply_to'] = [];
}

if ($model->getBackendType() == 'static' && !$model->getIsUserDefined()) {
Expand Down
45 changes: 45 additions & 0 deletions app/code/Magento/Catalog/Model/Attribute/Source/ApplyTo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Catalog\Model\Attribute\Source;

use Magento\Catalog\Api\ProductTypeListInterface;
use Magento\Framework\Data\OptionSourceInterface;

class ApplyTo implements OptionSourceInterface
{
/**
* @var ProductTypeListInterface
*/
private $productTypeList;

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

/**
* @inheritDoc
*/
public function toOptionArray()
{
$result = [];

foreach ($this->productTypeList->getProductTypes() as $productType) {
$result[] = [
'value' => $productType->getName(),
'label' => $productType->getLabel()
];
}

return $result;
}
}
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="AssertAdminTextAttributeAbsentOnProductFormActionGroup">
<annotations>
<description>Assert provided text Attribute is absent on the Product page.</description>
</annotations>
<arguments>
<argument name="attributeCode" type="string"/>
</arguments>

<dontSeeElement selector="{{AdminProductAttributesSection.attributeDropdownByCode(attributeCode)}}" stepKey="assertAttributeIsAbsent"/>
</actionGroup>
</actionGroups>
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="AssertAdminTextAttributePresentOnProductFormActionGroup">
<annotations>
<description>Assert provided text Attribute is present on the Product page.</description>
</annotations>
<arguments>
<argument name="attributeCode" type="string"/>
</arguments>

<seeElement selector="{{AdminProductAttributesSection.attributeTextInputByCode(attributeCode)}}" stepKey="assertAttributeIsPresent"/>
</actionGroup>
</actionGroups>
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?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="CreateProductAttributeForSimpleProductActionGroup" extends="CreateProductAttributeActionGroup" insertAfter="checkRequired">
<annotations>
<description>EXTENDS: createProductAttribute. Fills in the Apply To with "Simple" value.</description>
</annotations>

<click selector="{{AdvancedAttributePropertiesSection.AdvancedAttributePropertiesSectionToggle}}" stepKey="openAdvancedProperties"/>
<fillField stepKey="fillDefaultValue" selector="{{AdvancedAttributePropertiesSection.DefaultValueText}}" userInput="{{attribute.default_value}}"/>
<click selector="{{AdvancedAttributePropertiesSection.ApplyToSimple}}" stepKey="applyToSimpleProducts"/>
</actionGroup>
</actionGroups>
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,11 @@
<element name="UseInProductListing" type="select" selector="#used_in_product_listing"/>
<element name="UseInSearch" type="select" selector="#is_searchable"/>
<element name="VisibleInAdvancedSearch" type="select" selector="#is_visible_in_advanced_search"/>
<element name="ApplyToSimple" type="multiselect" selector="//select[@id='apply_to']/option[@value='simple']"/>
<element name="ApplyToVirtual" type="multiselect" selector="//select[@id='apply_to']/option[@value='virtual']"/>
<element name="ApplyToDownloadable" type="multiselect" selector="//select[@id='apply_to']/option[@value='downloadable']"/>
<element name="ApplyToConfigurable" type="multiselect" selector="//select[@id='apply_to']/option[@value='configurable']"/>
<element name="ApplyToGrouped" type="multiselect" selector="//select[@id='apply_to']/option[@value='grouped']"/>
<element name="ApplyToBundle" type="multiselect" selector="//select[@id='apply_to']/option[@value='bundle']"/>
</section>
</sections>
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?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="CreateProductAttributeEntityApplyToFieldTest">
<annotations>
<features value="Catalog"/>
<stories value="Create Product Attributes"/>
<title value="Create product attribute entity apply to field test"/>
<description value="Check if 'Apply to' field on Product Attribute creation form works properly"/>
<severity value="AVERAGE"/>
<group value="Catalog"/>
</annotations>

<before>
<createData entity="_defaultCategory" stepKey="createCategory"/>
<createData entity="SimpleProduct" stepKey="createSimpleProduct">
<requiredEntity createDataKey="createCategory"/>
</createData>
<createData entity="VirtualProduct" stepKey="createVirtualProduct">
<requiredEntity createDataKey="createCategory"/>
</createData>
<actionGroup ref="AdminLoginActionGroup" stepKey="loginAsAdmin"/>
</before>
<after>
<deleteData createDataKey="createSimpleProduct" stepKey="deleteSimpleProduct"/>
<deleteData createDataKey="createVirtualProduct" stepKey="deleteVirtualProduct"/>
<deleteData createDataKey="createCategory" stepKey="deleteCategory"/>
<actionGroup ref="DeleteAttributeSetByLabelActionGroup" stepKey="deleteCustomAttributeSet">
<argument name="label" value="{{CatalogAttributeSet.attribute_set_name}}"/>
</actionGroup>
<actionGroup ref="NavigateToEditProductAttributeActionGroup" stepKey="goToEditPage">
<argument name="ProductAttribute" value="{{textProductAttribute.attribute_code}}"/>
</actionGroup>
<click stepKey="clickDelete" selector="{{AttributePropertiesSection.DeleteAttribute}}"/>
<click stepKey="clickOk" selector="{{AttributeDeleteModalSection.confirm}}"/>
<waitForPageLoad stepKey="waitForDeletion"/>
<actionGroup ref="AdminLogoutActionGroup" stepKey="logout"/>
</after>

<!--Navigate to Stores > Attributes > Product.-->
<actionGroup ref="AdminOpenProductAttributePageActionGroup" stepKey="goToProductAttributes"/>

<!-- Create new Product Attribute for Simple products -->
<actionGroup ref="CreateProductAttributeForSimpleProductActionGroup" stepKey="createAttribute">
<argument name="attribute" value="textProductAttribute"/>
</actionGroup>

<!-- Add created attribute to Attribute Set -->
<actionGroup ref="AdminAddUnassignedAttributeToGroupActionGroup" stepKey="createCustomAttributeSet">
<argument name="label" value="{{CatalogAttributeSet.attribute_set_name}}"/>
<argument name="secondOption" value="{{textProductAttribute.attribute_code}}"/>
</actionGroup>

<!-- Check if Custom Attribute is applied for Simple product -->
<actionGroup ref="AdminProductPageOpenByIdActionGroup" stepKey="openSimpleProductPage">
<argument name="productId" value="$$createSimpleProduct.id$$"/>
</actionGroup>
<actionGroup ref="AdminProductPageSelectAttributeSetActionGroup" stepKey="selectAttributeSetSimple">
<argument name="attributeSetName" value="{{CatalogAttributeSet.attribute_set_name}}"/>
</actionGroup>
<actionGroup ref="AssertAdminTextAttributePresentOnProductFormActionGroup" stepKey="checkCustomAttributeSimpleProduct">
<argument name="attributeCode" value="{{textProductAttribute.attribute_code}}"/>
</actionGroup>

<!-- Check if Custom Attribute is not applied for Virtual product -->
<actionGroup ref="AdminProductPageOpenByIdActionGroup" stepKey="openVirtualProductPage">
<argument name="productId" value="$$createVirtualProduct.id$$"/>
</actionGroup>
<actionGroup ref="AdminProductPageSelectAttributeSetActionGroup" stepKey="selectAttributeSetVirtual">
<argument name="attributeSetName" value="{{CatalogAttributeSet.attribute_set_name}}"/>
</actionGroup>
<actionGroup ref="AssertAdminTextAttributeAbsentOnProductFormActionGroup" stepKey="checkCustomAttributeVirtualProduct">
<argument name="attributeCode" value="{{textProductAttribute.attribute_code}}"/>
</actionGroup>
</test>
</tests>
Loading