6
6
namespace Magento \Catalog \Test \Unit \Controller \Adminhtml \Product \Attribute ;
7
7
8
8
use Magento \Catalog \Controller \Adminhtml \Product \Attribute \Validate ;
9
+ use Magento \Eav \Model \Validator \Attribute \Code as AttributeCodeValidator ;
9
10
use Magento \Framework \Serialize \Serializer \FormData ;
10
11
use Magento \Catalog \Model \ResourceModel \Eav \Attribute ;
11
12
use Magento \Catalog \Test \Unit \Controller \Adminhtml \Product \AttributeTest ;
@@ -67,6 +68,11 @@ class ValidateTest extends AttributeTest
67
68
*/
68
69
private $ formDataSerializerMock ;
69
70
71
+ /**
72
+ * @var AttributeCodeValidator|\PHPUnit_Framework_MockObject_MockObject
73
+ */
74
+ private $ attributeCodeValidatorMock ;
75
+
70
76
protected function setUp ()
71
77
{
72
78
parent ::setUp ();
@@ -95,6 +101,9 @@ protected function setUp()
95
101
$ this ->formDataSerializerMock = $ this ->getMockBuilder (FormData::class)
96
102
->disableOriginalConstructor ()
97
103
->getMock ();
104
+ $ this ->attributeCodeValidatorMock = $ this ->getMockBuilder (AttributeCodeValidator::class)
105
+ ->disableOriginalConstructor ()
106
+ ->getMock ();
98
107
99
108
$ this ->contextMock ->expects ($ this ->any ())
100
109
->method ('getObjectManager ' )
@@ -117,6 +126,7 @@ protected function getModel()
117
126
'layoutFactory ' => $ this ->layoutFactoryMock ,
118
127
'multipleAttributeList ' => ['select ' => 'option ' ],
119
128
'formDataSerializer ' => $ this ->formDataSerializerMock ,
129
+ 'attributeCodeValidator ' => $ this ->attributeCodeValidatorMock ,
120
130
]
121
131
);
122
132
}
@@ -141,6 +151,12 @@ public function testExecute()
141
151
$ this ->attributeMock ->expects ($ this ->once ())
142
152
->method ('loadByCode ' )
143
153
->willReturnSelf ();
154
+
155
+ $ this ->attributeCodeValidatorMock ->expects ($ this ->once ())
156
+ ->method ('isValid ' )
157
+ ->with ('test_attribute_code ' )
158
+ ->willReturn (true );
159
+
144
160
$ this ->requestMock ->expects ($ this ->once ())
145
161
->method ('has ' )
146
162
->with ('new_attribute_set_name ' )
@@ -190,6 +206,11 @@ public function testUniqueValidation(array $options, $isError)
190
206
->with ($ serializedOptions )
191
207
->willReturn ($ options );
192
208
209
+ $ this ->attributeCodeValidatorMock ->expects ($ this ->once ())
210
+ ->method ('isValid ' )
211
+ ->with ('test_attribute_code ' )
212
+ ->willReturn (true );
213
+
193
214
$ this ->objectManagerMock ->expects ($ this ->once ())
194
215
->method ('create ' )
195
216
->willReturn ($ this ->attributeMock );
@@ -333,6 +354,11 @@ public function testEmptyOption(array $options, $result)
333
354
->method ('loadByCode ' )
334
355
->willReturnSelf ();
335
356
357
+ $ this ->attributeCodeValidatorMock ->expects ($ this ->once ())
358
+ ->method ('isValid ' )
359
+ ->with ('test_attribute_code ' )
360
+ ->willReturn (true );
361
+
336
362
$ this ->resultJsonFactoryMock ->expects ($ this ->once ())
337
363
->method ('create ' )
338
364
->willReturn ($ this ->resultJson );
@@ -444,6 +470,10 @@ public function testExecuteWithOptionsDataError()
444
470
[\Magento \Eav \Model \Entity \Attribute \Set::class, [], $ this ->attributeSetMock ]
445
471
]);
446
472
473
+ $ this ->attributeCodeValidatorMock
474
+ ->method ('isValid ' )
475
+ ->willReturn (true );
476
+
447
477
$ this ->attributeMock
448
478
->method ('loadByCode ' )
449
479
->willReturnSelf ();
@@ -463,4 +493,81 @@ public function testExecuteWithOptionsDataError()
463
493
464
494
$ this ->getModel ()->execute ();
465
495
}
496
+
497
+ /**
498
+ * Test execute with an invalid attribute code
499
+ *
500
+ * @dataProvider provideInvalidAttributeCodes
501
+ * @param string $attributeCode
502
+ * @param $result
503
+ * @throws \Magento\Framework\Exception\NotFoundException
504
+ */
505
+ public function testExecuteWithInvalidAttributeCode ($ attributeCode , $ result )
506
+ {
507
+ $ serializedOptions = '{"key":"value"} ' ;
508
+ $ this ->requestMock ->expects ($ this ->any ())
509
+ ->method ('getParam ' )
510
+ ->willReturnMap ([
511
+ ['frontend_label ' , null , null ],
512
+ ['frontend_input ' , 'select ' , 'multipleselect ' ],
513
+ ['attribute_code ' , null , $ attributeCode ],
514
+ ['new_attribute_set_name ' , null , 'test_attribute_set_name ' ],
515
+ ['message_key ' , Validate::DEFAULT_MESSAGE_KEY , 'message ' ],
516
+ ['serialized_options ' , '[] ' , $ serializedOptions ],
517
+ ]);
518
+
519
+ $ this ->formDataSerializerMock
520
+ ->expects ($ this ->once ())
521
+ ->method ('unserialize ' )
522
+ ->with ($ serializedOptions )
523
+ ->willReturn (["key " => "value " ]);
524
+
525
+ $ this ->objectManagerMock ->expects ($ this ->once ())
526
+ ->method ('create ' )
527
+ ->willReturn ($ this ->attributeMock );
528
+
529
+ $ this ->attributeMock ->expects ($ this ->once ())
530
+ ->method ('loadByCode ' )
531
+ ->willReturnSelf ();
532
+
533
+ $ this ->attributeCodeValidatorMock ->expects ($ this ->once ())
534
+ ->method ('isValid ' )
535
+ ->with ($ attributeCode )
536
+ ->willReturn (false );
537
+
538
+ $ this ->attributeCodeValidatorMock ->expects ($ this ->once ())
539
+ ->method ('getMessages ' )
540
+ ->willReturn (['Invalid Attribute Code. ' ]);
541
+
542
+ $ this ->resultJsonFactoryMock ->expects ($ this ->once ())
543
+ ->method ('create ' )
544
+ ->willReturn ($ this ->resultJson );
545
+
546
+ $ this ->resultJson ->expects ($ this ->once ())
547
+ ->method ('setJsonData ' )
548
+ ->willReturnArgument (0 );
549
+
550
+ $ response = $ this ->getModel ()->execute ();
551
+ $ responseObject = json_decode ($ response );
552
+
553
+ $ this ->assertEquals ($ responseObject , $ result );
554
+ }
555
+
556
+ /**
557
+ * Providing invalid attribute codes
558
+ *
559
+ * @return array
560
+ */
561
+ public function provideInvalidAttributeCodes ()
562
+ {
563
+ return [
564
+ 'invalid attribute code ' => [
565
+ '.attribute_code ' ,
566
+ (object ) [
567
+ 'error ' => true ,
568
+ 'message ' => 'Invalid Attribute Code. ' ,
569
+ ]
570
+ ]
571
+ ];
572
+ }
466
573
}
0 commit comments