Skip to content

Commit 10d20bf

Browse files
committed
magento/graphql-ce#761: [Customizable Options] Call to a member function format() on boolean
1 parent 063950f commit 10d20bf

File tree

5 files changed

+38
-112
lines changed

5 files changed

+38
-112
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ private function formatValues($values)
4848
$value = $values[$this->getOption()->getId()];
4949
$dateTime = \DateTime::createFromFormat(DateTime::DATETIME_PHP_FORMAT, $value);
5050

51-
if (!$dateTime) {
51+
if ($dateTime === false) {
5252
throw new GraphQlInputException(
5353
__('Invalid format provided. Please use \'Y-m-d H:i:s\' format.')
5454
);

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

Lines changed: 19 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,6 @@ class AddSimpleProductWithCustomOptionsToCartTest extends GraphQlAbstract
3131
*/
3232
private $getCustomOptionsValuesForQueryBySku;
3333

34-
/**
35-
* @var GetEmptyOptionsValuesForQueryBySku
36-
*/
37-
private $getEmptyOptionsValuesForQueryBySku;
38-
3934
/**
4035
* @inheritdoc
4136
*/
@@ -45,7 +40,6 @@ protected function setUp()
4540
$this->getMaskedQuoteIdByReservedOrderId = $objectManager->get(GetMaskedQuoteIdByReservedOrderId::class);
4641
$this->productCustomOptionsRepository = $objectManager->get(ProductCustomOptionRepositoryInterface::class);
4742
$this->getCustomOptionsValuesForQueryBySku = $objectManager->get(GetCustomOptionsValuesForQueryBySku::class);
48-
$this->getEmptyOptionsValuesForQueryBySku = $objectManager->get(GetEmptyOptionsValuesForQueryBySku::class);
4943
}
5044

5145
/**
@@ -63,7 +57,7 @@ public function testAddSimpleProductWithOptions()
6357

6458
$customOptionsValues = $this->getCustomOptionsValuesForQueryBySku->execute($sku);
6559
/* Generate customizable options fragment for GraphQl request */
66-
$queryCustomizableOptionValues = preg_replace('/"([^"]+)"\s*:\s*/', '$1:', json_encode($customOptionsValues));
60+
$queryCustomizableOptionValues = preg_replace('/"([^"]+)"\s*:\s*/', '$1:', json_encode(array_values($customOptionsValues)));
6761

6862
$customizableOptions = "customizable_options: {$queryCustomizableOptionValues}";
6963
$query = $this->getQuery($maskedQuoteId, $sku, $quantity, $customizableOptions);
@@ -74,13 +68,14 @@ public function testAddSimpleProductWithOptions()
7468
self::assertCount(1, $response['addSimpleProductsToCart']['cart']);
7569

7670
$customizableOptionsOutput = $response['addSimpleProductsToCart']['cart']['items'][0]['customizable_options'];
77-
$assignedOptionsCount = count($customOptionsValues);
78-
for ($counter = 0; $counter < $assignedOptionsCount; $counter++) {
79-
$expectedValues = $this->buildExpectedValuesArray($customOptionsValues[$counter]['value_string']);
71+
$count = 0;
72+
foreach ($customOptionsValues as $type => $value) {
73+
$expectedValues = $this->buildExpectedValuesArray($value['value_string'], $type);
8074
self::assertEquals(
8175
$expectedValues,
82-
$customizableOptionsOutput[$counter]['values']
76+
$customizableOptionsOutput[$count]['values']
8377
);
78+
$count++;
8479
}
8580
}
8681

@@ -106,54 +101,24 @@ public function testAddSimpleProductWithMissedRequiredOptionsSet()
106101
}
107102

108103
/**
109-
* Test adding a simple product to the shopping cart with Date customizable option assigned
104+
* Test adding a simple product with wrong format value for date option
110105
*
111-
* @magentoApiDataFixture Magento/Catalog/_files/product_simple_with_option_date.php
112-
* @magentoApiDataFixture Magento/Checkout/_files/active_quote.php
113-
*/
114-
public function testAddSimpleProductWithDateOption()
115-
{
116-
$sku = 'simple-product-1';
117-
$quantity = 1;
118-
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_order_1');
119-
120-
$customOptionsValues = $this->getCustomOptionsValuesForQueryBySku->execute($sku);
121-
$queryCustomizableOptionValues = preg_replace('/"([^"]+)"\s*:\s*/', '$1:', json_encode($customOptionsValues));
122-
$customizableOptions = "customizable_options: {$queryCustomizableOptionValues}";
123-
$query = $this->getQuery($maskedQuoteId, $sku, $quantity, $customizableOptions);
124-
125-
$response = $this->graphQlMutation($query);
126-
127-
self::assertArrayHasKey('items', $response['addSimpleProductsToCart']['cart']);
128-
self::assertCount(1, $response['addSimpleProductsToCart']['cart']);
129-
130-
$cartItem = $response['addSimpleProductsToCart']['cart']['items'][0];
131-
$customizableOptionOutput = $cartItem['customizable_options'][0]['values'][0]['value'];
132-
$expectedValue = date("M d, Y", strtotime($customOptionsValues[0]['value_string']));
133-
134-
self::assertEquals($expectedValue, $customizableOptionOutput);
135-
}
136-
137-
/**
138-
* Test adding a simple product with empty values for date option
139-
*
140-
* @magentoApiDataFixture Magento/Catalog/_files/product_simple_with_option_date.php
106+
* @magentoApiDataFixture Magento/Catalog/_files/product_simple_with_options.php
141107
* @magentoApiDataFixture Magento/Checkout/_files/active_quote.php
142108
*/
143-
public function testAddSimpleProductWithMissedDateOptionsSet()
109+
public function testAddSimpleProductWithWrongDateOptionFormat()
144110
{
145111
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_order_1');
146-
$sku = 'simple-product-1';
112+
$sku = 'simple';
147113
$quantity = 1;
148114

149-
$customOptionsValues = $this->getEmptyOptionsValuesForQueryBySku->execute($sku);
150-
$queryCustomizableOptionValues = preg_replace('/"([^"]+)"\s*:\s*/', '$1:', json_encode($customOptionsValues));
115+
$customOptionsValues = $this->getCustomOptionsValuesForQueryBySku->execute($sku);
116+
$customOptionsValues['date']['value_string'] = '12-12-12';
117+
$queryCustomizableOptionValues = preg_replace('/"([^"]+)"\s*:\s*/', '$1:', json_encode(array_values($customOptionsValues)));
151118
$customizableOptions = "customizable_options: {$queryCustomizableOptionValues}";
152119
$query = $this->getQuery($maskedQuoteId, $sku, $quantity, $customizableOptions);
153120

154-
self::expectExceptionMessage(
155-
'Invalid format provided. Please use \'Y-m-d H:i:s\' format.'
156-
);
121+
$this->expectExceptionMessage('Invalid format provided. Please use \'Y-m-d H:i:s\' format.');
157122

158123
$this->graphQlMutation($query);
159124
}
@@ -204,10 +169,14 @@ private function getQuery(string $maskedQuoteId, string $sku, float $quantity, s
204169
* Build the part of expected response.
205170
*
206171
* @param string $assignedValue
172+
* @param string $type option type
207173
* @return array
208174
*/
209-
private function buildExpectedValuesArray(string $assignedValue) : array
175+
private function buildExpectedValuesArray(string $assignedValue, string $type) : array
210176
{
177+
if ($type === 'date'){
178+
return [['value' => date('M d, Y', strtotime($assignedValue))]];
179+
}
211180
$assignedOptionsArray = explode(',', trim($assignedValue, '[]'));
212181
$expectedArray = [];
213182
foreach ($assignedOptionsArray as $assignedOption) {

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,25 +41,26 @@ public function execute(string $sku): array
4141
foreach ($customOptions as $customOption) {
4242
$optionType = $customOption->getType();
4343
if ($optionType == 'date') {
44-
$customOptionsValues[] = [
44+
$customOptionsValues[$optionType] = [
4545
'id' => (int)$customOption->getOptionId(),
46-
'value_string' => '2012-12-12 00:00:00'
46+
'value_string' => '2012-12-12 00:00:00',
4747
];
4848
} elseif ($optionType == 'field' || $optionType == 'area') {
49-
$customOptionsValues[] = [
49+
$customOptionsValues[$optionType] = [
5050
'id' => (int)$customOption->getOptionId(),
51-
'value_string' => 'test'
51+
'value_string' => 'test',
5252
];
5353
} elseif ($optionType == 'drop_down') {
5454
$optionSelectValues = $customOption->getValues();
55-
$customOptionsValues[] = [
55+
$customOptionsValues[$optionType] = [
5656
'id' => (int)$customOption->getOptionId(),
57-
'value_string' => reset($optionSelectValues)->getOptionTypeId()
57+
'value_string' => reset($optionSelectValues)->getOptionTypeId(),
5858
];
5959
} elseif ($optionType == 'multiple') {
60-
$customOptionsValues[] = [
60+
$customOptionsValues[$optionType] = [
6161
'id' => (int)$customOption->getOptionId(),
62-
'value_string' => '[' . implode(',', array_keys($customOption->getValues())) . ']'
62+
'value_string' => '[' . implode(',', array_keys($customOption->getValues())) . ']',
63+
6364
];
6465
}
6566
}

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

Lines changed: 0 additions & 53 deletions
This file was deleted.

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

Lines changed: 9 additions & 0 deletions
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)