15
15
use Magento \Framework \ObjectManagerInterface ;
16
16
use Magento \Framework \View \LayoutFactory ;
17
17
use Magento \Framework \View \LayoutInterface ;
18
+ use Magento \Framework \DataObject ;
18
19
19
20
/**
20
21
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
@@ -61,20 +62,24 @@ class ValidateTest extends AttributeTest
61
62
*/
62
63
protected $ layoutMock ;
63
64
65
+ /**
66
+ * @var DataObject
67
+ */
68
+ protected $ responseDataObject ;
69
+
64
70
protected function setUp ()
65
71
{
66
72
parent ::setUp ();
67
73
$ this ->resultJsonFactoryMock = $ this ->getMockBuilder (ResultJsonFactory::class)
68
74
->disableOriginalConstructor ()
75
+ ->setMethods (['create ' ])
69
76
->getMock ();
70
77
$ this ->resultJson = $ this ->getMockBuilder (ResultJson::class)
71
78
->disableOriginalConstructor ()
72
79
->getMock ();
73
80
$ this ->layoutFactoryMock = $ this ->getMockBuilder (LayoutFactory::class)
74
81
->disableOriginalConstructor ()
75
82
->getMock ();
76
- $ this ->objectManagerMock = $ this ->getMockBuilder (ObjectManagerInterface::class)
77
- ->getMockForAbstractClass ();
78
83
$ this ->attributeMock = $ this ->getMockBuilder (Attribute::class)
79
84
->disableOriginalConstructor ()
80
85
->getMock ();
@@ -86,10 +91,9 @@ protected function setUp()
86
91
->getMock ();
87
92
$ this ->layoutMock = $ this ->getMockBuilder (LayoutInterface::class)
88
93
->getMockForAbstractClass ();
89
-
90
- $ this ->contextMock ->expects ($ this ->any ())
91
- ->method ('getObjectManager ' )
92
- ->willReturn ($ this ->objectManagerMock );
94
+ $ this ->responseDataObject = $ this ->getMockBuilder (DataObject::class)
95
+ ->setMethods (null )
96
+ ->getMock ();
93
97
}
94
98
95
99
/**
@@ -100,13 +104,17 @@ protected function getModel()
100
104
return $ this ->objectManager ->getObject (
101
105
Validate::class,
102
106
[
103
- 'context ' => $ this ->contextMock ,
104
- 'attributeLabelCache ' => $ this ->attributeLabelCacheMock ,
105
- 'coreRegistry ' => $ this ->coreRegistryMock ,
106
- 'resultPageFactory ' => $ this ->resultPageFactoryMock ,
107
- 'resultJsonFactory ' => $ this ->resultJsonFactoryMock ,
108
- 'layoutFactory ' => $ this ->layoutFactoryMock ,
109
- 'multipleAttributeList ' => ['select ' => 'option ' ]
107
+ 'context ' => $ this ->contextMock ,
108
+ 'attributeLabelCache ' => $ this ->attributeLabelCacheMock ,
109
+ 'coreRegistry ' => $ this ->coreRegistryMock ,
110
+ 'resultPageFactory ' => $ this ->resultPageFactoryMock ,
111
+ 'resultJsonFactory ' => $ this ->resultJsonFactoryMock ,
112
+ 'layoutFactory ' => $ this ->layoutFactoryMock ,
113
+ 'multipleAttributeList ' => ['select ' => 'option ' , 'multipleselect ' => 'option ' ],
114
+ 'attributeResource ' => $ this ->attributeMock ,
115
+ 'attributeSetResource ' => $ this ->attributeSetMock ,
116
+ 'escaper ' => $ this ->escaperMock ,
117
+ 'responseObject ' => $ this ->responseDataObject ,
110
118
]
111
119
);
112
120
}
@@ -120,22 +128,9 @@ public function testExecute()
120
128
['attribute_code ' , null , 'test_attribute_code ' ],
121
129
['new_attribute_set_name ' , null , 'test_attribute_set_name ' ],
122
130
]);
123
- $ this ->objectManagerMock ->expects ($ this ->exactly (2 ))
124
- ->method ('create ' )
125
- ->willReturnMap ([
126
- [\Magento \Catalog \Model \ResourceModel \Eav \Attribute::class, [], $ this ->attributeMock ],
127
- [\Magento \Eav \Model \Entity \Attribute \Set::class, [], $ this ->attributeSetMock ]
128
- ]);
129
131
$ this ->attributeMock ->expects ($ this ->once ())
130
132
->method ('loadByCode ' )
131
133
->willReturnSelf ();
132
- $ this ->requestMock ->expects ($ this ->once ())
133
- ->method ('has ' )
134
- ->with ('new_attribute_set_name ' )
135
- ->willReturn (true );
136
- $ this ->attributeSetMock ->expects ($ this ->once ())
137
- ->method ('setEntityTypeId ' )
138
- ->willReturnSelf ();
139
134
$ this ->attributeSetMock ->expects ($ this ->once ())
140
135
->method ('load ' )
141
136
->willReturnSelf ();
@@ -155,13 +150,11 @@ public function testExecute()
155
150
/**
156
151
* @dataProvider provideUniqueData
157
152
* @param array $options
158
- * @param boolean $isError
159
153
* @throws \Magento\Framework\Exception\NotFoundException
160
154
*/
161
- public function testUniqueValidation (array $ options, $ isError )
155
+ public function testUniqueValidation (array $ options )
162
156
{
163
- $ countFunctionCalls = ($ isError ) ? 6 : 5 ;
164
- $ this ->requestMock ->expects ($ this ->exactly ($ countFunctionCalls ))
157
+ $ this ->requestMock ->expects ($ this ->any ())
165
158
->method ('getParam ' )
166
159
->willReturnMap ([
167
160
['frontend_label ' , null , null ],
@@ -171,19 +164,10 @@ public function testUniqueValidation(array $options, $isError)
171
164
['message_key ' , null , Validate::DEFAULT_MESSAGE_KEY ]
172
165
]);
173
166
174
- $ this ->objectManagerMock ->expects ($ this ->once ())
175
- ->method ('create ' )
176
- ->willReturn ($ this ->attributeMock );
177
-
178
167
$ this ->attributeMock ->expects ($ this ->once ())
179
168
->method ('loadByCode ' )
180
169
->willReturnSelf ();
181
170
182
- $ this ->requestMock ->expects ($ this ->once ())
183
- ->method ('has ' )
184
- ->with ('new_attribute_set_name ' )
185
- ->willReturn (false );
186
-
187
171
$ this ->resultJsonFactoryMock ->expects ($ this ->once ())
188
172
->method ('create ' )
189
173
->willReturn ($ this ->resultJson );
@@ -205,7 +189,7 @@ public function provideUniqueData()
205
189
"option_1 " => "" ,
206
190
"option_2 " => "" ,
207
191
]
208
- ], false
192
+ ]
209
193
],
210
194
'valid options ' => [
211
195
[
@@ -219,7 +203,7 @@ public function provideUniqueData()
219
203
"option_1 " => "" ,
220
204
"option_2 " => "" ,
221
205
]
222
- ], false
206
+ ]
223
207
],
224
208
'duplicate options ' => [
225
209
[
@@ -233,7 +217,7 @@ public function provideUniqueData()
233
217
"option_1 " => "" ,
234
218
"option_2 " => "" ,
235
219
]
236
- ], true
220
+ ]
237
221
],
238
222
'duplicate and deleted ' => [
239
223
[
@@ -247,8 +231,79 @@ public function provideUniqueData()
247
231
"option_1 " => "1 " ,
248
232
"option_2 " => "" ,
249
233
]
250
- ], false
234
+ ]
251
235
],
252
236
];
253
237
}
238
+
239
+ /**
240
+ * Check that empty admin scope labels will trigger error.
241
+ *
242
+ * @dataProvider provideEmptyOption
243
+ * @param array $options
244
+ * @throws \Magento\Framework\Exception\NotFoundException
245
+ */
246
+ public function testEmptyOption (array $ options , $ result )
247
+ {
248
+ $ this ->requestMock ->expects ($ this ->any ())
249
+ ->method ('getParam ' )
250
+ ->willReturnMap ([
251
+ ['frontend_label ' , null , null ],
252
+ ['frontend_input ' , 'select ' , 'multipleselect ' ],
253
+ ['attribute_code ' , null , "test_attribute_code " ],
254
+ ['new_attribute_set_name ' , null , 'test_attribute_set_name ' ],
255
+ ['option ' , null , $ options ],
256
+ [Validate::ARRAY_MESSAGE_KEY , Validate::DEFAULT_MESSAGE_KEY , Validate::DEFAULT_MESSAGE_KEY ]
257
+ ]);
258
+
259
+ $ this ->attributeMock ->expects ($ this ->once ())
260
+ ->method ('loadByCode ' )
261
+ ->willReturnSelf ();
262
+
263
+ $ this ->resultJsonFactoryMock ->expects ($ this ->once ())
264
+ ->method ('create ' )
265
+ ->willReturn ($ this ->resultJson );
266
+
267
+ $ this ->resultJson ->expects ($ this ->once ())
268
+ ->method ('setJsonData ' )
269
+ ->willReturnArgument (0 );
270
+
271
+ $ response = $ this ->getModel ()->execute ();
272
+ $ responseObject = json_decode ($ response );
273
+ $ this ->assertEquals ($ responseObject , $ result );
274
+ }
275
+
276
+ /**
277
+ * Dataprovider for testEmptyOption.
278
+ *
279
+ * @return array
280
+ */
281
+ public function provideEmptyOption ()
282
+ {
283
+ return [
284
+ 'empty admin scope options ' => [
285
+ [
286
+ 'value ' => [
287
+ "option_0 " => ['' ],
288
+ ],
289
+ ],
290
+ (object ) [
291
+ 'error ' => true ,
292
+ 'message ' => [
293
+ 'The value of Admin scope can \'t be empty. '
294
+ ]
295
+ ]
296
+ ],
297
+ 'not empty admin scope options ' => [
298
+ [
299
+ 'value ' => [
300
+ "option_0 " => ['asdads ' ],
301
+ ],
302
+ ],
303
+ (object ) [
304
+ 'error ' => false ,
305
+ ]
306
+ ]
307
+ ];
308
+ }
254
309
}
0 commit comments