init
This commit is contained in:
+516
@@ -0,0 +1,516 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\GraphQl\Bundle;
|
||||
|
||||
use Magento\Catalog\Api\ProductRepositoryInterface;
|
||||
use Magento\Framework\DataObject;
|
||||
use Magento\Quote\Model\Quote;
|
||||
use Magento\Quote\Model\QuoteIdToMaskedQuoteIdInterface;
|
||||
use Magento\Quote\Model\ResourceModel\Quote as QuoteResource;
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
use Magento\TestFramework\TestCase\GraphQlAbstract;
|
||||
|
||||
/**
|
||||
* Test adding bundled products to cart using the unified mutation mutation
|
||||
*/
|
||||
class AddBundleProductToCartSingleMutationTest extends GraphQlAbstract
|
||||
{
|
||||
/**
|
||||
* @var QuoteResource
|
||||
*/
|
||||
private $quoteResource;
|
||||
|
||||
/**
|
||||
* @var Quote
|
||||
*/
|
||||
private $quote;
|
||||
|
||||
/**
|
||||
* @var QuoteIdToMaskedQuoteIdInterface
|
||||
*/
|
||||
private $quoteIdToMaskedId;
|
||||
|
||||
/**
|
||||
* @var ProductRepositoryInterface
|
||||
*/
|
||||
private $productRepository;
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
$objectManager = Bootstrap::getObjectManager();
|
||||
$this->quoteResource = $objectManager->get(QuoteResource::class);
|
||||
$this->quote = $objectManager->create(Quote::class);
|
||||
$this->quoteIdToMaskedId = $objectManager->get(QuoteIdToMaskedQuoteIdInterface::class);
|
||||
$this->productRepository = $objectManager->get(ProductRepositoryInterface::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoApiDataFixture Magento/Bundle/_files/product_1.php
|
||||
* @magentoApiDataFixture Magento/Checkout/_files/active_quote.php
|
||||
*/
|
||||
public function testAddBundleProductToCart()
|
||||
{
|
||||
$sku = 'bundle-product';
|
||||
|
||||
$this->quoteResource->load(
|
||||
$this->quote,
|
||||
'test_order_1',
|
||||
'reserved_order_id'
|
||||
);
|
||||
|
||||
$product = $this->productRepository->get($sku);
|
||||
|
||||
/** @var $typeInstance \Magento\Bundle\Model\Product\Type */
|
||||
$typeInstance = $product->getTypeInstance();
|
||||
$typeInstance->setStoreFilter($product->getStoreId(), $product);
|
||||
/** @var $option \Magento\Bundle\Model\Option */
|
||||
$option = $typeInstance->getOptionsCollection($product)->getFirstItem();
|
||||
/** @var \Magento\Catalog\Model\Product $selection */
|
||||
$selection = $typeInstance->getSelectionsCollection([$option->getId()], $product)->getFirstItem();
|
||||
$optionId = $option->getId();
|
||||
$selectionId = $selection->getSelectionId();
|
||||
|
||||
$bundleOptionIdV2 = $this->generateBundleOptionIdV2((int) $optionId, (int) $selectionId, 1);
|
||||
$maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$this->quote->getId());
|
||||
|
||||
$query = <<<QUERY
|
||||
mutation {
|
||||
addProductsToCart(
|
||||
cartId: "{$maskedQuoteId}",
|
||||
cartItems: [
|
||||
{
|
||||
sku: "{$sku}"
|
||||
quantity: 1
|
||||
selected_options: [
|
||||
"{$bundleOptionIdV2}"
|
||||
]
|
||||
}
|
||||
]
|
||||
) {
|
||||
cart {
|
||||
items {
|
||||
id
|
||||
uid
|
||||
quantity
|
||||
product {
|
||||
sku
|
||||
}
|
||||
... on BundleCartItem {
|
||||
bundle_options {
|
||||
id
|
||||
uid
|
||||
label
|
||||
type
|
||||
values {
|
||||
id
|
||||
uid
|
||||
label
|
||||
price
|
||||
quantity
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
QUERY;
|
||||
|
||||
$response = $this->graphQlMutation($query);
|
||||
|
||||
self::assertArrayHasKey('addProductsToCart', $response);
|
||||
self::assertArrayHasKey('cart', $response['addProductsToCart']);
|
||||
$cart = $response['addProductsToCart']['cart'];
|
||||
$bundleItem = current($cart['items']);
|
||||
self::assertEquals($sku, $bundleItem['product']['sku']);
|
||||
$bundleItemOption = current($bundleItem['bundle_options']);
|
||||
self::assertEquals($optionId, $bundleItemOption['id']);
|
||||
self::assertEquals($option->getTitle(), $bundleItemOption['label']);
|
||||
self::assertEquals($option->getType(), $bundleItemOption['type']);
|
||||
$value = current($bundleItemOption['values']);
|
||||
self::assertEquals($selection->getSelectionId(), $value['id']);
|
||||
self::assertEquals((float) $selection->getSelectionPriceValue(), $value['price']);
|
||||
self::assertEquals(1, $value['quantity']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $optionId
|
||||
* @param int $selectionId
|
||||
* @param int $quantity
|
||||
* @return string
|
||||
*/
|
||||
private function generateBundleOptionIdV2(int $optionId, int $selectionId, int $quantity): string
|
||||
{
|
||||
return base64_encode("bundle/$optionId/$selectionId/$quantity");
|
||||
}
|
||||
|
||||
public function dataProviderTestUpdateBundleItemQuantity(): array
|
||||
{
|
||||
return [
|
||||
[2],
|
||||
[0],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoApiDataFixture Magento/Bundle/_files/product_1.php
|
||||
* @magentoApiDataFixture Magento/Checkout/_files/active_quote.php
|
||||
* @expectedExceptionMessage Please select all required options
|
||||
*/
|
||||
public function testAddBundleToCartWithWrongBundleOptions()
|
||||
{
|
||||
$this->quoteResource->load(
|
||||
$this->quote,
|
||||
'test_order_1',
|
||||
'reserved_order_id'
|
||||
);
|
||||
|
||||
$bundleOptionIdV2 = $this->generateBundleOptionIdV2((int) 1, (int) 1, 1);
|
||||
$maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$this->quote->getId());
|
||||
|
||||
$query = <<<QUERY
|
||||
mutation {
|
||||
addProductsToCart(
|
||||
cartId: "{$maskedQuoteId}",
|
||||
cartItems: [
|
||||
{
|
||||
sku: "bundle-product"
|
||||
quantity: 1
|
||||
selected_options: [
|
||||
"{$bundleOptionIdV2}"
|
||||
]
|
||||
}
|
||||
]
|
||||
) {
|
||||
cart {
|
||||
items {
|
||||
id
|
||||
uid
|
||||
quantity
|
||||
product {
|
||||
sku
|
||||
}
|
||||
... on BundleCartItem {
|
||||
bundle_options {
|
||||
id
|
||||
uid
|
||||
label
|
||||
type
|
||||
values {
|
||||
id
|
||||
uid
|
||||
label
|
||||
price
|
||||
quantity
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
user_errors {
|
||||
message
|
||||
}
|
||||
}
|
||||
}
|
||||
QUERY;
|
||||
|
||||
$response = $this->graphQlMutation($query);
|
||||
|
||||
self::assertEquals(
|
||||
"Please select all required options.",
|
||||
$response['addProductsToCart']['user_errors'][0]['message']
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoApiDataFixture Magento/Bundle/_files/product_with_multiple_options_and_custom_quantity.php
|
||||
* @magentoApiDataFixture Magento/Checkout/_files/active_quote.php
|
||||
* @dataProvider bundleItemOptionsDataProvider
|
||||
* @return void
|
||||
*/
|
||||
public function testAddBundleItemWithCustomOptionQuantity(
|
||||
string $optionQty0,
|
||||
string $optionQty1,
|
||||
string $expectedOptionQty0,
|
||||
string $expectedOptionQty1
|
||||
): void {
|
||||
$this->quoteResource->load(
|
||||
$this->quote,
|
||||
'test_order_1',
|
||||
'reserved_order_id'
|
||||
);
|
||||
$maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$this->quote->getId());
|
||||
$response = $this->graphQlQuery($this->getProductQuery("bundle-product"));
|
||||
$bundleItem = $response['products']['items'][0];
|
||||
$sku = $bundleItem['sku'];
|
||||
$bundleOptions = $bundleItem['items'];
|
||||
|
||||
$uId0 = $bundleOptions[0]['options'][0]['uid'];
|
||||
$uId1 = $bundleOptions[1]['options'][0]['uid'];
|
||||
$response = $this->graphQlMutation(
|
||||
$this->getMutationsQuery($maskedQuoteId, $uId0, $uId1, $sku, $optionQty0, $optionQty1)
|
||||
);
|
||||
$bundleOptions = $response['addProductsToCart']['cart']['items'][0]['bundle_options'];
|
||||
$this->assertEquals($expectedOptionQty0, $bundleOptions[0]['values'][0]['quantity']);
|
||||
$this->assertEquals($expectedOptionQty1, $bundleOptions[1]['values'][0]['quantity']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoApiDataFixture Magento/SalesRule/_files/coupon_code_with_wildcard.php
|
||||
* @magentoApiDataFixture Magento/Bundle/_files/bundle_product_with_dynamic_price.php
|
||||
* @magentoApiDataFixture Magento/Checkout/_files/active_quote.php
|
||||
* @return void
|
||||
*/
|
||||
public function testAddBundleProductToCartWithDiscount(): void
|
||||
{
|
||||
$this->quoteResource->load(
|
||||
$this->quote,
|
||||
'test_order_1',
|
||||
'reserved_order_id'
|
||||
);
|
||||
$maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$this->quote->getId());
|
||||
$response = $this->graphQlQuery($this->getProductQuery('bundle_product_with_dynamic_price'));
|
||||
$bundleItem = $response['products']['items'][0];
|
||||
$sku = $bundleItem['sku'];
|
||||
$bundleOptions = $bundleItem['items'];
|
||||
|
||||
$uId0 = $bundleOptions[0]['options'][0]['uid'];
|
||||
$uId1 = $bundleOptions[1]['options'][0]['uid'];
|
||||
$response = $this->graphQlMutation(
|
||||
$this->getMutationsQuery($maskedQuoteId, $uId0, $uId1, $sku, '1', '1')
|
||||
);
|
||||
$responseDataObject = new DataObject($response);
|
||||
$cartItems = $responseDataObject->getData('addProductsToCart/cart/items');
|
||||
self::assertIsArray($cartItems);
|
||||
self::assertCount(1, $cartItems);
|
||||
self::assertEquals($sku, $cartItems[0]['product']['sku']);
|
||||
|
||||
$couponCode = '2?ds5!2d';
|
||||
$query = $this->getCouponMutationsQuery($maskedQuoteId, $couponCode);
|
||||
$response = $this->graphQlMutation($query);
|
||||
$responseDataObject = new DataObject($response);
|
||||
$appliedCouponCode = $responseDataObject->getData('applyCouponToCart/cart/applied_coupon/code');
|
||||
self::assertEquals($couponCode, $appliedCouponCode);
|
||||
|
||||
$query = $this->getCartQueryWithDiscounts($maskedQuoteId);
|
||||
$response = $this->graphQlMutation($query);
|
||||
$responseDataObject = new DataObject($response);
|
||||
$discounts = $responseDataObject->getData('cart/prices/discounts');
|
||||
self::assertIsArray($discounts);
|
||||
self::assertCount(1, $discounts);
|
||||
self::assertEquals(5, $discounts[0]['amount']['value']);
|
||||
$cartItems = $responseDataObject->getData('cart/items');
|
||||
self::assertIsArray($cartItems);
|
||||
self::assertCount(1, $cartItems);
|
||||
self::assertEquals($sku, $cartItems[0]['product']['sku']);
|
||||
self::assertIsArray($cartItems[0]['prices']['discounts']);
|
||||
self::assertEquals(5, $cartItems[0]['prices']['discounts'][0]['amount']['value']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider for testAddBundleItemWithCustomOptionQuantity
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function bundleItemOptionsDataProvider(): array
|
||||
{
|
||||
return [
|
||||
[
|
||||
'optionQty0' => '10',
|
||||
'optionQty1' => '1',
|
||||
'expectedOptionQty0' => '10',
|
||||
'expectedOptionQty1' => '1',
|
||||
],
|
||||
[
|
||||
'optionQty0' => '5',
|
||||
'optionQty1' => '5',
|
||||
'expectedOptionQty0' => '5',
|
||||
'expectedOptionQty1' => '1',
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns GraphQL query for retrieving a product with customizable options
|
||||
*
|
||||
* @param string $sku
|
||||
* @return string
|
||||
*/
|
||||
private function getProductQuery(string $sku): string
|
||||
{
|
||||
return <<<QUERY
|
||||
{
|
||||
products(search: "{$sku}") {
|
||||
items {
|
||||
sku
|
||||
... on BundleProduct {
|
||||
items {
|
||||
sku
|
||||
option_id
|
||||
uid
|
||||
required
|
||||
type
|
||||
title
|
||||
options {
|
||||
uid
|
||||
label
|
||||
product {
|
||||
sku
|
||||
}
|
||||
can_change_quantity
|
||||
id
|
||||
uid
|
||||
price
|
||||
quantity
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
QUERY;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $maskedQuoteId
|
||||
* @param string $optionUid0
|
||||
* @param string $optionUid1
|
||||
* @param string $sku
|
||||
* @param string $optionQty0
|
||||
* @param string $optionQty1
|
||||
* @return string
|
||||
*/
|
||||
private function getMutationsQuery(
|
||||
string $maskedQuoteId,
|
||||
string $optionUid0,
|
||||
string $optionUid1,
|
||||
string $sku,
|
||||
string $optionQty0,
|
||||
string $optionQty1
|
||||
): string {
|
||||
return <<<QUERY
|
||||
mutation {
|
||||
addProductsToCart(
|
||||
cartId: "{$maskedQuoteId}",
|
||||
cartItems: [
|
||||
{
|
||||
sku: "{$sku}"
|
||||
quantity: 2
|
||||
selected_options: [
|
||||
"{$optionUid0}", "{$optionUid1}"
|
||||
],
|
||||
entered_options: [{
|
||||
uid: "{$optionUid0}"
|
||||
value: "{$optionQty0}"
|
||||
},
|
||||
{
|
||||
uid: "{$optionUid1}"
|
||||
value: "{$optionQty1}"
|
||||
}]
|
||||
}
|
||||
]
|
||||
) {
|
||||
cart {
|
||||
items {
|
||||
id
|
||||
uid
|
||||
quantity
|
||||
product {
|
||||
sku
|
||||
}
|
||||
... on BundleCartItem {
|
||||
bundle_options {
|
||||
id
|
||||
uid
|
||||
label
|
||||
type
|
||||
values {
|
||||
id
|
||||
uid
|
||||
label
|
||||
price
|
||||
quantity
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
user_errors {
|
||||
message
|
||||
}
|
||||
}
|
||||
}
|
||||
QUERY;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $maskedQuoteId
|
||||
* @param string $couponCode
|
||||
* @return string
|
||||
*/
|
||||
private function getCouponMutationsQuery(string $maskedQuoteId, string $couponCode): string
|
||||
{
|
||||
return <<<QUERY
|
||||
mutation {
|
||||
applyCouponToCart(input: {cart_id: "$maskedQuoteId", coupon_code: "$couponCode"}) {
|
||||
cart {
|
||||
id
|
||||
applied_coupon {
|
||||
code
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
QUERY;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $maskedQuoteId
|
||||
* @return string
|
||||
*/
|
||||
private function getCartQueryWithDiscounts(string $maskedQuoteId): string
|
||||
{
|
||||
return <<<QUERY
|
||||
{
|
||||
cart(cart_id: "$maskedQuoteId") {
|
||||
email
|
||||
items {
|
||||
uid
|
||||
prices {
|
||||
discounts {
|
||||
amount {
|
||||
value
|
||||
}
|
||||
}
|
||||
}
|
||||
product {
|
||||
sku
|
||||
}
|
||||
}
|
||||
applied_coupons {
|
||||
code
|
||||
}
|
||||
prices {
|
||||
discounts {
|
||||
amount {
|
||||
value
|
||||
}
|
||||
label
|
||||
}
|
||||
grand_total {
|
||||
value
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
QUERY;
|
||||
}
|
||||
}
|
||||
+505
@@ -0,0 +1,505 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\GraphQl\Bundle;
|
||||
|
||||
use Magento\Bundle\Test\Fixture\Link as BundleSelectionFixture;
|
||||
use Magento\Bundle\Test\Fixture\Option as BundleOptionFixture;
|
||||
use Magento\Bundle\Test\Fixture\Product as BundleProductFixture;
|
||||
use Magento\Catalog\Api\ProductRepositoryInterface;
|
||||
use Magento\Catalog\Test\Fixture\Product as ProductFixture;
|
||||
use Magento\TestFramework\TestCase\GraphQlAbstract;
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
use Magento\Quote\Model\Quote;
|
||||
use Magento\Quote\Model\QuoteIdToMaskedQuoteIdInterface;
|
||||
use Magento\Quote\Model\ResourceModel\Quote as QuoteResource;
|
||||
use Magento\TestFramework\Fixture\DataFixture;
|
||||
use Magento\TestFramework\Fixture\DataFixtureStorage;
|
||||
use Magento\TestFramework\Fixture\DataFixtureStorageManager;
|
||||
|
||||
/**
|
||||
* Test adding bundled products to cart
|
||||
*/
|
||||
class AddBundleProductToCartTest extends GraphQlAbstract
|
||||
{
|
||||
/**
|
||||
* @var QuoteResource
|
||||
*/
|
||||
private $quoteResource;
|
||||
|
||||
/**
|
||||
* @var Quote
|
||||
*/
|
||||
private $quote;
|
||||
|
||||
/**
|
||||
* @var QuoteIdToMaskedQuoteIdInterface
|
||||
*/
|
||||
private $quoteIdToMaskedId;
|
||||
|
||||
/**
|
||||
* @var ProductRepositoryInterface
|
||||
*/
|
||||
private $productRepository;
|
||||
|
||||
/**
|
||||
* @var DataFixtureStorage
|
||||
*/
|
||||
private $fixtures;
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
$objectManager = Bootstrap::getObjectManager();
|
||||
$this->quoteResource = $objectManager->get(QuoteResource::class);
|
||||
$this->quote = $objectManager->create(Quote::class);
|
||||
$this->quoteIdToMaskedId = $objectManager->get(QuoteIdToMaskedQuoteIdInterface::class);
|
||||
$this->productRepository = $objectManager->get(ProductRepositoryInterface::class);
|
||||
$this->fixtures = $objectManager->get(DataFixtureStorageManager::class)->getStorage();
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoApiDataFixture Magento/Bundle/_files/product_1.php
|
||||
* @magentoApiDataFixture Magento/Checkout/_files/active_quote.php
|
||||
*/
|
||||
public function testAddBundleProductToCart()
|
||||
{
|
||||
$sku = 'bundle-product';
|
||||
|
||||
$this->quoteResource->load(
|
||||
$this->quote,
|
||||
'test_order_1',
|
||||
'reserved_order_id'
|
||||
);
|
||||
|
||||
$product = $this->productRepository->get($sku);
|
||||
|
||||
/** @var $typeInstance \Magento\Bundle\Model\Product\Type */
|
||||
$typeInstance = $product->getTypeInstance();
|
||||
$typeInstance->setStoreFilter($product->getStoreId(), $product);
|
||||
/** @var $option \Magento\Bundle\Model\Option */
|
||||
$option = $typeInstance->getOptionsCollection($product)->getFirstItem();
|
||||
/** @var \Magento\Catalog\Model\Product $selection */
|
||||
$selection = $typeInstance->getSelectionsCollection([$option->getId()], $product)->getFirstItem();
|
||||
$optionId = $option->getId();
|
||||
$selectionId = $selection->getSelectionId();
|
||||
|
||||
$maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$this->quote->getId());
|
||||
|
||||
$query = <<<QUERY
|
||||
mutation {
|
||||
addBundleProductsToCart(input:{
|
||||
cart_id:"{$maskedQuoteId}"
|
||||
cart_items:[
|
||||
{
|
||||
data:{
|
||||
sku:"{$sku}"
|
||||
quantity:1
|
||||
}
|
||||
bundle_options:[
|
||||
{
|
||||
id:{$optionId}
|
||||
quantity:1
|
||||
value:[
|
||||
"{$selectionId}"
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}) {
|
||||
cart {
|
||||
items {
|
||||
id
|
||||
uid
|
||||
quantity
|
||||
product {
|
||||
sku
|
||||
}
|
||||
... on BundleCartItem {
|
||||
bundle_options {
|
||||
id
|
||||
uid
|
||||
label
|
||||
type
|
||||
values {
|
||||
id
|
||||
uid
|
||||
label
|
||||
price
|
||||
quantity
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
QUERY;
|
||||
|
||||
$response = $this->graphQlMutation($query);
|
||||
|
||||
$this->assertArrayHasKey('addBundleProductsToCart', $response);
|
||||
$this->assertArrayHasKey('cart', $response['addBundleProductsToCart']);
|
||||
$cart = $response['addBundleProductsToCart']['cart'];
|
||||
$bundleItem = current($cart['items']);
|
||||
$this->assertEquals($sku, $bundleItem['product']['sku']);
|
||||
$bundleItemOption = current($bundleItem['bundle_options']);
|
||||
$this->assertEquals($optionId, $bundleItemOption['id']);
|
||||
$this->assertEquals($option->getTitle(), $bundleItemOption['label']);
|
||||
$this->assertEquals($option->getType(), $bundleItemOption['type']);
|
||||
$value = current($bundleItemOption['values']);
|
||||
$this->assertEquals($selection->getSelectionId(), $value['id']);
|
||||
$this->assertEquals((float) $selection->getSelectionPriceValue(), $value['price']);
|
||||
$this->assertEquals(1, $value['quantity']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoApiDataFixture Magento/Bundle/_files/quote_with_bundle_and_options.php
|
||||
* @dataProvider dataProviderTestUpdateBundleItemQuantity
|
||||
*/
|
||||
public function testUpdateBundleItemQuantity(int $quantity)
|
||||
{
|
||||
$this->quoteResource->load(
|
||||
$this->quote,
|
||||
'test_cart_with_bundle_and_options',
|
||||
'reserved_order_id'
|
||||
);
|
||||
|
||||
$item = current($this->quote->getAllVisibleItems());
|
||||
|
||||
$maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$this->quote->getId());
|
||||
$mutation = <<<QUERY
|
||||
mutation {
|
||||
updateCartItems(
|
||||
input: {
|
||||
cart_id: "{$maskedQuoteId}"
|
||||
cart_items: {
|
||||
cart_item_id: {$item->getId()}
|
||||
quantity: {$quantity}
|
||||
}
|
||||
}
|
||||
) {
|
||||
cart {
|
||||
items {
|
||||
id
|
||||
quantity
|
||||
product {
|
||||
sku
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
QUERY;
|
||||
|
||||
$response = $this->graphQlMutation($mutation);
|
||||
|
||||
$this->assertArrayHasKey('updateCartItems', $response);
|
||||
$this->assertArrayHasKey('cart', $response['updateCartItems']);
|
||||
$cart = $response['updateCartItems']['cart'];
|
||||
if ($quantity === 0) {
|
||||
$this->assertCount(0, $cart['items']);
|
||||
return;
|
||||
}
|
||||
|
||||
$bundleItem = current($cart['items']);
|
||||
$this->assertEquals($quantity, $bundleItem['quantity']);
|
||||
}
|
||||
|
||||
public function dataProviderTestUpdateBundleItemQuantity(): array
|
||||
{
|
||||
return [
|
||||
[2],
|
||||
[0],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoApiDataFixture Magento/Bundle/_files/product_1.php
|
||||
* @magentoApiDataFixture Magento/Checkout/_files/active_quote.php
|
||||
*/
|
||||
public function testAddBundleToCartWithoutOptions()
|
||||
{
|
||||
$this->expectException(\Exception::class);
|
||||
$this->expectExceptionMessage('Please select all required options');
|
||||
|
||||
$this->quoteResource->load(
|
||||
$this->quote,
|
||||
'test_order_1',
|
||||
'reserved_order_id'
|
||||
);
|
||||
|
||||
$maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$this->quote->getId());
|
||||
|
||||
$query = <<<QUERY
|
||||
mutation {
|
||||
addBundleProductsToCart(input:{
|
||||
cart_id:"{$maskedQuoteId}"
|
||||
cart_items:[
|
||||
{
|
||||
data:{
|
||||
sku:"bundle-product"
|
||||
quantity:1
|
||||
}
|
||||
bundle_options:[
|
||||
{
|
||||
id:555
|
||||
quantity:1
|
||||
value:[
|
||||
"555"
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}) {
|
||||
cart {
|
||||
items {
|
||||
id
|
||||
quantity
|
||||
product {
|
||||
sku
|
||||
}
|
||||
... on BundleCartItem {
|
||||
bundle_options {
|
||||
id
|
||||
label
|
||||
type
|
||||
values {
|
||||
id
|
||||
label
|
||||
price
|
||||
quantity
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
QUERY;
|
||||
|
||||
$this->graphQlMutation($query);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoApiDataFixture Magento/Bundle/_files/product_with_multiple_options_radio_select.php
|
||||
* @magentoApiDataFixture Magento/Checkout/_files/active_quote.php
|
||||
*/
|
||||
public function testAddBundleToCartWithRadioAndSelectErr()
|
||||
{
|
||||
$this->expectException(\Exception::class);
|
||||
$this->expectExceptionMessage('Option type (select, radio) should have only one element.');
|
||||
|
||||
$sku = 'bundle-product';
|
||||
|
||||
$this->quoteResource->load(
|
||||
$this->quote,
|
||||
'test_order_1',
|
||||
'reserved_order_id'
|
||||
);
|
||||
|
||||
$product = $this->productRepository->get($sku);
|
||||
|
||||
/** @var $typeInstance \Magento\Bundle\Model\Product\Type */
|
||||
$typeInstance = $product->getTypeInstance();
|
||||
$typeInstance->setStoreFilter($product->getStoreId(), $product);
|
||||
/** @var $option \Magento\Bundle\Model\Option */
|
||||
$options = $typeInstance->getOptionsCollection($product);
|
||||
|
||||
$selectionIds = [];
|
||||
$optionIds = [];
|
||||
foreach ($options as $option) {
|
||||
$type = $option->getType();
|
||||
|
||||
/** @var \Magento\Catalog\Model\Product $selection */
|
||||
$selections = $typeInstance->getSelectionsCollection([$option->getId()], $product);
|
||||
$optionIds[$type] = $option->getId();
|
||||
|
||||
foreach ($selections->getItems() as $selection) {
|
||||
$selectionIds[$type][] = $selection->getSelectionId();
|
||||
}
|
||||
}
|
||||
|
||||
$maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$this->quote->getId());
|
||||
|
||||
$query = <<<QUERY
|
||||
mutation {
|
||||
addBundleProductsToCart(input:{
|
||||
cart_id:"{$maskedQuoteId}"
|
||||
cart_items:[
|
||||
{
|
||||
data:{
|
||||
sku:"{$sku}"
|
||||
quantity:1
|
||||
}
|
||||
bundle_options:[
|
||||
{
|
||||
id:{$optionIds['select']}
|
||||
quantity:1
|
||||
value:[
|
||||
"{$selectionIds['select'][0]}"
|
||||
"{$selectionIds['select'][1]}"
|
||||
]
|
||||
},
|
||||
{
|
||||
id:{$optionIds['radio']}
|
||||
quantity:1
|
||||
value:[
|
||||
"{$selectionIds['radio'][0]}"
|
||||
"{$selectionIds['radio'][1]}"
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}) {
|
||||
cart {
|
||||
items {
|
||||
id
|
||||
quantity
|
||||
product {
|
||||
sku
|
||||
}
|
||||
... on BundleCartItem {
|
||||
bundle_options {
|
||||
id
|
||||
label
|
||||
type
|
||||
values {
|
||||
id
|
||||
label
|
||||
price
|
||||
quantity
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
QUERY;
|
||||
|
||||
$this->graphQlMutation($query);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoApiDataFixture Magento/Checkout/_files/active_quote.php
|
||||
*/
|
||||
#[
|
||||
DataFixture(ProductFixture::class, ['sku' => 'simple-1', 'price' => 10], 'p1'),
|
||||
DataFixture(ProductFixture::class, ['sku' => 'simple2', 'price' => 20], 'p2'),
|
||||
DataFixture(BundleSelectionFixture::class, ['sku' => '$p1.sku$', 'price' => 10, 'price_type' => 0], 'link1'),
|
||||
DataFixture(BundleSelectionFixture::class, ['sku' => '$p2.sku$', 'price' => 25, 'price_type' => 1], 'link2'),
|
||||
DataFixture(BundleOptionFixture::class, ['title' => 'Checkbox Options', 'type' => 'checkbox',
|
||||
'required' => 1,'product_links' => ['$link1$', '$link2$']], 'opt1'),
|
||||
DataFixture(BundleOptionFixture::class, ['title' => 'Multiselect Options', 'type' => 'multi',
|
||||
'required' => 1,'product_links' => ['$link1$', '$link2$']], 'opt2'),
|
||||
DataFixture(
|
||||
BundleProductFixture::class,
|
||||
['sku' => 'bundle-product-multiselect-checkbox-options','price' => 50,'price_type' => 1,
|
||||
'_options' => ['$opt1$', '$opt2$']],
|
||||
'bundle-product-multiselect-checkbox-options'
|
||||
),
|
||||
]
|
||||
public function testAddBundleToCartWithEmptyMultiselectOptionValue()
|
||||
{
|
||||
$this->expectException(\Exception::class);
|
||||
$this->expectExceptionMessage('Please select all required options.');
|
||||
|
||||
$this->quoteResource->load(
|
||||
$this->quote,
|
||||
'test_order_1',
|
||||
'reserved_order_id'
|
||||
);
|
||||
$sku = 'bundle-product-multiselect-checkbox-options';
|
||||
|
||||
$product = $this->fixtures->get($sku);
|
||||
|
||||
/** @var $typeInstance \Magento\Bundle\Model\Product\Type */
|
||||
$typeInstance = $product->getTypeInstance();
|
||||
$typeInstance->setStoreFilter($product->getStoreId(), $product);
|
||||
/** @var $option \Magento\Bundle\Model\Option */
|
||||
$options = $typeInstance->getOptionsCollection($product);
|
||||
|
||||
$selectionIds = [];
|
||||
$optionIds = [];
|
||||
foreach ($options as $option) {
|
||||
$type = $option->getType();
|
||||
|
||||
/** @var \Magento\Catalog\Model\Product $selection */
|
||||
$selections = $typeInstance->getSelectionsCollection([$option->getId()], $product);
|
||||
$optionIds[$type] = $option->getId();
|
||||
|
||||
foreach ($selections->getItems() as $selection) {
|
||||
$selectionIds[$type][] = $selection->getSelectionId();
|
||||
}
|
||||
}
|
||||
|
||||
$maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$this->quote->getId());
|
||||
|
||||
$query = <<<QUERY
|
||||
mutation {
|
||||
addBundleProductsToCart(input:{
|
||||
cart_id: "{$maskedQuoteId}"
|
||||
cart_items: [
|
||||
{
|
||||
data: {
|
||||
sku: "{$sku}"
|
||||
quantity: 1
|
||||
}
|
||||
bundle_options: [
|
||||
{
|
||||
id: {$optionIds['multi']}
|
||||
quantity: 1
|
||||
value: [
|
||||
""
|
||||
]
|
||||
},
|
||||
{
|
||||
id: {$optionIds['checkbox']}
|
||||
quantity: 1
|
||||
value: [
|
||||
"{$selectionIds['checkbox'][0]}"
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}) {
|
||||
cart {
|
||||
items {
|
||||
id
|
||||
quantity
|
||||
product {
|
||||
sku
|
||||
}
|
||||
... on BundleCartItem {
|
||||
bundle_options {
|
||||
id
|
||||
label
|
||||
type
|
||||
values {
|
||||
id
|
||||
label
|
||||
price
|
||||
quantity
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
QUERY;
|
||||
|
||||
$this->graphQlMutation($query);
|
||||
}
|
||||
}
|
||||
+132
@@ -0,0 +1,132 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\GraphQl\Bundle;
|
||||
|
||||
use Magento\TestFramework\TestCase\GraphQlAbstract;
|
||||
|
||||
class BundleProductMainPriceTest extends GraphQlAbstract
|
||||
{
|
||||
public function getQuery()
|
||||
{
|
||||
$productSku = 'fixed_bundle_product_with_special_price';
|
||||
return <<<QUERY
|
||||
{
|
||||
products(filter:{ sku:{eq:"{$productSku}"}})
|
||||
{
|
||||
items {
|
||||
url_key
|
||||
sku
|
||||
price_range {
|
||||
minimum_price {
|
||||
final_price {
|
||||
value
|
||||
currency
|
||||
}
|
||||
discount {
|
||||
percent_off
|
||||
amount_off
|
||||
}
|
||||
regular_price {
|
||||
value
|
||||
currency
|
||||
} }
|
||||
maximum_price {
|
||||
final_price {
|
||||
value
|
||||
currency
|
||||
}
|
||||
regular_price {
|
||||
value
|
||||
currency
|
||||
}
|
||||
discount {
|
||||
percent_off
|
||||
amount_off
|
||||
}
|
||||
} }
|
||||
... on BundleProduct {
|
||||
price_details{
|
||||
main_price
|
||||
main_final_price
|
||||
discount_percentage
|
||||
}
|
||||
dynamic_sku
|
||||
dynamic_price
|
||||
dynamic_weight
|
||||
price_view
|
||||
ship_bundle_items
|
||||
items {
|
||||
uid
|
||||
title
|
||||
required
|
||||
type
|
||||
position
|
||||
sku
|
||||
options {
|
||||
uid
|
||||
quantity
|
||||
position
|
||||
is_default
|
||||
price
|
||||
price_type
|
||||
can_change_quantity
|
||||
label
|
||||
product {
|
||||
uid
|
||||
name
|
||||
sku
|
||||
price_range {
|
||||
minimum_price {
|
||||
final_price {
|
||||
value
|
||||
}
|
||||
regular_price {
|
||||
value
|
||||
} }
|
||||
maximum_price {
|
||||
final_price {
|
||||
value
|
||||
currency
|
||||
}
|
||||
regular_price {
|
||||
value
|
||||
currency
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
__typename
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
QUERY;
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoApiDataFixture Magento/Bundle/_files/fixed_bundle_product_with_special_price.php
|
||||
* @return void
|
||||
*/
|
||||
public function testBundleProductPriceDetails(): void
|
||||
{
|
||||
$query = $this->getQuery();
|
||||
$response = $this->graphQlQuery($query);
|
||||
$product = $response['products']['items'][0];
|
||||
$this->assertArrayHasKey('price_details', $product);
|
||||
$priceDetails = $product['price_details'];
|
||||
$this->assertArrayHasKey('main_price', $priceDetails);
|
||||
$this->assertArrayHasKey('main_final_price', $priceDetails);
|
||||
$this->assertArrayHasKey('discount_percentage', $priceDetails);
|
||||
$this->assertEquals(50.0, $priceDetails['main_price']);
|
||||
$this->assertEquals(40.0, $priceDetails['main_final_price']);
|
||||
$this->assertEquals(20, $priceDetails['discount_percentage']);
|
||||
}
|
||||
}
|
||||
+322
@@ -0,0 +1,322 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\GraphQl\Bundle;
|
||||
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
use Magento\TestFramework\Helper\CompareArraysRecursively;
|
||||
use Magento\TestFramework\TestCase\GraphQlAbstract;
|
||||
|
||||
/**
|
||||
* Bundle product with multiple options test.
|
||||
*/
|
||||
class BundleProductMultipleOptionsTest extends GraphQlAbstract
|
||||
{
|
||||
/**
|
||||
* @var CompareArraysRecursively
|
||||
*/
|
||||
private $compareArraysRecursively;
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
$objectManager = Bootstrap::getObjectManager();
|
||||
$this->compareArraysRecursively = $objectManager->create(CompareArraysRecursively::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoApiDataFixture Magento/Bundle/_files/product_with_multiple_options.php
|
||||
* @param array $bundleProductDataProvider
|
||||
*
|
||||
* @dataProvider getBundleProductDataProvider
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function testBundleProductWithMultipleOptions(array $bundleProductDataProvider): void
|
||||
{
|
||||
$productSku = 'bundle-product';
|
||||
$query
|
||||
= <<<QUERY
|
||||
{
|
||||
products(filter: {sku: {eq: "{$productSku}"}}) {
|
||||
items {
|
||||
sku
|
||||
type_id
|
||||
id
|
||||
name
|
||||
... on BundleProduct {
|
||||
dynamic_sku
|
||||
dynamic_price
|
||||
dynamic_weight
|
||||
price_view
|
||||
ship_bundle_items
|
||||
items {
|
||||
option_id
|
||||
title
|
||||
required
|
||||
type
|
||||
position
|
||||
sku
|
||||
options {
|
||||
id
|
||||
quantity
|
||||
position
|
||||
is_default
|
||||
price
|
||||
price_type
|
||||
can_change_quantity
|
||||
label
|
||||
product {
|
||||
id
|
||||
name
|
||||
sku
|
||||
type_id
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
QUERY;
|
||||
|
||||
$response = $this->graphQlQuery($query);
|
||||
$this->assertBundleProduct($response, $bundleProductDataProvider);
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert bundle product response.
|
||||
*
|
||||
* @param array $response
|
||||
* @param array $bundleProductDataProvider
|
||||
*/
|
||||
private function assertBundleProduct(array $response, array $bundleProductDataProvider): void
|
||||
{
|
||||
$this->assertNotEmpty($response['products']['items'], 'Precondition failed: "items" must not be empty');
|
||||
$productItems = $response['products']['items'];
|
||||
|
||||
foreach ($bundleProductDataProvider as $key => $data) {
|
||||
$diff = $this->compareArraysRecursively->execute($data, $productItems[$key]);
|
||||
self::assertEquals([], $diff, "Actual response doesn't equal to expected data");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Bundle product data provider.
|
||||
*
|
||||
* @return array
|
||||
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
|
||||
*/
|
||||
public function getBundleProductDataProvider(): array
|
||||
{
|
||||
return [
|
||||
'products' => [
|
||||
'items' => [
|
||||
[
|
||||
'sku' => 'bundle-product',
|
||||
'type_id' => 'bundle',
|
||||
'name' => 'Bundle Product',
|
||||
'dynamic_sku' => true,
|
||||
'dynamic_price' => false,
|
||||
'dynamic_weight' => true,
|
||||
'price_view' => 'AS_LOW_AS',
|
||||
'ship_bundle_items' => 'TOGETHER',
|
||||
'items' => [
|
||||
[
|
||||
'title' => 'Option 1',
|
||||
'required' => true,
|
||||
'type' => 'select',
|
||||
'position' => 1,
|
||||
'sku' => 'bundle-product',
|
||||
'options' => [
|
||||
[
|
||||
'quantity' => 1,
|
||||
'position' => 0,
|
||||
'is_default' => false,
|
||||
'price' => 0,
|
||||
'price_type' => 'FIXED',
|
||||
'can_change_quantity' => false,
|
||||
'label' => 'Simple Product1',
|
||||
'product' => [
|
||||
'name' => 'Simple Product1',
|
||||
'sku' => 'simple1',
|
||||
'type_id' => 'simple',
|
||||
],
|
||||
],
|
||||
[
|
||||
'quantity' => 1,
|
||||
'position' => 0,
|
||||
'is_default' => false,
|
||||
'price' => 0,
|
||||
'price_type' => 'FIXED',
|
||||
'can_change_quantity' => false,
|
||||
'label' => 'Simple Product2',
|
||||
'product' => [
|
||||
'name' => 'Simple Product2',
|
||||
'sku' => 'simple2',
|
||||
'type_id' => 'simple',
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
[
|
||||
'title' => 'Option 2',
|
||||
'required' => true,
|
||||
'type' => 'radio',
|
||||
'position' => 2,
|
||||
'sku' => 'bundle-product',
|
||||
'options' => [
|
||||
[
|
||||
'quantity' => 1,
|
||||
'position' => 0,
|
||||
'is_default' => false,
|
||||
'price' => 0,
|
||||
'price_type' => 'FIXED',
|
||||
'can_change_quantity' => false,
|
||||
'label' => 'Simple Product1',
|
||||
'product' => [
|
||||
'name' => 'Simple Product1',
|
||||
'sku' => 'simple1',
|
||||
'type_id' => 'simple',
|
||||
],
|
||||
],
|
||||
[
|
||||
|
||||
'quantity' => 1,
|
||||
'position' => 0,
|
||||
'is_default' => false,
|
||||
'price' => 0,
|
||||
'price_type' => 'FIXED',
|
||||
'can_change_quantity' => false,
|
||||
'label' => 'Simple Product2',
|
||||
'product' => [
|
||||
'name' => 'Simple Product2',
|
||||
'sku' => 'simple2',
|
||||
'type_id' => 'simple',
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
[
|
||||
'title' => 'Option 3',
|
||||
'required' => true,
|
||||
'type' => 'checkbox',
|
||||
'position' => 3,
|
||||
'sku' => 'bundle-product',
|
||||
'options' => [
|
||||
[
|
||||
'quantity' => 1,
|
||||
'position' => 0,
|
||||
'is_default' => false,
|
||||
'price' => 0,
|
||||
'price_type' => 'FIXED',
|
||||
'can_change_quantity' => false,
|
||||
'label' => 'Simple Product1',
|
||||
'product' => [
|
||||
'name' => 'Simple Product1',
|
||||
'sku' => 'simple1',
|
||||
'type_id' => 'simple',
|
||||
],
|
||||
],
|
||||
[
|
||||
'quantity' => 1,
|
||||
'position' => 0,
|
||||
'is_default' => false,
|
||||
'price' => 0,
|
||||
'price_type' => 'FIXED',
|
||||
'can_change_quantity' => false,
|
||||
'label' => 'Simple Product2',
|
||||
'product' => [
|
||||
'name' => 'Simple Product2',
|
||||
'sku' => 'simple2',
|
||||
'type_id' => 'simple',
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
[
|
||||
'title' => 'Option 4',
|
||||
'required' => true,
|
||||
'type' => 'multi',
|
||||
'position' => 4,
|
||||
'sku' => 'bundle-product',
|
||||
'options' => [
|
||||
[
|
||||
'quantity' => 1,
|
||||
'position' => 0,
|
||||
'is_default' => false,
|
||||
'price' => 0,
|
||||
'price_type' => 'FIXED',
|
||||
'can_change_quantity' => false,
|
||||
'label' => 'Simple Product1',
|
||||
'product' => [
|
||||
'name' => 'Simple Product1',
|
||||
'sku' => 'simple1',
|
||||
'type_id' => 'simple',
|
||||
],
|
||||
],
|
||||
[
|
||||
'quantity' => 1,
|
||||
'position' => 0,
|
||||
'is_default' => false,
|
||||
'price' => 0,
|
||||
'price_type' => 'FIXED',
|
||||
'can_change_quantity' => false,
|
||||
'label' => 'Simple Product2',
|
||||
'product' => [
|
||||
'name' => 'Simple Product2',
|
||||
'sku' => 'simple2',
|
||||
'type_id' => 'simple',
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
[
|
||||
'title' => 'Option 5',
|
||||
'required' => false,
|
||||
'type' => 'multi',
|
||||
'position' => 5,
|
||||
'sku' => 'bundle-product',
|
||||
'options' => [
|
||||
[
|
||||
'quantity' => 1,
|
||||
'position' => 0,
|
||||
'is_default' => false,
|
||||
'price' => 0,
|
||||
'price_type' => 'FIXED',
|
||||
'can_change_quantity' => false,
|
||||
'label' => 'Simple Product1',
|
||||
'product' => [
|
||||
'name' => 'Simple Product1',
|
||||
'sku' => 'simple1',
|
||||
'type_id' => 'simple',
|
||||
],
|
||||
],
|
||||
[
|
||||
'quantity' => 1,
|
||||
'position' => 0,
|
||||
'is_default' => false,
|
||||
'price' => 0,
|
||||
'price_type' => 'FIXED',
|
||||
'can_change_quantity' => false,
|
||||
'label' => 'Simple Product2',
|
||||
'product' => [
|
||||
'name' => 'Simple Product2',
|
||||
'sku' => 'simple2',
|
||||
'type_id' => 'simple',
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,579 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\GraphQl\Bundle;
|
||||
|
||||
use Magento\Bundle\Model\Product\OptionList;
|
||||
use Magento\Bundle\Test\Fixture\Option as BundleOptionFixture;
|
||||
use Magento\Bundle\Test\Fixture\Product as BundleProductFixture;
|
||||
use Magento\Catalog\Api\Data\ProductInterface;
|
||||
use Magento\Catalog\Api\ProductRepositoryInterface;
|
||||
use Magento\Catalog\Test\Fixture\Product as ProductFixture;
|
||||
use Magento\Framework\App\Config\ScopeConfigInterface;
|
||||
use Magento\TestFramework\Fixture\DataFixture;
|
||||
use Magento\TestFramework\ObjectManager;
|
||||
use Magento\TestFramework\TestCase\GraphQlAbstract;
|
||||
use Magento\Store\Model\StoreManagerInterface;
|
||||
use Magento\Catalog\Model\Product\Attribute\Source\Status as ProductStatus;
|
||||
use Magento\TestFramework\Fixture\DataFixtureStorage;
|
||||
use Magento\TestFramework\Fixture\DataFixtureStorageManager;
|
||||
|
||||
/**
|
||||
* Test querying Bundle products
|
||||
*/
|
||||
class BundleProductViewTest extends GraphQlAbstract
|
||||
{
|
||||
private const KEY_PRICE_TYPE_FIXED = 'FIXED';
|
||||
private const KEY_PRICE_TYPE_DYNAMIC = 'DYNAMIC';
|
||||
|
||||
/**
|
||||
* @magentoApiDataFixture Magento/Bundle/_files/product_1.php
|
||||
*/
|
||||
public function testAllFieldsBundleProducts()
|
||||
{
|
||||
$productSku = 'bundle-product';
|
||||
$query
|
||||
= <<<QUERY
|
||||
{
|
||||
products(filter: {sku: {eq: "{$productSku}"}})
|
||||
{
|
||||
items{
|
||||
sku
|
||||
type_id
|
||||
id
|
||||
name
|
||||
... on PhysicalProductInterface {
|
||||
weight
|
||||
}
|
||||
... on BundleProduct {
|
||||
dynamic_sku
|
||||
dynamic_price
|
||||
dynamic_weight
|
||||
price_view
|
||||
ship_bundle_items
|
||||
items {
|
||||
option_id
|
||||
title
|
||||
required
|
||||
type
|
||||
position
|
||||
sku
|
||||
price_range{
|
||||
maximum_price {
|
||||
final_price {
|
||||
currency
|
||||
value
|
||||
}
|
||||
}
|
||||
minimum_price {
|
||||
final_price {
|
||||
currency
|
||||
value
|
||||
}
|
||||
}
|
||||
}
|
||||
options {
|
||||
id
|
||||
quantity
|
||||
position
|
||||
is_default
|
||||
price
|
||||
price_type
|
||||
can_change_quantity
|
||||
label
|
||||
product {
|
||||
id
|
||||
name
|
||||
sku
|
||||
type_id
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
QUERY;
|
||||
|
||||
$response = $this->graphQlQuery($query);
|
||||
|
||||
/** @var ProductRepositoryInterface $productRepository */
|
||||
$productRepository = ObjectManager::getInstance()->get(ProductRepositoryInterface::class);
|
||||
$bundleProduct = $productRepository->get($productSku, false, null, true);
|
||||
if ((bool)$bundleProduct->getShipmentType()) {
|
||||
$this->assertEquals('SEPARATELY', $response['products']['items'][0]['ship_bundle_items']);
|
||||
} else {
|
||||
$this->assertEquals('TOGETHER', $response['products']['items'][0]['ship_bundle_items']);
|
||||
}
|
||||
if ((bool)$bundleProduct->getPriceView()) {
|
||||
$this->assertEquals('AS_LOW_AS', $response['products']['items'][0]['price_view']);
|
||||
} else {
|
||||
$this->assertEquals('PRICE_RANGE', $response['products']['items'][0]['price_view']);
|
||||
}
|
||||
$this->assertBundleBaseFields($bundleProduct, $response['products']['items'][0]);
|
||||
$product = $response['products']['items'][0]['items'][0];
|
||||
$this->assertEquals(10, $product['price_range']['maximum_price']['final_price']['value']);
|
||||
$this->assertEquals(10, $product['price_range']['minimum_price']['final_price']['value']);
|
||||
|
||||
$this->assertBundleProductOptions($bundleProduct, $response['products']['items'][0]);
|
||||
$this->assertNotEmpty(
|
||||
$response['products']['items'][0]['items'],
|
||||
"Precondition failed: 'items' must not be empty"
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoApiDataFixture Magento/Bundle/_files/bundle_product_with_not_visible_children.php
|
||||
*/
|
||||
public function testBundleProductWithNotVisibleChildren()
|
||||
{
|
||||
$productSku = 'bundle-product-1';
|
||||
$query
|
||||
= <<<QUERY
|
||||
{
|
||||
products(filter: {sku: {eq: "{$productSku}"}})
|
||||
{
|
||||
items{
|
||||
sku
|
||||
type_id
|
||||
id
|
||||
name
|
||||
... on PhysicalProductInterface {
|
||||
weight
|
||||
}
|
||||
... on BundleProduct {
|
||||
dynamic_sku
|
||||
dynamic_price
|
||||
dynamic_weight
|
||||
price_view
|
||||
ship_bundle_items
|
||||
items {
|
||||
option_id
|
||||
title
|
||||
required
|
||||
type
|
||||
position
|
||||
sku
|
||||
options {
|
||||
id
|
||||
quantity
|
||||
position
|
||||
is_default
|
||||
price
|
||||
price_type
|
||||
can_change_quantity
|
||||
label
|
||||
product {
|
||||
id
|
||||
name
|
||||
sku
|
||||
type_id
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
QUERY;
|
||||
|
||||
/** @var \Magento\Config\Model\ResourceModel\Config $config */
|
||||
$config = ObjectManager::getInstance()->get(\Magento\Config\Model\ResourceModel\Config::class);
|
||||
$config->saveConfig(
|
||||
\Magento\CatalogInventory\Model\Configuration::XML_PATH_SHOW_OUT_OF_STOCK,
|
||||
0,
|
||||
ScopeConfigInterface::SCOPE_TYPE_DEFAULT,
|
||||
0
|
||||
);
|
||||
ObjectManager::getInstance()->get(\Magento\Framework\App\Cache::class)
|
||||
->clean(\Magento\Framework\App\Config::CACHE_TAG);
|
||||
$response = $this->graphQlQuery($query);
|
||||
$this->assertNotEmpty(
|
||||
$response['products']['items'],
|
||||
"Precondition failed: 'items' must not be empty"
|
||||
);
|
||||
|
||||
/** @var ProductRepositoryInterface $productRepository */
|
||||
$productRepository = ObjectManager::getInstance()->get(ProductRepositoryInterface::class);
|
||||
$bundleProduct = $productRepository->get($productSku, false, null, true);
|
||||
if ((bool)$bundleProduct->getShipmentType()) {
|
||||
$this->assertEquals('SEPARATELY', $response['products']['items'][0]['ship_bundle_items']);
|
||||
} else {
|
||||
$this->assertEquals('TOGETHER', $response['products']['items'][0]['ship_bundle_items']);
|
||||
}
|
||||
if ((bool)$bundleProduct->getPriceView()) {
|
||||
$this->assertEquals('AS_LOW_AS', $response['products']['items'][0]['price_view']);
|
||||
} else {
|
||||
$this->assertEquals('PRICE_RANGE', $response['products']['items'][0]['price_view']);
|
||||
}
|
||||
$this->assertBundleBaseFields($bundleProduct, $response['products']['items'][0]);
|
||||
|
||||
$this->assertBundleProductOptions($bundleProduct, $response['products']['items'][0]);
|
||||
$this->assertNotEmpty(
|
||||
$response['products']['items'][0]['items'],
|
||||
"Precondition failed: 'items' must not be empty"
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ProductInterface $product
|
||||
* @param array $actualResponse
|
||||
*/
|
||||
private function assertBundleBaseFields($product, $actualResponse)
|
||||
{
|
||||
$assertionMap = [
|
||||
['response_field' => 'sku', 'expected_value' => $product->getSku()],
|
||||
['response_field' => 'type_id', 'expected_value' => $product->getTypeId()],
|
||||
['response_field' => 'id', 'expected_value' => $product->getId()],
|
||||
['response_field' => 'name', 'expected_value' => $product->getName()],
|
||||
['response_field' => 'weight', 'expected_value' => $product->getWeight()],
|
||||
['response_field' => 'dynamic_price', 'expected_value' => !(bool)$product->getPriceType()],
|
||||
['response_field' => 'dynamic_weight', 'expected_value' => !(bool)$product->getWeightType()],
|
||||
['response_field' => 'dynamic_sku', 'expected_value' => !(bool)$product->getSkuType()]
|
||||
];
|
||||
|
||||
$this->assertResponseFields($actualResponse, $assertionMap);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ProductInterface $product
|
||||
* @param array $actualResponse
|
||||
*/
|
||||
private function assertBundleProductOptions($product, $actualResponse)
|
||||
{
|
||||
$this->assertNotEmpty(
|
||||
$actualResponse['items'],
|
||||
"Precondition failed: 'bundle product items' must not be empty"
|
||||
);
|
||||
/** @var OptionList $optionList */
|
||||
$optionList = ObjectManager::getInstance()->get(\Magento\Bundle\Model\Product\OptionList::class);
|
||||
$options = $optionList->getItems($product);
|
||||
$option = $options[0];
|
||||
/** @var \Magento\Bundle\Api\Data\LinkInterface $bundleProductLinks */
|
||||
$bundleProductLinks = $option->getProductLinks();
|
||||
$bundleProductLink = $bundleProductLinks[0];
|
||||
$childProductSku = $bundleProductLink->getSku();
|
||||
$productRepository = ObjectManager::getInstance()->get(ProductRepositoryInterface::class);
|
||||
$childProduct = $productRepository->get($childProductSku);
|
||||
$this->assertCount(1, $options);
|
||||
$this->assertResponseFields(
|
||||
$actualResponse['items'][0],
|
||||
[
|
||||
'option_id' => $option->getOptionId(),
|
||||
'title' => $option->getTitle(),
|
||||
'required' =>(bool)$option->getRequired(),
|
||||
'type' => $option->getType(),
|
||||
'position' => $option->getPosition(),
|
||||
'sku' => $option->getSku()
|
||||
]
|
||||
);
|
||||
$this->assertResponseFields(
|
||||
$actualResponse['items'][0]['options'][0],
|
||||
[
|
||||
'id' => $bundleProductLink->getId(),
|
||||
'quantity' => (int)$bundleProductLink->getQty(),
|
||||
'position' => $bundleProductLink->getPosition(),
|
||||
'is_default' => (bool)$bundleProductLink->getIsDefault(),
|
||||
'price_type' => self::KEY_PRICE_TYPE_FIXED,
|
||||
'can_change_quantity' => $bundleProductLink->getCanChangeQuantity()
|
||||
]
|
||||
);
|
||||
$this->assertEquals(
|
||||
$childProduct->getName(),
|
||||
$actualResponse['items'][0]['options'][0]['label']
|
||||
);
|
||||
$this->assertResponseFields(
|
||||
$actualResponse['items'][0]['options'][0]['product'],
|
||||
[
|
||||
'id' => $childProduct->getId(),
|
||||
'name' => $childProduct->getName(),
|
||||
'type_id' => $childProduct->getTypeId(),
|
||||
'sku' => $childProduct->getSku()
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoApiDataFixture Magento/Bundle/_files/product_with_multiple_options_1.php
|
||||
*/
|
||||
public function testAndMaxMinPriceBundleProduct()
|
||||
{
|
||||
$productSku = 'bundle-product';
|
||||
$query
|
||||
= <<<QUERY
|
||||
{
|
||||
products(filter: {sku: {eq: "{$productSku}"}})
|
||||
{
|
||||
items{
|
||||
id
|
||||
type_id
|
||||
... on PhysicalProductInterface {
|
||||
weight
|
||||
}
|
||||
price {
|
||||
minimalPrice {
|
||||
amount {
|
||||
value
|
||||
currency
|
||||
}
|
||||
adjustments {
|
||||
amount {
|
||||
value
|
||||
currency
|
||||
}
|
||||
code
|
||||
description
|
||||
}
|
||||
}
|
||||
maximalPrice {
|
||||
amount {
|
||||
value
|
||||
currency
|
||||
}
|
||||
adjustments {
|
||||
amount {
|
||||
value
|
||||
currency
|
||||
}
|
||||
code
|
||||
description
|
||||
}
|
||||
}
|
||||
regularPrice {
|
||||
amount {
|
||||
value
|
||||
currency
|
||||
}
|
||||
adjustments {
|
||||
amount {
|
||||
value
|
||||
currency
|
||||
}
|
||||
code
|
||||
description
|
||||
}
|
||||
}
|
||||
}
|
||||
... on BundleProduct {
|
||||
dynamic_sku
|
||||
dynamic_price
|
||||
dynamic_weight
|
||||
price_view
|
||||
ship_bundle_items
|
||||
items {
|
||||
options {
|
||||
label
|
||||
product {
|
||||
id
|
||||
name
|
||||
sku
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
QUERY;
|
||||
$response = $this->graphQlQuery($query);
|
||||
|
||||
/** @var ProductRepositoryInterface $productRepository */
|
||||
$productRepository = ObjectManager::getInstance()->get(ProductRepositoryInterface::class);
|
||||
$bundleProduct = $productRepository->get($productSku, false, null, true);
|
||||
/** @var \Magento\Framework\Pricing\PriceInfo\Base $priceInfo */
|
||||
$priceInfo = $bundleProduct->getPriceInfo();
|
||||
$priceCode = \Magento\Catalog\Pricing\Price\FinalPrice::PRICE_CODE;
|
||||
$minimalPrice = $priceInfo->getPrice($priceCode)->getMinimalPrice()->getValue();
|
||||
$maximalPrice = $priceInfo->getPrice($priceCode)->getMaximalPrice()->getValue();
|
||||
$this->assertEquals(
|
||||
$minimalPrice,
|
||||
$response['products']['items'][0]['price']['minimalPrice']['amount']['value']
|
||||
);
|
||||
$this->assertEquals(
|
||||
$maximalPrice,
|
||||
$response['products']['items'][0]['price']['maximalPrice']['amount']['value']
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoApiDataFixture Magento/Bundle/_files/product_1.php
|
||||
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
|
||||
*/
|
||||
public function testNonExistentFieldQtyExceptionOnBundleProduct()
|
||||
{
|
||||
$productSku = 'bundle-product';
|
||||
$query
|
||||
= <<<QUERY
|
||||
{
|
||||
products(filter: {sku: {eq: "{$productSku}"}})
|
||||
{
|
||||
items{
|
||||
id
|
||||
type_id
|
||||
qty
|
||||
... on PhysicalProductInterface {
|
||||
weight
|
||||
}
|
||||
|
||||
... on BundleProduct {
|
||||
dynamic_sku
|
||||
dynamic_price
|
||||
dynamic_weight
|
||||
price_view
|
||||
ship_bundle_items
|
||||
bundle_product_links {
|
||||
id
|
||||
name
|
||||
sku
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
QUERY;
|
||||
|
||||
$this->expectException(\Exception::class);
|
||||
$this->expectExceptionMessage(
|
||||
'GraphQL response contains errors: Cannot query field "qty" on type "ProductInterface".'
|
||||
);
|
||||
$this->graphQlQuery($query);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoApiDataFixture Magento/Bundle/_files/product_1.php
|
||||
*/
|
||||
public function testBundleProductWithDisabledProductOption()
|
||||
{
|
||||
/** @var StoreManagerInterface $storeManager */
|
||||
$storeManager = ObjectManager::getInstance()->get(StoreManagerInterface::class);
|
||||
$storeIdDefault = $storeManager->getDefaultStoreView()->getId();
|
||||
/** @var ProductRepositoryInterface $productRepository */
|
||||
$productRepository = ObjectManager::getInstance()->get(ProductRepositoryInterface::class);
|
||||
$simpleProduct = $productRepository->get('simple', false, $storeIdDefault, true);
|
||||
$simpleProduct->setStatus(ProductStatus::STATUS_DISABLED);
|
||||
$simpleProduct->setStoreIds([$storeIdDefault]);
|
||||
$productRepository->save($simpleProduct);
|
||||
|
||||
$productSku = 'bundle-product';
|
||||
$query
|
||||
= <<<QUERY
|
||||
{
|
||||
products(filter: {sku: {eq: "{$productSku}"}})
|
||||
{
|
||||
items{
|
||||
sku
|
||||
type_id
|
||||
id
|
||||
name
|
||||
... on PhysicalProductInterface {
|
||||
weight
|
||||
}
|
||||
... on BundleProduct {
|
||||
dynamic_sku
|
||||
dynamic_price
|
||||
dynamic_weight
|
||||
price_view
|
||||
ship_bundle_items
|
||||
items {
|
||||
option_id
|
||||
title
|
||||
required
|
||||
type
|
||||
position
|
||||
sku
|
||||
options {
|
||||
id
|
||||
quantity
|
||||
position
|
||||
is_default
|
||||
price
|
||||
price_type
|
||||
can_change_quantity
|
||||
label
|
||||
product {
|
||||
id
|
||||
name
|
||||
sku
|
||||
type_id
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
QUERY;
|
||||
|
||||
$response = $this->graphQlQuery($query);
|
||||
$this->assertEmpty($response['products']['items']);
|
||||
}
|
||||
|
||||
#[
|
||||
DataFixture(ProductFixture::class, ['price' => 10], 'p1'),
|
||||
DataFixture(ProductFixture::class, ['price' => 20], 'p2'),
|
||||
DataFixture(BundleOptionFixture::class, ['product_links' => ['$p1$']], 'opt1'),
|
||||
DataFixture(BundleOptionFixture::class, ['product_links' => ['$p2$']], 'opt2'),
|
||||
DataFixture(
|
||||
BundleProductFixture::class,
|
||||
['id' => 3, 'sku' => '4bundle-product','_options' => ['$opt1$', '$opt2$']],
|
||||
'4bundle-product'
|
||||
),
|
||||
DataFixture(
|
||||
BundleProductFixture::class,
|
||||
['id' => 4, 'sku' => '5bundle-product','_options' => ['$opt1$', '$opt2$']],
|
||||
'5bundle-product'
|
||||
),
|
||||
]
|
||||
public function testBundleProductHavingSKUAsNextBundleProductId()
|
||||
{
|
||||
|
||||
$productSku = '4bundle-product';
|
||||
$query
|
||||
= <<<QUERY
|
||||
{
|
||||
products(filter: {sku: {eq: "{$productSku}"}})
|
||||
{
|
||||
items{
|
||||
id
|
||||
type_id
|
||||
name
|
||||
sku
|
||||
... on BundleProduct {
|
||||
items {
|
||||
option_id
|
||||
title
|
||||
required
|
||||
type
|
||||
position
|
||||
sku
|
||||
options {
|
||||
id
|
||||
quantity
|
||||
position
|
||||
is_default
|
||||
price
|
||||
price_type
|
||||
can_change_quantity
|
||||
label
|
||||
product {
|
||||
id
|
||||
name
|
||||
sku
|
||||
type_id
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
QUERY;
|
||||
|
||||
$response = $this->graphQlQuery($query);
|
||||
$this->assertNotEmpty($response['products']['items']);
|
||||
}
|
||||
}
|
||||
+156
@@ -0,0 +1,156 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\GraphQl\Bundle;
|
||||
|
||||
use Exception;
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
use Magento\TestFramework\Helper\CompareArraysRecursively;
|
||||
use Magento\TestFramework\TestCase\GraphQlAbstract;
|
||||
|
||||
/**
|
||||
* class BundleProductWithDisabledProductTest
|
||||
*
|
||||
* Test Bundle product with disabled product and verify graphQl response
|
||||
*/
|
||||
class BundleProductWithDisabledProductTest extends GraphQlAbstract
|
||||
{
|
||||
/**
|
||||
* @var CompareArraysRecursively
|
||||
*/
|
||||
private $compareArraysRecursively;
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
$objectManager = Bootstrap::getObjectManager();
|
||||
$this->compareArraysRecursively = $objectManager->create(CompareArraysRecursively::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test Bundle product with disabled product test.
|
||||
*
|
||||
* @magentoApiDataFixture Magento/Bundle/_files/bundle_product_with_disabled_product_options.php
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
public function testBundleProductWithMultipleOptionsWithDisabledProduct(): void
|
||||
{
|
||||
$categorySku = 'c1';
|
||||
$query
|
||||
= <<<QUERY
|
||||
{
|
||||
categoryList(filters: {url_path: {eq: "{$categorySku}"}}) {
|
||||
children_count
|
||||
id
|
||||
url_path
|
||||
url_key
|
||||
id
|
||||
products {
|
||||
total_count
|
||||
items {
|
||||
... on BundleProduct {
|
||||
id
|
||||
categories {
|
||||
id
|
||||
name
|
||||
description
|
||||
}
|
||||
dynamic_price
|
||||
price_range {
|
||||
minimum_price {
|
||||
regular_price {
|
||||
value
|
||||
currency
|
||||
}
|
||||
}
|
||||
maximum_price {
|
||||
regular_price {
|
||||
value
|
||||
currency
|
||||
}
|
||||
}
|
||||
}
|
||||
sku
|
||||
name
|
||||
short_description {
|
||||
html
|
||||
}
|
||||
description {
|
||||
html
|
||||
}
|
||||
stock_status
|
||||
__typename
|
||||
url_key
|
||||
items {
|
||||
position
|
||||
uid
|
||||
option_id
|
||||
options {
|
||||
uid
|
||||
label
|
||||
id
|
||||
price
|
||||
quantity
|
||||
product {
|
||||
... on VirtualProduct {
|
||||
sku
|
||||
stock_status
|
||||
name
|
||||
}
|
||||
price_range {
|
||||
minimum_price {
|
||||
regular_price {
|
||||
value
|
||||
currency
|
||||
}
|
||||
}
|
||||
maximum_price {
|
||||
regular_price {
|
||||
value
|
||||
currency
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
QUERY;
|
||||
|
||||
$response = $this->graphQlQuery($query);
|
||||
$this->assertBundleProduct($response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert bundle product response.
|
||||
*
|
||||
* @param array $response
|
||||
*/
|
||||
private function assertBundleProduct(array $response): void
|
||||
{
|
||||
$this->assertNotEmpty(
|
||||
$response['categoryList'][0]['products']['items'],
|
||||
'Precondition failed: "items" must not be empty'
|
||||
);
|
||||
$productItems = end($response['categoryList'][0]['products']['items'])['items'];
|
||||
$this->assertEquals(3, count($productItems[0]['options']));
|
||||
$this->assertEquals('virtual1', $productItems[0]['options'][0]['product']['sku']);
|
||||
$this->assertEquals('virtual2', $productItems[0]['options'][1]['product']['sku']);
|
||||
$this->assertEquals('virtual3', $productItems[0]['options'][2]['product']['sku']);
|
||||
$this->assertEquals(3, count($productItems[1]['options']));
|
||||
$this->assertEquals('virtual1', $productItems[1]['options'][0]['product']['sku']);
|
||||
$this->assertEquals('virtual2', $productItems[1]['options'][1]['product']['sku']);
|
||||
$this->assertEquals('virtual3', $productItems[1]['options'][2]['product']['sku']);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\GraphQl\Bundle;
|
||||
|
||||
use Magento\Bundle\Test\Fixture\Link as BundleSelectionFixture;
|
||||
use Magento\Bundle\Test\Fixture\Option as BundleOptionFixture;
|
||||
use Magento\Bundle\Test\Fixture\Product as BundleProductFixture;
|
||||
use Magento\Catalog\Test\Fixture\Category as CategoryFixture;
|
||||
use Magento\Catalog\Test\Fixture\Product as ProductFixture;
|
||||
use Magento\CatalogInventory\Model\Configuration as CatalogInventoryConfiguration;
|
||||
use Magento\TestFramework\Fixture\Config as ConfigFixture;
|
||||
use Magento\TestFramework\Fixture\DataFixture;
|
||||
use Magento\TestFramework\TestCase\GraphQlAbstract;
|
||||
|
||||
class CategoryListTest extends GraphQlAbstract
|
||||
{
|
||||
#[
|
||||
ConfigFixture(CatalogInventoryConfiguration::XML_PATH_SHOW_OUT_OF_STOCK, '1', 'store', 'default'),
|
||||
DataFixture(CategoryFixture::class, ['url_path' => 'cat1'], 'cat1'),
|
||||
DataFixture(ProductFixture::class, ['sku' => 's1', 'stock_item' => ['is_in_stock' => false]], 's1'),
|
||||
DataFixture(ProductFixture::class, ['sku' => 's2'], 's2'),
|
||||
DataFixture(BundleSelectionFixture::class, ['sku' => '$s1.sku$', 'price' => 10, 'price_type' => 0], 'link1'),
|
||||
DataFixture(BundleSelectionFixture::class, ['sku' => '$s2.sku$', 'price' => 20, 'price_type' => 0], 'link2'),
|
||||
DataFixture(BundleOptionFixture::class, ['product_links' => ['$link1$', '$link2$']], 'opt1'),
|
||||
DataFixture(
|
||||
BundleProductFixture::class,
|
||||
['sku' => 'bundle1', 'category_ids' => ['$cat1.id$'], '_options' => ['$opt1$']],
|
||||
'bundle1'
|
||||
),
|
||||
]
|
||||
public function testOutOfStockBundleSelectionWithEnabledShowOutOfStock(): void
|
||||
{
|
||||
$query = $this->getQuery('cat1');
|
||||
$response = $this->graphQlQuery($query);
|
||||
self::assertNotEmpty($response['categoryList']);
|
||||
$categoryList = $response['categoryList'][0];
|
||||
self::assertNotEmpty($categoryList['products']['items']);
|
||||
$bundle = $categoryList['products']['items'][0];
|
||||
self::assertEquals('bundle1', $bundle['sku']);
|
||||
self::assertCount(2, $bundle['items'][0]['options']);
|
||||
self::assertEquals('s1', $bundle['items'][0]['options'][0]['product']['sku']);
|
||||
self::assertEquals('s2', $bundle['items'][0]['options'][1]['product']['sku']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $urlPath
|
||||
* @return string
|
||||
*/
|
||||
private function getQuery(string $urlPath): string
|
||||
{
|
||||
$query = <<<QUERY
|
||||
{
|
||||
categoryList(filters: {url_path: {eq: "$urlPath"}}) {
|
||||
id
|
||||
name
|
||||
products {
|
||||
total_count
|
||||
items {
|
||||
sku
|
||||
... on BundleProduct {
|
||||
items {
|
||||
options {
|
||||
product {
|
||||
sku
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
QUERY;
|
||||
|
||||
return $query;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user