Skip to content

add check for attribute option values #25421

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

Merged
merged 5 commits into from
Dec 3, 2019
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
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ private function checkUniqueOption(DataObject $response, array $options = null)
private function checkEmptyOption(DataObject $response, array $optionsForCheck = null)
{
foreach ($optionsForCheck as $optionValues) {
if (isset($optionValues[0]) && $optionValues[0] == '') {
if (isset($optionValues[0]) && trim($optionValues[0]) == '') {
$this->setMessageToResponse($response, [__("The value of Admin scope can't be empty.")]);
$response->setError(true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,18 +136,22 @@ public function testExecute()
$serializedOptions = '{"key":"value"}';
$this->requestMock->expects($this->any())
->method('getParam')
->willReturnMap([
->willReturnMap(
[
['frontend_label', null, 'test_frontend_label'],
['attribute_code', null, 'test_attribute_code'],
['new_attribute_set_name', null, 'test_attribute_set_name'],
['serialized_options', '[]', $serializedOptions],
]);
]
);
$this->objectManagerMock->expects($this->exactly(2))
->method('create')
->willReturnMap([
->willReturnMap(
[
[\Magento\Catalog\Model\ResourceModel\Eav\Attribute::class, [], $this->attributeMock],
[\Magento\Eav\Model\Entity\Attribute\Set::class, [], $this->attributeSetMock]
]);
]
);
$this->attributeMock->expects($this->once())
->method('loadByCode')
->willReturnSelf();
Expand Down Expand Up @@ -182,23 +186,25 @@ public function testExecute()

/**
* @dataProvider provideUniqueData
* @param array $options
* @param boolean $isError
* @throws \Magento\Framework\Exception\NotFoundException
* @param array $options
* @param boolean $isError
* @throws \Magento\Framework\Exception\NotFoundException
*/
public function testUniqueValidation(array $options, $isError)
{
$serializedOptions = '{"key":"value"}';
$countFunctionCalls = ($isError) ? 6 : 5;
$this->requestMock->expects($this->exactly($countFunctionCalls))
->method('getParam')
->willReturnMap([
->willReturnMap(
[
['frontend_label', null, null],
['attribute_code', null, "test_attribute_code"],
['new_attribute_set_name', null, 'test_attribute_set_name'],
['message_key', null, Validate::DEFAULT_MESSAGE_KEY],
['serialized_options', '[]', $serializedOptions],
]);
]
);

$this->formDataSerializerMock
->expects($this->once())
Expand Down Expand Up @@ -323,22 +329,24 @@ public function provideUniqueData()
* Check that empty admin scope labels will trigger error.
*
* @dataProvider provideEmptyOption
* @param array $options
* @throws \Magento\Framework\Exception\NotFoundException
* @param array $options
* @throws \Magento\Framework\Exception\NotFoundException
*/
public function testEmptyOption(array $options, $result)
{
$serializedOptions = '{"key":"value"}';
$this->requestMock->expects($this->any())
->method('getParam')
->willReturnMap([
->willReturnMap(
[
['frontend_label', null, null],
['frontend_input', 'select', 'multipleselect'],
['attribute_code', null, "test_attribute_code"],
['new_attribute_set_name', null, 'test_attribute_set_name'],
['message_key', Validate::DEFAULT_MESSAGE_KEY, 'message'],
['serialized_options', '[]', $serializedOptions],
]);
]
);

$this->formDataSerializerMock
->expects($this->once())
Expand Down Expand Up @@ -439,6 +447,129 @@ public function provideEmptyOption()
];
}

/**
* Check that admin scope labels which only contain spaces will trigger error.
*
* @dataProvider provideWhitespaceOption
* @param array $options
* @param $result
* @throws \Magento\Framework\Exception\NotFoundException
*/
public function testWhitespaceOption(array $options, $result)
{
$serializedOptions = '{"key":"value"}';
$this->requestMock->expects($this->any())
->method('getParam')
->willReturnMap(
[
['frontend_label', null, null],
['frontend_input', 'select', 'multipleselect'],
['attribute_code', null, "test_attribute_code"],
['new_attribute_set_name', null, 'test_attribute_set_name'],
['message_key', Validate::DEFAULT_MESSAGE_KEY, 'message'],
['serialized_options', '[]', $serializedOptions],
]
);

$this->formDataSerializerMock
->expects($this->once())
->method('unserialize')
->with($serializedOptions)
->willReturn($options);

$this->objectManagerMock->expects($this->once())
->method('create')
->willReturn($this->attributeMock);

$this->attributeMock->expects($this->once())
->method('loadByCode')
->willReturnSelf();

$this->attributeCodeValidatorMock->expects($this->once())
->method('isValid')
->with('test_attribute_code')
->willReturn(true);

$this->resultJsonFactoryMock->expects($this->once())
->method('create')
->willReturn($this->resultJson);

$this->resultJson->expects($this->once())
->method('setJsonData')
->willReturnArgument(0);

$response = $this->getModel()->execute();
$responseObject = json_decode($response);
$this->assertEquals($responseObject, $result);
}

/**
* Dataprovider for testWhitespaceOption.
*
* @return array
*/
public function provideWhitespaceOption()
{
return [
'whitespace admin scope options' => [
[
'option' => [
'value' => [
"option_0" => [' '],
],
],
],
(object) [
'error' => true,
'message' => 'The value of Admin scope can\'t be empty.',
]
],
'not empty admin scope options' => [
[
'option' => [
'value' => [
"option_0" => ['asdads'],
],
],
],
(object) [
'error' => false,
]
],
'whitespace admin scope options and deleted' => [
[
'option' => [
'value' => [
"option_0" => [' '],
],
'delete' => [
'option_0' => '1',
],
],
],
(object) [
'error' => false,
],
],
'whitespace admin scope options and not deleted' => [
[
'option' => [
'value' => [
"option_0" => [' '],
],
'delete' => [
'option_0' => '0',
],
],
],
(object) [
'error' => true,
'message' => 'The value of Admin scope can\'t be empty.',
],
],
];
}

/**
* @throws \Magento\Framework\Exception\NotFoundException
*/
Expand All @@ -449,13 +580,15 @@ public function testExecuteWithOptionsDataError()
. "If the error persists, please try again later.";
$this->requestMock->expects($this->any())
->method('getParam')
->willReturnMap([
->willReturnMap(
[
['frontend_label', null, 'test_frontend_label'],
['attribute_code', null, 'test_attribute_code'],
['new_attribute_set_name', null, 'test_attribute_set_name'],
['message_key', Validate::DEFAULT_MESSAGE_KEY, 'message'],
['serialized_options', '[]', $serializedOptions],
]);
]
);

$this->formDataSerializerMock
->expects($this->once())
Expand All @@ -465,10 +598,12 @@ public function testExecuteWithOptionsDataError()

$this->objectManagerMock
->method('create')
->willReturnMap([
->willReturnMap(
[
[\Magento\Catalog\Model\ResourceModel\Eav\Attribute::class, [], $this->attributeMock],
[\Magento\Eav\Model\Entity\Attribute\Set::class, [], $this->attributeSetMock]
]);
]
);

$this->attributeCodeValidatorMock
->method('isValid')
Expand All @@ -485,10 +620,14 @@ public function testExecuteWithOptionsDataError()
->willReturn($this->resultJson);
$this->resultJson->expects($this->once())
->method('setJsonData')
->with(json_encode([
'error' => true,
'message' => $message
]))
->with(
json_encode(
[
'error' => true,
'message' => $message
]
)
)
->willReturnSelf();

$this->getModel()->execute();
Expand All @@ -498,23 +637,25 @@ public function testExecuteWithOptionsDataError()
* Test execute with an invalid attribute code
*
* @dataProvider provideInvalidAttributeCodes
* @param string $attributeCode
* @param $result
* @throws \Magento\Framework\Exception\NotFoundException
* @param string $attributeCode
* @param $result
* @throws \Magento\Framework\Exception\NotFoundException
*/
public function testExecuteWithInvalidAttributeCode($attributeCode, $result)
{
$serializedOptions = '{"key":"value"}';
$this->requestMock->expects($this->any())
->method('getParam')
->willReturnMap([
->willReturnMap(
[
['frontend_label', null, null],
['frontend_input', 'select', 'multipleselect'],
['attribute_code', null, $attributeCode],
['new_attribute_set_name', null, 'test_attribute_set_name'],
['message_key', Validate::DEFAULT_MESSAGE_KEY, 'message'],
['serialized_options', '[]', $serializedOptions],
]);
]
);

$this->formDataSerializerMock
->expects($this->once())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ define([
errorOption,
allOptions = [];

newOption.label = $.trim(newOption.label);

if (_.isEmpty(newOption.label)) {
return false;
}
Expand Down