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

Fixed issue 27051: Saving an attribute with backend_type static #27369

Merged
merged 11 commits into from
Jun 25, 2020
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,10 @@ public function execute()
unset($data['apply_to']);
}

if ($model->getBackendType() == 'static' && !$model->getIsUserDefined()) {
$data['frontend_class'] = $model->getFrontendClass();
}

$model->addData($data);

if (!$attributeId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@

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

use Magento\Backend\Model\Session;
use Magento\Backend\Model\View\Result\Redirect as ResultRedirect;
use Magento\Catalog\Api\Data\ProductAttributeInterface;
use Magento\Catalog\Controller\Adminhtml\Product\Attribute\Save;
use Magento\Catalog\Helper\Product as ProductHelper;
use Magento\Catalog\Model\Product\Attribute\Frontend\Inputtype\Presentation;
use Magento\Catalog\Model\Product\AttributeSet\Build;
use Magento\Catalog\Model\Product\AttributeSet\BuildFactory;
use Magento\Catalog\Model\ResourceModel\Eav\AttributeFactory;
Expand All @@ -31,63 +33,64 @@

/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
* @SuppressWarnings(PHPMD.TooManyFields)
*/
class SaveTest extends AttributeTest
{
/**
* @var BuildFactory|MockObject
*/
protected $buildFactoryMock;
private $buildFactoryMock;

/**
* @var FilterManager|MockObject
*/
protected $filterManagerMock;
private $filterManagerMock;

/**
* @var ProductHelper|MockObject
*/
protected $productHelperMock;
private $productHelperMock;

/**
* @var AttributeFactory|MockObject
*/
protected $attributeFactoryMock;
private $attributeFactoryMock;

/**
* @var ValidatorFactory|MockObject
*/
protected $validatorFactoryMock;
private $validatorFactoryMock;

/**
* @var CollectionFactory|MockObject
*/
protected $groupCollectionFactoryMock;
private $groupCollectionFactoryMock;

/**
* @var LayoutFactory|MockObject
*/
protected $layoutFactoryMock;
private $layoutFactoryMock;

/**
* @var ResultRedirect|MockObject
*/
protected $redirectMock;
private $redirectMock;

/**
* @var AttributeSet|MockObject
* @var AttributeSetInterface|MockObject
*/
protected $attributeSetMock;
private $attributeSetMock;

/**
* @var Build|MockObject
*/
protected $builderMock;
private $builderMock;

/**
* @var InputTypeValidator|MockObject
*/
protected $inputTypeValidatorMock;
private $inputTypeValidatorMock;

/**
* @var FormData|MockObject
Expand All @@ -104,19 +107,34 @@ class SaveTest extends AttributeTest
*/
private $attributeCodeValidatorMock;

/**
* @var Presentation|MockObject
*/
private $presentationMock;

/**
* @var Session|MockObject
*/

private $sessionMock;

protected function setUp(): void
{
parent::setUp();
$this->filterManagerMock = $this->createMock(FilterManager::class);
$this->productHelperMock = $this->createMock(ProductHelper::class);
$this->attributeSetMock = $this->createMock(AttributeSetInterface::class);
$this->builderMock = $this->createMock(Build::class);
$this->inputTypeValidatorMock = $this->createMock(InputTypeValidator::class);
$this->formDataSerializerMock = $this->createMock(FormData::class);
$this->attributeCodeValidatorMock = $this->createMock(AttributeCodeValidator::class);
$this->presentationMock = $this->createMock(Presentation::class);
$this->sessionMock = $this->createMock(Session::class);
$this->layoutFactoryMock = $this->createMock(LayoutFactory::class);
$this->buildFactoryMock = $this->getMockBuilder(BuildFactory::class)
->setMethods(['create'])
->disableOriginalConstructor()
->getMock();
$this->filterManagerMock = $this->getMockBuilder(FilterManager::class)
->disableOriginalConstructor()
->getMock();
$this->productHelperMock = $this->getMockBuilder(ProductHelper::class)
->disableOriginalConstructor()
->getMock();
$this->attributeFactoryMock = $this->getMockBuilder(AttributeFactory::class)
->setMethods(['create'])
->disableOriginalConstructor()
Expand All @@ -129,32 +147,23 @@ protected function setUp(): void
->setMethods(['create'])
->disableOriginalConstructor()
->getMock();
$this->layoutFactoryMock = $this->getMockBuilder(LayoutFactory::class)
->disableOriginalConstructor()
->getMock();
$this->redirectMock = $this->getMockBuilder(ResultRedirect::class)
->setMethods(['setData', 'setPath'])
->disableOriginalConstructor()
->getMock();
$this->attributeSetMock = $this->getMockBuilder(AttributeSetInterface::class)
->disableOriginalConstructor()
->getMockForAbstractClass();
$this->builderMock = $this->getMockBuilder(Build::class)
->disableOriginalConstructor()
->getMock();
$this->inputTypeValidatorMock = $this->getMockBuilder(InputTypeValidator::class)
->disableOriginalConstructor()
->getMock();
$this->formDataSerializerMock = $this->getMockBuilder(FormData::class)
->disableOriginalConstructor()
->getMock();
$this->attributeCodeValidatorMock = $this->getMockBuilder(AttributeCodeValidator::class)
->disableOriginalConstructor()
->getMock();
$this->productAttributeMock = $this->getMockBuilder(ProductAttributeInterface::class)
->setMethods(['getId', 'get'])
->getMockForAbstractClass();

->setMethods(
[
'getId',
'get',
'getBackendTypeByInput',
'getDefaultValueByInput',
'getBackendType',
'getFrontendClass',
'addData',
'save'
]
)->getMockForAbstractClass();
$this->buildFactoryMock->expects($this->any())
->method('create')
->willReturn($this->builderMock);
Expand All @@ -167,7 +176,7 @@ protected function setUp(): void
}

/**
* {@inheritdoc}
* @inheritdoc
*/
protected function getModel()
{
Expand All @@ -184,7 +193,9 @@ protected function getModel()
'groupCollectionFactory' => $this->groupCollectionFactoryMock,
'layoutFactory' => $this->layoutFactoryMock,
'formDataSerializer' => $this->formDataSerializerMock,
'attributeCodeValidator' => $this->attributeCodeValidatorMock
'attributeCodeValidator' => $this->attributeCodeValidatorMock,
'presentation' => $this->presentationMock,
'_session' => $this->sessionMock
]);
}

Expand Down Expand Up @@ -214,6 +225,67 @@ public function testExecuteWithEmptyData()
$this->assertInstanceOf(ResultRedirect::class, $this->getModel()->execute());
}

public function testExecuteSaveFrontendClass()
{
$data = [
'frontend_input' => 'test_frontend_input',
];

$this->requestMock->expects($this->any())
->method('getParam')
->willReturnMap([
['isAjax', null, null],
['serialized_options', '[]', ''],
['set', null, 1],
['attribute_code', null, 'test_attribute_code'],
]);
$this->formDataSerializerMock
->expects($this->once())
->method('unserialize')
->with('')
->willReturn([]);
$this->requestMock->expects($this->once())
->method('getPostValue')
->willReturn($data);
$this->inputTypeValidatorMock->expects($this->any())
->method('isValid')
->with($data['frontend_input'])
->willReturn(true);
$this->presentationMock->expects($this->once())
->method('convertPresentationDataToInputType')
->willReturn($data);
$this->productHelperMock->expects($this->once())
->method('getAttributeSourceModelByInputType')
->with($data['frontend_input'])
->willReturn(null);
$this->productHelperMock->expects($this->once())
->method('getAttributeBackendModelByInputType')
->with($data['frontend_input'])
->willReturn(null);
$this->productAttributeMock->expects($this->once())
->method('getBackendTypeByInput')
->with($data['frontend_input'])
->willReturnSelf('test_backend_type');
$this->productAttributeMock->expects($this->once())
->method('getDefaultValueByInput')
->with($data['frontend_input'])
->willReturn(null);
$this->productAttributeMock->expects($this->once())
->method('getBackendType')
->willReturn('static');
$this->productAttributeMock->expects($this->once())
->method('getFrontendClass')
->willReturn('static');
$this->resultFactoryMock->expects($this->any())
->method('create')
->willReturn($this->redirectMock);
$this->redirectMock->expects($this->any())
->method('setPath')
->willReturnSelf();

$this->assertInstanceOf(ResultRedirect::class, $this->getModel()->execute());
}

public function testExecute()
{
$data = [
Expand Down