Skip to content

Commit

Permalink
#20526: Static test fix.
Browse files Browse the repository at this point in the history
  • Loading branch information
p-bystritsky committed Mar 28, 2019
1 parent 40683de commit 392cd1b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@

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

use Magento\Catalog\Controller\Adminhtml\Product\Attribute as AttributeAction;
use Magento\Eav\Model\Validator\Attribute\Code as AttributeCodeValidator;
use Magento\Framework\Serialize\Serializer\FormData;
use Magento\Framework\App\Action\HttpGetActionInterface;
use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\DataObject;
use Magento\Catalog\Controller\Adminhtml\Product\Attribute as AttributeAction;
use Magento\Framework\Serialize\Serializer\FormData;

/**
* Product attribute validate controller.
Expand Down Expand Up @@ -80,7 +80,8 @@ public function __construct(
$this->formDataSerializer = $formDataSerializer ?: ObjectManager::getInstance()
->get(FormData::class);
$this->attributeCodeValidator = $attributeCodeValidator ?: ObjectManager::getInstance()->get(
AttributeCodeValidator::class);
AttributeCodeValidator::class
);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion app/code/Magento/Eav/Model/Entity/Attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ class Attribute extends \Magento\Eav\Model\Entity\Attribute\AbstractAttribute im
* @param DateTimeFormatterInterface $dateTimeFormatter
* @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
* @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
* @param AttributeCodeValidator|null $attributeCodeValidator
* @param array $data
* @param AttributeCodeValidator|null $attributeCodeValidator
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
* @codeCoverageIgnore
*/
Expand Down
25 changes: 19 additions & 6 deletions app/code/Magento/Eav/Setup/EavSetup.php
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,7 @@ private function _getValue($array, $key, $default = null)
* @param array $attr
* @return $this
* @throws LocalizedException
* @throws \Zend_Validate_Exception
*/
public function addAttribute($entityTypeId, $code, array $attr)
{
Expand All @@ -809,12 +810,7 @@ public function addAttribute($entityTypeId, $code, array $attr)
$this->attributeMapper->map($attr, $entityTypeId)
);

$attributeCode = isset($data['attribute_code']) ? $data['attribute_code'] : '';
if (!$this->attributeCodeValidator->isValid($attributeCode)) {
$errorMessage = implode("\n", $this->attributeCodeValidator->getMessages());

throw new LocalizedException(__($errorMessage));
}
$this->validateAttributeCode($data);

$sortOrder = isset($attr['sort_order']) ? $attr['sort_order'] : null;
$attributeId = $this->getAttribute($entityTypeId, $code, 'attribute_id');
Expand Down Expand Up @@ -1535,4 +1531,21 @@ private function _insertAttributeAdditionalData($entityTypeId, array $data)

return $this;
}

/**
* Validate attribute code.
*
* @param array $data
* @throws LocalizedException
* @throws \Zend_Validate_Exception
*/
private function validateAttributeCode(array $data): void
{
$attributeCode = $data['attribute_code'] ?? '';
if (!$this->attributeCodeValidator->isValid($attributeCode)) {
$errorMessage = implode('\n', $this->attributeCodeValidator->getMessages());

throw new LocalizedException(__($errorMessage));
}
}
}

0 comments on commit 392cd1b

Please sign in to comment.