Skip to content

Commit a9d1594

Browse files
committed
add unit test for attribute creation
1 parent d8e02f3 commit a9d1594

File tree

1 file changed

+121
-0
lines changed
  • app/code/Magento/Catalog/Test/Unit/Controller/Adminhtml/Product/Attribute

1 file changed

+121
-0
lines changed

app/code/Magento/Catalog/Test/Unit/Controller/Adminhtml/Product/Attribute/ValidateTest.php

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,127 @@ public function provideEmptyOption()
439439
];
440440
}
441441

442+
/**
443+
* Check that admin scope labels which only contain spaces will trigger error.
444+
*
445+
* @dataProvider provideWhitespaceOption
446+
* @param array $options
447+
* @param $result
448+
* @throws \Magento\Framework\Exception\NotFoundException
449+
*/
450+
public function testWhitespaceOption(array $options, $result)
451+
{
452+
$serializedOptions = '{"key":"value"}';
453+
$this->requestMock->expects($this->any())
454+
->method('getParam')
455+
->willReturnMap([
456+
['frontend_label', null, null],
457+
['frontend_input', 'select', 'multipleselect'],
458+
['attribute_code', null, "test_attribute_code"],
459+
['new_attribute_set_name', null, 'test_attribute_set_name'],
460+
['message_key', Validate::DEFAULT_MESSAGE_KEY, 'message'],
461+
['serialized_options', '[]', $serializedOptions],
462+
]);
463+
464+
$this->formDataSerializerMock
465+
->expects($this->once())
466+
->method('unserialize')
467+
->with($serializedOptions)
468+
->willReturn($options);
469+
470+
$this->objectManagerMock->expects($this->once())
471+
->method('create')
472+
->willReturn($this->attributeMock);
473+
474+
$this->attributeMock->expects($this->once())
475+
->method('loadByCode')
476+
->willReturnSelf();
477+
478+
$this->attributeCodeValidatorMock->expects($this->once())
479+
->method('isValid')
480+
->with('test_attribute_code')
481+
->willReturn(true);
482+
483+
$this->resultJsonFactoryMock->expects($this->once())
484+
->method('create')
485+
->willReturn($this->resultJson);
486+
487+
$this->resultJson->expects($this->once())
488+
->method('setJsonData')
489+
->willReturnArgument(0);
490+
491+
$response = $this->getModel()->execute();
492+
$responseObject = json_decode($response);
493+
$this->assertEquals($responseObject, $result);
494+
}
495+
496+
/**
497+
* Dataprovider for testWhitespaceOption.
498+
*
499+
* @return array
500+
*/
501+
public function provideWhitespaceOption()
502+
{
503+
return [
504+
'whitespace admin scope options' => [
505+
[
506+
'option' => [
507+
'value' => [
508+
"option_0" => [' '],
509+
],
510+
],
511+
],
512+
(object) [
513+
'error' => true,
514+
'message' => 'The value of Admin scope can\'t be empty.',
515+
]
516+
],
517+
'not empty admin scope options' => [
518+
[
519+
'option' => [
520+
'value' => [
521+
"option_0" => ['asdads'],
522+
],
523+
],
524+
],
525+
(object) [
526+
'error' => false,
527+
]
528+
],
529+
'whitespace admin scope options and deleted' => [
530+
[
531+
'option' => [
532+
'value' => [
533+
"option_0" => [' '],
534+
],
535+
'delete' => [
536+
'option_0' => '1',
537+
],
538+
],
539+
],
540+
(object) [
541+
'error' => false,
542+
],
543+
],
544+
'whitespace admin scope options and not deleted' => [
545+
[
546+
'option' => [
547+
'value' => [
548+
"option_0" => [' '],
549+
],
550+
'delete' => [
551+
'option_0' => '0',
552+
],
553+
],
554+
],
555+
(object) [
556+
'error' => true,
557+
'message' => 'The value of Admin scope can\'t be empty.',
558+
],
559+
],
560+
];
561+
}
562+
442563
/**
443564
* @throws \Magento\Framework\Exception\NotFoundException
444565
*/

0 commit comments

Comments
 (0)