From 439830da50d2e54e112cd5583cae5bbd4752dfa4 Mon Sep 17 00:00:00 2001 From: Sergey Dovbenko Date: Mon, 5 Aug 2019 20:49:19 +0000 Subject: [PATCH 1/9] Set GraphQlInputException when not formatted --- .../CatalogGraphQl/Model/Product/Option/DateType.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/app/code/Magento/CatalogGraphQl/Model/Product/Option/DateType.php b/app/code/Magento/CatalogGraphQl/Model/Product/Option/DateType.php index e1106a3f696e4..40d77e36659a2 100644 --- a/app/code/Magento/CatalogGraphQl/Model/Product/Option/DateType.php +++ b/app/code/Magento/CatalogGraphQl/Model/Product/Option/DateType.php @@ -10,6 +10,7 @@ use Magento\Catalog\Model\Product\Option\Type\Date as ProductDateOptionType; use Magento\Framework\Exception\LocalizedException; use Magento\Framework\Stdlib\DateTime; +use Magento\Framework\GraphQl\Exception\GraphQlInputException; /** * @inheritdoc @@ -43,6 +44,13 @@ private function formatValues($values) if (isset($values[$this->getOption()->getId()])) { $value = $values[$this->getOption()->getId()]; $dateTime = \DateTime::createFromFormat(DateTime::DATETIME_PHP_FORMAT, $value); + + if (!$dateTime) { + throw new GraphQlInputException( + __('Invalid format provided. Please use \'Y-m-d H:i:s\' format.') + ); + } + $values[$this->getOption()->getId()] = [ 'date' => $value, 'year' => $dateTime->format('Y'), From 80b848a36567deb2540f4cfd6c3d005dfa0bf25e Mon Sep 17 00:00:00 2001 From: Sergey Dovbenko Date: Tue, 6 Aug 2019 20:32:44 +0000 Subject: [PATCH 2/9] Added SuppressWarnings to the class --- .../Magento/CatalogGraphQl/Model/Product/Option/DateType.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/app/code/Magento/CatalogGraphQl/Model/Product/Option/DateType.php b/app/code/Magento/CatalogGraphQl/Model/Product/Option/DateType.php index 40d77e36659a2..5205115b772ed 100644 --- a/app/code/Magento/CatalogGraphQl/Model/Product/Option/DateType.php +++ b/app/code/Magento/CatalogGraphQl/Model/Product/Option/DateType.php @@ -13,7 +13,10 @@ use Magento\Framework\GraphQl\Exception\GraphQlInputException; /** - * @inheritdoc + * CatalogGraphQl product option date type. + * + * @author Magento Core Team + * @SuppressWarnings(PHPMD.CookieAndSessionMisuse) */ class DateType extends ProductDateOptionType { From ab4e73654bb2b9eceb010e6adc3368435e696326 Mon Sep 17 00:00:00 2001 From: Sergey Dovbenko Date: Sun, 22 Sep 2019 16:09:20 +0000 Subject: [PATCH 3/9] Covered with api-functional Tests --- ...mpleProductWithCustomOptionsToCartTest.php | 58 +++++++++++++++++++ .../GetCustomOptionsValuesForQueryBySku.php | 7 ++- .../GetEmptyOptionsValuesForQueryBySku.php | 53 +++++++++++++++++ 3 files changed, 117 insertions(+), 1 deletion(-) create mode 100644 dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/GetEmptyOptionsValuesForQueryBySku.php diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/AddSimpleProductWithCustomOptionsToCartTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/AddSimpleProductWithCustomOptionsToCartTest.php index b0b116b0cddad..272c0df29e04b 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/AddSimpleProductWithCustomOptionsToCartTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/AddSimpleProductWithCustomOptionsToCartTest.php @@ -31,6 +31,11 @@ class AddSimpleProductWithCustomOptionsToCartTest extends GraphQlAbstract */ private $getCustomOptionsValuesForQueryBySku; + /** + * @var GetEmptyOptionsValuesForQueryBySku + */ + private $getEmptyOptionsValuesForQueryBySku; + /** * @inheritdoc */ @@ -40,6 +45,7 @@ protected function setUp() $this->getMaskedQuoteIdByReservedOrderId = $objectManager->get(GetMaskedQuoteIdByReservedOrderId::class); $this->productCustomOptionsRepository = $objectManager->get(ProductCustomOptionRepositoryInterface::class); $this->getCustomOptionsValuesForQueryBySku = $objectManager->get(GetCustomOptionsValuesForQueryBySku::class); + $this->getEmptyOptionsValuesForQueryBySku = $objectManager->get(GetEmptyOptionsValuesForQueryBySku::class); } /** @@ -99,6 +105,58 @@ public function testAddSimpleProductWithMissedRequiredOptionsSet() $this->graphQlMutation($query); } + /** + * Test adding a simple product to the shopping cart with Date customizable option assigned + * + * @magentoApiDataFixture Magento/Catalog/_files/product_simple_with_option_date.php + * @magentoApiDataFixture Magento/Checkout/_files/active_quote.php + */ + public function testAddSimpleProductWithDateOption() + { + $sku = 'simple-product-1'; + $quantity = 1; + $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_order_1'); + + $customOptionsValues = $this->getCustomOptionsValuesForQueryBySku->execute($sku); + $queryCustomizableOptionValues = preg_replace('/"([^"]+)"\s*:\s*/', '$1:', json_encode($customOptionsValues)); + $customizableOptions = "customizable_options: {$queryCustomizableOptionValues}"; + $query = $this->getQuery($maskedQuoteId, $sku, $quantity, $customizableOptions); + + $response = $this->graphQlMutation($query); + + self::assertArrayHasKey('items', $response['addSimpleProductsToCart']['cart']); + self::assertCount(1, $response['addSimpleProductsToCart']['cart']); + + $customizableOptionOutput = $response['addSimpleProductsToCart']['cart']['items'][0]['customizable_options'][0]['values'][0]['value']; + $expectedValue = date("M d, Y", strtotime($customOptionsValues[0]['value_string'])); + + self::assertEquals($expectedValue, $customizableOptionOutput); + } + + /** + * Test adding a simple product with empty values for date option + * + * @magentoApiDataFixture Magento/Catalog/_files/product_simple_with_option_date.php + * @magentoApiDataFixture Magento/Checkout/_files/active_quote.php + */ + public function testAddSimpleProductWithMissedDateOptionsSet() + { + $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_order_1'); + $sku = 'simple-product-1'; + $quantity = 1; + + $customOptionsValues = $this->getEmptyOptionsValuesForQueryBySku->execute($sku); + $queryCustomizableOptionValues = preg_replace('/"([^"]+)"\s*:\s*/', '$1:', json_encode($customOptionsValues)); + $customizableOptions = "customizable_options: {$queryCustomizableOptionValues}"; + $query = $this->getQuery($maskedQuoteId, $sku, $quantity, $customizableOptions); + + self::expectExceptionMessage( + 'Invalid format provided. Please use \'Y-m-d H:i:s\' format.' + ); + + $this->graphQlMutation($query); + } + /** * @param string $maskedQuoteId * @param string $sku diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/GetCustomOptionsValuesForQueryBySku.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/GetCustomOptionsValuesForQueryBySku.php index 7514eb1c4e1d0..8bc17cba0bf72 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/GetCustomOptionsValuesForQueryBySku.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/GetCustomOptionsValuesForQueryBySku.php @@ -40,7 +40,12 @@ public function execute(string $sku): array foreach ($customOptions as $customOption) { $optionType = $customOption->getType(); - if ($optionType == 'field' || $optionType == 'area') { + if ($optionType == 'date') { + $customOptionsValues[] = [ + 'id' => (int)$customOption->getOptionId(), + 'value_string' => '2012-12-12 00:00:00' + ]; + } elseif ($optionType == 'field' || $optionType == 'area') { $customOptionsValues[] = [ 'id' => (int)$customOption->getOptionId(), 'value_string' => 'test' diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/GetEmptyOptionsValuesForQueryBySku.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/GetEmptyOptionsValuesForQueryBySku.php new file mode 100644 index 0000000000000..b6c0fecf0f1ce --- /dev/null +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/GetEmptyOptionsValuesForQueryBySku.php @@ -0,0 +1,53 @@ +productCustomOptionRepository = $productCustomOptionRepository; + } + + /** + * Returns array of empty options for the product + * + * @param string $sku + * @return array + */ + public function execute(string $sku): array + { + $customOptions = $this->productCustomOptionRepository->getList($sku); + $customOptionsValues = []; + + foreach ($customOptions as $customOption) { + $optionType = $customOption->getType(); + if ($optionType == 'date') { + $customOptionsValues[] = [ + 'id' => (int)$customOption->getOptionId(), + 'value_string' => '' + ]; + } + } + + return $customOptionsValues; + } +} From 1e821dd8aa6f11c9fbef3c2f6b118c03c3b17a92 Mon Sep 17 00:00:00 2001 From: Sergey Dovbenko Date: Tue, 24 Sep 2019 19:59:12 +0000 Subject: [PATCH 4/9] Corrected Code Styles --- .../Quote/AddSimpleProductWithCustomOptionsToCartTest.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/AddSimpleProductWithCustomOptionsToCartTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/AddSimpleProductWithCustomOptionsToCartTest.php index 272c0df29e04b..95f2e05e63958 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/AddSimpleProductWithCustomOptionsToCartTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/AddSimpleProductWithCustomOptionsToCartTest.php @@ -127,7 +127,8 @@ public function testAddSimpleProductWithDateOption() self::assertArrayHasKey('items', $response['addSimpleProductsToCart']['cart']); self::assertCount(1, $response['addSimpleProductsToCart']['cart']); - $customizableOptionOutput = $response['addSimpleProductsToCart']['cart']['items'][0]['customizable_options'][0]['values'][0]['value']; + $cartItem = $response['addSimpleProductsToCart']['cart']['items'][0]; + $customizableOptionOutput = $cartItem['customizable_options'][0]['values'][0]['value']; $expectedValue = date("M d, Y", strtotime($customOptionsValues[0]['value_string'])); self::assertEquals($expectedValue, $customizableOptionOutput); From 10d20bf302f7676d7b7801b7b78f0ff61f630e37 Mon Sep 17 00:00:00 2001 From: Lena Orobei Date: Mon, 14 Oct 2019 11:33:36 -0500 Subject: [PATCH 5/9] magento/graphql-ce#761: [Customizable Options] Call to a member function format() on boolean --- .../Model/Product/Option/DateType.php | 2 +- ...mpleProductWithCustomOptionsToCartTest.php | 69 +++++-------------- .../GetCustomOptionsValuesForQueryBySku.php | 17 ++--- .../GetEmptyOptionsValuesForQueryBySku.php | 53 -------------- .../_files/product_simple_with_options.php | 9 +++ 5 files changed, 38 insertions(+), 112 deletions(-) delete mode 100644 dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/GetEmptyOptionsValuesForQueryBySku.php diff --git a/app/code/Magento/CatalogGraphQl/Model/Product/Option/DateType.php b/app/code/Magento/CatalogGraphQl/Model/Product/Option/DateType.php index 5205115b772ed..984467080a2d6 100644 --- a/app/code/Magento/CatalogGraphQl/Model/Product/Option/DateType.php +++ b/app/code/Magento/CatalogGraphQl/Model/Product/Option/DateType.php @@ -48,7 +48,7 @@ private function formatValues($values) $value = $values[$this->getOption()->getId()]; $dateTime = \DateTime::createFromFormat(DateTime::DATETIME_PHP_FORMAT, $value); - if (!$dateTime) { + if ($dateTime === false) { throw new GraphQlInputException( __('Invalid format provided. Please use \'Y-m-d H:i:s\' format.') ); diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/AddSimpleProductWithCustomOptionsToCartTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/AddSimpleProductWithCustomOptionsToCartTest.php index 95f2e05e63958..7d1d15105b81d 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/AddSimpleProductWithCustomOptionsToCartTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/AddSimpleProductWithCustomOptionsToCartTest.php @@ -31,11 +31,6 @@ class AddSimpleProductWithCustomOptionsToCartTest extends GraphQlAbstract */ private $getCustomOptionsValuesForQueryBySku; - /** - * @var GetEmptyOptionsValuesForQueryBySku - */ - private $getEmptyOptionsValuesForQueryBySku; - /** * @inheritdoc */ @@ -45,7 +40,6 @@ protected function setUp() $this->getMaskedQuoteIdByReservedOrderId = $objectManager->get(GetMaskedQuoteIdByReservedOrderId::class); $this->productCustomOptionsRepository = $objectManager->get(ProductCustomOptionRepositoryInterface::class); $this->getCustomOptionsValuesForQueryBySku = $objectManager->get(GetCustomOptionsValuesForQueryBySku::class); - $this->getEmptyOptionsValuesForQueryBySku = $objectManager->get(GetEmptyOptionsValuesForQueryBySku::class); } /** @@ -63,7 +57,7 @@ public function testAddSimpleProductWithOptions() $customOptionsValues = $this->getCustomOptionsValuesForQueryBySku->execute($sku); /* Generate customizable options fragment for GraphQl request */ - $queryCustomizableOptionValues = preg_replace('/"([^"]+)"\s*:\s*/', '$1:', json_encode($customOptionsValues)); + $queryCustomizableOptionValues = preg_replace('/"([^"]+)"\s*:\s*/', '$1:', json_encode(array_values($customOptionsValues))); $customizableOptions = "customizable_options: {$queryCustomizableOptionValues}"; $query = $this->getQuery($maskedQuoteId, $sku, $quantity, $customizableOptions); @@ -74,13 +68,14 @@ public function testAddSimpleProductWithOptions() self::assertCount(1, $response['addSimpleProductsToCart']['cart']); $customizableOptionsOutput = $response['addSimpleProductsToCart']['cart']['items'][0]['customizable_options']; - $assignedOptionsCount = count($customOptionsValues); - for ($counter = 0; $counter < $assignedOptionsCount; $counter++) { - $expectedValues = $this->buildExpectedValuesArray($customOptionsValues[$counter]['value_string']); + $count = 0; + foreach ($customOptionsValues as $type => $value) { + $expectedValues = $this->buildExpectedValuesArray($value['value_string'], $type); self::assertEquals( $expectedValues, - $customizableOptionsOutput[$counter]['values'] + $customizableOptionsOutput[$count]['values'] ); + $count++; } } @@ -106,54 +101,24 @@ public function testAddSimpleProductWithMissedRequiredOptionsSet() } /** - * Test adding a simple product to the shopping cart with Date customizable option assigned + * Test adding a simple product with wrong format value for date option * - * @magentoApiDataFixture Magento/Catalog/_files/product_simple_with_option_date.php - * @magentoApiDataFixture Magento/Checkout/_files/active_quote.php - */ - public function testAddSimpleProductWithDateOption() - { - $sku = 'simple-product-1'; - $quantity = 1; - $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_order_1'); - - $customOptionsValues = $this->getCustomOptionsValuesForQueryBySku->execute($sku); - $queryCustomizableOptionValues = preg_replace('/"([^"]+)"\s*:\s*/', '$1:', json_encode($customOptionsValues)); - $customizableOptions = "customizable_options: {$queryCustomizableOptionValues}"; - $query = $this->getQuery($maskedQuoteId, $sku, $quantity, $customizableOptions); - - $response = $this->graphQlMutation($query); - - self::assertArrayHasKey('items', $response['addSimpleProductsToCart']['cart']); - self::assertCount(1, $response['addSimpleProductsToCart']['cart']); - - $cartItem = $response['addSimpleProductsToCart']['cart']['items'][0]; - $customizableOptionOutput = $cartItem['customizable_options'][0]['values'][0]['value']; - $expectedValue = date("M d, Y", strtotime($customOptionsValues[0]['value_string'])); - - self::assertEquals($expectedValue, $customizableOptionOutput); - } - - /** - * Test adding a simple product with empty values for date option - * - * @magentoApiDataFixture Magento/Catalog/_files/product_simple_with_option_date.php + * @magentoApiDataFixture Magento/Catalog/_files/product_simple_with_options.php * @magentoApiDataFixture Magento/Checkout/_files/active_quote.php */ - public function testAddSimpleProductWithMissedDateOptionsSet() + public function testAddSimpleProductWithWrongDateOptionFormat() { $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_order_1'); - $sku = 'simple-product-1'; + $sku = 'simple'; $quantity = 1; - $customOptionsValues = $this->getEmptyOptionsValuesForQueryBySku->execute($sku); - $queryCustomizableOptionValues = preg_replace('/"([^"]+)"\s*:\s*/', '$1:', json_encode($customOptionsValues)); + $customOptionsValues = $this->getCustomOptionsValuesForQueryBySku->execute($sku); + $customOptionsValues['date']['value_string'] = '12-12-12'; + $queryCustomizableOptionValues = preg_replace('/"([^"]+)"\s*:\s*/', '$1:', json_encode(array_values($customOptionsValues))); $customizableOptions = "customizable_options: {$queryCustomizableOptionValues}"; $query = $this->getQuery($maskedQuoteId, $sku, $quantity, $customizableOptions); - self::expectExceptionMessage( - 'Invalid format provided. Please use \'Y-m-d H:i:s\' format.' - ); + $this->expectExceptionMessage('Invalid format provided. Please use \'Y-m-d H:i:s\' format.'); $this->graphQlMutation($query); } @@ -204,10 +169,14 @@ private function getQuery(string $maskedQuoteId, string $sku, float $quantity, s * Build the part of expected response. * * @param string $assignedValue + * @param string $type option type * @return array */ - private function buildExpectedValuesArray(string $assignedValue) : array + private function buildExpectedValuesArray(string $assignedValue, string $type) : array { + if ($type === 'date'){ + return [['value' => date('M d, Y', strtotime($assignedValue))]]; + } $assignedOptionsArray = explode(',', trim($assignedValue, '[]')); $expectedArray = []; foreach ($assignedOptionsArray as $assignedOption) { diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/GetCustomOptionsValuesForQueryBySku.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/GetCustomOptionsValuesForQueryBySku.php index 8bc17cba0bf72..f08849533da2c 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/GetCustomOptionsValuesForQueryBySku.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/GetCustomOptionsValuesForQueryBySku.php @@ -41,25 +41,26 @@ public function execute(string $sku): array foreach ($customOptions as $customOption) { $optionType = $customOption->getType(); if ($optionType == 'date') { - $customOptionsValues[] = [ + $customOptionsValues[$optionType] = [ 'id' => (int)$customOption->getOptionId(), - 'value_string' => '2012-12-12 00:00:00' + 'value_string' => '2012-12-12 00:00:00', ]; } elseif ($optionType == 'field' || $optionType == 'area') { - $customOptionsValues[] = [ + $customOptionsValues[$optionType] = [ 'id' => (int)$customOption->getOptionId(), - 'value_string' => 'test' + 'value_string' => 'test', ]; } elseif ($optionType == 'drop_down') { $optionSelectValues = $customOption->getValues(); - $customOptionsValues[] = [ + $customOptionsValues[$optionType] = [ 'id' => (int)$customOption->getOptionId(), - 'value_string' => reset($optionSelectValues)->getOptionTypeId() + 'value_string' => reset($optionSelectValues)->getOptionTypeId(), ]; } elseif ($optionType == 'multiple') { - $customOptionsValues[] = [ + $customOptionsValues[$optionType] = [ 'id' => (int)$customOption->getOptionId(), - 'value_string' => '[' . implode(',', array_keys($customOption->getValues())) . ']' + 'value_string' => '[' . implode(',', array_keys($customOption->getValues())) . ']', + ]; } } diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/GetEmptyOptionsValuesForQueryBySku.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/GetEmptyOptionsValuesForQueryBySku.php deleted file mode 100644 index b6c0fecf0f1ce..0000000000000 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/GetEmptyOptionsValuesForQueryBySku.php +++ /dev/null @@ -1,53 +0,0 @@ -productCustomOptionRepository = $productCustomOptionRepository; - } - - /** - * Returns array of empty options for the product - * - * @param string $sku - * @return array - */ - public function execute(string $sku): array - { - $customOptions = $this->productCustomOptionRepository->getList($sku); - $customOptionsValues = []; - - foreach ($customOptions as $customOption) { - $optionType = $customOption->getType(); - if ($optionType == 'date') { - $customOptionsValues[] = [ - 'id' => (int)$customOption->getOptionId(), - 'value_string' => '' - ]; - } - } - - return $customOptionsValues; - } -} diff --git a/dev/tests/integration/testsuite/Magento/Catalog/_files/product_simple_with_options.php b/dev/tests/integration/testsuite/Magento/Catalog/_files/product_simple_with_options.php index 93c7ba61c6f49..67288bec86ad5 100644 --- a/dev/tests/integration/testsuite/Magento/Catalog/_files/product_simple_with_options.php +++ b/dev/tests/integration/testsuite/Magento/Catalog/_files/product_simple_with_options.php @@ -106,6 +106,15 @@ 'sort_order' => 2, ], ], + ], + [ + 'title' => 'date option', + 'type' => 'date', + 'price' => 80.0, + 'price_type' => 'fixed', + 'sku' => 'date option sku', + 'is_require' => false, + 'sort_order' => 6 ] ]; From 0809b691d5406fef955c46f18aef14d21def24c8 Mon Sep 17 00:00:00 2001 From: Lena Orobei Date: Mon, 14 Oct 2019 11:56:21 -0500 Subject: [PATCH 6/9] magento/graphql-ce#761: [Customizable Options] Call to a member function format() on boolean --- .../Model/Product/Option/DateType.php | 5 +-- ...mpleProductWithCustomOptionsToCartTest.php | 12 ++++-- .../GetCustomOptionsValuesForQueryBySku.php | 41 ++++++++----------- 3 files changed, 28 insertions(+), 30 deletions(-) diff --git a/app/code/Magento/CatalogGraphQl/Model/Product/Option/DateType.php b/app/code/Magento/CatalogGraphQl/Model/Product/Option/DateType.php index 984467080a2d6..2293ea643c94c 100644 --- a/app/code/Magento/CatalogGraphQl/Model/Product/Option/DateType.php +++ b/app/code/Magento/CatalogGraphQl/Model/Product/Option/DateType.php @@ -13,10 +13,7 @@ use Magento\Framework\GraphQl\Exception\GraphQlInputException; /** - * CatalogGraphQl product option date type. - * - * @author Magento Core Team - * @SuppressWarnings(PHPMD.CookieAndSessionMisuse) + * CatalogGraphQl product option date type */ class DateType extends ProductDateOptionType { diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/AddSimpleProductWithCustomOptionsToCartTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/AddSimpleProductWithCustomOptionsToCartTest.php index 7d1d15105b81d..82ae9331bc4f3 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/AddSimpleProductWithCustomOptionsToCartTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/AddSimpleProductWithCustomOptionsToCartTest.php @@ -57,7 +57,10 @@ public function testAddSimpleProductWithOptions() $customOptionsValues = $this->getCustomOptionsValuesForQueryBySku->execute($sku); /* Generate customizable options fragment for GraphQl request */ - $queryCustomizableOptionValues = preg_replace('/"([^"]+)"\s*:\s*/', '$1:', json_encode(array_values($customOptionsValues))); + $queryCustomizableOptionValues = preg_replace( + '/"([^"]+)"\s*:\s*/', '$1:', + json_encode(array_values($customOptionsValues)) + ); $customizableOptions = "customizable_options: {$queryCustomizableOptionValues}"; $query = $this->getQuery($maskedQuoteId, $sku, $quantity, $customizableOptions); @@ -114,7 +117,10 @@ public function testAddSimpleProductWithWrongDateOptionFormat() $customOptionsValues = $this->getCustomOptionsValuesForQueryBySku->execute($sku); $customOptionsValues['date']['value_string'] = '12-12-12'; - $queryCustomizableOptionValues = preg_replace('/"([^"]+)"\s*:\s*/', '$1:', json_encode(array_values($customOptionsValues))); + $queryCustomizableOptionValues = preg_replace( + '/"([^"]+)"\s*:\s*/', '$1:', + json_encode(array_values($customOptionsValues)) + ); $customizableOptions = "customizable_options: {$queryCustomizableOptionValues}"; $query = $this->getQuery($maskedQuoteId, $sku, $quantity, $customizableOptions); @@ -174,7 +180,7 @@ private function getQuery(string $maskedQuoteId, string $sku, float $quantity, s */ private function buildExpectedValuesArray(string $assignedValue, string $type) : array { - if ($type === 'date'){ + if ($type === 'date') { return [['value' => date('M d, Y', strtotime($assignedValue))]]; } $assignedOptionsArray = explode(',', trim($assignedValue, '[]')); diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/GetCustomOptionsValuesForQueryBySku.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/GetCustomOptionsValuesForQueryBySku.php index f08849533da2c..62cacd3e07c16 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/GetCustomOptionsValuesForQueryBySku.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/GetCustomOptionsValuesForQueryBySku.php @@ -40,31 +40,26 @@ public function execute(string $sku): array foreach ($customOptions as $customOption) { $optionType = $customOption->getType(); - if ($optionType == 'date') { - $customOptionsValues[$optionType] = [ - 'id' => (int)$customOption->getOptionId(), - 'value_string' => '2012-12-12 00:00:00', - ]; - } elseif ($optionType == 'field' || $optionType == 'area') { - $customOptionsValues[$optionType] = [ - 'id' => (int)$customOption->getOptionId(), - 'value_string' => 'test', - ]; - } elseif ($optionType == 'drop_down') { - $optionSelectValues = $customOption->getValues(); - $customOptionsValues[$optionType] = [ - 'id' => (int)$customOption->getOptionId(), - 'value_string' => reset($optionSelectValues)->getOptionTypeId(), - ]; - } elseif ($optionType == 'multiple') { - $customOptionsValues[$optionType] = [ - 'id' => (int)$customOption->getOptionId(), - 'value_string' => '[' . implode(',', array_keys($customOption->getValues())) . ']', - - ]; + $customOptionsValues[$optionType]['id'] = (int)$customOption->getOptionId(); + switch ($optionType) { + case 'date': + $customOptionsValues[$optionType]['value_string'] = '2012-12-12 00:00:00'; + break; + case 'field': + case 'area': + $customOptionsValues[$optionType]['value_string'] = 'test'; + break; + case 'drop_down': + $optionSelectValues = $customOption->getValues(); + $customOptionsValues[$optionType]['value_string'] = + reset($optionSelectValues)->getOptionTypeId(); + break; + case 'multiple': + $customOptionsValues[$optionType]['value_string'] = + '[' . implode(',', array_keys($customOption->getValues())) . ']'; + break; } } - return $customOptionsValues; } } From d2039f2cfea4a731791b4c340fb9cb73979f04f7 Mon Sep 17 00:00:00 2001 From: Lena Orobei Date: Mon, 14 Oct 2019 12:25:39 -0500 Subject: [PATCH 7/9] magento/graphql-ce#761: [Customizable Options] Call to a member function format() on boolean --- .../CatalogGraphQl/Model/Product/Option/DateType.php | 2 ++ .../Quote/AddSimpleProductWithCustomOptionsToCartTest.php | 6 ++++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/app/code/Magento/CatalogGraphQl/Model/Product/Option/DateType.php b/app/code/Magento/CatalogGraphQl/Model/Product/Option/DateType.php index 2293ea643c94c..cd582ffda9244 100644 --- a/app/code/Magento/CatalogGraphQl/Model/Product/Option/DateType.php +++ b/app/code/Magento/CatalogGraphQl/Model/Product/Option/DateType.php @@ -14,6 +14,8 @@ /** * CatalogGraphQl product option date type + * + * @SuppressWarnings(PHPMD.CookieAndSessionMisuse) */ class DateType extends ProductDateOptionType { diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/AddSimpleProductWithCustomOptionsToCartTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/AddSimpleProductWithCustomOptionsToCartTest.php index 82ae9331bc4f3..5c2bc10bf771e 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/AddSimpleProductWithCustomOptionsToCartTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/AddSimpleProductWithCustomOptionsToCartTest.php @@ -58,7 +58,8 @@ public function testAddSimpleProductWithOptions() $customOptionsValues = $this->getCustomOptionsValuesForQueryBySku->execute($sku); /* Generate customizable options fragment for GraphQl request */ $queryCustomizableOptionValues = preg_replace( - '/"([^"]+)"\s*:\s*/', '$1:', + '/"([^"]+)"\s*:\s*/', + '$1:', json_encode(array_values($customOptionsValues)) ); @@ -118,7 +119,8 @@ public function testAddSimpleProductWithWrongDateOptionFormat() $customOptionsValues = $this->getCustomOptionsValuesForQueryBySku->execute($sku); $customOptionsValues['date']['value_string'] = '12-12-12'; $queryCustomizableOptionValues = preg_replace( - '/"([^"]+)"\s*:\s*/', '$1:', + '/"([^"]+)"\s*:\s*/', + '$1:', json_encode(array_values($customOptionsValues)) ); $customizableOptions = "customizable_options: {$queryCustomizableOptionValues}"; From facce4c963381ef3c43ac0d01726820537c2f7dd Mon Sep 17 00:00:00 2001 From: Lena Orobei Date: Mon, 14 Oct 2019 13:33:22 -0500 Subject: [PATCH 8/9] magento/graphql-ce#761: [Customizable Options] Call to a member function format() on boolean --- .../AddDownloadableProductWithCustomOptionsToCartTest.php | 6 +++++- .../Quote/AddVirtualProductWithCustomOptionsToCartTest.php | 6 +++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/AddDownloadableProductWithCustomOptionsToCartTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/AddDownloadableProductWithCustomOptionsToCartTest.php index fa7d1194c7f83..60ced7f8cb2a5 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/AddDownloadableProductWithCustomOptionsToCartTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/AddDownloadableProductWithCustomOptionsToCartTest.php @@ -58,7 +58,11 @@ public function testAddDownloadableProductWithOptions() $customOptionsValues = $this->getCustomOptionsValuesForQueryBySku->execute($sku); /* Generate customizable options fragment for GraphQl request */ - $queryCustomizableOptionValues = preg_replace('/"([^"]+)"\s*:\s*/', '$1:', json_encode($customOptionsValues)); + $queryCustomizableOptionValues = preg_replace( + '/"([^"]+)"\s*:\s*/', + '$1:', + json_encode(array_values($customOptionsValues)) + ); $customizableOptions = "customizable_options: {$queryCustomizableOptionValues}"; $query = $this->getQuery($maskedQuoteId, $qty, $sku, $customizableOptions, $linkId); diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/AddVirtualProductWithCustomOptionsToCartTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/AddVirtualProductWithCustomOptionsToCartTest.php index a8088b0b46b87..4a043a7315e23 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/AddVirtualProductWithCustomOptionsToCartTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/AddVirtualProductWithCustomOptionsToCartTest.php @@ -57,7 +57,11 @@ public function testAddVirtualProductWithOptions() $customOptionsValues = $this->getCustomOptionsValuesForQueryBySku->execute($sku); /* Generate customizable options fragment for GraphQl request */ - $queryCustomizableOptionValues = preg_replace('/"([^"]+)"\s*:\s*/', '$1:', json_encode($customOptionsValues)); + $queryCustomizableOptionValues = preg_replace( + '/"([^"]+)"\s*:\s*/', + '$1:', + json_encode(array_values($customOptionsValues)) + ); $customizableOptions = "customizable_options: {$queryCustomizableOptionValues}"; $query = $this->getQuery($maskedQuoteId, $sku, $quantity, $customizableOptions); From 7653126e8eba4593cc7433715312540062af3508 Mon Sep 17 00:00:00 2001 From: Lena Orobei Date: Mon, 14 Oct 2019 14:37:51 -0500 Subject: [PATCH 9/9] magento/graphql-ce#761: [Customizable Options] Call to a member function format() on boolean --- ...AddDownloadableProductWithCustomOptionsToCartTest.php | 9 +++++---- .../AddVirtualProductWithCustomOptionsToCartTest.php | 9 +++++---- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/AddDownloadableProductWithCustomOptionsToCartTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/AddDownloadableProductWithCustomOptionsToCartTest.php index 60ced7f8cb2a5..8b8973ad0fd95 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/AddDownloadableProductWithCustomOptionsToCartTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/AddDownloadableProductWithCustomOptionsToCartTest.php @@ -72,13 +72,14 @@ public function testAddDownloadableProductWithOptions() self::assertCount($qty, $response['addDownloadableProductsToCart']['cart']); $customizableOptionsOutput = $response['addDownloadableProductsToCart']['cart']['items'][0]['customizable_options']; - $assignedOptionsCount = count($customOptionsValues); - for ($counter = 0; $counter < $assignedOptionsCount; $counter++) { - $expectedValues = $this->buildExpectedValuesArray($customOptionsValues[$counter]['value_string']); + $count = 0; + foreach ($customOptionsValues as $value) { + $expectedValues = $this->buildExpectedValuesArray($value['value_string']); self::assertEquals( $expectedValues, - $customizableOptionsOutput[$counter]['values'] + $customizableOptionsOutput[$count]['values'] ); + $count++; } } diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/AddVirtualProductWithCustomOptionsToCartTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/AddVirtualProductWithCustomOptionsToCartTest.php index 4a043a7315e23..561318889e325 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/AddVirtualProductWithCustomOptionsToCartTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/AddVirtualProductWithCustomOptionsToCartTest.php @@ -72,13 +72,14 @@ public function testAddVirtualProductWithOptions() self::assertCount(1, $response['addVirtualProductsToCart']['cart']); $customizableOptionsOutput = $response['addVirtualProductsToCart']['cart']['items'][0]['customizable_options']; - $assignedOptionsCount = count($customOptionsValues); - for ($counter = 0; $counter < $assignedOptionsCount; $counter++) { - $expectedValues = $this->buildExpectedValuesArray($customOptionsValues[$counter]['value_string']); + $count = 0; + foreach ($customOptionsValues as $value) { + $expectedValues = $this->buildExpectedValuesArray($value['value_string']); self::assertEquals( $expectedValues, - $customizableOptionsOutput[$counter]['values'] + $customizableOptionsOutput[$count]['values'] ); + $count++; } }