GuestPaymentInformationManagementPluginTest.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Persistent\Model\Checkout;
  7. /**
  8. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  9. */
  10. class GuestPaymentInformationManagementPluginTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /**
  13. * @var \Magento\Persistent\Helper\Session
  14. */
  15. protected $persistentSessionHelper;
  16. /**
  17. * @var \Magento\Customer\Model\Session
  18. */
  19. protected $customerSession;
  20. /**
  21. * @var \Magento\Checkout\Model\Session
  22. */
  23. protected $checkoutSession;
  24. /**
  25. * @var \Magento\Quote\Api\CartRepositoryInterface
  26. */
  27. protected $cartRepository;
  28. /**
  29. * @var \Magento\Quote\Api\CartManagementInterface
  30. */
  31. protected $cartManagement;
  32. /**
  33. * @var \Magento\Customer\Api\CustomerRepositoryInterface
  34. */
  35. protected $customerFactory;
  36. /**
  37. * @var \Magento\Quote\Api\CartItemRepositoryInterface
  38. */
  39. protected $cartItemRepository;
  40. /**
  41. * @var \Magento\Quote\Model\QuoteIdMask
  42. */
  43. protected $quoteIdMaskFactory;
  44. /**
  45. * @var \Magento\Quote\Api\PaymentMethodManagementInterface
  46. */
  47. protected $paymentMethodManagement;
  48. /**
  49. * @var \Magento\Quote\Api\BillingAddressManagementInterface
  50. */
  51. protected $billingAddressManagement;
  52. /**
  53. * @var \Magento\Quote\Model\ShippingAddressManagementInterface
  54. */
  55. protected $shippingAddressManagement;
  56. /**
  57. * @var \Magento\Quote\Api\ShippingMethodManagementInterface
  58. */
  59. protected $shippingEstimateManagement;
  60. /**
  61. * @var \Magento\Checkout\Api\TotalsInformationManagementInterface
  62. */
  63. protected $totalsInformationManagement;
  64. /**
  65. * @var \Magento\Framework\ObjectManagerInterface
  66. */
  67. protected $objectManager;
  68. public function setUp()
  69. {
  70. $this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  71. $this->customerSession = $this->objectManager->get(\Magento\Customer\Model\Session::class);
  72. $this->persistentSessionHelper = $this->objectManager->create(\Magento\Persistent\Helper\Session::class);
  73. $this->customerFactory = $this->objectManager->create(
  74. \Magento\Customer\Model\CustomerFactory::class
  75. );
  76. $this->checkoutSession = $this->objectManager->create(\Magento\Checkout\Model\Session::class);
  77. $this->cartRepository = $this->objectManager->create(\Magento\Quote\Api\CartRepositoryInterface::class);
  78. $this->cartManagement = $this->objectManager->create(\Magento\Quote\Api\CartManagementInterface::class);
  79. $this->cartItemRepository = $this->objectManager->create(\Magento\Quote\Api\CartItemRepositoryInterface::class);
  80. $this->quoteIdMaskFactory = $this->objectManager->create(\Magento\Quote\Model\QuoteIdMaskFactory::class);
  81. $this->paymentMethodManagement = $this->objectManager->create(
  82. \Magento\Quote\Api\PaymentMethodManagementInterface::class
  83. );
  84. $this->billingAddressManagement = $this->objectManager->create(
  85. \Magento\Quote\Api\BillingAddressManagementInterface::class
  86. );
  87. $this->shippingEstimateManagement = $this->objectManager->create(
  88. \Magento\Quote\Api\ShippingMethodManagementInterface::class
  89. );
  90. $this->totalsInformationManagement = $this->objectManager->create(
  91. \Magento\Checkout\Api\TotalsInformationManagementInterface::class
  92. );
  93. $this->shippingAddressManagement = $this->objectManager->create(
  94. \Magento\Quote\Model\ShippingAddressManagementInterface::class
  95. );
  96. }
  97. /**
  98. * Test builds out a persistent customer shopping cart, emulates a
  99. * session expiring, and checks out with the persisted cart as a guest.
  100. *
  101. * Expected - Order contains guest email, not customer email.
  102. *
  103. * @magentoConfigFixture current_store persistent/options/customer 1
  104. * @magentoConfigFixture current_store persistent/options/enabled 1
  105. * @magentoConfigFixture current_store persistent/options/remember_enabled 1
  106. * @magentoConfigFixture current_store persistent/options/remember_default 1
  107. * @magentoConfigFixture current_store payment/substitution/active 1
  108. * @magentoAppArea frontend
  109. * @magentoAppIsolation enabled
  110. * @magentoDataFixture Magento/Customer/_files/customer.php
  111. * @magentoDataFixture Magento/Customer/_files/customer_address.php
  112. * @magentoDataFixture Magento/Catalog/_files/product_simple.php
  113. * @magentoDbIsolation disabled
  114. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  115. */
  116. public function testBeforeSavePaymentInformationAndPlaceOrder()
  117. {
  118. $guestEmail = 'guest@example.com';
  119. //Retrieve customer from repository
  120. /** @var \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository */
  121. $customerRepository = $this->objectManager->create(\Magento\Customer\Api\CustomerRepositoryInterface::class);
  122. $customer = $customerRepository->getById(1);
  123. $this->customerSession->loginById($customer->getId());
  124. //Retrieve product from repository
  125. /** @var \Magento\Catalog\Api\ProductRepositoryInterface $productRepository */
  126. $productRepository = $this->objectManager->create(\Magento\Catalog\Api\ProductRepositoryInterface::class);
  127. $product = $productRepository->getById(1);
  128. $product->setOptions(null);
  129. $productRepository->save($product);
  130. //Add item to newly created customer cart
  131. $cartId = $this->cartManagement->createEmptyCartForCustomer($customer->getId());
  132. /** @var \Magento\Quote\Api\Data\CartItemInterface $quoteItem */
  133. $quoteItem = $this->objectManager->create(\Magento\Quote\Api\Data\CartItemInterface::class);
  134. $quoteItem->setQuoteId($cartId);
  135. $quoteItem->setProduct($product);
  136. $quoteItem->setQty(2);
  137. $this->cartItemRepository->save($quoteItem);
  138. //Fill out address data
  139. /** @var \Magento\Quote\Api\Data\AddressInterface $billingAddress */
  140. $billingAddress = $this->objectManager->create(\Magento\Quote\Api\Data\AddressInterface::class);
  141. $billingAddress->setFirstname('guestFirst');
  142. $billingAddress->setLastname('guestLast');
  143. $billingAddress->setEmail($guestEmail);
  144. $billingAddress->setStreet('guestStreet');
  145. $billingAddress->setCity('Austin');
  146. $billingAddress->setTelephone('1342587690');
  147. $billingAddress->setPostcode('14325');
  148. $billingAddress->setRegionId(12);
  149. $billingAddress->setCountryId('US');
  150. /** @var \Magento\Quote\Api\Data\AddressInterface $shippingAddress */
  151. $shippingAddress = $this->objectManager->create(\Magento\Quote\Api\Data\AddressInterface::class);
  152. $shippingAddress->setFirstname('guestFirst');
  153. $shippingAddress->setLastname('guestLast');
  154. $shippingAddress->setEmail(null);
  155. $shippingAddress->setStreet('guestStreet');
  156. $shippingAddress->setCity('Austin');
  157. $shippingAddress->setTelephone('1342587690');
  158. $shippingAddress->setPostcode('14325');
  159. $shippingAddress->setRegionId(12);
  160. $shippingAddress->setCountryId('US');
  161. $shippingAddress->setSameAsBilling(true);
  162. $this->shippingAddressManagement->assign($cartId, $shippingAddress);
  163. $shippingAddress = $this->shippingAddressManagement->get($cartId);
  164. //Determine shipping options and collect totals
  165. /** @var \Magento\Checkout\Api\Data\TotalsInformationInterface $totals */
  166. $totals = $this->objectManager->create(\Magento\Checkout\Api\Data\TotalsInformationInterface::class);
  167. $totals->setAddress($shippingAddress);
  168. $totals->setShippingCarrierCode('flatrate');
  169. $totals->setShippingMethodCode('flatrate');
  170. $this->totalsInformationManagement->calculate($cartId, $totals);
  171. //Select payment method
  172. /** @var \Magento\Quote\Api\Data\PaymentInterface $payment */
  173. $payment = $this->objectManager->create(\Magento\Quote\Api\Data\PaymentInterface::class);
  174. $payment->setMethod('checkmo');
  175. $this->paymentMethodManagement->set($cartId, $payment);
  176. $quote = $this->cartRepository->get($cartId);
  177. //Verify checkout session contains correct quote data
  178. $this->checkoutSession->clearQuote();
  179. $this->checkoutSession->setQuoteId($quote->getId());
  180. //Set up persistent session data and expire customer session
  181. $this->persistentSessionHelper->getSession()->setCustomerId($customer->getId())
  182. ->setPersistentCookie(10000, '');
  183. $this->persistentSessionHelper->getSession()->removePersistentCookie()->setPersistentCookie(100000000, '');
  184. $this->customerSession->setIsCustomerEmulated(true)->expireSessionCookie();
  185. //Grab masked quote Id to pass to payment manager
  186. /** @var $quoteIdMask \Magento\Quote\Model\QuoteIdMask */
  187. $quoteIdMask = $this->quoteIdMaskFactory->create()->load(
  188. $this->checkoutSession->getQuote()->getId(),
  189. 'quote_id'
  190. );
  191. $maskedCartId = $quoteIdMask->getMaskedId();
  192. //Submit order as expired/emulated customer
  193. /** @var \Magento\Checkout\Model\GuestPaymentInformationManagement $paymentManagement */
  194. $paymentManagement = $this->objectManager->create(
  195. \Magento\Checkout\Model\GuestPaymentInformationManagement::class
  196. );
  197. //Grab created order data
  198. $orderId = $paymentManagement->savePaymentInformationAndPlaceOrder(
  199. $maskedCartId,
  200. $guestEmail,
  201. $quote->getPayment(),
  202. $billingAddress
  203. );
  204. /** @var \Magento\Sales\Api\OrderRepositoryInterface $orderRepo */
  205. $orderRepo = $this->objectManager->create(\Magento\Sales\Api\OrderRepositoryInterface::class);
  206. $order = $orderRepo->get($orderId);
  207. //Assert order tied to guest email
  208. $this->assertEquals($guestEmail, $order->getCustomerEmail());
  209. }
  210. }