magento2-docker/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/PlaceOrderTest.php

429 lines
20 KiB
PHP
Executable File

<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\GraphQl\Quote\Guest;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Registry;
use Magento\GraphQl\Quote\GetMaskedQuoteIdByReservedOrderId;
use Magento\Sales\Api\OrderRepositoryInterface;
use Magento\Sales\Model\OrderFactory;
use Magento\Sales\Model\ResourceModel\Order\CollectionFactory;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\TestCase\GraphQlAbstract;
/**
* Test for placing an order for guest
*/
class PlaceOrderTest extends GraphQlAbstract
{
/**
* @var GetMaskedQuoteIdByReservedOrderId
*/
private $getMaskedQuoteIdByReservedOrderId;
/**
* @var CollectionFactory
*/
private $orderCollectionFactory;
/**
* @var OrderRepositoryInterface
*/
private $orderRepository;
/**
* @var Registry
*/
private $registry;
/**
* @var OrderFactory
*/
private $orderFactory;
/**
* @inheritdoc
*/
protected function setUp(): void
{
$objectManager = Bootstrap::getObjectManager();
$this->getMaskedQuoteIdByReservedOrderId = $objectManager->get(GetMaskedQuoteIdByReservedOrderId::class);
$this->orderCollectionFactory = $objectManager->get(CollectionFactory::class);
$this->orderRepository = $objectManager->get(OrderRepositoryInterface::class);
$this->orderFactory = $objectManager->get(OrderFactory::class);
$this->registry = $objectManager->get(Registry::class);
/** @var ScopeConfigInterface $scopeConfig */
$scopeConfig = $objectManager->get(ScopeConfigInterface::class);
$scopeConfig->clean();
}
/**
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoConfigFixture default_store carriers/flatrate/active 1
* @magentoConfigFixture default_store carriers/tablerate/active 1
* @magentoConfigFixture default_store carriers/freeshipping/active 1
* @magentoConfigFixture default_store payment/banktransfer/active 1
* @magentoConfigFixture default_store payment/cashondelivery/active 1
* @magentoConfigFixture default_store payment/checkmo/active 1
* @magentoConfigFixture default_store payment/purchaseorder/active 1
* @magentoConfigFixture default_store customer/create_account/auto_group_assign 0
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/set_guest_email.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_billing_address.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_flatrate_shipping_method.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_checkmo_payment_method.php
*/
public function testPlaceOrder()
{
$reservedOrderId = 'test_quote';
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute($reservedOrderId);
$query = $this->getQuery($maskedQuoteId);
$response = $this->graphQlMutation($query);
self::assertArrayHasKey('placeOrder', $response);
self::assertArrayHasKey('order_number', $response['placeOrder']['order']);
self::assertEquals($reservedOrderId, $response['placeOrder']['order']['order_number']);
$orderIncrementId = $response['placeOrder']['order']['order_number'];
$order = $this->orderFactory->create();
$order->loadByIncrementId($orderIncrementId);
$this->assertNotEmpty($order->getEmailSent());
}
/**
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoConfigFixture default_store carriers/flatrate/active 1
* @magentoConfigFixture default_store carriers/tablerate/active 1
* @magentoConfigFixture default_store carriers/freeshipping/active 1
* @magentoConfigFixture default_store payment/banktransfer/active 1
* @magentoConfigFixture default_store payment/cashondelivery/active 1
* @magentoConfigFixture default_store payment/checkmo/active 1
* @magentoConfigFixture default_store payment/purchaseorder/active 1
* @magentoConfigFixture default_store customer/create_account/auto_group_assign 1
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/set_guest_email.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_billing_address.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_flatrate_shipping_method.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_checkmo_payment_method.php
*/
public function testPlaceOrderWithAutoGroup()
{
$reservedOrderId = 'test_quote';
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute($reservedOrderId);
$query = $this->getQuery($maskedQuoteId);
$response = $this->graphQlMutation($query);
self::assertArrayHasKey('placeOrder', $response);
self::assertArrayHasKey('order_number', $response['placeOrder']['order']);
self::assertEquals($reservedOrderId, $response['placeOrder']['order']['order_number']);
$orderIncrementId = $response['placeOrder']['order']['order_number'];
$order = $this->orderFactory->create();
$order->loadByIncrementId($orderIncrementId);
$this->assertNotEmpty($order->getEmailSent());
}
/**
* @magentoConfigFixture default_store customer/create_account/auto_group_assign 0
*/
public function testPlaceOrderIfCartIdIsEmpty()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Required parameter "cart_id" is missing');
$maskedQuoteId = '';
$query = $this->getQuery($maskedQuoteId);
$this->graphQlMutation($query);
}
/**
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoConfigFixture default_store carriers/flatrate/active 1
* @magentoConfigFixture default_store carriers/tablerate/active 1
* @magentoConfigFixture default_store carriers/freeshipping/active 1
* @magentoConfigFixture default_store payment/banktransfer/active 1
* @magentoConfigFixture default_store payment/cashondelivery/active 1
* @magentoConfigFixture default_store payment/checkmo/active 1
* @magentoConfigFixture default_store payment/purchaseorder/active 1
* @magentoConfigFixture default_store customer/create_account/auto_group_assign 0
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_billing_address.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_flatrate_shipping_method.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_checkmo_payment_method.php
*
*/
public function testPlaceOrderWithNoEmail()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Guest email for cart is missing.');
$reservedOrderId = 'test_quote';
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute($reservedOrderId);
$query = $this->getQuery($maskedQuoteId);
$this->graphQlMutation($query);
}
/**
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/set_guest_email.php
*/
public function testPlaceOrderWithNoItemsInCart()
{
$reservedOrderId = 'test_quote';
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute($reservedOrderId);
$query = $this->getQuery($maskedQuoteId);
self::expectExceptionMessage(
'Unable to place order: A server error stopped your order from being placed. ' .
'Please try to place your order again'
);
$this->graphQlMutation($query);
}
/**
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/set_guest_email.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
*/
public function testPlaceOrderWithNoShippingAddress()
{
$reservedOrderId = 'test_quote';
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute($reservedOrderId);
$query = $this->getQuery($maskedQuoteId);
self::expectExceptionMessage(
'Unable to place order: Some addresses can\'t be used due to the configurations for specific countries'
);
$this->graphQlMutation($query);
}
/**
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/set_guest_email.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
*/
public function testPlaceOrderWithNoShippingMethod()
{
$reservedOrderId = 'test_quote';
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute($reservedOrderId);
$query = $this->getQuery($maskedQuoteId);
self::expectExceptionMessage(
'Unable to place order: The shipping method is missing. Select the shipping method and try again'
);
$this->graphQlMutation($query);
}
/**
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoConfigFixture default_store carriers/flatrate/active 1
* @magentoConfigFixture default_store carriers/tablerate/active 1
* @magentoConfigFixture default_store carriers/freeshipping/active 1
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/set_guest_email.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_flatrate_shipping_method.php
*/
public function testPlaceOrderWithNoBillingAddress()
{
$reservedOrderId = 'test_quote';
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute($reservedOrderId);
$query = $this->getQuery($maskedQuoteId);
self::expectExceptionMessageMatches(
'/Unable to place order: Please check the billing address information*/'
);
$this->graphQlMutation($query);
}
/**
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoConfigFixture default_store carriers/flatrate/active 1
* @magentoConfigFixture default_store carriers/tablerate/active 1
* @magentoConfigFixture default_store carriers/freeshipping/active 1
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/set_guest_email.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_billing_address.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_flatrate_shipping_method.php
*/
public function testPlaceOrderWithNoPaymentMethod()
{
$reservedOrderId = 'test_quote';
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute($reservedOrderId);
$query = $this->getQuery($maskedQuoteId);
self::expectExceptionMessage('Unable to place order: Enter a valid payment method and try again');
$this->graphQlMutation($query);
}
/**
* @magentoConfigFixture cataloginventory/options/enable_inventory_check 1
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoConfigFixture default_store carriers/flatrate/active 1
* @magentoConfigFixture default_store carriers/tablerate/active 1
* @magentoConfigFixture default_store carriers/freeshipping/active 1
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/set_guest_email.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_billing_address.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_flatrate_shipping_method.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/set_simple_product_out_of_stock.php
*/
public function testPlaceOrderWithOutOfStockProduct()
{
$reservedOrderId = 'test_quote';
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute($reservedOrderId);
$query = $this->getQuery($maskedQuoteId);
self::expectExceptionMessage('Unable to place order: Some of the products are out of stock');
$this->graphQlMutation($query);
}
/**
* @magentoConfigFixture cataloginventory/options/enable_inventory_check 0
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoConfigFixture default_store carriers/flatrate/active 1
* @magentoConfigFixture default_store carriers/tablerate/active 1
* @magentoConfigFixture default_store carriers/freeshipping/active 1
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/set_guest_email.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_billing_address.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_flatrate_shipping_method.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/set_simple_product_out_of_stock.php
*/
public function testPlaceOrderWithOutOfStockProductWithDisabledInventoryCheck()
{
$reservedOrderId = 'test_quote';
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute($reservedOrderId);
$query = $this->getQuery($maskedQuoteId);
self::expectExceptionMessage('Unable to place order: Enter a valid payment method and try again.');
$this->graphQlMutation($query);
}
/**
* _security
* @magentoApiDataFixture Magento/Customer/_files/three_customers.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoConfigFixture default_store carriers/flatrate/active 1
* @magentoConfigFixture default_store carriers/tablerate/active 1
* @magentoConfigFixture default_store carriers/freeshipping/active 1
* @magentoConfigFixture default_store payment/banktransfer/active 1
* @magentoConfigFixture default_store payment/cashondelivery/active 1
* @magentoConfigFixture default_store payment/checkmo/active 1
* @magentoConfigFixture default_store payment/purchaseorder/active 1
* @magentoConfigFixture default_store customer/create_account/auto_group_assign 0
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_billing_address.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_flatrate_shipping_method.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_checkmo_payment_method.php
*/
public function testPlaceOrderOfCustomerCart()
{
$reservedOrderId = 'test_quote';
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute($reservedOrderId);
$query = $this->getQuery($maskedQuoteId);
self::expectExceptionMessageMatches('/The current user cannot perform operations on cart*/');
$this->graphQlMutation($query);
}
/**
* Test place order with gift message options
*
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoConfigFixture default_store carriers/flatrate/active 1
* @magentoConfigFixture default_store carriers/tablerate/active 1
* @magentoConfigFixture default_store carriers/freeshipping/active 1
* @magentoConfigFixture default_store payment/banktransfer/active 1
* @magentoConfigFixture default_store payment/cashondelivery/active 1
* @magentoConfigFixture default_store payment/checkmo/active 1
* @magentoConfigFixture default_store payment/purchaseorder/active 1
* @magentoConfigFixture sales/gift_options/allow_order 1
* @magentoConfigFixture default_store customer/create_account/auto_group_assign 1
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/set_guest_email.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_gift_options.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_billing_address.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_flatrate_shipping_method.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_checkmo_payment_method.php
*/
public function testPlaceOrderWithGiftMessage()
{
$reservedOrderId = 'test_quote';
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute($reservedOrderId);
$query = $this->getQuery($maskedQuoteId);
$response = $this->graphQlMutation($query);
self::assertArrayHasKey('placeOrder', $response);
self::assertArrayHasKey('order_number', $response['placeOrder']['order']);
self::assertEquals($reservedOrderId, $response['placeOrder']['order']['order_number']);
$orderIncrementId = $response['placeOrder']['order']['order_number'];
$order = $this->orderFactory->create();
$order->loadByIncrementId($orderIncrementId);
$this->assertNotEmpty($order->getGiftMessageId());
}
/**
* @param string $maskedQuoteId
* @return string
*/
private function getQuery(string $maskedQuoteId): string
{
return <<<QUERY
mutation {
placeOrder(input: {cart_id: "{$maskedQuoteId}"}) {
order {
order_number
}
}
}
QUERY;
}
/**
* @inheritdoc
*/
protected function tearDown(): void
{
$this->registry->unregister('isSecureArea');
$this->registry->register('isSecureArea', true);
$orderCollection = $this->orderCollectionFactory->create();
foreach ($orderCollection as $order) {
$this->orderRepository->delete($order);
}
$this->registry->unregister('isSecureArea');
$this->registry->register('isSecureArea', false);
parent::tearDown();
}
}