Skip to content

Commit 8dd6936

Browse files
author
Eric Bohanon
committed
MAGETWO-87147: Format customizable options to fit new schema
1 parent e140877 commit 8dd6936

File tree

4 files changed

+174
-67
lines changed

4 files changed

+174
-67
lines changed

app/code/Magento/CatalogGraphQl/Model/CustomizableOptionTypeResolver.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ class CustomizableOptionTypeResolver implements TypeResolverInterface
2121
*/
2222
private $mapper;
2323

24+
/**
25+
* @param MapperInterface $mapper
26+
*/
27+
public function __construct(MapperInterface $mapper)
28+
{
29+
$this->mapper = $mapper;
30+
}
31+
2432
/**
2533
* {@inheritDoc}
2634
*/

app/code/Magento/CatalogGraphQl/Model/Resolver/Products/DataProvider/Product/Formatter/Options.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,23 @@ public function format(Product $product, array $productData = [])
2525
if (!empty($product->getOptions())) {
2626
/** @var Option $option */
2727
foreach ($product->getOptions() as $key => $option) {
28+
unset($productData['options'][$key]);
2829
$productData['options'][$key] = $option->getData();
30+
$productData['options'][$key]['required'] = $option->getIsRequire();
2931
$productData['options'][$key]['product_sku'] = $option->getProductSku();
32+
3033
$values = $option->getValues() ?: [];
3134
/** @var Option\Value $value */
32-
foreach ($values as $value) {
33-
$productData['options'][$key]['values'][] = $value->getData();
35+
foreach ($values as $valueKey => $value) {
36+
$productData['options'][$key]['value'][$valueKey] = $value->getData();
37+
$productData['options'][$key]['value'][$valueKey]['price_type']
38+
= $value->getPriceType() !== null ? strtoupper($value->getPriceType()) : 'DYNAMIC';
39+
}
40+
41+
if (empty($values)) {
42+
$productData['options'][$key]['value'] = $option->getData();
43+
$productData['options'][$key]['value']['price_type']
44+
= $option->getPriceType() !== null ? strtoupper($option->getPriceType()) : 'DYNAMIC';
3445
}
3546
}
3647
}

app/code/Magento/CatalogGraphQl/etc/graphql.xml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -403,14 +403,17 @@
403403
<type xsi:type="OutputType" name="CustomizableFieldOption">
404404
<implements interface="CustomizableOptionInterface" copyFields="true"/>
405405
<field xsi:type="ObjectOutputField" name="value" type="CustomizableFieldValue"/>
406+
<field xsi:type="ScalarOutputField" name="product_sku" type="String"/>
406407
</type>
407408
<type xsi:type="OutputType" name="CustomizableFileOption">
408409
<implements interface="CustomizableOptionInterface" copyFields="true"/>
409410
<field xsi:type="ObjectOutputField" name="value" type="CustomizableFileValue"/>
411+
<field xsi:type="ScalarOutputField" name="product_sku" type="String"/>
410412
</type>
411413
<type xsi:type="OutputType" name="CustomizableDateOption">
412414
<implements interface="CustomizableOptionInterface" copyFields="true"/>
413415
<field xsi:type="ObjectOutputField" name="value" type="CustomizableDateValue"/>
416+
<field xsi:type="ScalarOutputField" name="product_sku" type="String"/>
414417
</type>
415418
<type xsi:type="OutputType" name="CustomizableDropDownOption">
416419
<implements interface="CustomizableOptionInterface" copyFields="true"/>
@@ -423,13 +426,13 @@
423426
<type xsi:type="OutputType" name="CustomizableAreaOption">
424427
<implements interface="CustomizableOptionInterface" copyFields="true"/>
425428
<field xsi:type="ObjectOutputField" name="value" type="CustomizableAreaValue"/>
429+
<field xsi:type="ScalarOutputField" name="product_sku" type="String"/>
426430
</type>
427431
<type xsi:type="OutputType" name="CustomizableFieldValue">
428432
<field xsi:type="ScalarOutputField" name="price" type="Float"/>
429433
<field xsi:type="ObjectOutputField" name="price_type" type="PriceTypeEnum"/>
430434
<field xsi:type="ScalarOutputField" name="sku" type="String"/>
431435
<field xsi:type="ScalarOutputField" name="max_characters" type="Int"/>
432-
<field xsi:type="ScalarOutputField" name="product_sku" type="String"/>
433436
</type>
434437
<type xsi:type="OutputType" name="CustomizableFileValue">
435438
<field xsi:type="ScalarOutputField" name="price" type="Float"/>
@@ -438,33 +441,32 @@
438441
<field xsi:type="ScalarOutputField" name="file_extension" type="String"/>
439442
<field xsi:type="ScalarOutputField" name="image_size_x" type="Int"/>
440443
<field xsi:type="ScalarOutputField" name="image_size_y" type="Int"/>
441-
<field xsi:type="ScalarOutputField" name="product_sku" type="String"/>
442444
</type>
443445
<type xsi:type="OutputType" name="CustomizableDateValue">
444446
<field xsi:type="ScalarOutputField" name="price" type="Float"/>
445447
<field xsi:type="ObjectOutputField" name="price_type" type="PriceTypeEnum"/>
446448
<field xsi:type="ScalarOutputField" name="sku" type="String"/>
447-
<field xsi:type="ScalarOutputField" name="product_sku" type="String"/>
448449
</type>
449450
<type xsi:type="OutputType" name="CustomizableDropDownValue">
450451
<field xsi:type="ScalarOutputField" name="option_type_id" type="Int"/>
451452
<field xsi:type="ScalarOutputField" name="price" type="Float"/>
452453
<field xsi:type="ObjectOutputField" name="price_type" type="PriceTypeEnum"/>
453454
<field xsi:type="ScalarOutputField" name="sku" type="String"/>
454455
<field xsi:type="ScalarOutputField" name="title" type="String"/>
456+
<field xsi:type="ScalarOutputField" name="sort_order" type="Int"/>
455457
</type>
456458
<type xsi:type="OutputType" name="CustomizableRadioValue">
457459
<field xsi:type="ScalarOutputField" name="option_type_id" type="Int"/>
458460
<field xsi:type="ScalarOutputField" name="price" type="Float"/>
459461
<field xsi:type="ObjectOutputField" name="price_type" type="PriceTypeEnum"/>
460462
<field xsi:type="ScalarOutputField" name="sku" type="String"/>
461463
<field xsi:type="ScalarOutputField" name="title" type="String"/>
464+
<field xsi:type="ScalarOutputField" name="sort_order" type="Int"/>
462465
</type>
463466
<type xsi:type="OutputType" name="CustomizableAreaValue">
464467
<field xsi:type="ScalarOutputField" name="price" type="Float"/>
465468
<field xsi:type="ObjectOutputField" name="price_type" type="PriceTypeEnum"/>
466469
<field xsi:type="ScalarOutputField" name="sku" type="String"/>
467-
<field xsi:type="ScalarOutputField" name="product_sku" type="String"/>
468470
<field xsi:type="ScalarOutputField" name="max_characters" type="Int"/>
469471
</type>
470472
<type xsi:type="Enum" name="PriceTypeEnum">

dev/tests/api-functional/testsuite/Magento/GraphQl/Catalog/ProductViewTest.php

Lines changed: 147 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -88,30 +88,69 @@ public function testQueryAllFieldsSimpleProduct()
8888
new_from_date
8989
new_to_date
9090
options_container
91-
options
92-
{
93-
file_extension
94-
image_size_x
95-
image_size_y
96-
is_require
97-
max_characters
98-
option_id
99-
price
100-
price_type
101-
product_sku
102-
sku
103-
sort_order
91+
... on CustomizableProductInterface {
92+
options {
10493
title
105-
type
106-
values
107-
{
108-
title
109-
sort_order
94+
required
95+
sort_order
96+
... on CustomizableFieldOption {
97+
product_sku
98+
field_option: value {
99+
sku
110100
price
111101
price_type
102+
max_characters
103+
}
104+
}
105+
... on CustomizableAreaOption {
106+
product_sku
107+
area_option: value {
108+
sku
109+
price
110+
price_type
111+
max_characters
112+
}
113+
}
114+
... on CustomizableDateOption {
115+
product_sku
116+
date_option: value {
117+
sku
118+
price
119+
price_type
120+
}
121+
}
122+
... on CustomizableDropDownOption {
123+
drop_down_option: value {
124+
option_type_id
112125
sku
126+
price
127+
price_type
128+
title
129+
sort_order
130+
}
131+
}
132+
... on CustomizableRadioOption {
133+
radio_option: value {
113134
option_type_id
135+
sku
136+
price
137+
price_type
138+
title
139+
sort_order
140+
}
114141
}
142+
...on CustomizableFileOption {
143+
product_sku
144+
file_option: value {
145+
sku
146+
price
147+
price_type
148+
file_extension
149+
image_size_x
150+
image_size_y
151+
}
152+
}
153+
}
115154
}
116155
page_layout
117156
price {
@@ -288,30 +327,67 @@ public function testQueryMediaGalleryEntryFieldsSimpleProduct()
288327
new_from_date
289328
new_to_date
290329
options_container
291-
options
292-
{
293-
file_extension
294-
image_size_x
295-
image_size_y
296-
is_require
297-
max_characters
298-
option_id
299-
price
300-
price_type
301-
product_sku
302-
sku
303-
sort_order
330+
... on CustomizableProductInterface {
331+
field_options: options {
304332
title
305-
type
306-
values
307-
{
308-
title
309-
sort_order
333+
required
334+
sort_order
335+
... on CustomizableFieldOption {
336+
product_sku
337+
field_option: value {
338+
sku
310339
price
311340
price_type
341+
max_characters
342+
}
343+
}
344+
... on CustomizableAreaOption {
345+
product_sku
346+
area_option: value {
312347
sku
348+
price
349+
price_type
350+
max_characters
351+
}
352+
}
353+
... on CustomizableDateOption {
354+
product_sku
355+
date_option: value {
356+
sku
357+
price
358+
price_type
359+
}
360+
}
361+
... on CustomizableDropDownOption {
362+
drop_down_option: value {
363+
option_type_id
364+
sku
365+
price
366+
price_type
367+
title
368+
}
369+
}
370+
... on CustomizableRadioOption {
371+
radio_option: value {
313372
option_type_id
373+
sku
374+
price
375+
price_type
376+
title
377+
}
314378
}
379+
...on CustomizableFileOption {
380+
product_sku
381+
file_option: value {
382+
sku
383+
price
384+
price_type
385+
file_extension
386+
image_size_x
387+
image_size_y
388+
}
389+
}
390+
}
315391
}
316392
page_layout
317393
price {
@@ -534,61 +610,71 @@ private function assertOptions($product, $actualResponse)
534610
$option = null;
535611
/** @var \Magento\Catalog\Model\Product\Option $optionSelect */
536612
foreach ($productOptions as $optionSelect) {
537-
if ($optionSelect->getOptionId() == $optionsArray['option_id']) {
613+
if ($optionSelect->getTitle() == $optionsArray['title']) {
538614
$option = $optionSelect;
539615
break;
540616
}
541617
}
542618
$assertionMap = [
543-
['response_field' => 'product_sku', 'expected_value' => $option->getProductSku()],
544619
['response_field' => 'sort_order', 'expected_value' => $option->getSortOrder()],
545620
['response_field' => 'title', 'expected_value' => $option->getTitle()],
546-
['response_field' => 'type', 'expected_value' => $option->getType()],
547-
['response_field' => 'option_id', 'expected_value' => $option->getOptionId()],
548-
['response_field' => 'is_require', 'expected_value' => $option->getIsRequire()],
549-
['response_field' => 'sort_order', 'expected_value' => $option->getSortOrder()]
621+
['response_field' => 'required', 'expected_value' => $option->getIsRequire()]
550622
];
551623

552624
if (!empty($option->getValues())) {
553-
$value = current($optionsArray['values']);
625+
$valueKeyName = $option->getType() === 'radio' ? 'radio_option' : 'drop_down_option';
626+
$value = current($optionsArray[$valueKeyName]);
554627
/** @var \Magento\Catalog\Model\Product\Option\Value $productValue */
555628
$productValue = current($option->getValues());
556629
$assertionMapValues = [
557630
['response_field' => 'title', 'expected_value' => $productValue->getTitle()],
558631
['response_field' => 'sort_order', 'expected_value' => $productValue->getSortOrder()],
559632
['response_field' => 'price', 'expected_value' => $productValue->getPrice()],
560-
['response_field' => 'price_type', 'expected_value' => $productValue->getPriceType()],
633+
['response_field' => 'price_type', 'expected_value' => strtoupper($productValue->getPriceType())],
561634
['response_field' => 'sku', 'expected_value' => $productValue->getSku()],
562635
['response_field' => 'option_type_id', 'expected_value' => $productValue->getOptionTypeId()]
563636
];
564637
$this->assertResponseFields($value, $assertionMapValues);
565638
} else {
639+
$assertionMap = array_merge(
640+
$assertionMap,
641+
[
642+
['response_field' => 'product_sku', 'expected_value' => $option->getProductSku()],
643+
]
644+
);
645+
$valueKeyName = "";
566646
if ($option->getType() === 'file') {
567-
$assertionMap = array_merge(
568-
$assertionMap,
569-
[
570-
['response_field' => 'file_extension', 'expected_value' => $option->getFileExtension()],
571-
['response_field' => 'image_size_x', 'expected_value' => $option->getImageSizeX()],
572-
['response_field' => 'image_size_y', 'expected_value' => $option->getImageSizeY()]
573-
]
574-
);
647+
$valueKeyName = 'file_option';
648+
$valueAssertionMap = [
649+
['response_field' => 'file_extension', 'expected_value' => $option->getFileExtension()],
650+
['response_field' => 'image_size_x', 'expected_value' => $option->getImageSizeX()],
651+
['response_field' => 'image_size_y', 'expected_value' => $option->getImageSizeY()]
652+
];
575653
} elseif ($option->getType() === 'area') {
576-
$assertionMap = array_merge(
577-
$assertionMap,
578-
[
579-
['response_field' => 'max_characters', 'expected_value' => $option->getMaxCharacters()],
580-
]
581-
);
654+
$valueKeyName = 'area_option';
655+
$valueAssertionMap = [
656+
['response_field' => 'max_characters', 'expected_value' => $option->getMaxCharacters()],
657+
];
658+
} elseif ($option->getType() === 'field') {
659+
$valueKeyName = 'field_option';
660+
$valueAssertionMap = [
661+
['response_field' => 'max_characters', 'expected_value' => $option->getMaxCharacters()]
662+
];
663+
} else {
664+
$valueKeyName = 'date_option';
665+
$valueAssertionMap = [];
582666
}
583667

584-
$assertionMap = array_merge(
585-
$assertionMap,
668+
$valueAssertionMap = array_merge(
669+
$valueAssertionMap,
586670
[
587671
['response_field' => 'price', 'expected_value' => $option->getPrice()],
588-
['response_field' => 'price_type', 'expected_value' => $option->getPriceType()],
672+
['response_field' => 'price_type', 'expected_value' => strtoupper($option->getPriceType())],
589673
['response_field' => 'sku', 'expected_value' => $option->getSku()]
590674
]
591675
);
676+
677+
$this->assertResponseFields($optionsArray[$valueKeyName], $valueAssertionMap);
592678
}
593679
$this->assertResponseFields($optionsArray, $assertionMap);
594680
}

0 commit comments

Comments
 (0)