|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +namespace Magento\CheckoutAgreements\Model\Checkout\Plugin; |
| 7 | + |
| 8 | +class GuestValidationTest extends \PHPUnit_Framework_TestCase |
| 9 | +{ |
| 10 | + /** |
| 11 | + * @var \Magento\Checkout\Model\Session |
| 12 | + */ |
| 13 | + private $checkoutSession; |
| 14 | + |
| 15 | + /** |
| 16 | + * @var \Magento\Quote\Api\CartRepositoryInterface |
| 17 | + */ |
| 18 | + private $cartRepository; |
| 19 | + |
| 20 | + /** |
| 21 | + * @var \Magento\Quote\Api\CartManagementInterface |
| 22 | + */ |
| 23 | + private $cartManagement; |
| 24 | + |
| 25 | + /** |
| 26 | + * @var \Magento\Quote\Api\CartItemRepositoryInterface |
| 27 | + */ |
| 28 | + private $cartItemRepository; |
| 29 | + |
| 30 | + /** |
| 31 | + * @var \Magento\Quote\Model\QuoteIdMask |
| 32 | + */ |
| 33 | + private $quoteIdMaskFactory; |
| 34 | + |
| 35 | + /** |
| 36 | + * @var \Magento\Quote\Api\PaymentMethodManagementInterface |
| 37 | + */ |
| 38 | + private $paymentMethodManagement; |
| 39 | + |
| 40 | + /** |
| 41 | + * @var \Magento\Quote\Model\ShippingAddressManagementInterface |
| 42 | + */ |
| 43 | + private $shippingAddressManagement; |
| 44 | + |
| 45 | + /** |
| 46 | + * @var \Magento\Checkout\Api\TotalsInformationManagementInterface |
| 47 | + */ |
| 48 | + private $totalsInformationManagement; |
| 49 | + |
| 50 | + /** |
| 51 | + * @var \Magento\Framework\ObjectManagerInterface |
| 52 | + */ |
| 53 | + private $objectManager; |
| 54 | + |
| 55 | + public function setUp() |
| 56 | + { |
| 57 | + $this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); |
| 58 | + $this->checkoutSession = $this->objectManager->create(\Magento\Checkout\Model\Session::class); |
| 59 | + $this->cartRepository = $this->objectManager->create(\Magento\Quote\Api\CartRepositoryInterface::class); |
| 60 | + $this->cartManagement = $this->objectManager->create(\Magento\Quote\Api\CartManagementInterface::class); |
| 61 | + $this->cartItemRepository = $this->objectManager->create(\Magento\Quote\Api\CartItemRepositoryInterface::class); |
| 62 | + $this->quoteIdMaskFactory = $this->objectManager->create(\Magento\Quote\Model\QuoteIdMaskFactory::class); |
| 63 | + $this->paymentMethodManagement = $this->objectManager->create( |
| 64 | + \Magento\Quote\Api\PaymentMethodManagementInterface::class |
| 65 | + ); |
| 66 | + $this->totalsInformationManagement = $this->objectManager->create( |
| 67 | + \Magento\Checkout\Api\TotalsInformationManagementInterface::class |
| 68 | + ); |
| 69 | + $this->shippingAddressManagement = $this->objectManager->create( |
| 70 | + \Magento\Quote\Model\ShippingAddressManagementInterface::class |
| 71 | + ); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Expected - Order fail with exception. |
| 76 | + * |
| 77 | + * @magentoConfigFixture current_store payment/substitution/active 1 |
| 78 | + * @magentoConfigFixture default_store checkout/options/enable_agreements 1 |
| 79 | + * @magentoDataFixture Magento/CheckoutAgreements/_files/multi_agreements_active_with_text.php |
| 80 | + * @magentoAppArea frontend |
| 81 | + * @magentoAppIsolation enabled |
| 82 | + * @magentoDataFixture Magento/Catalog/_files/product_simple.php |
| 83 | + * @magentoDbIsolation disabled |
| 84 | + * @SuppressWarnings(PHPMD.ExcessiveMethodLength) |
| 85 | + * @dataProvider dataProvider |
| 86 | + * @param string[] $agreementNames |
| 87 | + */ |
| 88 | + public function testBeforeSavePaymentInformationAndPlaceOrder($agreementNames) |
| 89 | + { |
| 90 | + $guestEmail = 'guest@example.com'; |
| 91 | + $carrierCode = 'flatrate'; |
| 92 | + $shippingMethodCode = 'flatrate'; |
| 93 | + $paymentMethod = 'checkmo'; |
| 94 | + $paymentExtension = $this->getPaymentExtension($agreementNames); |
| 95 | + $product = $this->getProduct(1); |
| 96 | + $shippingAddress = $this->getShippingAddress(); |
| 97 | + $billingAddress = $this->getBillingAddress($guestEmail); |
| 98 | + $payment = $this->getPayment($paymentMethod, $paymentExtension); |
| 99 | + |
| 100 | + //Create cart and add product to it |
| 101 | + $cartId = $this->cartManagement->createEmptyCart(); |
| 102 | + $this->addProductToCart($product, $cartId); |
| 103 | + |
| 104 | + //Assign shipping address |
| 105 | + $this->shippingAddressManagement->assign($cartId, $shippingAddress); |
| 106 | + $shippingAddress = $this->shippingAddressManagement->get($cartId); |
| 107 | + |
| 108 | + //Calculate totals |
| 109 | + $totals = $this->getTotals($shippingAddress, $carrierCode, $shippingMethodCode); |
| 110 | + $this->totalsInformationManagement->calculate($cartId, $totals); |
| 111 | + |
| 112 | + //Set payment method |
| 113 | + $this->paymentMethodManagement->set($cartId, $payment); |
| 114 | + |
| 115 | + //Verify checkout session contains correct quote data |
| 116 | + $quote = $this->cartRepository->get($cartId); |
| 117 | + $this->checkoutSession->clearQuote(); |
| 118 | + $this->checkoutSession->setQuoteId($quote->getId()); |
| 119 | + |
| 120 | + //Grab masked quote Id to pass to payment manager |
| 121 | + /** @var $quoteIdMask \Magento\Quote\Model\QuoteIdMask */ |
| 122 | + $quoteIdMask = $this->quoteIdMaskFactory->create()->load( |
| 123 | + $this->checkoutSession->getQuote()->getId(), |
| 124 | + 'quote_id' |
| 125 | + ); |
| 126 | + $maskedCartId = $quoteIdMask->getMaskedId(); |
| 127 | + |
| 128 | + $paymentManagement = $this->objectManager->create( |
| 129 | + \Magento\Checkout\Model\GuestPaymentInformationManagement::class |
| 130 | + ); |
| 131 | + |
| 132 | + try { |
| 133 | + $orderId = $paymentManagement->savePaymentInformationAndPlaceOrder( |
| 134 | + $maskedCartId, |
| 135 | + $guestEmail, |
| 136 | + $payment, |
| 137 | + $billingAddress |
| 138 | + ); |
| 139 | + $this->assertNotNull($orderId); |
| 140 | + } catch (\Magento\Framework\Exception\CouldNotSaveException $e) { |
| 141 | + $this->assertEquals( |
| 142 | + __('Please agree to all the terms and conditions before placing the order.'), |
| 143 | + $e->getMessage() |
| 144 | + ); |
| 145 | + } |
| 146 | + |
| 147 | + } |
| 148 | + |
| 149 | + public function dataProvider() |
| 150 | + { |
| 151 | + return [ |
| 152 | + [[]], |
| 153 | + [['First Checkout Agreement (active)']], |
| 154 | + [['First Checkout Agreement (active)', 'Second Checkout Agreement (active)']] |
| 155 | + ]; |
| 156 | + } |
| 157 | + |
| 158 | + /** |
| 159 | + * @param string $guestEmail |
| 160 | + * @return \Magento\Quote\Api\Data\AddressInterface |
| 161 | + */ |
| 162 | + private function getBillingAddress($guestEmail) |
| 163 | + { |
| 164 | + /** @var \Magento\Quote\Api\Data\AddressInterface $billingAddress */ |
| 165 | + $billingAddress = $this->objectManager->create(\Magento\Quote\Api\Data\AddressInterface::class); |
| 166 | + $billingAddress->setFirstname('First'); |
| 167 | + $billingAddress->setLastname('Last'); |
| 168 | + $billingAddress->setEmail($guestEmail); |
| 169 | + $billingAddress->setStreet('Street'); |
| 170 | + $billingAddress->setCity('City'); |
| 171 | + $billingAddress->setTelephone('1234567890'); |
| 172 | + $billingAddress->setPostcode('12345'); |
| 173 | + $billingAddress->setRegionId(12); |
| 174 | + $billingAddress->setCountryId('US'); |
| 175 | + return $billingAddress; |
| 176 | + } |
| 177 | + |
| 178 | + /** |
| 179 | + * @param string $agreementName |
| 180 | + * @return \Magento\CheckoutAgreements\Model\Agreement |
| 181 | + */ |
| 182 | + private function getAgreement($agreementName) |
| 183 | + { |
| 184 | + $agreement = $this->objectManager->create(\Magento\CheckoutAgreements\Model\Agreement::class); |
| 185 | + $agreement->load($agreementName, 'name'); |
| 186 | + return $agreement; |
| 187 | + } |
| 188 | + |
| 189 | + /** |
| 190 | + * @param string[] $agreementNames |
| 191 | + * @return \Magento\Quote\Api\Data\PaymentExtension |
| 192 | + */ |
| 193 | + private function getPaymentExtension($agreementNames) |
| 194 | + { |
| 195 | + $agreementIds = []; |
| 196 | + foreach ($agreementNames as $agreementName) { |
| 197 | + $agreementIds[] = $this->getAgreement($agreementName)->getAgreementId(); |
| 198 | + } |
| 199 | + $extension = $this->objectManager->get(\Magento\Quote\Api\Data\PaymentExtension::class); |
| 200 | + $extension->setAgreementIds($agreementIds); |
| 201 | + return $extension; |
| 202 | + } |
| 203 | + |
| 204 | + /** |
| 205 | + * @param int $productId |
| 206 | + * @return \Magento\Catalog\Api\Data\ProductInterface |
| 207 | + */ |
| 208 | + private function getProduct($productId) |
| 209 | + { |
| 210 | + /** @var \Magento\Catalog\Api\ProductRepositoryInterface $productRepository */ |
| 211 | + $productRepository = $this->objectManager->create(\Magento\Catalog\Api\ProductRepositoryInterface::class); |
| 212 | + $product = $productRepository->getById($productId); |
| 213 | + $product->setOptions(null); |
| 214 | + $productRepository->save($product); |
| 215 | + return $product; |
| 216 | + } |
| 217 | + |
| 218 | + /** |
| 219 | + * @param \Magento\Catalog\Api\Data\ProductInterface $product |
| 220 | + * @param string $cartId |
| 221 | + */ |
| 222 | + private function addProductToCart($product, $cartId) |
| 223 | + { |
| 224 | + /** @var \Magento\Quote\Api\Data\CartItemInterface $quoteItem */ |
| 225 | + $quoteItem = $this->objectManager->create(\Magento\Quote\Api\Data\CartItemInterface::class); |
| 226 | + $quoteItem->setQuoteId($cartId); |
| 227 | + $quoteItem->setProduct($product); |
| 228 | + $quoteItem->setQty(2); |
| 229 | + $this->cartItemRepository->save($quoteItem); |
| 230 | + } |
| 231 | + |
| 232 | + /** |
| 233 | + * @return \Magento\Quote\Api\Data\AddressInterface |
| 234 | + */ |
| 235 | + private function getShippingAddress() |
| 236 | + { |
| 237 | + $shippingAddress = $this->objectManager->create(\Magento\Quote\Api\Data\AddressInterface::class); |
| 238 | + $shippingAddress->setFirstname('First'); |
| 239 | + $shippingAddress->setLastname('Last'); |
| 240 | + $shippingAddress->setEmail(null); |
| 241 | + $shippingAddress->setStreet('Street'); |
| 242 | + $shippingAddress->setCity('City'); |
| 243 | + $shippingAddress->setTelephone('1234567890'); |
| 244 | + $shippingAddress->setPostcode('12345'); |
| 245 | + $shippingAddress->setRegionId(12); |
| 246 | + $shippingAddress->setCountryId('US'); |
| 247 | + $shippingAddress->setSameAsBilling(true); |
| 248 | + return $shippingAddress; |
| 249 | + } |
| 250 | + |
| 251 | + /** |
| 252 | + * @param \Magento\Quote\Api\Data\AddressInterface $shippingAddress |
| 253 | + * @param string $carrierCode |
| 254 | + * @param string $methodCode |
| 255 | + * @return \Magento\Checkout\Api\Data\TotalsInformationInterface |
| 256 | + */ |
| 257 | + private function getTotals($shippingAddress, $carrierCode, $methodCode) |
| 258 | + { |
| 259 | + /** @var \Magento\Checkout\Api\Data\TotalsInformationInterface $totals */ |
| 260 | + $totals = $this->objectManager->create(\Magento\Checkout\Api\Data\TotalsInformationInterface::class); |
| 261 | + $totals->setAddress($shippingAddress); |
| 262 | + $totals->setShippingCarrierCode($carrierCode); |
| 263 | + $totals->setShippingMethodCode($methodCode); |
| 264 | + |
| 265 | + return $totals; |
| 266 | + } |
| 267 | + |
| 268 | + /** |
| 269 | + * @param $paymentMethod |
| 270 | + * @param $paymentExtension |
| 271 | + * @return \Magento\Quote\Api\Data\PaymentInterface |
| 272 | + */ |
| 273 | + private function getPayment($paymentMethod, $paymentExtension) |
| 274 | + { |
| 275 | + $payment = $this->objectManager->create(\Magento\Quote\Api\Data\PaymentInterface::class); |
| 276 | + $payment->setMethod($paymentMethod); |
| 277 | + $payment->setExtensionAttributes($paymentExtension); |
| 278 | + return $payment; |
| 279 | + } |
| 280 | +} |
0 commit comments