Skip to content
Merged
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
92 changes: 73 additions & 19 deletions app/code/Magento/Customer/Model/AttributeMetadataConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,36 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Customer\Model;

use Magento\Customer\Api\Data\OptionInterfaceFactory;
use Magento\Customer\Api\Data\ValidationRuleInterfaceFactory;
use Magento\Customer\Api\Data\AttributeMetadataInterfaceFactory;
use Magento\Eav\Api\Data\AttributeDefaultValueInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\ObjectManager;

/**
* Converter for AttributeMetadata
*/
class AttributeMetadataConverter
{
/**
* Attribute Code get options from system config
*
* @var array
*/
private const ATTRIBUTE_CODE_LIST_FROM_SYSTEM_CONFIG = ['prefix', 'suffix'];

/**
* XML Path to get address config
*
* @var string
*/
private const XML_CUSTOMER_ADDRESS = 'customer/address/';

/**
* @var OptionInterfaceFactory
*/
Expand All @@ -35,24 +53,32 @@ class AttributeMetadataConverter
*/
protected $dataObjectHelper;

/**
* @var ScopeConfigInterface
*/
private $scopeConfig;

/**
* Initialize the Converter
*
* @param OptionInterfaceFactory $optionFactory
* @param ValidationRuleInterfaceFactory $validationRuleFactory
* @param AttributeMetadataInterfaceFactory $attributeMetadataFactory
* @param \Magento\Framework\Api\DataObjectHelper $dataObjectHelper
* @param ScopeConfigInterface $scopeConfig
*/
public function __construct(
OptionInterfaceFactory $optionFactory,
ValidationRuleInterfaceFactory $validationRuleFactory,
AttributeMetadataInterfaceFactory $attributeMetadataFactory,
\Magento\Framework\Api\DataObjectHelper $dataObjectHelper
\Magento\Framework\Api\DataObjectHelper $dataObjectHelper,
ScopeConfigInterface $scopeConfig = null
) {
$this->optionFactory = $optionFactory;
$this->validationRuleFactory = $validationRuleFactory;
$this->attributeMetadataFactory = $attributeMetadataFactory;
$this->dataObjectHelper = $dataObjectHelper;
$this->scopeConfig = $scopeConfig ?? ObjectManager::getInstance()->get(ScopeConfigInterface::class);
}

/**
Expand All @@ -64,28 +90,34 @@ public function __construct(
public function createMetadataAttribute($attribute)
{
$options = [];
if ($attribute->usesSource()) {
foreach ($attribute->getSource()->getAllOptions() as $option) {
$optionDataObject = $this->optionFactory->create();
if (!is_array($option['value'])) {
$optionDataObject->setValue($option['value']);
} else {
$optionArray = [];
foreach ($option['value'] as $optionArrayValues) {
$optionObject = $this->optionFactory->create();
$this->dataObjectHelper->populateWithArray(
$optionObject,
$optionArrayValues,
\Magento\Customer\Api\Data\OptionInterface::class
);
$optionArray[] = $optionObject;

if (in_array($attribute->getAttributeCode(), self::ATTRIBUTE_CODE_LIST_FROM_SYSTEM_CONFIG)) {
$options = $this->getOptionFromConfig($attribute->getAttributeCode());
} else {
if ($attribute->usesSource()) {
foreach ($attribute->getSource()->getAllOptions() as $option) {
$optionDataObject = $this->optionFactory->create();
if (!is_array($option['value'])) {
$optionDataObject->setValue($option['value']);
} else {
$optionArray = [];
foreach ($option['value'] as $optionArrayValues) {
$optionObject = $this->optionFactory->create();
$this->dataObjectHelper->populateWithArray(
$optionObject,
$optionArrayValues,
\Magento\Customer\Api\Data\OptionInterface::class
);
$optionArray[] = $optionObject;
}
$optionDataObject->setOptions($optionArray);
}
$optionDataObject->setOptions($optionArray);
$optionDataObject->setLabel($option['label']);
$options[] = $optionDataObject;
}
$optionDataObject->setLabel($option['label']);
$options[] = $optionDataObject;
}
}

$validationRules = [];
foreach ((array)$attribute->getValidateRules() as $name => $value) {
$validationRule = $this->validationRuleFactory->create()
Expand Down Expand Up @@ -122,4 +154,26 @@ public function createMetadataAttribute($attribute)
->setIsFilterableInGrid($attribute->getIsFilterableInGrid())
->setIsSearchableInGrid($attribute->getIsSearchableInGrid());
}

/**
* Get option from System Config instead of Use Source (Prefix, Suffix)
*
* @param string $attributeCode
* @return \Magento\Customer\Api\Data\OptionInterface[]
*/
private function getOptionFromConfig($attributeCode)
{
$result = [];
$value = $this->scopeConfig->getValue(self::XML_CUSTOMER_ADDRESS . $attributeCode . '_options');
if ($value) {
$optionArray = explode(';', $value);
foreach ($optionArray as $value) {
$optionObject = $this->optionFactory->create();
$optionObject->setLabel($value);
$optionObject->setValue($value);
$result[] = $optionObject;
}
}
return $result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@

namespace Magento\Customer\Api;

use Magento\Config\Model\ResourceModel\Config;
use Magento\Customer\Api\Data\AddressInterface as Address;
use Magento\Customer\Model\Data\AttributeMetadata;
use Magento\Framework\App\Config\ReinitableConfigInterface;
use Magento\TestFramework\ObjectManager;
use Magento\TestFramework\TestCase\WebapiAbstract;

/**
Expand All @@ -19,15 +22,40 @@ class AddressMetadataTest extends WebapiAbstract
const SERVICE_VERSION = "V1";
const RESOURCE_PATH = "/V1/attributeMetadata/customerAddress";

/**
* @var Config $config
*/
private $resourceConfig;

/**
* @var ReinitableConfigInterface
*/
private $reinitConfig;

/**
* @inheritdoc
*/
protected function setUp()
{
parent::setUp();

$objectManager = ObjectManager::getInstance();
$this->resourceConfig = $objectManager->get(Config::class);
$this->reinitConfig = $objectManager->get(ReinitableConfigInterface::class);
}

/**
* Test retrieval of attribute metadata for the address entity type.
*
* @param string $attributeCode The attribute code of the requested metadata.
* @param array $expectedMetadata Expected entity metadata for the attribute code.
* @dataProvider getAttributeMetadataDataProvider
* @magentoDbIsolation disabled
*/
public function testGetAttributeMetadata($attributeCode, $expectedMetadata)
public function testGetAttributeMetadata($attributeCode, $configOptions, $expectedMetadata)
{
$this->initConfig($configOptions);

$serviceInfo = [
'rest' => [
'resourcePath' => self::RESOURCE_PATH . "/attribute/$attributeCode",
Expand All @@ -54,12 +82,14 @@ public function testGetAttributeMetadata($attributeCode, $expectedMetadata)
* Data provider for testGetAttributeMetadata.
*
* @return array
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
public function getAttributeMetadataDataProvider()
{
return [
Address::POSTCODE => [
Address::POSTCODE,
[],
[
AttributeMetadata::FRONTEND_INPUT => 'text',
AttributeMetadata::INPUT_FILTER => '',
Expand All @@ -83,7 +113,85 @@ public function getAttributeMetadataDataProvider()
AttributeMetadata::IS_SEARCHABLE_IN_GRID => true,
AttributeMetadata::ATTRIBUTE_CODE => 'postcode',
],
]
],
'prefix' => [
'prefix',
[
['path' => 'customer/address/prefix_show', 'value' => 'opt'],
['path' => 'customer/address/prefix_options', 'value' => 'prefA;prefB']
],
[
AttributeMetadata::FRONTEND_INPUT => 'text',
AttributeMetadata::INPUT_FILTER => '',
AttributeMetadata::STORE_LABEL => 'Name Prefix',
AttributeMetadata::MULTILINE_COUNT => 0,
AttributeMetadata::VALIDATION_RULES => [],
AttributeMetadata::VISIBLE => false,
AttributeMetadata::REQUIRED => false,
AttributeMetadata::DATA_MODEL => '',
AttributeMetadata::OPTIONS => [
[
'label' => 'prefA',
'value' => 'prefA',
],
[
'label' => 'prefB',
'value' => 'prefB',
],
],
AttributeMetadata::FRONTEND_CLASS => '',
AttributeMetadata::USER_DEFINED => false,
AttributeMetadata::SORT_ORDER => 10,
AttributeMetadata::FRONTEND_LABEL => 'Name Prefix',
AttributeMetadata::NOTE => '',
AttributeMetadata::SYSTEM => false,
AttributeMetadata::BACKEND_TYPE => 'static',
AttributeMetadata::IS_USED_IN_GRID => false,
AttributeMetadata::IS_VISIBLE_IN_GRID => false,
AttributeMetadata::IS_FILTERABLE_IN_GRID => false,
AttributeMetadata::IS_SEARCHABLE_IN_GRID => false,
AttributeMetadata::ATTRIBUTE_CODE => 'prefix',
],
],
'suffix' => [
'suffix',
[
['path' => 'customer/address/suffix_show', 'value' => 'opt'],
['path' => 'customer/address/suffix_options', 'value' => 'suffA;suffB']
],
[
AttributeMetadata::FRONTEND_INPUT => 'text',
AttributeMetadata::INPUT_FILTER => '',
AttributeMetadata::STORE_LABEL => 'Name Suffix',
AttributeMetadata::MULTILINE_COUNT => 0,
AttributeMetadata::VALIDATION_RULES => [],
AttributeMetadata::VISIBLE => false,
AttributeMetadata::REQUIRED => false,
AttributeMetadata::DATA_MODEL => '',
AttributeMetadata::OPTIONS => [
[
'label' => 'suffA',
'value' => 'suffA',
],
[
'label' => 'suffB',
'value' => 'suffB',
],
],
AttributeMetadata::FRONTEND_CLASS => '',
AttributeMetadata::USER_DEFINED => false,
AttributeMetadata::SORT_ORDER => 50,
AttributeMetadata::FRONTEND_LABEL => 'Name Suffix',
AttributeMetadata::NOTE => '',
AttributeMetadata::SYSTEM => false,
AttributeMetadata::BACKEND_TYPE => 'static',
AttributeMetadata::IS_USED_IN_GRID => false,
AttributeMetadata::IS_VISIBLE_IN_GRID => false,
AttributeMetadata::IS_FILTERABLE_IN_GRID => false,
AttributeMetadata::IS_SEARCHABLE_IN_GRID => false,
AttributeMetadata::ATTRIBUTE_CODE => 'suffix',
],
],
];
}

Expand All @@ -106,7 +214,7 @@ public function testGetAllAttributesMetadata()

$attributeMetadata = $this->_webApiCall($serviceInfo);
$this->assertCount(19, $attributeMetadata);
$postcode = $this->getAttributeMetadataDataProvider()[Address::POSTCODE][1];
$postcode = $this->getAttributeMetadataDataProvider()[Address::POSTCODE][2];
$validationResult = $this->checkMultipleAttributesValidationRules($postcode, $attributeMetadata);
list($postcode, $attributeMetadata) = $validationResult;
$this->assertContains($postcode, $attributeMetadata);
Expand Down Expand Up @@ -187,7 +295,7 @@ public function getAttributesDataProvider()
return [
[
'customer_address_edit',
$attributeMetadata[Address::POSTCODE][1],
$attributeMetadata[Address::POSTCODE][2],
]
];
}
Expand All @@ -200,6 +308,7 @@ public function getAttributesDataProvider()
* @param array $actualResult
* @return array
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* phpcs:disable Generic.Metrics.NestingLevel
*/
public function checkValidationRules($expectedResult, $actualResult)
{
Expand Down Expand Up @@ -235,6 +344,7 @@ public function checkValidationRules($expectedResult, $actualResult)
}
return [$expectedResult, $actualResult];
}
//phpcs:enable

/**
* Check specific attribute validation rules in set of multiple attributes
Expand Down Expand Up @@ -277,4 +387,19 @@ public static function tearDownAfterClass()
$attribute->delete();
}
}

/**
* Set core config data.
*
* @param $configOptions
*/
private function initConfig(array $configOptions): void
{
if ($configOptions) {
foreach ($configOptions as $option) {
$this->resourceConfig->saveConfig($option['path'], $option['value']);
}
}
$this->reinitConfig->reinit();
}
}