BundleProductViewTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\GraphQl\Bundle;
  8. use Magento\Bundle\Model\Product\OptionList;
  9. use Magento\Catalog\Api\Data\ProductInterface;
  10. use Magento\Catalog\Api\ProductRepositoryInterface;
  11. use Magento\Framework\EntityManager\MetadataPool;
  12. use Magento\Framework\App\Config\ScopeConfigInterface;
  13. use Magento\TestFramework\ObjectManager;
  14. use Magento\TestFramework\TestCase\GraphQlAbstract;
  15. class BundleProductViewTest extends GraphQlAbstract
  16. {
  17. const KEY_PRICE_TYPE_FIXED = 'FIXED';
  18. const KEY_PRICE_TYPE_DYNAMIC = 'DYNAMIC';
  19. /**
  20. * @magentoApiDataFixture Magento/Bundle/_files/product_1.php
  21. */
  22. public function testAllFieldsBundleProducts()
  23. {
  24. $productSku = 'bundle-product';
  25. $query
  26. = <<<QUERY
  27. {
  28. products(filter: {sku: {eq: "{$productSku}"}})
  29. {
  30. items{
  31. sku
  32. type_id
  33. id
  34. name
  35. attribute_set_id
  36. ... on PhysicalProductInterface {
  37. weight
  38. }
  39. ... on BundleProduct {
  40. dynamic_sku
  41. dynamic_price
  42. dynamic_weight
  43. price_view
  44. ship_bundle_items
  45. items {
  46. option_id
  47. title
  48. required
  49. type
  50. position
  51. sku
  52. options {
  53. id
  54. qty
  55. position
  56. is_default
  57. price
  58. price_type
  59. can_change_quantity
  60. label
  61. product {
  62. id
  63. name
  64. sku
  65. type_id
  66. }
  67. }
  68. }
  69. }
  70. }
  71. }
  72. }
  73. QUERY;
  74. $response = $this->graphQlQuery($query);
  75. /** @var ProductRepositoryInterface $productRepository */
  76. $productRepository = ObjectManager::getInstance()->get(ProductRepositoryInterface::class);
  77. /** @var MetadataPool $metadataPool */
  78. $metadataPool = ObjectManager::getInstance()->get(MetadataPool::class);
  79. $bundleProduct = $productRepository->get($productSku, false, null, true);
  80. $bundleProduct->setId(
  81. $bundleProduct->getData($metadataPool->getMetadata(ProductInterface::class)->getLinkField())
  82. );
  83. if ((bool)$bundleProduct->getShipmentType()) {
  84. $this->assertEquals('SEPARATELY', $response['products']['items'][0]['ship_bundle_items']);
  85. } else {
  86. $this->assertEquals('TOGETHER', $response['products']['items'][0]['ship_bundle_items']);
  87. }
  88. if ((bool)$bundleProduct->getPriceView()) {
  89. $this->assertEquals('AS_LOW_AS', $response['products']['items'][0]['price_view']);
  90. } else {
  91. $this->assertEquals('PRICE_RANGE', $response['products']['items'][0]['price_view']);
  92. }
  93. $this->assertBundleBaseFields($bundleProduct, $response['products']['items'][0]);
  94. $this->assertBundleProductOptions($bundleProduct, $response['products']['items'][0]);
  95. $this->assertNotEmpty(
  96. $response['products']['items'][0]['items'],
  97. "Precondition failed: 'items' must not be empty"
  98. );
  99. }
  100. /**
  101. * @magentoApiDataFixture Magento/Bundle/_files/bundle_product_with_not_visible_children.php
  102. */
  103. public function testBundleProdutWithNotVisibleChildren()
  104. {
  105. $productSku = 'bundle-product-1';
  106. $query
  107. = <<<QUERY
  108. {
  109. products(filter: {sku: {eq: "{$productSku}"}})
  110. {
  111. items{
  112. sku
  113. type_id
  114. id
  115. name
  116. attribute_set_id
  117. ... on PhysicalProductInterface {
  118. weight
  119. }
  120. ... on BundleProduct {
  121. dynamic_sku
  122. dynamic_price
  123. dynamic_weight
  124. price_view
  125. ship_bundle_items
  126. items {
  127. option_id
  128. title
  129. required
  130. type
  131. position
  132. sku
  133. options {
  134. id
  135. qty
  136. position
  137. is_default
  138. price
  139. price_type
  140. can_change_quantity
  141. label
  142. product {
  143. id
  144. name
  145. sku
  146. type_id
  147. }
  148. }
  149. }
  150. }
  151. }
  152. }
  153. }
  154. QUERY;
  155. /** @var \Magento\Config\Model\ResourceModel\Config $config */
  156. $config = ObjectManager::getInstance()->get(\Magento\Config\Model\ResourceModel\Config::class);
  157. $config->saveConfig(
  158. \Magento\CatalogInventory\Model\Configuration::XML_PATH_SHOW_OUT_OF_STOCK,
  159. 0,
  160. ScopeConfigInterface::SCOPE_TYPE_DEFAULT,
  161. 0
  162. );
  163. ObjectManager::getInstance()->get(\Magento\Framework\App\Cache::class)
  164. ->clean(\Magento\Framework\App\Config::CACHE_TAG);
  165. $response = $this->graphQlQuery($query);
  166. $this->assertNotEmpty(
  167. $response['products']['items'],
  168. "Precondition failed: 'items' must not be empty"
  169. );
  170. /** @var ProductRepositoryInterface $productRepository */
  171. $productRepository = ObjectManager::getInstance()->get(ProductRepositoryInterface::class);
  172. /** @var MetadataPool $metadataPool */
  173. $metadataPool = ObjectManager::getInstance()->get(MetadataPool::class);
  174. $bundleProduct = $productRepository->get($productSku, false, null, true);
  175. $bundleProduct->setId(
  176. $bundleProduct->getData($metadataPool->getMetadata(ProductInterface::class)->getLinkField())
  177. );
  178. if ((bool)$bundleProduct->getShipmentType()) {
  179. $this->assertEquals('SEPARATELY', $response['products']['items'][0]['ship_bundle_items']);
  180. } else {
  181. $this->assertEquals('TOGETHER', $response['products']['items'][0]['ship_bundle_items']);
  182. }
  183. if ((bool)$bundleProduct->getPriceView()) {
  184. $this->assertEquals('AS_LOW_AS', $response['products']['items'][0]['price_view']);
  185. } else {
  186. $this->assertEquals('PRICE_RANGE', $response['products']['items'][0]['price_view']);
  187. }
  188. $this->assertBundleBaseFields($bundleProduct, $response['products']['items'][0]);
  189. $this->assertBundleProductOptions($bundleProduct, $response['products']['items'][0]);
  190. $this->assertNotEmpty(
  191. $response['products']['items'][0]['items'],
  192. "Precondition failed: 'items' must not be empty"
  193. );
  194. }
  195. /**
  196. * @param ProductInterface $product
  197. * @param array $actualResponse
  198. */
  199. private function assertBundleBaseFields($product, $actualResponse)
  200. {
  201. $assertionMap = [
  202. ['response_field' => 'sku', 'expected_value' => $product->getSku()],
  203. ['response_field' => 'type_id', 'expected_value' => $product->getTypeId()],
  204. ['response_field' => 'id', 'expected_value' => $product->getId()],
  205. ['response_field' => 'name', 'expected_value' => $product->getName()],
  206. ['response_field' => 'attribute_set_id', 'expected_value' => $product->getAttributeSetId()],
  207. ['response_field' => 'weight', 'expected_value' => $product->getWeight()],
  208. ['response_field' => 'dynamic_price', 'expected_value' => !(bool)$product->getPriceType()],
  209. ['response_field' => 'dynamic_weight', 'expected_value' => !(bool)$product->getWeightType()],
  210. ['response_field' => 'dynamic_sku', 'expected_value' => !(bool)$product->getSkuType()]
  211. ];
  212. $this->assertResponseFields($actualResponse, $assertionMap);
  213. }
  214. /**
  215. * @param ProductInterface $product
  216. * @param array $actualResponse
  217. */
  218. private function assertBundleProductOptions($product, $actualResponse)
  219. {
  220. $this->assertNotEmpty(
  221. $actualResponse['items'],
  222. "Precondition failed: 'bundle product items' must not be empty"
  223. );
  224. $metadataPool = ObjectManager::getInstance()->get(MetadataPool::class);
  225. /** @var OptionList $optionList */
  226. $optionList = ObjectManager::getInstance()->get(\Magento\Bundle\Model\Product\OptionList::class);
  227. $options = $optionList->getItems($product);
  228. $option = $options[0];
  229. /** @var \Magento\Bundle\Api\Data\LinkInterface $bundleProductLinks */
  230. $bundleProductLinks = $option->getProductLinks();
  231. $bundleProductLink = $bundleProductLinks[0];
  232. $childProductSku = $bundleProductLink->getSku();
  233. $productRepository = ObjectManager::getInstance()->get(ProductRepositoryInterface::class);
  234. $childProduct = $productRepository->get($childProductSku);
  235. /** @var MetadataPool $metadataPool */
  236. $childProduct->setId(
  237. $childProduct->getData($metadataPool->getMetadata(ProductInterface::class)->getLinkField())
  238. );
  239. $this->assertEquals(1, count($options));
  240. $this->assertResponseFields(
  241. $actualResponse['items'][0],
  242. [
  243. 'option_id' => $option->getOptionId(),
  244. 'title' => $option->getTitle(),
  245. 'required' =>(bool)$option->getRequired(),
  246. 'type' => $option->getType(),
  247. 'position' => $option->getPosition(),
  248. 'sku' => $option->getSku()
  249. ]
  250. );
  251. $this->assertResponseFields(
  252. $actualResponse['items'][0]['options'][0],
  253. [
  254. 'id' => $bundleProductLink->getId(),
  255. 'qty' => (int)$bundleProductLink->getQty(),
  256. 'position' => $bundleProductLink->getPosition(),
  257. 'is_default' => (bool)$bundleProductLink->getIsDefault(),
  258. 'price_type' => self::KEY_PRICE_TYPE_FIXED,
  259. 'can_change_quantity' => $bundleProductLink->getCanChangeQuantity(),
  260. 'label' => $childProduct->getName()
  261. ]
  262. );
  263. $this->assertResponseFields(
  264. $actualResponse['items'][0]['options'][0]['product'],
  265. [
  266. 'id' => $childProduct->getId(),
  267. 'name' => $childProduct->getName(),
  268. 'type_id' => $childProduct->getTypeId(),
  269. 'sku' => $bundleProductLink->getSku()
  270. ]
  271. );
  272. }
  273. /**
  274. * @magentoApiDataFixture Magento/Bundle/_files/product_with_multiple_options_1.php
  275. */
  276. public function testAndMaxMinPriceBundleProduct()
  277. {
  278. $productSku = 'bundle-product';
  279. $query
  280. = <<<QUERY
  281. {
  282. products(filter: {sku: {eq: "{$productSku}"}})
  283. {
  284. items{
  285. id
  286. type_id
  287. ... on PhysicalProductInterface {
  288. weight
  289. }
  290. price {
  291. minimalPrice {
  292. amount {
  293. value
  294. currency
  295. }
  296. adjustments {
  297. amount {
  298. value
  299. currency
  300. }
  301. code
  302. description
  303. }
  304. }
  305. maximalPrice {
  306. amount {
  307. value
  308. currency
  309. }
  310. adjustments {
  311. amount {
  312. value
  313. currency
  314. }
  315. code
  316. description
  317. }
  318. }
  319. regularPrice {
  320. amount {
  321. value
  322. currency
  323. }
  324. adjustments {
  325. amount {
  326. value
  327. currency
  328. }
  329. code
  330. description
  331. }
  332. }
  333. }
  334. ... on BundleProduct {
  335. dynamic_sku
  336. dynamic_price
  337. dynamic_weight
  338. price_view
  339. ship_bundle_items
  340. items {
  341. options {
  342. label
  343. product {
  344. id
  345. name
  346. sku
  347. }
  348. }
  349. }
  350. }
  351. }
  352. }
  353. }
  354. QUERY;
  355. $response = $this->graphQlQuery($query);
  356. /** @var ProductRepositoryInterface $productRepository */
  357. $productRepository = ObjectManager::getInstance()->get(ProductRepositoryInterface::class);
  358. $bundleProduct = $productRepository->get($productSku, false, null, true);
  359. /** @var \Magento\Framework\Pricing\PriceInfo\Base $priceInfo */
  360. $priceInfo = $bundleProduct->getPriceInfo();
  361. $priceCode = \Magento\Catalog\Pricing\Price\FinalPrice::PRICE_CODE;
  362. $minimalPrice = $priceInfo->getPrice($priceCode)->getMinimalPrice()->getValue();
  363. $maximalPrice = $priceInfo->getPrice($priceCode)->getMaximalPrice()->getValue();
  364. $this->assertEquals(
  365. $minimalPrice,
  366. $response['products']['items'][0]['price']['minimalPrice']['amount']['value']
  367. );
  368. $this->assertEquals(
  369. $maximalPrice,
  370. $response['products']['items'][0]['price']['maximalPrice']['amount']['value']
  371. );
  372. }
  373. /**
  374. * @magentoApiDataFixture Magento/Bundle/_files/product_1.php
  375. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  376. */
  377. public function testNonExistentFieldQtyExceptionOnBundleProduct()
  378. {
  379. $productSku = 'bundle-product';
  380. $query
  381. = <<<QUERY
  382. {
  383. products(filter: {sku: {eq: "{$productSku}"}})
  384. {
  385. items{
  386. id
  387. type_id
  388. qty
  389. ... on PhysicalProductInterface {
  390. weight
  391. }
  392. ... on BundleProduct {
  393. dynamic_sku
  394. dynamic_price
  395. dynamic_weight
  396. price_view
  397. ship_bundle_items
  398. bundle_product_links {
  399. id
  400. name
  401. sku
  402. }
  403. }
  404. }
  405. }
  406. }
  407. QUERY;
  408. $this->expectException(\Exception::class);
  409. $this->expectExceptionMessage('GraphQL response contains errors: Cannot'. ' ' .
  410. 'query field "qty" on type "ProductInterface".');
  411. $this->graphQlQuery($query);
  412. }
  413. }