This commit is contained in:
2026-09-15 10:26:34 +08:00
commit ecf36afa56
10493 changed files with 1404888 additions and 0 deletions
@@ -0,0 +1,214 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\GraphQl\CatalogCustomer;
use Magento\GraphQl\GetCustomerAuthenticationHeader;
use Magento\Store\Api\StoreRepositoryInterface;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\ObjectManager;
use Magento\TestFramework\TestCase\GraphQlAbstract;
class PriceTiersTest extends GraphQlAbstract
{
/**
* @var ObjectManager
*/
private $objectManager;
/**
* @var GetCustomerAuthenticationHeader
*/
private $getCustomerAuthenticationHeader;
protected function setUp(): void
{
$this->objectManager = Bootstrap::getObjectManager();
$this->getCustomerAuthenticationHeader = $this->objectManager->get(GetCustomerAuthenticationHeader::class);
}
/**
* @magentoApiDataFixture Magento/Catalog/_files/product_simple.php
*/
public function testAllGroups()
{
$productSku = 'simple';
$query = $this->getProductSearchQuery($productSku);
$response = $this->graphQlQuery($query);
$itemTiers = $response['products']['items'][0]['price_tiers'];
$this->assertCount(5, $itemTiers);
$this->assertEquals(round(8 * 2, 2), $this->getValueForQuantity(2, $itemTiers));
$this->assertEquals(round(5 * 3, 2), $this->getValueForQuantity(3, $itemTiers));
$this->assertEquals(round(6 * 3.2, 2), $this->getValueForQuantity(3.2, $itemTiers));
}
/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/Catalog/_files/simple_product_with_tier_prices_for_multiple_groups.php
*/
public function testLoggedInCustomer()
{
$productSku = 'simple';
$query = $this->getProductSearchQuery($productSku);
$response = $this->graphQlQuery(
$query,
[],
'',
$this->getCustomerAuthenticationHeader->execute('customer@example.com', 'password')
);
$itemTiers = $response['products']['items'][0]['price_tiers'];
$this->assertCount(5, $itemTiers);
$this->assertEquals(round(9.25 * 2, 2), $this->getValueForQuantity(2, $itemTiers));
$this->assertEquals(round(8.25 * 3, 2), $this->getValueForQuantity(3, $itemTiers));
$this->assertEquals(round(7.25 * 5, 2), $this->getValueForQuantity(5, $itemTiers));
$this->assertEquals(round(9.00 * 7, 2), $this->getValueForQuantity(7, $itemTiers));
$this->assertEquals(round(7.25 * 8, 2), $this->getValueForQuantity(8, $itemTiers));
}
/**
* @magentoApiDataFixture Magento/Store/_files/second_store_with_second_currency.php
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/Catalog/_files/simple_product_with_tier_prices_for_multiple_groups.php
*/
public function testSecondStoreViewWithCurrencyRate()
{
$storeViewCode = 'fixture_second_store';
$storeRepository = $this->objectManager->get(StoreRepositoryInterface::class);
$rate = $storeRepository->get($storeViewCode)->getCurrentCurrencyRate();
$productSku = 'simple';
$query = $this->getProductSearchQuery($productSku);
$headers = array_merge(
$this->getCustomerAuthenticationHeader->execute('customer@example.com', 'password'),
$this->getHeaderStore($storeViewCode)
);
$response = $this->graphQlQuery(
$query,
[],
'',
$headers
);
$itemTiers = $response['products']['items'][0]['price_tiers'];
$this->assertCount(5, $itemTiers);
$this->assertEquals(round((9.25 * 2) * $rate, 2), $this->getValueForQuantity(2, $itemTiers));
$this->assertEquals(round((8.25 * 3) * $rate, 2), $this->getValueForQuantity(3, $itemTiers));
$this->assertEquals(round((7.25 * 5) * $rate, 2), $this->getValueForQuantity(5, $itemTiers));
}
/**
* @magentoApiDataFixture Magento/Catalog/_files/simple_product_with_tier_prices_for_multiple_groups.php
*/
public function testGetLowestPriceForGuest()
{
$productSku = 'simple';
$query = $this->getProductSearchQuery($productSku);
$response = $this->graphQlQuery($query);
$itemTiers = $response['products']['items'][0]['price_tiers'];
$this->assertCount(2, $itemTiers);
$this->assertEquals(round((8.25 * 7), 2), $this->getValueForQuantity(7, $itemTiers));
$this->assertEquals(round((7.25 * 8), 2), $this->getValueForQuantity(8, $itemTiers));
}
/**
* @magentoApiDataFixture Magento/Catalog/_files/second_product_simple.php
* @magentoApiDataFixture Magento/Catalog/_files/three_simple_products_with_tier_price.php
*/
public function testProductTierPricesAreCorrectlyReturned()
{
$productSku = 'simple';
$query = <<<QUERY
{
products(search: "{$productSku}") {
items {
sku
name
price_tiers {
quantity
final_price {
value
}
}
}
}
}
QUERY;
$response = $this->graphQlQuery($query);
$productsWithTierPrices = ['simple_1','simple_2','simple_3'];
foreach ($response['products']['items'] as $key => $item) {
if (in_array($item['sku'], $productsWithTierPrices)) {
$this->assertCount(1, $response['products']['items'][$key]['price_tiers']);
} else {
if (empty($response['products']['items'][$key]['price_tiers'])) {
$this->assertCount(0, $response['products']['items'][$key]['price_tiers']);
}
}
}
}
/**
* Get the tier price value for the given product quantity
*
* @param float $quantity
* @param array $tiers
* @return float
*/
private function getValueForQuantity(float $quantity, array $tiers): float
{
$filteredResult = array_values(array_filter($tiers, function ($tier) use ($quantity) {
if ((float)$tier['quantity'] == $quantity) {
return $tier;
}
}));
return (float)$filteredResult[0]['final_price']['value'];
}
/**
* Get a query which user filter for product sku and returns price_tiers
*
* @param string $productSku
* @return string
*/
private function getProductSearchQuery(string $productSku): string
{
return <<<QUERY
{
products(filter: {sku: {eq: "{$productSku}"}}) {
items {
price_tiers {
final_price {
currency
value
}
discount {
amount_off
percent_off
}
quantity
}
}
}
}
QUERY;
}
/**
* Get array that would be used in request header
*
* @param string $storeViewCode
* @return array
*/
private function getHeaderStore(string $storeViewCode): array
{
return ['Store' => $storeViewCode];
}
}
@@ -0,0 +1,94 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\GraphQl\CatalogCustomer;
use Magento\TestFramework\TestCase\GraphQlAbstract;
use Magento\Store\Api\StoreRepositoryInterface;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\ObjectManager;
class SpecialPriceTest extends GraphQlAbstract
{
/**
* @var ObjectManager
*/
private $objectManager;
protected function setUp(): void
{
$this->objectManager = Bootstrap::getObjectManager();
}
/**
* @magentoApiDataFixture Magento/Catalog/_files/product_special_price.php
*/
public function testSpecialPrice()
{
$productSku = 'simple';
$query = $this->getProductSearchQuery($productSku);
$response = $this->graphQlQuery($query);
$specialPrice = (float)$response['products']['items'][0]['special_price'];
$this->assertEquals(5.99, $specialPrice);
}
/**
* @magentoApiDataFixture Magento/Store/_files/second_store_with_second_currency.php
* @magentoApiDataFixture Magento/Catalog/_files/product_special_price.php
*/
public function testSpecialPriceWithCurrencyRate()
{
$storeViewCode = 'fixture_second_store';
$storeRepository = $this->objectManager->get(StoreRepositoryInterface::class);
$rate = $storeRepository->get($storeViewCode)->getCurrentCurrencyRate();
$productSku = 'simple';
$query = $this->getProductSearchQuery($productSku);
$headers = $this->getHeaderStore($storeViewCode);
$response = $this->graphQlQuery(
$query,
[],
'',
$headers
);
$specialPrice = (float)$response['products']['items'][0]['special_price'];
$this->assertEquals(round(5.99 * $rate, 2), $specialPrice);
}
/**
* Get a query which user filter for product sku and returns special_price
*
* @param string $productSku
* @return string
*/
private function getProductSearchQuery(string $productSku): string
{
return <<<QUERY
{
products(filter: {sku: {eq: "{$productSku}"}}) {
items {
special_price
}
}
}
QUERY;
}
/**
* Get array that would be used in request header
*
* @param string $storeViewCode
* @return array
*/
private function getHeaderStore(string $storeViewCode): array
{
return ['Store' => $storeViewCode];
}
}
@@ -0,0 +1,332 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\GraphQl\CatalogCustomer;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\TestFramework\TestCase\GraphQlAbstract;
use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Api\Data\ProductTierPriceInterfaceFactory;
use Magento\GraphQl\Quote\GetMaskedQuoteIdByReservedOrderId;
use Magento\Integration\Api\CustomerTokenServiceInterface;
class TierPricesForCustomersTest extends GraphQlAbstract
{
/** @var \Magento\TestFramework\ObjectManager */
private $objectManager;
/** @var GetMaskedQuoteIdByReservedOrderId */
private $getMaskedQuoteIdByReservedOrderId;
/** @var CustomerTokenServiceInterface */
private $customerTokenService;
protected function setUp(): void
{
$this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
$this->getMaskedQuoteIdByReservedOrderId = $this->objectManager->get(GetMaskedQuoteIdByReservedOrderId::class);
$this->customerTokenService = $this->objectManager->get(CustomerTokenServiceInterface::class);
}
/**
* @magentoApiDataFixture Magento/Catalog/_files/product_simple.php
* @magentoApiDataFixture Magento/Customer/_files/customer.php
*/
public function testTierPricesForGeneralGroup()
{
$productSku = 'simple';
$productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
$product = $productRepository->get($productSku, false, null, true);
$tierPriceData =[
[
'customer_group_id' => 1,
'percentage_value'=> null,
'qty'=> 2,
'value'=> 8
]
];
$this->saveTierPrices($product, $tierPriceData);
$query = $this->getProductSearchQuery($productSku);
// Query response with headers for customers
$response = $this->graphQlQuery($query, [], '', $this->getHeaderMap());
$this->assertNotEmpty($response['products']['items'][0]['tier_prices']);
$this->assertArrayHasKey('tier_prices', $response['products']['items'][0]);
$expectedResponse = [
[
'customer_group_id' => 1,
'percentage_value'=> null,
'qty'=> 2,
'value'=> 8
]
];
$this->assertResponseFields($response['products']['items'][0]['tier_prices'], $expectedResponse);
$this->assertCount(1, $response['products']['items'][0]['tier_prices']);
}
/**
* @magentoApiDataFixture Magento/Catalog/_files/product_simple.php
* @magentoApiDataFixture Magento/Customer/_files/customer.php
*/
public function testTierPricesForGeneralAndAllCustomerGroups()
{
$productSku = 'simple';
$productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
$product = $productRepository->get($productSku, false, null, true);
$tierPriceData = [
[
'customer_group_id' => \Magento\Customer\Model\Group::CUST_GROUP_ALL,
'percentage_value'=> null,
'qty'=> 3,
'value'=> 6
],
[
'customer_group_id' => 1,
'percentage_value'=> null,
'qty'=> 6,
'value'=> 7
]
];
$this->saveTierPrices($product, $tierPriceData);
$query = $this->getProductSearchQuery($productSku);
// Query response with headers for customers
$response = $this->graphQlQuery($query, [], '', $this->getHeaderMap());
$this->assertNotEmpty($response['products']['items'][0]['tier_prices']);
$this->assertArrayHasKey('tier_prices', $response['products']['items'][0]);
$expectedResponse = [
[
'customer_group_id' => \Magento\Customer\Model\Group::CUST_GROUP_ALL,
'percentage_value'=> null,
'qty'=> 3,
'value'=> 6
],
[
'customer_group_id' => 1,
'percentage_value'=> null,
'qty'=> 6,
'value'=> 7
]
];
$this->assertResponseFields($response['products']['items'][0]['tier_prices'], $expectedResponse);
$this->assertCount(2, $response['products']['items'][0]['tier_prices']);
}
/**
* @magentoApiDataFixture Magento/Catalog/_files/product_simple.php
* @magentoApiDataFixture Magento/Customer/_files/customer.php
*/
public function testTierPricesForNotLoggedInGroupOnly()
{
$productSku = 'simple';
$productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
$product = $productRepository->get($productSku, false, null, true);
$tierPriceData = [
[
'customer_group_id' => \Magento\Customer\Model\Group::NOT_LOGGED_IN_ID,
'percentage_value'=> null,
'qty'=> 4,
'value'=> 6
]
];
$this->saveTierPrices($product, $tierPriceData);
$query = $this->getProductSearchQuery($productSku);
// Query response with headers for customers
$response = $this->graphQlQuery($query, [], '', $this->getHeaderMap());
$this->assertEmpty($response['products']['items'][0]['tier_prices']);
$expectedResponse = [];
$this->assertResponseFields($response['products']['items'][0]['tier_prices'], $expectedResponse);
}
/**
* @magentoApiDataFixture Magento/Catalog/_files/product_simple.php
* @magentoApiDataFixture Magento/Customer/_files/customer.php
*/
public function testTierPricesForNotLoggedInAndGeneralGroups()
{
$productSku = 'simple';
$productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
$product = $productRepository->get($productSku, false, null, true);
$tierPriceData = [
[
'customer_group_id' => \Magento\Customer\Model\Group::NOT_LOGGED_IN_ID,
'percentage_value'=> null,
'qty'=> 7,
'value'=> 6.5
],
[
'customer_group_id' => 1,
'percentage_value'=> null,
'qty'=> 6,
'value'=> 5
]
];
$this->saveTierPrices($product, $tierPriceData);
$query = $this->getProductSearchQuery($productSku);
// Query response with headers for customers
$response = $this->graphQlQuery($query, [], '', $this->getHeaderMap());
$this->assertNotEmpty($response['products']['items'][0]['tier_prices']);
$this->assertArrayHasKey('tier_prices', $response['products']['items'][0]);
$expectedResponse = [
[
'customer_group_id' => 1,
'percentage_value'=> null,
'qty'=> 6,
'value'=> 5
]
];
$this->assertResponseFields($response['products']['items'][0]['tier_prices'], $expectedResponse);
$this->assertCount(1, $response['products']['items'][0]['tier_prices']);
}
/**
* @magentoApiDataFixture Magento/Catalog/_files/product_simple.php
* @magentoApiDataFixture Magento/Customer/_files/customer.php
*/
public function testTierPricesForAllCustomerGroupsOnly()
{
$productSku = 'simple';
$productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
$product = $productRepository->get($productSku, false, null, true);
$tierPriceData = [
[
'customer_group_id' => \Magento\Customer\Model\Group::CUST_GROUP_ALL,
'percentage_value'=> null,
'qty'=> 4,
'value'=> 6
],
];
$this->saveTierPrices($product, $tierPriceData);
$query = $this->getProductSearchQuery($productSku);
// Query response with headers for customers
$response = $this->graphQlQuery($query, [], '', $this->getHeaderMap());
$this->assertNotEmpty($response['products']['items'][0]['tier_prices']);
$this->assertArrayHasKey('tier_prices', $response['products']['items'][0]);
$expectedResponse = [
[
'customer_group_id' => \Magento\Customer\Model\Group::CUST_GROUP_ALL,
'percentage_value'=> null,
'qty'=> 4,
'value'=> 6
]
];
$this->assertResponseFields($response['products']['items'][0]['tier_prices'], $expectedResponse);
$this->assertCount(1, $response['products']['items'][0]['tier_prices']);
}
/**
* @magentoApiDataFixture Magento/Catalog/_files/product_simple.php
* @magentoApiDataFixture Magento/Customer/_files/customer.php
*/
public function testTierPricesForAllCustomerGroupsAndNotLoggedInGroup()
{
$productSku = 'simple';
$productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
$product = $productRepository->get($productSku, false, null, true);
$tierPriceData = [
[
'customer_group_id' => \Magento\Customer\Model\Group::CUST_GROUP_ALL,
'percentage_value'=> null,
'qty'=> 2,
'value'=> 8
],
[
'customer_group_id' => \Magento\Customer\Model\Group::NOT_LOGGED_IN_ID,
'percentage_value'=> null,
'qty'=> 4,
'value'=> 6.5
],
];
$this->saveTierPrices($product, $tierPriceData);
$query = $this->getProductSearchQuery($productSku);
// Query response with headers for customers
$response = $this->graphQlQuery($query, [], '', $this->getHeaderMap());
$this->assertNotEmpty($response['products']['items'][0]['tier_prices']);
$this->assertArrayHasKey('tier_prices', $response['products']['items'][0]);
$expectedResponse = [
[
'customer_group_id' => \Magento\Customer\Model\Group::CUST_GROUP_ALL,
'percentage_value'=> null,
'qty'=> 2,
'value'=> 8
]
];
$this->assertResponseFields($response['products']['items'][0]['tier_prices'], $expectedResponse);
$this->assertCount(1, $response['products']['items'][0]['tier_prices']);
}
/**
* @param ProductInterface $product
* @param array $tierPriceData
*/
private function saveTierPrices($product, $tierPriceData)
{
$tierPrices = [];
/** @var ProductTierPriceInterfaceFactory $tierPriceFactory */
$tierPriceFactory = $this->objectManager
->get(\Magento\Catalog\Api\Data\ProductTierPriceInterfaceFactory::class);
foreach ($tierPriceData as $tierPrice) {
$tierPrices[] = $tierPriceFactory->create(
[
'data' => $tierPrice
]
);
}
$product->setTierPrices($tierPrices);
$product->save();
}
/**
* @param string $maskedQuoteId
* @return string
*/
private function getProductSearchQuery(string $productSku): string
{
return <<<QUERY
{
products(filter: {sku: {eq: "{$productSku}"}})
{
items {
sku
name
tier_prices {
customer_group_id
percentage_value
qty
value
}
}
}
}
QUERY;
}
/**
* @param string $username
* @param string $password
* @return array
*/
private function getHeaderMap(string $username = 'customer@example.com', string $password = 'password'): array
{
$customerToken = $this->customerTokenService->createCustomerAccessToken($username, $password);
$headerMap = [ 'Authorization' => 'Bearer ' . $customerToken];
return $headerMap;
}
}
@@ -0,0 +1,301 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\GraphQl\CatalogCustomer;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\TestFramework\TestCase\GraphQlAbstract;
use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Api\Data\ProductTierPriceInterfaceFactory;
class TierPricesForGuestsTest extends GraphQlAbstract
{
/**
* @var \Magento\TestFramework\ObjectManager
*/
private $objectManager;
protected function setUp(): void
{
$this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
}
/**
* @magentoApiDataFixture Magento/Catalog/_files/product_simple.php
*/
public function testTierPricesForGeneralGroup()
{
$productSku = 'simple';
$productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
$product = $productRepository->get($productSku, false, null, true);
$tierPriceData =[
[
'customer_group_id' => 1,
'percentage_value'=> null,
'qty'=> 2,
'value'=> 8
]
];
$this->saveTierPrices($product, $tierPriceData);
$query = $this->getProductSearchQuery($productSku);
$response = $this->graphQlQuery($query);
$expectedResponse = [];
$this->assertEmpty($response['products']['items'][0]['tier_prices']);
$this->assertResponseFields($response['products']['items'][0]['tier_prices'], $expectedResponse);
}
/**
* @magentoApiDataFixture Magento/Catalog/_files/product_simple.php
*/
public function testTierPricesForGeneralAndAllCustomerGroups()
{
$productSku = 'simple';
$productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
$product = $productRepository->get($productSku, false, null, true);
$tierPriceData = [
[
'customer_group_id' => \Magento\Customer\Model\Group::CUST_GROUP_ALL,
'percentage_value'=> null,
'qty'=> 2,
'value'=> 6
],
[
'customer_group_id' => 1,
'percentage_value'=> null,
'qty'=> 2,
'value'=> 8
]
];
$this->saveTierPrices($product, $tierPriceData);
$query = $this->getProductSearchQuery($productSku);
$response = $this->graphQlQuery($query);
$this->assertNotEmpty($response['products']['items'][0]['tier_prices']);
$this->assertArrayHasKey('tier_prices', $response['products']['items'][0]);
$expectedResponse = [
[
'customer_group_id' => \Magento\Customer\Model\Group::CUST_GROUP_ALL,
'percentage_value'=> null,
'qty'=> 2,
'value'=> 6
]
];
$this->assertResponseFields($response['products']['items'][0]['tier_prices'], $expectedResponse);
}
/**
* @magentoApiDataFixture Magento/Catalog/_files/product_simple.php
*/
public function testTierPricesForNotLoggedInGroupOnly()
{
$productSku = 'simple';
$productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
$product = $productRepository->get($productSku, false, null, true);
$tierPriceData = [
[
'customer_group_id' => \Magento\Customer\Model\Group::NOT_LOGGED_IN_ID,
'percentage_value'=> null,
'qty'=> 4,
'value'=> 6
]
];
$this->saveTierPrices($product, $tierPriceData);
$query = $this->getProductSearchQuery($productSku);
$response = $this->graphQlQuery($query);
$this->assertNotEmpty($response['products']['items'][0]['tier_prices']);
$this->assertArrayHasKey('tier_prices', $response['products']['items'][0]);
$expectedResponse = [
[
'customer_group_id' => \Magento\Customer\Model\Group::NOT_LOGGED_IN_ID,
'percentage_value'=> null,
'qty'=> 4,
'value'=> 6
]
];
$this->assertResponseFields($response['products']['items'][0]['tier_prices'], $expectedResponse);
$this->assertCount(1, $response['products']['items'][0]['tier_prices']);
}
/**
* @magentoApiDataFixture Magento/Catalog/_files/product_simple.php
*/
public function testTierPricesForNotLoggedInAndGeneralGroups()
{
$productSku = 'simple';
$productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
$product = $productRepository->get($productSku, false, null, true);
$tierPriceData = [
[
'customer_group_id' => \Magento\Customer\Model\Group::NOT_LOGGED_IN_ID,
'percentage_value'=> null,
'qty'=> 6,
'value'=> 6.5
],
[
'customer_group_id' => 1,
'percentage_value'=> null,
'qty'=> 5,
'value'=> 6
]
];
$this->saveTierPrices($product, $tierPriceData);
$query = $this->getProductSearchQuery($productSku);
$response = $this->graphQlQuery($query);
$this->assertNotEmpty($response['products']['items'][0]['tier_prices']);
$this->assertArrayHasKey('tier_prices', $response['products']['items'][0]);
$expectedResponse = [
[
'customer_group_id' => \Magento\Customer\Model\Group::NOT_LOGGED_IN_ID,
'percentage_value'=> null,
'qty'=> 6,
'value'=> 6.5
]
];
$this->assertResponseFields($response['products']['items'][0]['tier_prices'], $expectedResponse);
$this->assertCount(1, $response['products']['items'][0]['tier_prices']);
}
/**
* @magentoApiDataFixture Magento/Catalog/_files/product_simple.php
*/
public function testTierPricesForAllCustomerGroupsOnly()
{
$productSku = 'simple';
$productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
$product = $productRepository->get($productSku, false, null, true);
$tierPriceData = [
[
'customer_group_id' => \Magento\Customer\Model\Group::CUST_GROUP_ALL,
'percentage_value'=> null,
'qty'=> 6,
'value'=> 5
],
];
$this->saveTierPrices($product, $tierPriceData);
$query = $this->getProductSearchQuery($productSku);
$response = $this->graphQlQuery($query);
$this->assertNotEmpty($response['products']['items'][0]['tier_prices']);
$this->assertArrayHasKey('tier_prices', $response['products']['items'][0]);
$expectedResponse = [
[
'customer_group_id' => \Magento\Customer\Model\Group::CUST_GROUP_ALL,
'percentage_value'=> null,
'qty'=> 6,
'value'=> 5
]
];
$this->assertResponseFields($response['products']['items'][0]['tier_prices'], $expectedResponse);
$this->assertCount(1, $response['products']['items'][0]['tier_prices']);
}
/**
* @magentoApiDataFixture Magento/Catalog/_files/product_simple.php
*/
public function testTierPricesForAllCustomerGroupsAndNotLoggedInGroup()
{
$productSku = 'simple';
$productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
$product = $productRepository->get($productSku, false, null, true);
$tierPriceData = [
[
'customer_group_id' => \Magento\Customer\Model\Group::CUST_GROUP_ALL,
'percentage_value'=> null,
'qty'=> 2,
'value'=> 8
],
[
'customer_group_id' => \Magento\Customer\Model\Group::NOT_LOGGED_IN_ID,
'percentage_value'=> null,
'qty'=> 4,
'value'=> 6.5
],
];
$this->saveTierPrices($product, $tierPriceData);
$query = $this->getProductSearchQuery($productSku);
$response = $this->graphQlQuery($query);
$this->assertNotEmpty($response['products']['items'][0]['tier_prices']);
$this->assertArrayHasKey('tier_prices', $response['products']['items'][0]);
$expectedResponse = [
[
'customer_group_id' => \Magento\Customer\Model\Group::CUST_GROUP_ALL,
'percentage_value'=> null,
'qty'=> 2,
'value'=> 8
],
[
'customer_group_id' => \Magento\Customer\Model\Group::NOT_LOGGED_IN_ID,
'percentage_value'=> null,
'qty'=> 4,
'value'=> 6.5
]
];
$this->assertResponseFields($response['products']['items'][0]['tier_prices'], $expectedResponse);
$this->assertCount(2, $response['products']['items'][0]['tier_prices']);
}
/**
* @param ProductInterface $product
* @param array $tierPriceData
*/
private function saveTierPrices($product, $tierPriceData)
{
$tierPrices = [];
/** @var ProductTierPriceInterfaceFactory $tierPriceFactory */
$tierPriceFactory = $this->objectManager
->get(\Magento\Catalog\Api\Data\ProductTierPriceInterfaceFactory::class);
foreach ($tierPriceData as $tierPrice) {
$tierPrices[] = $tierPriceFactory->create(
[
'data' => $tierPrice
]
);
}
$product->setTierPrices($tierPrices);
$product->save();
}
/**
* @param string $maskedQuoteId
* @return string
*/
private function getProductSearchQuery(string $productSku): string
{
return <<<QUERY
{
products(filter: {sku: {eq: "{$productSku}"}})
{
items {
sku
name
tier_prices {
customer_group_id
percentage_value
qty
value
}
}
}
}
QUERY;
}
}