From ab4e73654bb2b9eceb010e6adc3368435e696326 Mon Sep 17 00:00:00 2001 From: Sergey Dovbenko Date: Sun, 22 Sep 2019 16:09:20 +0000 Subject: [PATCH] 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; + } +}