Skip to content

Commit d8d7d83

Browse files
committed
Small improvements and API Test coveragefor ID_V2
1 parent 54625d9 commit d8d7d83

File tree

9 files changed

+363
-29
lines changed

9 files changed

+363
-29
lines changed

app/code/Magento/BundleGraphQl/Model/Resolver/Options/BundleEnteredOptionValueIdV2.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,11 @@ public function resolve(
4646
array $args = null
4747
) {
4848
if (!isset($value['option_id']) || empty($value['option_id'])) {
49-
throw new GraphQlInputException(__('Wrong format option data: option_id should not be empty.'));
49+
throw new GraphQlInputException(__('Wrong format option data: "option_id" should not be empty.'));
5050
}
5151

5252
if (!isset($value['selection_id']) || empty($value['selection_id'])) {
53-
throw new GraphQlInputException(__('Wrong format option data: selection_id should not be empty.'));
53+
throw new GraphQlInputException(__('Wrong format option data: "selection_id" should not be empty.'));
5454
}
5555

5656
$optionDetails = [

app/code/Magento/CatalogGraphQl/Model/Resolver/Product/CustomizableEnteredOptionValueIdV2.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function resolve(
4646
array $args = null
4747
) {
4848
if (!isset($value['option_id']) || empty($value['option_id'])) {
49-
throw new GraphQlInputException(__('Wrong format option data: option_id should not be empty.'));
49+
throw new GraphQlInputException(__('Wrong format option data: "option_id" should not be empty.'));
5050
}
5151

5252
$optionDetails = [

app/code/Magento/CatalogGraphQl/Model/Resolver/Product/CustomizableSelectedOptionValueIdV2.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,11 @@ public function resolve(
4646
array $args = null
4747
) {
4848
if (!isset($value['option_id']) || empty($value['option_id'])) {
49-
throw new GraphQlInputException(__('Wrong format option data: option_id should not be empty.'));
49+
throw new GraphQlInputException(__('Wrong format option data: "option_id" should not be empty.'));
5050
}
5151

5252
if (!isset($value['option_type_id']) || empty($value['option_type_id'])) {
53-
throw new GraphQlInputException(__('Wrong format option data: option_type_id should not be empty.'));
53+
throw new GraphQlInputException(__('Wrong format option data: "option_type_id" should not be empty.'));
5454
}
5555

5656
$optionDetails = [

app/code/Magento/ConfigurableProductGraphQl/Model/Resolver/Variant/Attributes/ConfigurableAttributeIdV2.php

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,22 @@
77

88
namespace Magento\ConfigurableProductGraphQl\Model\Resolver\Variant\Attributes;
99

10+
use Magento\Catalog\Model\Product;
1011
use Magento\Eav\Model\ResourceModel\Entity\Attribute;
11-
use Magento\Framework\Exception\LocalizedException;
1212
use Magento\Framework\GraphQl\Config\Element\Field;
13+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
1314
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
14-
use Magento\Framework\GraphQl\Query\Resolver\Value;
1515
use Magento\Framework\GraphQl\Query\ResolverInterface;
1616
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
1717

1818
/**
19-
* @inheritdoc
20-
*
2119
* Format new option id_v2 in base64 encode for super attribute options
2220
*/
2321
class ConfigurableAttributeIdV2 implements ResolverInterface
2422
{
23+
/**
24+
* Option type name
25+
*/
2526
private const OPTION_TYPE = 'configurable';
2627

2728
/**
@@ -30,8 +31,6 @@ class ConfigurableAttributeIdV2 implements ResolverInterface
3031
private $eavAttribute;
3132

3233
/**
33-
* ConfigurableAttributeIdV2 constructor.
34-
*
3534
* @param Attribute $eavAttribute
3635
*/
3736
public function __construct(Attribute $eavAttribute)
@@ -40,18 +39,19 @@ public function __construct(Attribute $eavAttribute)
4039
}
4140

4241
/**
43-
* @inheritdoc
44-
*
45-
* Create new option id_v2 that encodes details for each option and in most cases can be presented
46-
* as base64("<option-type>/<attribute-id>/<value-index>")
42+
* Create a option id_v2 for super attribute in "<option-type>/<attribute-id>/<value-index>" format
4743
*
4844
* @param Field $field
4945
* @param ContextInterface $context
5046
* @param ResolveInfo $info
5147
* @param array|null $value
5248
* @param array|null $args
53-
* @return Value|mixed|string
54-
* @throws LocalizedException
49+
*
50+
* @return string
51+
*
52+
* @throws GraphQlInputException
53+
*
54+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
5555
*/
5656
public function resolve(
5757
Field $field,
@@ -60,24 +60,29 @@ public function resolve(
6060
array $value = null,
6161
array $args = null
6262
) {
63-
$attribute_id = $this->eavAttribute->getIdByCode('catalog_product', $value['code']);
64-
$optionDetails = [
65-
self::OPTION_TYPE,
66-
$attribute_id,
67-
$value['value_index']
68-
];
63+
if (!isset($value['code']) || empty($value['code'])) {
64+
throw new GraphQlInputException(__('Wrong format option data: "code" should not be empty.'));
65+
}
66+
67+
$attributeId = $this->eavAttribute->getIdByCode(Product::ENTITY, $value['code']);
6968

70-
if (empty($attribute_id)) {
71-
throw new LocalizedException(__('Wrong format option data: attribute_id should not be empty.'));
69+
if (empty($attributeId)) {
70+
throw new GraphQlInputException(__('Wrong format option data: "attribute_id" should not be empty.'));
7271
}
7372

7473
if (!isset($value['value_index']) || empty($value['value_index'])) {
75-
throw new LocalizedException(__('Wrong format option data: value_index should not be empty.'));
74+
throw new GraphQlInputException(__('Wrong format option data: "value_index" should not be empty.'));
7675
}
7776

78-
// phpcs:ignore Magento2.Functions.DiscouragedFunction
79-
$content = \implode('/', $optionDetails);
77+
$optionDetails = [
78+
self::OPTION_TYPE,
79+
$attributeId,
80+
$value['value_index']
81+
];
8082

83+
$content = implode('/', $optionDetails);
84+
85+
// phpcs:ignore Magento2.Functions.DiscouragedFunction
8186
return base64_encode($content);
8287
}
8388
}

app/code/Magento/ConfigurableProductGraphQl/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"magento/module-catalog": "*",
88
"magento/module-configurable-product": "*",
99
"magento/module-catalog-graph-ql": "*",
10+
"magento/module-eav": "*",
1011
"magento/module-quote": "*",
1112
"magento/module-quote-graph-ql": "*",
1213
"magento/framework": "*"

app/code/Magento/DownloadableGraphQl/Resolver/Product/DownloadableLinksValueIdV2.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
*/
1818
class DownloadableLinksValueIdV2 implements ResolverInterface
1919
{
20+
/**
21+
* Option type name
22+
*/
2023
private const OPTION_TYPE = 'downloadable';
2124

2225
/**
@@ -30,7 +33,7 @@ public function resolve(
3033
array $args = null
3134
) {
3235
if (!isset($value['id']) || empty($value['id'])) {
33-
throw new GraphQlInputException(__('Wrong format option data: `id` should not be empty.'));
36+
throw new GraphQlInputException(__('Wrong format option data: "id" should not be empty.'));
3437
}
3538

3639
$optionDetails = [
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\GraphQl\Catalog\Options\IdV2;
9+
10+
use Magento\TestFramework\TestCase\GraphQlAbstract;
11+
12+
/**
13+
* Test for product custom options ID_V2
14+
*/
15+
class CustomizableOptionsIdV2Test extends GraphQlAbstract
16+
{
17+
/**
18+
* @magentoApiDataFixture Magento/Catalog/_files/product_simple_with_full_option_set.php
19+
*/
20+
public function testQueryIdV2ForCustomizableOptions()
21+
{
22+
$productSku = 'simple';
23+
$query = $this->getQuery($productSku);
24+
$response = $this->graphQlQuery($query);
25+
$responseProduct = $response['products']['items'][0];
26+
self::assertNotEmpty($responseProduct['options']);
27+
28+
foreach ($responseProduct['options'] as $option) {
29+
if (isset($option['entered_option'])) {
30+
$enteredOption = $option['entered_option'];
31+
$idV2 = $this->getIdV2ForEnteredValue($option['option_id']);
32+
33+
self::assertEquals($idV2, $enteredOption['id_v2']);
34+
} elseif (isset($option['selected_option'])) {
35+
$this->assertNotEmpty($option['selected_option']);
36+
37+
foreach ($option['selected_option'] as $selectedOption) {
38+
$idV2 = $this->getIdV2ForSelectedValue($option['option_id'], $selectedOption['option_type_id']);
39+
self::assertEquals($idV2, $selectedOption['id_v2']);
40+
}
41+
}
42+
}
43+
}
44+
45+
/**
46+
* Get IdV2 for entered option
47+
*
48+
* @param int $optionId
49+
*
50+
* @return string
51+
*/
52+
private function getIdV2ForEnteredValue(int $optionId): string
53+
{
54+
return base64_encode('custom-option/' . $optionId);
55+
}
56+
57+
/**
58+
* Get IdV2 for selected option
59+
*
60+
* @param int $optionId
61+
* @param int $optionValueId
62+
*
63+
* @return string
64+
*/
65+
private function getIdV2ForSelectedValue(int $optionId, int $optionValueId): string
66+
{
67+
return base64_encode('custom-option/' . $optionId . '/' . $optionValueId);
68+
}
69+
70+
/**
71+
* Get query
72+
*
73+
* @param string $sku
74+
*
75+
* @return string
76+
*/
77+
private function getQuery(string $sku): string
78+
{
79+
return <<<QUERY
80+
query {
81+
products(filter: { sku: { eq: "$sku" } }) {
82+
items {
83+
sku
84+
85+
... on CustomizableProductInterface {
86+
options {
87+
option_id
88+
title
89+
90+
... on CustomizableRadioOption {
91+
option_id
92+
selected_option: value {
93+
option_type_id
94+
id_v2
95+
}
96+
}
97+
98+
... on CustomizableDropDownOption {
99+
option_id
100+
selected_option: value {
101+
option_type_id
102+
id_v2
103+
}
104+
}
105+
106+
... on CustomizableMultipleOption {
107+
option_id
108+
selected_option: value {
109+
option_type_id
110+
id_v2
111+
}
112+
}
113+
114+
... on CustomizableCheckboxOption {
115+
option_id
116+
selected_option: value {
117+
option_type_id
118+
id_v2
119+
}
120+
}
121+
122+
... on CustomizableAreaOption {
123+
option_id
124+
entered_option: value {
125+
id_v2
126+
}
127+
}
128+
129+
... on CustomizableFieldOption {
130+
option_id
131+
entered_option: value {
132+
id_v2
133+
}
134+
}
135+
136+
... on CustomizableFileOption {
137+
option_id
138+
entered_option: value {
139+
id_v2
140+
}
141+
}
142+
143+
... on CustomizableDateOption {
144+
option_id
145+
entered_option: value {
146+
id_v2
147+
}
148+
}
149+
}
150+
}
151+
}
152+
}
153+
}
154+
QUERY;
155+
}
156+
}

0 commit comments

Comments
 (0)