123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432 |
- <?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\Catalog\Api\Data\ProductInterface;
- use Magento\Catalog\Api\ProductRepositoryInterface;
- use Magento\Framework\EntityManager\MetadataPool;
- use Magento\Framework\App\Config\ScopeConfigInterface;
- use Magento\TestFramework\ObjectManager;
- use Magento\TestFramework\TestCase\GraphQlAbstract;
- class BundleProductViewTest extends GraphQlAbstract
- {
- const KEY_PRICE_TYPE_FIXED = 'FIXED';
- 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
- attribute_set_id
- ... 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
- qty
- 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);
- /** @var MetadataPool $metadataPool */
- $metadataPool = ObjectManager::getInstance()->get(MetadataPool::class);
- $bundleProduct = $productRepository->get($productSku, false, null, true);
- $bundleProduct->setId(
- $bundleProduct->getData($metadataPool->getMetadata(ProductInterface::class)->getLinkField())
- );
- 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"
- );
- }
- /**
- * @magentoApiDataFixture Magento/Bundle/_files/bundle_product_with_not_visible_children.php
- */
- public function testBundleProdutWithNotVisibleChildren()
- {
- $productSku = 'bundle-product-1';
- $query
- = <<<QUERY
- {
- products(filter: {sku: {eq: "{$productSku}"}})
- {
- items{
- sku
- type_id
- id
- name
- attribute_set_id
- ... 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
- qty
- 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);
- /** @var MetadataPool $metadataPool */
- $metadataPool = ObjectManager::getInstance()->get(MetadataPool::class);
- $bundleProduct = $productRepository->get($productSku, false, null, true);
- $bundleProduct->setId(
- $bundleProduct->getData($metadataPool->getMetadata(ProductInterface::class)->getLinkField())
- );
- 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' => 'attribute_set_id', 'expected_value' => $product->getAttributeSetId()],
- ['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"
- );
- $metadataPool = ObjectManager::getInstance()->get(MetadataPool::class);
- /** @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);
- /** @var MetadataPool $metadataPool */
- $childProduct->setId(
- $childProduct->getData($metadataPool->getMetadata(ProductInterface::class)->getLinkField())
- );
- $this->assertEquals(1, count($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(),
- 'qty' => (int)$bundleProductLink->getQty(),
- 'position' => $bundleProductLink->getPosition(),
- 'is_default' => (bool)$bundleProductLink->getIsDefault(),
- 'price_type' => self::KEY_PRICE_TYPE_FIXED,
- 'can_change_quantity' => $bundleProductLink->getCanChangeQuantity(),
- 'label' => $childProduct->getName()
- ]
- );
- $this->assertResponseFields(
- $actualResponse['items'][0]['options'][0]['product'],
- [
- 'id' => $childProduct->getId(),
- 'name' => $childProduct->getName(),
- 'type_id' => $childProduct->getTypeId(),
- 'sku' => $bundleProductLink->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);
- }
- }
|