11<?php
22/**
3- * Copyright © Magento, Inc. All rights reserved.
4- * See COPYING.txt for license details .
3+ * Copyright 2012 Adobe
4+ * All Rights Reserved .
55 */
66declare (strict_types=1 );
77
88namespace Magento \Eav \Test \Unit \Model \ResourceModel \Entity ;
99
1010use Magento \Catalog \Model \Product \Attribute \Source \Status ;
11+ use Magento \Catalog \Model \Product \Attribute \Source \Layout ;
1112use Magento \Eav \Model \Config ;
1213use Magento \Eav \Model \Entity \Attribute \Source \Table ;
1314use Magento \Eav \Model \ResourceModel \Entity \Attribute ;
@@ -99,15 +100,13 @@ public function testSaveOptionSystemAttribute()
99100 )->method (
100101 'fetchRow '
101102 )->willReturnMap (
102-
103+ [
103104 [
104- [
105- 'SELECT `eav_attribute`.* FROM `eav_attribute` ' .
106- 'WHERE (attribute_code="status") AND (entity_type_id="4") ' ,
107- $ attributeData ,
108- ],
109- ]
110-
105+ 'SELECT `eav_attribute`.* FROM `eav_attribute` ' .
106+ 'WHERE (attribute_code="status") AND (entity_type_id="4") ' ,
107+ $ attributeData ,
108+ ],
109+ ]
111110 );
112111 $ connectionMock ->expects (
113112 $ this ->once ()
@@ -123,6 +122,76 @@ public function testSaveOptionSystemAttribute()
123122 $ resourceModel ->save ($ model );
124123 }
125124
125+ /**
126+ * Verify select option with multiple choice save one default value
127+ *
128+ * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
129+ */
130+ public function testSaveSelectDefaultOptionAttribute ()
131+ {
132+ list ($ connectionMock , $ resourceModel ) = $ this ->_prepareResourceModel ();
133+
134+ $ originalData = [
135+ 'entity_type_id ' => 4 ,
136+ 'attribute_code ' => 'custom_layout ' ,
137+ 'backend_model ' => null ,
138+ 'backend_type ' => 'varchar ' ,
139+ 'frontend_input ' => 'select ' ,
140+ 'frontend_label ' => 'Test Dropdown ' ,
141+ 'frontend_class ' => null ,
142+ 'source_model ' => Layout::class,
143+ 'is_required ' => 0 ,
144+ 'is_user_defined ' => 1 ,
145+ 'is_unique ' => 0 ,
146+ 'default_value ' => "option_3 " ,
147+ ];
148+
149+ $ attributeData = [
150+ 'id ' => '999 ' ,
151+ 'attribute_id ' => '123 ' ,
152+ 'entity_type_id ' => 4 ,
153+ 'attribute_code ' => 'custom_layout ' ,
154+ 'backend_model ' => null ,
155+ 'backend_type ' => 'varchar ' ,
156+ 'frontend_input ' => 'select ' ,
157+ 'frontend_label ' => 'Test Dropdown ' ,
158+ 'frontend_class ' => null ,
159+ 'source_model ' => Layout::class,
160+ 'is_required ' => 0 ,
161+ 'is_user_defined ' => 1 ,
162+ 'is_unique ' => 0 ,
163+ 'default_value ' => "option_3 " ,
164+ 'default ' => ['option_1 ' ],
165+ 'sort_order ' => 1 ,
166+ ];
167+
168+ $ objectManagerHelper = new ObjectManager ($ this );
169+ /** @var AbstractModel $model */
170+ $ arguments = $ objectManagerHelper ->getConstructArguments (AbstractModel::class);
171+ $ arguments ['data ' ] = $ attributeData ;
172+ $ arguments ['context ' ] = $ this ->contextMock ;
173+ $ model = $ this ->getMockBuilder (AbstractModel::class)
174+ ->onlyMethods (['save ' ])
175+ ->setConstructorArgs ($ arguments )
176+ ->getMock ();
177+ $ model ->setOption (
178+ [
179+ 'delete ' =>
180+ [
181+ 'option_1 ' => ['choice_1 ' , 'Frontend choice_1 ' ],
182+ 'option_2 ' => ['choice_2 ' , 'Frontend choice_2 ' ],
183+ 'option_3 ' => ['choice_3 ' , 'Frontend choice_3 ' ]
184+ ]
185+ ]
186+ );
187+
188+ $ connectionMock ->expects ($ this ->any ())
189+ ->method ('update ' )
190+ ->with ('eav_attribute ' , $ this ->logicalOr ($ originalData , ['default_value ' => "option_1 " ]));
191+
192+ $ resourceModel ->save ($ model );
193+ }
194+
126195 /**
127196 * @covers \Magento\Eav\Model\ResourceModel\Entity\Attribute::_saveOption
128197 */
@@ -171,24 +240,22 @@ public function testSaveOptionNewUserDefinedAttribute()
171240 )->method (
172241 'update '
173242 )->willReturnMap (
174-
175- [[ 'eav_attribute ' , ['default_value ' => '' ], ['attribute_id = ? ' => 123 ], 1 ] ]
176-
243+ [
244+ ['eav_attribute ' , ['default_value ' => '' ], ['attribute_id = ? ' => 123 ], 1 ]
245+ ]
177246 );
178247 $ connectionMock ->expects (
179248 $ this ->once ()
180249 )->method (
181250 'fetchRow '
182251 )->willReturnMap (
183-
252+ [
184253 [
185- [
186- 'SELECT `eav_attribute`.* FROM `eav_attribute` ' .
187- 'WHERE (attribute_code="a_dropdown") AND (entity_type_id="4") ' ,
188- false ,
189- ],
190- ]
191-
254+ 'SELECT `eav_attribute`.* FROM `eav_attribute` ' .
255+ 'WHERE (attribute_code="a_dropdown") AND (entity_type_id="4") ' ,
256+ false ,
257+ ],
258+ ]
192259 );
193260 $ connectionMock ->expects (
194261 $ this ->once ()
@@ -202,22 +269,20 @@ public function testSaveOptionNewUserDefinedAttribute()
202269 )->method (
203270 'insert '
204271 )->willReturnMap (
205-
272+ [
273+ ['eav_attribute ' , $ attributeData , 1 ],
274+ ['eav_attribute_option ' , ['attribute_id ' => 123 , 'sort_order ' => 0 ], 1 ],
206275 [
207- ['eav_attribute ' , $ attributeData , 1 ],
208- ['eav_attribute_option ' , ['attribute_id ' => 123 , 'sort_order ' => 0 ], 1 ],
209- [
210- 'eav_attribute_option_value ' ,
211- ['option_id ' => 123 , 'store_id ' => 0 , 'value ' => 'Backend Label ' ],
212- 1
213- ],
214- [
215- 'eav_attribute_option_value ' ,
216- ['option_id ' => 123 , 'store_id ' => 1 , 'value ' => 'Frontend Label ' ],
217- 1
218- ],
219- ]
220-
276+ 'eav_attribute_option_value ' ,
277+ ['option_id ' => 123 , 'store_id ' => 0 , 'value ' => 'Backend Label ' ],
278+ 1
279+ ],
280+ [
281+ 'eav_attribute_option_value ' ,
282+ ['option_id ' => 123 , 'store_id ' => 1 , 'value ' => 'Frontend Label ' ],
283+ 1
284+ ],
285+ ]
221286 );
222287 $ connectionMock ->expects ($ this ->any ())->method ('getTransactionLevel ' )->willReturn (1 );
223288
0 commit comments