From 3359cfa671573656d9a6754397160609389af365 Mon Sep 17 00:00:00 2001 From: Oleh Usik Date: Wed, 3 Mar 2021 13:53:05 +0200 Subject: [PATCH 1/6] Fixed issue with tier price issue --- .../Model/Resolver/PriceTiers.php | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/app/code/Magento/CatalogCustomerGraphQl/Model/Resolver/PriceTiers.php b/app/code/Magento/CatalogCustomerGraphQl/Model/Resolver/PriceTiers.php index efba88ff154bb..12054fcc315d9 100644 --- a/app/code/Magento/CatalogCustomerGraphQl/Model/Resolver/PriceTiers.php +++ b/app/code/Magento/CatalogCustomerGraphQl/Model/Resolver/PriceTiers.php @@ -19,7 +19,6 @@ use Magento\Framework\GraphQl\Query\ResolverInterface; use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; use Magento\Framework\Pricing\PriceCurrencyInterface; -use Magento\Store\Api\Data\StoreInterface; /** * Resolver for price_tiers @@ -125,6 +124,10 @@ public function resolve( return []; } + if (!$product->getTierPrices()) { + return []; + } + $productId = (int)$product->getId(); $this->tiers->addProductFilter($productId); @@ -152,7 +155,7 @@ private function formatAndFilterTierPrices( array $tierPrices, string $currencyCode ): array { - + $this->resetFormatAndFilterTierPrices(); foreach ($tierPrices as $key => $tierPrice) { $tierPrice->setValue($this->priceCurrency->convertAndRound($tierPrice->getValue())); $this->formatTierPrices($productPrice, $currencyCode, $tierPrice); @@ -212,4 +215,14 @@ private function filterTierPrices( $this->tierPricesQty[$qty] = $key; } } + + /** + * Remove all element from formatAndFilterTierPrices + */ + private function resetFormatAndFilterTierPrices() + { + foreach ($this->formatAndFilterTierPrices as $key => $value) { + unset($this->formatAndFilterTierPrices[$key]); + } + } } From 706d2ec8829a8ab82d426a4b9447212ea80f293a Mon Sep 17 00:00:00 2001 From: Oleh Usik Date: Wed, 3 Mar 2021 15:09:01 +0200 Subject: [PATCH 2/6] Added test and fix minor issue --- .../CatalogCustomerGraphQl/composer.json | 3 +- .../CatalogCustomer/PriceTiersTest.php | 28 +++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/app/code/Magento/CatalogCustomerGraphQl/composer.json b/app/code/Magento/CatalogCustomerGraphQl/composer.json index a7c887af0379b..ce80ee602327e 100644 --- a/app/code/Magento/CatalogCustomerGraphQl/composer.json +++ b/app/code/Magento/CatalogCustomerGraphQl/composer.json @@ -7,8 +7,7 @@ "magento/framework": "*", "magento/module-catalog": "*", "magento/module-customer": "*", - "magento/module-catalog-graph-ql": "*", - "magento/module-store": "*" + "magento/module-catalog-graph-ql": "*" }, "license": [ "OSL-3.0", diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/CatalogCustomer/PriceTiersTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/CatalogCustomer/PriceTiersTest.php index 767dcc5e0ab5c..4c9aa4e643c45 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/CatalogCustomer/PriceTiersTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/CatalogCustomer/PriceTiersTest.php @@ -117,6 +117,34 @@ public function testGetLowestPriceForGuest() $this->assertEquals(round(7.25, 2), $this->getValueForQuantity(8, $itemTiers)); } + /** + * @magentoApiDataFixture Magento/Catalog/_files/three_simple_products_with_tier_price.php + */ + public function testGetCorrectDisplaingTierPriceForProducts() + { + $productSku = 'simple'; + $query = <<graphQlQuery($query); + $this->assertCount(1, $response['products']['items'][0]['price_tiers']); + $this->assertCount(1, $response['products']['items'][1]['price_tiers']); + $this->assertCount(1, $response['products']['items'][2]['price_tiers']); + } + /** * Get the tier price value for the given product quantity * From 457b0ea97f7c05e8d8fe20ab8de4ef924cddff6d Mon Sep 17 00:00:00 2001 From: Oleh Usik Date: Wed, 3 Mar 2021 18:29:50 +0200 Subject: [PATCH 3/6] improved solution and test coverage --- .../Model/Resolver/PriceTiers.php | 13 +----- .../CatalogCustomer/PriceTiersTest.php | 4 +- .../simple_product_without_tier_price.php | 44 +++++++++++++++++++ ...le_product_without_tier_price_rollback.php | 29 ++++++++++++ 4 files changed, 78 insertions(+), 12 deletions(-) create mode 100644 dev/tests/integration/testsuite/Magento/Catalog/_files/simple_product_without_tier_price.php create mode 100644 dev/tests/integration/testsuite/Magento/Catalog/_files/simple_product_without_tier_price_rollback.php diff --git a/app/code/Magento/CatalogCustomerGraphQl/Model/Resolver/PriceTiers.php b/app/code/Magento/CatalogCustomerGraphQl/Model/Resolver/PriceTiers.php index 12054fcc315d9..3c6cc849081ee 100644 --- a/app/code/Magento/CatalogCustomerGraphQl/Model/Resolver/PriceTiers.php +++ b/app/code/Magento/CatalogCustomerGraphQl/Model/Resolver/PriceTiers.php @@ -155,7 +155,8 @@ private function formatAndFilterTierPrices( array $tierPrices, string $currencyCode ): array { - $this->resetFormatAndFilterTierPrices(); + $this->formatAndFilterTierPrices = []; + $this->tierPricesQty = []; foreach ($tierPrices as $key => $tierPrice) { $tierPrice->setValue($this->priceCurrency->convertAndRound($tierPrice->getValue())); $this->formatTierPrices($productPrice, $currencyCode, $tierPrice); @@ -215,14 +216,4 @@ private function filterTierPrices( $this->tierPricesQty[$qty] = $key; } } - - /** - * Remove all element from formatAndFilterTierPrices - */ - private function resetFormatAndFilterTierPrices() - { - foreach ($this->formatAndFilterTierPrices as $key => $value) { - unset($this->formatAndFilterTierPrices[$key]); - } - } } diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/CatalogCustomer/PriceTiersTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/CatalogCustomer/PriceTiersTest.php index 4c9aa4e643c45..1927fff8fb2e0 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/CatalogCustomer/PriceTiersTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/CatalogCustomer/PriceTiersTest.php @@ -118,6 +118,7 @@ public function testGetLowestPriceForGuest() } /** + * @magentoApiDataFixture Magento/Catalog/_files/simple_product_without_tier_price.php * @magentoApiDataFixture Magento/Catalog/_files/three_simple_products_with_tier_price.php */ public function testGetCorrectDisplaingTierPriceForProducts() @@ -140,9 +141,10 @@ public function testGetCorrectDisplaingTierPriceForProducts() } QUERY; $response = $this->graphQlQuery($query); - $this->assertCount(1, $response['products']['items'][0]['price_tiers']); + $this->assertCount(0, $response['products']['items'][0]['price_tiers']); $this->assertCount(1, $response['products']['items'][1]['price_tiers']); $this->assertCount(1, $response['products']['items'][2]['price_tiers']); + $this->assertCount(1, $response['products']['items'][3]['price_tiers']); } /** diff --git a/dev/tests/integration/testsuite/Magento/Catalog/_files/simple_product_without_tier_price.php b/dev/tests/integration/testsuite/Magento/Catalog/_files/simple_product_without_tier_price.php new file mode 100644 index 0000000000000..6e48b110ec991 --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/Catalog/_files/simple_product_without_tier_price.php @@ -0,0 +1,44 @@ +get(WebsiteRepositoryInterface::class)->get('admin'); + +/** @var ProductRepositoryInterface $productRepository */ +$productRepository = $objectManager->get(ProductRepositoryInterface::class); +/** @var $product Product */ +$product = $objectManager->create(Product::class); +$product->setTypeId(Type::TYPE_SIMPLE) +->setAttributeSetId($product->getDefaultAttributeSetId()) +->setWebsiteIds([1]) +->setName('Simple Product ' . 10) +->setSku('simple_10') +->setPrice(22) +->setWeight(1) +->setVisibility(Visibility::VISIBILITY_BOTH) +->setStatus(Status::STATUS_ENABLED) +->setStockData( + [ + 'use_config_manage_stock' => 1, + 'qty' => 100, + 'is_qty_decimal' => 0, + 'is_in_stock' => 1, + ] +); + +$productRepository->save($product); diff --git a/dev/tests/integration/testsuite/Magento/Catalog/_files/simple_product_without_tier_price_rollback.php b/dev/tests/integration/testsuite/Magento/Catalog/_files/simple_product_without_tier_price_rollback.php new file mode 100644 index 0000000000000..328ab57843f4c --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/Catalog/_files/simple_product_without_tier_price_rollback.php @@ -0,0 +1,29 @@ +getInstance()->reinitialize(); + +/** @var Registry $registry */ +$registry = Bootstrap::getObjectManager()->get(Registry::class); + +$registry->unregister('isSecureArea'); +$registry->register('isSecureArea', true); + +/** @var ProductRepositoryInterface $productRepository */ +$productRepository = Bootstrap::getObjectManager()->get(ProductRepositoryInterface::class); +try { + $product = $productRepository->get('simple_10', false, null, true); + $productRepository->delete($product); +} catch (NoSuchEntityException $e) { +} +$registry->unregister('isSecureArea'); +$registry->register('isSecureArea', false); \ No newline at end of file From d5c419115ef738ed6c02403dcf8184032df3e308 Mon Sep 17 00:00:00 2001 From: Oleh Usik Date: Wed, 3 Mar 2021 20:51:54 +0200 Subject: [PATCH 4/6] add changes to test --- .../GraphQl/CatalogCustomer/PriceTiersTest.php | 14 +++++++++----- .../_files/simple_product_without_tier_price.php | 4 ++-- .../simple_product_without_tier_price_rollback.php | 2 +- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/CatalogCustomer/PriceTiersTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/CatalogCustomer/PriceTiersTest.php index 1927fff8fb2e0..72c958a4dff6a 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/CatalogCustomer/PriceTiersTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/CatalogCustomer/PriceTiersTest.php @@ -128,7 +128,7 @@ public function testGetCorrectDisplaingTierPriceForProducts() { products(search: "{$productSku}") { items { - id + sku name price_tiers { quantity @@ -141,10 +141,14 @@ public function testGetCorrectDisplaingTierPriceForProducts() } QUERY; $response = $this->graphQlQuery($query); - $this->assertCount(0, $response['products']['items'][0]['price_tiers']); - $this->assertCount(1, $response['products']['items'][1]['price_tiers']); - $this->assertCount(1, $response['products']['items'][2]['price_tiers']); - $this->assertCount(1, $response['products']['items'][3]['price_tiers']); + foreach ($response['products']['items'] as $key => $item) { + if ($item['sku'] == 'simple_19') { + $this->assertCount(0, $response['products']['items'][$key]['price_tiers']); + } + if (in_array($item['sku'], ['simple_1','simple_2','simple_3'])) { + $this->assertCount(1, $response['products']['items'][$key]['price_tiers']); + } + } } /** diff --git a/dev/tests/integration/testsuite/Magento/Catalog/_files/simple_product_without_tier_price.php b/dev/tests/integration/testsuite/Magento/Catalog/_files/simple_product_without_tier_price.php index 6e48b110ec991..1757f11542572 100644 --- a/dev/tests/integration/testsuite/Magento/Catalog/_files/simple_product_without_tier_price.php +++ b/dev/tests/integration/testsuite/Magento/Catalog/_files/simple_product_without_tier_price.php @@ -26,8 +26,8 @@ $product->setTypeId(Type::TYPE_SIMPLE) ->setAttributeSetId($product->getDefaultAttributeSetId()) ->setWebsiteIds([1]) -->setName('Simple Product ' . 10) -->setSku('simple_10') +->setName('Simple Product 19') +->setSku('simple_19') ->setPrice(22) ->setWeight(1) ->setVisibility(Visibility::VISIBILITY_BOTH) diff --git a/dev/tests/integration/testsuite/Magento/Catalog/_files/simple_product_without_tier_price_rollback.php b/dev/tests/integration/testsuite/Magento/Catalog/_files/simple_product_without_tier_price_rollback.php index 328ab57843f4c..af099fb10cfdf 100644 --- a/dev/tests/integration/testsuite/Magento/Catalog/_files/simple_product_without_tier_price_rollback.php +++ b/dev/tests/integration/testsuite/Magento/Catalog/_files/simple_product_without_tier_price_rollback.php @@ -21,7 +21,7 @@ /** @var ProductRepositoryInterface $productRepository */ $productRepository = Bootstrap::getObjectManager()->get(ProductRepositoryInterface::class); try { - $product = $productRepository->get('simple_10', false, null, true); + $product = $productRepository->get('simple_19', false, null, true); $productRepository->delete($product); } catch (NoSuchEntityException $e) { } From 022d7bf598ea584a5423b3dc58372463df0a7579 Mon Sep 17 00:00:00 2001 From: Oleh Usik Date: Wed, 10 Mar 2021 19:22:03 +0200 Subject: [PATCH 5/6] Test adjustment --- .../CatalogCustomer/PriceTiersTest.php | 11 ++--- .../simple_product_without_tier_price.php | 44 ------------------- ...le_product_without_tier_price_rollback.php | 29 ------------ 3 files changed, 6 insertions(+), 78 deletions(-) delete mode 100644 dev/tests/integration/testsuite/Magento/Catalog/_files/simple_product_without_tier_price.php delete mode 100644 dev/tests/integration/testsuite/Magento/Catalog/_files/simple_product_without_tier_price_rollback.php diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/CatalogCustomer/PriceTiersTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/CatalogCustomer/PriceTiersTest.php index 72c958a4dff6a..fed358e1753c0 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/CatalogCustomer/PriceTiersTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/CatalogCustomer/PriceTiersTest.php @@ -118,7 +118,7 @@ public function testGetLowestPriceForGuest() } /** - * @magentoApiDataFixture Magento/Catalog/_files/simple_product_without_tier_price.php + * @magentoApiDataFixture Magento/Catalog/_files/second_product_simple.php * @magentoApiDataFixture Magento/Catalog/_files/three_simple_products_with_tier_price.php */ public function testGetCorrectDisplaingTierPriceForProducts() @@ -141,12 +141,13 @@ public function testGetCorrectDisplaingTierPriceForProducts() } QUERY; $response = $this->graphQlQuery($query); + $productsWithTierPrices = ['simple_1','simple_2','simple_3']; + foreach ($response['products']['items'] as $key => $item) { - if ($item['sku'] == 'simple_19') { - $this->assertCount(0, $response['products']['items'][$key]['price_tiers']); - } - if (in_array($item['sku'], ['simple_1','simple_2','simple_3'])) { + if (in_array($item['sku'], $productsWithTierPrices)) { $this->assertCount(1, $response['products']['items'][$key]['price_tiers']); + } else { + $this->assertCount(0, $response['products']['items'][$key]['price_tiers']); } } } diff --git a/dev/tests/integration/testsuite/Magento/Catalog/_files/simple_product_without_tier_price.php b/dev/tests/integration/testsuite/Magento/Catalog/_files/simple_product_without_tier_price.php deleted file mode 100644 index 1757f11542572..0000000000000 --- a/dev/tests/integration/testsuite/Magento/Catalog/_files/simple_product_without_tier_price.php +++ /dev/null @@ -1,44 +0,0 @@ -get(WebsiteRepositoryInterface::class)->get('admin'); - -/** @var ProductRepositoryInterface $productRepository */ -$productRepository = $objectManager->get(ProductRepositoryInterface::class); -/** @var $product Product */ -$product = $objectManager->create(Product::class); -$product->setTypeId(Type::TYPE_SIMPLE) -->setAttributeSetId($product->getDefaultAttributeSetId()) -->setWebsiteIds([1]) -->setName('Simple Product 19') -->setSku('simple_19') -->setPrice(22) -->setWeight(1) -->setVisibility(Visibility::VISIBILITY_BOTH) -->setStatus(Status::STATUS_ENABLED) -->setStockData( - [ - 'use_config_manage_stock' => 1, - 'qty' => 100, - 'is_qty_decimal' => 0, - 'is_in_stock' => 1, - ] -); - -$productRepository->save($product); diff --git a/dev/tests/integration/testsuite/Magento/Catalog/_files/simple_product_without_tier_price_rollback.php b/dev/tests/integration/testsuite/Magento/Catalog/_files/simple_product_without_tier_price_rollback.php deleted file mode 100644 index af099fb10cfdf..0000000000000 --- a/dev/tests/integration/testsuite/Magento/Catalog/_files/simple_product_without_tier_price_rollback.php +++ /dev/null @@ -1,29 +0,0 @@ -getInstance()->reinitialize(); - -/** @var Registry $registry */ -$registry = Bootstrap::getObjectManager()->get(Registry::class); - -$registry->unregister('isSecureArea'); -$registry->register('isSecureArea', true); - -/** @var ProductRepositoryInterface $productRepository */ -$productRepository = Bootstrap::getObjectManager()->get(ProductRepositoryInterface::class); -try { - $product = $productRepository->get('simple_19', false, null, true); - $productRepository->delete($product); -} catch (NoSuchEntityException $e) { -} -$registry->unregister('isSecureArea'); -$registry->register('isSecureArea', false); \ No newline at end of file From f494cb1b43cb520143b2c54091b7a9874ad1797c Mon Sep 17 00:00:00 2001 From: Oleh Usik Date: Tue, 30 Mar 2021 10:08:59 +0300 Subject: [PATCH 6/6] Update dev/tests/api-functional/testsuite/Magento/GraphQl/CatalogCustomer/PriceTiersTest.php Co-authored-by: Eduard Chitoraga --- .../Magento/GraphQl/CatalogCustomer/PriceTiersTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/CatalogCustomer/PriceTiersTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/CatalogCustomer/PriceTiersTest.php index fed358e1753c0..071f9a6ea5121 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/CatalogCustomer/PriceTiersTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/CatalogCustomer/PriceTiersTest.php @@ -121,7 +121,7 @@ public function testGetLowestPriceForGuest() * @magentoApiDataFixture Magento/Catalog/_files/second_product_simple.php * @magentoApiDataFixture Magento/Catalog/_files/three_simple_products_with_tier_price.php */ - public function testGetCorrectDisplaingTierPriceForProducts() + public function testProductTierPricesAreCorrectlyReturned() { $productSku = 'simple'; $query = <<