From bafb07d1ae3ab551b06a9cbad197bd5cffb6a2ce Mon Sep 17 00:00:00 2001 From: Yaroslav Rogoza Date: Fri, 23 Oct 2020 16:06:20 +0200 Subject: [PATCH 1/3] Fixed exposing special price if the current date does not intersects with the period --- .../CatalogGraphQl/Model/Resolver/Product/SpecialPrice.php | 6 ++++++ .../CollectionProcessor/RequiredColumnsProcessor.php | 4 ++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/app/code/Magento/CatalogGraphQl/Model/Resolver/Product/SpecialPrice.php b/app/code/Magento/CatalogGraphQl/Model/Resolver/Product/SpecialPrice.php index c80cc3744876f..75156a308f600 100644 --- a/app/code/Magento/CatalogGraphQl/Model/Resolver/Product/SpecialPrice.php +++ b/app/code/Magento/CatalogGraphQl/Model/Resolver/Product/SpecialPrice.php @@ -7,6 +7,7 @@ namespace Magento\CatalogGraphQl\Model\Resolver\Product; +use Magento\Framework\Exception\LocalizedException; use Magento\Framework\GraphQl\Config\Element\Field; use Magento\Framework\GraphQl\Query\ResolverInterface; use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; @@ -25,6 +26,11 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value { /** @var ProductInterface $product */ $product = $value['model']; + + if (!isset($value['model'])) { + throw new LocalizedException(__('"model" value should be specified')); + } + /** @var PricingSpecialPrice $specialPrice */ $specialPrice = $product->getPriceInfo()->getPrice(PricingSpecialPrice::PRICE_CODE); diff --git a/app/code/Magento/CatalogGraphQl/Model/Resolver/Products/DataProvider/Product/CollectionProcessor/RequiredColumnsProcessor.php b/app/code/Magento/CatalogGraphQl/Model/Resolver/Products/DataProvider/Product/CollectionProcessor/RequiredColumnsProcessor.php index b545047d01541..721441cd08d62 100644 --- a/app/code/Magento/CatalogGraphQl/Model/Resolver/Products/DataProvider/Product/CollectionProcessor/RequiredColumnsProcessor.php +++ b/app/code/Magento/CatalogGraphQl/Model/Resolver/Products/DataProvider/Product/CollectionProcessor/RequiredColumnsProcessor.php @@ -36,8 +36,8 @@ public function process( ContextInterface $context = null ): Collection { $collection->addAttributeToSelect('special_price'); - $collection->addAttributeToSelect('special_price_from'); - $collection->addAttributeToSelect('special_price_to'); + $collection->addAttributeToSelect('special_from_date'); + $collection->addAttributeToSelect('special_to_date'); $collection->addAttributeToSelect('tax_class_id'); return $collection; From 0153d51561791c81b627d8ff22485faf7ec0e7ce Mon Sep 17 00:00:00 2001 From: Yaroslav Rogoza Date: Fri, 23 Oct 2020 17:48:49 +0200 Subject: [PATCH 2/3] Added test coverage --- .../Model/Resolver/Product/SpecialPrice.php | 6 +- .../GraphQl/Catalog/ProductPriceTest.php | 79 +++++++++++++++++-- .../set_simple_product_special_price.php | 21 +++++ ...mple_product_special_price_future_date.php | 21 +++++ 4 files changed, 118 insertions(+), 9 deletions(-) create mode 100644 dev/tests/integration/testsuite/Magento/GraphQl/Catalog/_files/set_simple_product_special_price.php create mode 100644 dev/tests/integration/testsuite/Magento/GraphQl/Catalog/_files/set_simple_product_special_price_future_date.php diff --git a/app/code/Magento/CatalogGraphQl/Model/Resolver/Product/SpecialPrice.php b/app/code/Magento/CatalogGraphQl/Model/Resolver/Product/SpecialPrice.php index 75156a308f600..954607f1f66e2 100644 --- a/app/code/Magento/CatalogGraphQl/Model/Resolver/Product/SpecialPrice.php +++ b/app/code/Magento/CatalogGraphQl/Model/Resolver/Product/SpecialPrice.php @@ -24,13 +24,13 @@ class SpecialPrice implements ResolverInterface */ public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null) { - /** @var ProductInterface $product */ - $product = $value['model']; - if (!isset($value['model'])) { throw new LocalizedException(__('"model" value should be specified')); } + /** @var ProductInterface $product */ + $product = $value['model']; + /** @var PricingSpecialPrice $specialPrice */ $specialPrice = $product->getPriceInfo()->getPrice(PricingSpecialPrice::PRICE_CODE); diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Catalog/ProductPriceTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Catalog/ProductPriceTest.php index f1c1be44ccd13..bd48bb34cce6a 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Catalog/ProductPriceTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Catalog/ProductPriceTest.php @@ -832,6 +832,73 @@ public function testProductWithCatalogDiscount() } } + /** + * Check if the special price visible if the current date is in the date range set + * for the special price + * + * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php + * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/set_simple_product_special_price.php + */ + public function testSpecialPriceVisibleIfInDateRange() + { + $query = <<graphQlQuery($query); + $productInformation = $result['products']['items'][0]; + $productRegularPrice = $productInformation['price_range']['minimum_price']['regular_price']['value']; + + self::assertEquals('10', $productRegularPrice); + self::assertEquals('5.99', $productInformation['special_price']); + } + + + /** + * Check if the special price is not visible if the current date is not in the date range set + * for the special price + * + * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php + * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/set_simple_product_special_price_future_date.php + */ + public function testSpecialPriceNotVisibleIfNotInDateRange() + { + $query = <<graphQlQuery($query); + $productInformation = $result['products']['items'][0]; + $productRegularPrice = $productInformation['price_range']['minimum_price']['regular_price']['value']; + + self::assertEquals('10', $productRegularPrice); + self::assertEquals(null, $productInformation['special_price']); + } + /** * Get GraphQl query to fetch products by sku * @@ -909,7 +976,7 @@ private function getQueryConfigurableProductAndVariants(array $sku): string name sku price_range { - minimum_price {regular_price + minimum_price {regular_price { value currency @@ -949,13 +1016,13 @@ private function getQueryConfigurableProductAndVariants(array $sku): string ... on ConfigurableProduct{ variants{ product{ - + sku price_range { minimum_price {regular_price {value} final_price { value - + } discount { amount_off @@ -965,11 +1032,11 @@ private function getQueryConfigurableProductAndVariants(array $sku): string maximum_price { regular_price { value - + } final_price { value - + } discount { amount_off @@ -985,7 +1052,7 @@ private function getQueryConfigurableProductAndVariants(array $sku): string final_price{value} quantity } - + } } } diff --git a/dev/tests/integration/testsuite/Magento/GraphQl/Catalog/_files/set_simple_product_special_price.php b/dev/tests/integration/testsuite/Magento/GraphQl/Catalog/_files/set_simple_product_special_price.php new file mode 100644 index 0000000000000..04ba6b74d3f6e --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/GraphQl/Catalog/_files/set_simple_product_special_price.php @@ -0,0 +1,21 @@ +get(ProductRepositoryInterface::class); + +$product = $productRepository->get('simple_product'); +$product->setSpecialPrice('5.99'); + +$product->setSpecialFromDate(date('Y-m-d', strtotime('-1 day'))); +$product->setSpecialToDate(date('Y-m-d', strtotime('+1 day'))); + +$productRepository->save($product); diff --git a/dev/tests/integration/testsuite/Magento/GraphQl/Catalog/_files/set_simple_product_special_price_future_date.php b/dev/tests/integration/testsuite/Magento/GraphQl/Catalog/_files/set_simple_product_special_price_future_date.php new file mode 100644 index 0000000000000..66bd3d25ed40a --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/GraphQl/Catalog/_files/set_simple_product_special_price_future_date.php @@ -0,0 +1,21 @@ +get(ProductRepositoryInterface::class); + +$product = $productRepository->get('simple_product'); +$product->setSpecialPrice('5.99'); + +$product->setSpecialFromDate(date('Y-m-d', strtotime('+3 day'))); +$product->setSpecialToDate(date('Y-m-d', strtotime('+5 day'))); + +$productRepository->save($product); From 2aca82de615973e6543714985f0064a5cb849c88 Mon Sep 17 00:00:00 2001 From: Yaroslav Rogoza Date: Tue, 17 Nov 2020 17:40:57 +0100 Subject: [PATCH 3/3] Removed unnecessary extra line --- .../testsuite/Magento/GraphQl/Catalog/ProductPriceTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Catalog/ProductPriceTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Catalog/ProductPriceTest.php index 4d9bad9c1f86a..c92fe81cb5586 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Catalog/ProductPriceTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Catalog/ProductPriceTest.php @@ -914,7 +914,6 @@ public function testSpecialPriceVisibleIfInDateRange() self::assertEquals('5.99', $productInformation['special_price']); } - /** * Check if the special price is not visible if the current date is not in the date range set * for the special price