Skip to content

Commit c1bffab

Browse files
ENGCOM-6077: Set GraphQlInputException when not formatted #804
- Merge Pull Request magento/graphql-ce#804 from sedonik/graphql-ce:date-type-not-hide-formatting - Merged commits: 1. 439830d 2. 80b848a 3. 6550d06 4. ab4e736 5. 1e821dd 6. 594cb63 7. 295372e 8. 063950f 9. 10d20bf 10. 0809b69 11. d2039f2 12. facce4c 13. 7653126
2 parents 0dcb19e + 7653126 commit c1bffab

File tree

6 files changed

+100
-34
lines changed

6 files changed

+100
-34
lines changed

app/code/Magento/CatalogGraphQl/Model/Product/Option/DateType.php

+11-1
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@
1010
use Magento\Catalog\Model\Product\Option\Type\Date as ProductDateOptionType;
1111
use Magento\Framework\Exception\LocalizedException;
1212
use Magento\Framework\Stdlib\DateTime;
13+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
1314

1415
/**
15-
* @inheritdoc
16+
* CatalogGraphQl product option date type
17+
*
18+
* @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
1619
*/
1720
class DateType extends ProductDateOptionType
1821
{
@@ -43,6 +46,13 @@ private function formatValues($values)
4346
if (isset($values[$this->getOption()->getId()])) {
4447
$value = $values[$this->getOption()->getId()];
4548
$dateTime = \DateTime::createFromFormat(DateTime::DATETIME_PHP_FORMAT, $value);
49+
50+
if ($dateTime === false) {
51+
throw new GraphQlInputException(
52+
__('Invalid format provided. Please use \'Y-m-d H:i:s\' format.')
53+
);
54+
}
55+
4656
$values[$this->getOption()->getId()] = [
4757
'date' => $value,
4858
'year' => $dateTime->format('Y'),

dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/AddDownloadableProductWithCustomOptionsToCartTest.php

+10-5
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,11 @@ public function testAddDownloadableProductWithOptions()
5858

5959
$customOptionsValues = $this->getCustomOptionsValuesForQueryBySku->execute($sku);
6060
/* Generate customizable options fragment for GraphQl request */
61-
$queryCustomizableOptionValues = preg_replace('/"([^"]+)"\s*:\s*/', '$1:', json_encode($customOptionsValues));
61+
$queryCustomizableOptionValues = preg_replace(
62+
'/"([^"]+)"\s*:\s*/',
63+
'$1:',
64+
json_encode(array_values($customOptionsValues))
65+
);
6266
$customizableOptions = "customizable_options: {$queryCustomizableOptionValues}";
6367

6468
$query = $this->getQuery($maskedQuoteId, $qty, $sku, $customizableOptions, $linkId);
@@ -68,13 +72,14 @@ public function testAddDownloadableProductWithOptions()
6872
self::assertCount($qty, $response['addDownloadableProductsToCart']['cart']);
6973
$customizableOptionsOutput =
7074
$response['addDownloadableProductsToCart']['cart']['items'][0]['customizable_options'];
71-
$assignedOptionsCount = count($customOptionsValues);
72-
for ($counter = 0; $counter < $assignedOptionsCount; $counter++) {
73-
$expectedValues = $this->buildExpectedValuesArray($customOptionsValues[$counter]['value_string']);
75+
$count = 0;
76+
foreach ($customOptionsValues as $value) {
77+
$expectedValues = $this->buildExpectedValuesArray($value['value_string']);
7478
self::assertEquals(
7579
$expectedValues,
76-
$customizableOptionsOutput[$counter]['values']
80+
$customizableOptionsOutput[$count]['values']
7781
);
82+
$count++;
7883
}
7984
}
8085

dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/AddSimpleProductWithCustomOptionsToCartTest.php

+42-6
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,11 @@ public function testAddSimpleProductWithOptions()
5757

5858
$customOptionsValues = $this->getCustomOptionsValuesForQueryBySku->execute($sku);
5959
/* Generate customizable options fragment for GraphQl request */
60-
$queryCustomizableOptionValues = preg_replace('/"([^"]+)"\s*:\s*/', '$1:', json_encode($customOptionsValues));
60+
$queryCustomizableOptionValues = preg_replace(
61+
'/"([^"]+)"\s*:\s*/',
62+
'$1:',
63+
json_encode(array_values($customOptionsValues))
64+
);
6165

6266
$customizableOptions = "customizable_options: {$queryCustomizableOptionValues}";
6367
$query = $this->getQuery($maskedQuoteId, $sku, $quantity, $customizableOptions);
@@ -68,13 +72,14 @@ public function testAddSimpleProductWithOptions()
6872
self::assertCount(1, $response['addSimpleProductsToCart']['cart']);
6973

7074
$customizableOptionsOutput = $response['addSimpleProductsToCart']['cart']['items'][0]['customizable_options'];
71-
$assignedOptionsCount = count($customOptionsValues);
72-
for ($counter = 0; $counter < $assignedOptionsCount; $counter++) {
73-
$expectedValues = $this->buildExpectedValuesArray($customOptionsValues[$counter]['value_string']);
75+
$count = 0;
76+
foreach ($customOptionsValues as $type => $value) {
77+
$expectedValues = $this->buildExpectedValuesArray($value['value_string'], $type);
7478
self::assertEquals(
7579
$expectedValues,
76-
$customizableOptionsOutput[$counter]['values']
80+
$customizableOptionsOutput[$count]['values']
7781
);
82+
$count++;
7883
}
7984
}
8085

@@ -99,6 +104,33 @@ public function testAddSimpleProductWithMissedRequiredOptionsSet()
99104
$this->graphQlMutation($query);
100105
}
101106

107+
/**
108+
* Test adding a simple product with wrong format value for date option
109+
*
110+
* @magentoApiDataFixture Magento/Catalog/_files/product_simple_with_options.php
111+
* @magentoApiDataFixture Magento/Checkout/_files/active_quote.php
112+
*/
113+
public function testAddSimpleProductWithWrongDateOptionFormat()
114+
{
115+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_order_1');
116+
$sku = 'simple';
117+
$quantity = 1;
118+
119+
$customOptionsValues = $this->getCustomOptionsValuesForQueryBySku->execute($sku);
120+
$customOptionsValues['date']['value_string'] = '12-12-12';
121+
$queryCustomizableOptionValues = preg_replace(
122+
'/"([^"]+)"\s*:\s*/',
123+
'$1:',
124+
json_encode(array_values($customOptionsValues))
125+
);
126+
$customizableOptions = "customizable_options: {$queryCustomizableOptionValues}";
127+
$query = $this->getQuery($maskedQuoteId, $sku, $quantity, $customizableOptions);
128+
129+
$this->expectExceptionMessage('Invalid format provided. Please use \'Y-m-d H:i:s\' format.');
130+
131+
$this->graphQlMutation($query);
132+
}
133+
102134
/**
103135
* @param string $maskedQuoteId
104136
* @param string $sku
@@ -145,10 +177,14 @@ private function getQuery(string $maskedQuoteId, string $sku, float $quantity, s
145177
* Build the part of expected response.
146178
*
147179
* @param string $assignedValue
180+
* @param string $type option type
148181
* @return array
149182
*/
150-
private function buildExpectedValuesArray(string $assignedValue) : array
183+
private function buildExpectedValuesArray(string $assignedValue, string $type) : array
151184
{
185+
if ($type === 'date') {
186+
return [['value' => date('M d, Y', strtotime($assignedValue))]];
187+
}
152188
$assignedOptionsArray = explode(',', trim($assignedValue, '[]'));
153189
$expectedArray = [];
154190
foreach ($assignedOptionsArray as $assignedOption) {

dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/AddVirtualProductWithCustomOptionsToCartTest.php

+10-5
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,11 @@ public function testAddVirtualProductWithOptions()
5757

5858
$customOptionsValues = $this->getCustomOptionsValuesForQueryBySku->execute($sku);
5959
/* Generate customizable options fragment for GraphQl request */
60-
$queryCustomizableOptionValues = preg_replace('/"([^"]+)"\s*:\s*/', '$1:', json_encode($customOptionsValues));
60+
$queryCustomizableOptionValues = preg_replace(
61+
'/"([^"]+)"\s*:\s*/',
62+
'$1:',
63+
json_encode(array_values($customOptionsValues))
64+
);
6165

6266
$customizableOptions = "customizable_options: {$queryCustomizableOptionValues}";
6367
$query = $this->getQuery($maskedQuoteId, $sku, $quantity, $customizableOptions);
@@ -68,13 +72,14 @@ public function testAddVirtualProductWithOptions()
6872
self::assertCount(1, $response['addVirtualProductsToCart']['cart']);
6973

7074
$customizableOptionsOutput = $response['addVirtualProductsToCart']['cart']['items'][0]['customizable_options'];
71-
$assignedOptionsCount = count($customOptionsValues);
72-
for ($counter = 0; $counter < $assignedOptionsCount; $counter++) {
73-
$expectedValues = $this->buildExpectedValuesArray($customOptionsValues[$counter]['value_string']);
75+
$count = 0;
76+
foreach ($customOptionsValues as $value) {
77+
$expectedValues = $this->buildExpectedValuesArray($value['value_string']);
7478
self::assertEquals(
7579
$expectedValues,
76-
$customizableOptionsOutput[$counter]['values']
80+
$customizableOptionsOutput[$count]['values']
7781
);
82+
$count++;
7883
}
7984
}
8085

dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/GetCustomOptionsValuesForQueryBySku.php

+18-17
Original file line numberDiff line numberDiff line change
@@ -40,25 +40,26 @@ public function execute(string $sku): array
4040

4141
foreach ($customOptions as $customOption) {
4242
$optionType = $customOption->getType();
43-
if ($optionType == 'field' || $optionType == 'area') {
44-
$customOptionsValues[] = [
45-
'id' => (int)$customOption->getOptionId(),
46-
'value_string' => 'test'
47-
];
48-
} elseif ($optionType == 'drop_down') {
49-
$optionSelectValues = $customOption->getValues();
50-
$customOptionsValues[] = [
51-
'id' => (int)$customOption->getOptionId(),
52-
'value_string' => reset($optionSelectValues)->getOptionTypeId()
53-
];
54-
} elseif ($optionType == 'multiple') {
55-
$customOptionsValues[] = [
56-
'id' => (int)$customOption->getOptionId(),
57-
'value_string' => '[' . implode(',', array_keys($customOption->getValues())) . ']'
58-
];
43+
$customOptionsValues[$optionType]['id'] = (int)$customOption->getOptionId();
44+
switch ($optionType) {
45+
case 'date':
46+
$customOptionsValues[$optionType]['value_string'] = '2012-12-12 00:00:00';
47+
break;
48+
case 'field':
49+
case 'area':
50+
$customOptionsValues[$optionType]['value_string'] = 'test';
51+
break;
52+
case 'drop_down':
53+
$optionSelectValues = $customOption->getValues();
54+
$customOptionsValues[$optionType]['value_string'] =
55+
reset($optionSelectValues)->getOptionTypeId();
56+
break;
57+
case 'multiple':
58+
$customOptionsValues[$optionType]['value_string'] =
59+
'[' . implode(',', array_keys($customOption->getValues())) . ']';
60+
break;
5961
}
6062
}
61-
6263
return $customOptionsValues;
6364
}
6465
}

dev/tests/integration/testsuite/Magento/Catalog/_files/product_simple_with_options.php

+9
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,15 @@
106106
'sort_order' => 2,
107107
],
108108
],
109+
],
110+
[
111+
'title' => 'date option',
112+
'type' => 'date',
113+
'price' => 80.0,
114+
'price_type' => 'fixed',
115+
'sku' => 'date option sku',
116+
'is_require' => false,
117+
'sort_order' => 6
109118
]
110119
];
111120

0 commit comments

Comments
 (0)