ExpressTest.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Paypal\Controller;
  7. use Magento\Checkout\Model\Session;
  8. use Magento\Framework\Session\Generic as GenericSession;
  9. use Magento\Paypal\Model\Api\Nvp;
  10. use Magento\Paypal\Model\Api\Type\Factory as ApiFactory;
  11. use Magento\Paypal\Model\Session as PaypalSession;
  12. use Magento\Quote\Model\Quote;
  13. use Magento\TestFramework\Helper\Bootstrap;
  14. /**
  15. * Tests of Paypal Express actions
  16. *
  17. * @package Magento\Paypal\Controller
  18. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  19. */
  20. class ExpressTest extends \Magento\TestFramework\TestCase\AbstractController
  21. {
  22. /**
  23. * @magentoDataFixture Magento/Sales/_files/quote.php
  24. * @magentoDataFixture Magento/Paypal/_files/quote_payment.php
  25. */
  26. public function testReviewAction()
  27. {
  28. $quote = Bootstrap::getObjectManager()->create(Quote::class);
  29. $quote->load('test01', 'reserved_order_id');
  30. Bootstrap::getObjectManager()->get(
  31. Session::class
  32. )->setQuoteId(
  33. $quote->getId()
  34. );
  35. $this->dispatch('paypal/express/review');
  36. $html = $this->getResponse()->getBody();
  37. $this->assertContains('Simple Product', $html);
  38. $this->assertContains('Review', $html);
  39. $this->assertContains('/paypal/express/placeOrder/', $html);
  40. }
  41. /**
  42. * @magentoDataFixture Magento/Paypal/_files/quote_payment_express.php
  43. * @magentoConfigFixture current_store paypal/general/business_account merchant_2012050718_biz@example.com
  44. */
  45. public function testCancelAction()
  46. {
  47. $quote = $this->_objectManager->create(Quote::class);
  48. $quote->load('100000002', 'reserved_order_id');
  49. $order = $this->_objectManager->create(\Magento\Sales\Model\Order::class);
  50. $order->load('100000002', 'increment_id');
  51. $session = $this->_objectManager->get(Session::class);
  52. $session->setLoadInactive(true);
  53. $session->setLastRealOrderId(
  54. $order->getRealOrderId()
  55. )->setLastOrderId(
  56. $order->getId()
  57. )->setLastQuoteId(
  58. $order->getQuoteId()
  59. )->setQuoteId(
  60. $order->getQuoteId()
  61. );
  62. /** @var $paypalSession Generic */
  63. $paypalSession = $this->_objectManager->get(PaypalSession::class);
  64. $paypalSession->setExpressCheckoutToken('token');
  65. $this->dispatch('paypal/express/cancel');
  66. $order->load('100000002', 'increment_id');
  67. $this->assertEquals('canceled', $order->getState());
  68. $this->assertEquals($session->getQuote()->getGrandTotal(), $quote->getGrandTotal());
  69. $this->assertEquals($session->getQuote()->getItemsCount(), $quote->getItemsCount());
  70. }
  71. /**
  72. * Test ensures only that customer data was copied to quote correctly.
  73. *
  74. * Note that test does not verify communication during remote calls to PayPal.
  75. *
  76. * @magentoDataFixture Magento/Sales/_files/quote.php
  77. * @magentoDataFixture Magento/Customer/_files/customer.php
  78. */
  79. public function testStartActionCustomerToQuote()
  80. {
  81. $fixtureCustomerId = 1;
  82. $fixtureCustomerEmail = 'customer@example.com';
  83. $fixtureCustomerFirstname = 'John';
  84. $fixtureQuoteReserveId = 'test01';
  85. /** Preconditions */
  86. /** @var \Magento\Customer\Model\Session $customerSession */
  87. $customerSession = $this->_objectManager->get(\Magento\Customer\Model\Session::class);
  88. /** @var \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository */
  89. $customerRepository = $this->_objectManager->get(\Magento\Customer\Api\CustomerRepositoryInterface::class);
  90. $customerData = $customerRepository->getById($fixtureCustomerId);
  91. $customerSession->setCustomerDataObject($customerData);
  92. /** @var Quote $quote */
  93. $quote = $this->_objectManager->create(Quote::class);
  94. $quote->load($fixtureQuoteReserveId, 'reserved_order_id');
  95. /** @var Session $checkoutSession */
  96. $checkoutSession = $this->_objectManager->get(Session::class);
  97. $checkoutSession->setQuoteId($quote->getId());
  98. /** Preconditions check */
  99. $this->assertNotEquals(
  100. $fixtureCustomerEmail,
  101. $quote->getCustomerEmail(),
  102. "Precondition failed: customer email in quote is invalid."
  103. );
  104. $this->assertNotEquals(
  105. $fixtureCustomerFirstname,
  106. $quote->getCustomerFirstname(),
  107. "Precondition failed: customer first name in quote is invalid."
  108. );
  109. /** Execute SUT */
  110. $this->dispatch('paypal/express/start');
  111. /** Check if customer data was copied to quote correctly */
  112. /** @var Quote $updatedQuote */
  113. $updatedQuote = $this->_objectManager->create(Quote::class);
  114. $updatedQuote->load($fixtureQuoteReserveId, 'reserved_order_id');
  115. $this->assertEquals(
  116. $fixtureCustomerEmail,
  117. $updatedQuote->getCustomer()->getEmail(),
  118. "Customer email in quote is invalid."
  119. );
  120. $this->assertEquals(
  121. $fixtureCustomerFirstname,
  122. $updatedQuote->getCustomer()->getFirstname(),
  123. "Customer first name in quote is invalid."
  124. );
  125. }
  126. /**
  127. * Test return action with configurable product.
  128. *
  129. * @magentoDataFixture Magento/Paypal/_files/quote_express_configurable.php
  130. */
  131. public function testReturnAction()
  132. {
  133. $quote = $this->_objectManager->create(Quote::class);
  134. $quote->load('test_cart_with_configurable', 'reserved_order_id');
  135. $payment = $quote->getPayment();
  136. $payment->setMethod(\Magento\Paypal\Model\Config::METHOD_WPP_EXPRESS)
  137. ->setAdditionalInformation(\Magento\Paypal\Model\Express\Checkout::PAYMENT_INFO_TRANSPORT_PAYER_ID, 123);
  138. $quote->save();
  139. $this->_objectManager->removeSharedInstance(Session::class);
  140. $session = $this->_objectManager->get(Session::class);
  141. $session->setQuoteId($quote->getId());
  142. $nvpMethods = [
  143. 'setToken',
  144. 'setPayerId',
  145. 'setAmount',
  146. 'setPaymentAction',
  147. 'setNotifyUrl',
  148. 'setInvNum',
  149. 'setCurrencyCode',
  150. 'setPaypalCart',
  151. 'setIsLineItemsEnabled',
  152. 'setAddress',
  153. 'setBillingAddress',
  154. 'callDoExpressCheckoutPayment',
  155. 'callGetExpressCheckoutDetails',
  156. 'getExportedBillingAddress'
  157. ];
  158. $nvpMock = $this->getMockBuilder(Nvp::class)
  159. ->setMethods($nvpMethods)
  160. ->disableOriginalConstructor()
  161. ->getMock();
  162. foreach ($nvpMethods as $method) {
  163. $nvpMock->method($method)
  164. ->willReturnSelf();
  165. }
  166. $apiFactoryMock = $this->getMockBuilder(ApiFactory::class)
  167. ->disableOriginalConstructor()
  168. ->setMethods(['create'])
  169. ->getMock();
  170. $apiFactoryMock->method('create')
  171. ->with(Nvp::class)
  172. ->willReturn($nvpMock);
  173. $this->_objectManager->addSharedInstance($apiFactoryMock, ApiFactory::class);
  174. $sessionMock = $this->getMockBuilder(GenericSession::class)
  175. ->setMethods(['getExpressCheckoutToken'])
  176. ->setConstructorArgs(
  177. [
  178. $this->_objectManager->get(\Magento\Framework\App\Request\Http::class),
  179. $this->_objectManager->get(\Magento\Framework\Session\SidResolverInterface::class),
  180. $this->_objectManager->get(\Magento\Framework\Session\Config\ConfigInterface::class),
  181. $this->_objectManager->get(\Magento\Framework\Session\SaveHandlerInterface::class),
  182. $this->_objectManager->get(\Magento\Framework\Session\ValidatorInterface::class),
  183. $this->_objectManager->get(\Magento\Framework\Session\StorageInterface::class),
  184. $this->_objectManager->get(\Magento\Framework\Stdlib\CookieManagerInterface::class),
  185. $this->_objectManager->get(\Magento\Framework\Stdlib\Cookie\CookieMetadataFactory::class),
  186. $this->_objectManager->get(\Magento\Framework\App\State::class),
  187. ]
  188. )
  189. ->getMock();
  190. $sessionMock->method('getExpressCheckoutToken')
  191. ->willReturn(true);
  192. $this->_objectManager->addSharedInstance($sessionMock, PaypalSession::class);
  193. $this->dispatch('paypal/express/returnAction');
  194. $this->assertRedirect($this->stringContains('checkout/onepage/success'));
  195. $this->_objectManager->removeSharedInstance(ApiFactory::class);
  196. $this->_objectManager->removeSharedInstance(PaypalSession::class);
  197. }
  198. }