GuestPaymentMethodManagementTest.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Quote\Api;
  7. class GuestPaymentMethodManagementTest extends \Magento\TestFramework\TestCase\WebapiAbstract
  8. {
  9. const SERVICE_VERSION = 'V1';
  10. const SERVICE_NAME = 'quoteGuestPaymentMethodManagementV1';
  11. const RESOURCE_PATH = '/V1/guest-carts/';
  12. /**
  13. * @var \Magento\TestFramework\ObjectManager
  14. */
  15. protected $objectManager;
  16. protected function setUp()
  17. {
  18. $this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  19. }
  20. protected function tearDown()
  21. {
  22. $this->deleteCart('test_order_1');
  23. $this->deleteCart('test_order_1_with_payment');
  24. $this->deleteCart('test_order_with_virtual_product');
  25. $this->deleteCart('test_order_with_virtual_product_without_address');
  26. parent::tearDown();
  27. }
  28. /**
  29. * Delete quote by given reserved order ID
  30. *
  31. * @param string $reservedOrderId
  32. * @return void
  33. */
  34. protected function deleteCart($reservedOrderId)
  35. {
  36. try {
  37. /** @var $cart \Magento\Quote\Model\Quote */
  38. $cart = $this->objectManager->get(\Magento\Quote\Model\Quote::class);
  39. $cart->load($reservedOrderId, 'reserved_order_id');
  40. if (!$cart->getId()) {
  41. throw new \InvalidArgumentException('There is no quote with provided reserved order ID.');
  42. }
  43. $cart->delete();
  44. /** @var \Magento\Quote\Model\QuoteIdMask $quoteIdMask */
  45. $quoteIdMask = $this->objectManager->create(\Magento\Quote\Model\QuoteIdMask::class);
  46. $quoteIdMask->load($cart->getId(), 'quote_id');
  47. $quoteIdMask->delete();
  48. } catch (\InvalidArgumentException $e) {
  49. // Do nothing if cart fixture was not used
  50. }
  51. }
  52. /**
  53. * @magentoApiDataFixture Magento/Checkout/_files/quote_with_payment_saved.php
  54. */
  55. public function testReSetPayment()
  56. {
  57. /** @var \Magento\Quote\Model\Quote $quote */
  58. $quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
  59. $quote->load('test_order_1_with_payment', 'reserved_order_id');
  60. $cartId = $this->getMaskedCartId($quote->getId());
  61. $serviceInfo = [
  62. 'rest' => [
  63. 'resourcePath' => self::RESOURCE_PATH . $cartId . '/selected-payment-method',
  64. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT,
  65. ],
  66. 'soap' => [
  67. 'service' => self::SERVICE_NAME,
  68. 'serviceVersion' => self::SERVICE_VERSION,
  69. 'operation' => self::SERVICE_NAME . 'set',
  70. ],
  71. ];
  72. $requestData = [
  73. "cartId" => $cartId,
  74. "method" => [
  75. 'method' => 'checkmo',
  76. 'po_number' => null
  77. ],
  78. ];
  79. $this->assertNotNull($this->_webApiCall($serviceInfo, $requestData));
  80. }
  81. /**
  82. * @magentoApiDataFixture Magento/Checkout/_files/quote_with_virtual_product_and_address.php
  83. */
  84. public function testSetPaymentWithVirtualProduct()
  85. {
  86. /** @var \Magento\Quote\Model\Quote $quote */
  87. $quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
  88. $quote->load('test_order_with_virtual_product', 'reserved_order_id');
  89. $cartId = $this->getMaskedCartId($quote->getId());
  90. $serviceInfo = [
  91. 'rest' => [
  92. 'resourcePath' => self::RESOURCE_PATH . $cartId . '/selected-payment-method',
  93. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT,
  94. ],
  95. 'soap' => [
  96. 'service' => self::SERVICE_NAME,
  97. 'serviceVersion' => self::SERVICE_VERSION,
  98. 'operation' => self::SERVICE_NAME . 'set',
  99. ],
  100. ];
  101. $requestData = [
  102. "cartId" => $cartId,
  103. "method" => [
  104. 'method' => 'checkmo',
  105. 'po_number' => '200'
  106. ],
  107. ];
  108. $this->assertNotNull($this->_webApiCall($serviceInfo, $requestData));
  109. }
  110. /**
  111. * @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php
  112. */
  113. public function testSetPaymentWithSimpleProduct()
  114. {
  115. /** @var \Magento\Quote\Model\Quote $quote */
  116. $quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
  117. $quote->load('test_order_1', 'reserved_order_id');
  118. $cartId = $this->getMaskedCartId($quote->getId());
  119. $serviceInfo = [
  120. 'rest' => [
  121. 'resourcePath' => self::RESOURCE_PATH . $cartId . '/selected-payment-method',
  122. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT,
  123. ],
  124. 'soap' => [
  125. 'service' => self::SERVICE_NAME,
  126. 'serviceVersion' => self::SERVICE_VERSION,
  127. 'operation' => self::SERVICE_NAME . 'set',
  128. ],
  129. ];
  130. $requestData = [
  131. "cartId" => $cartId,
  132. "method" => [
  133. 'method' => 'checkmo',
  134. 'po_number' => '200'
  135. ],
  136. ];
  137. $this->assertNotNull($this->_webApiCall($serviceInfo, $requestData));
  138. }
  139. /**
  140. * @magentoApiDataFixture Magento/Checkout/_files/quote_with_simple_product_saved.php
  141. * @expectedException \Exception
  142. * @expectedExceptionMessage The shipping address is missing. Set the address and try again.
  143. */
  144. public function testSetPaymentWithSimpleProductWithoutAddress()
  145. {
  146. /** @var \Magento\Quote\Model\Quote $quote */
  147. $quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
  148. $quote->load('test_order_with_simple_product_without_address', 'reserved_order_id');
  149. $cartId = $this->getMaskedCartId($quote->getId());
  150. $serviceInfo = [
  151. 'rest' => [
  152. 'resourcePath' => self::RESOURCE_PATH . $cartId . '/selected-payment-method',
  153. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT,
  154. ],
  155. 'soap' => [
  156. 'service' => self::SERVICE_NAME,
  157. 'serviceVersion' => self::SERVICE_VERSION,
  158. 'operation' => self::SERVICE_NAME . 'set',
  159. ],
  160. ];
  161. $requestData = [
  162. "cartId" => $cartId,
  163. "method" => [
  164. 'method' => 'checkmo',
  165. 'po_number' => '200'
  166. ],
  167. ];
  168. $this->assertNotNull($this->_webApiCall($serviceInfo, $requestData));
  169. }
  170. /**
  171. * @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php
  172. */
  173. public function testGetList()
  174. {
  175. /** @var \Magento\Quote\Model\Quote $quote */
  176. $quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
  177. $quote->load('test_order_1', 'reserved_order_id');
  178. $cartId = $this->getMaskedCartId($quote->getId());
  179. $serviceInfo = [
  180. 'rest' => [
  181. 'resourcePath' => self::RESOURCE_PATH . $cartId . '/payment-methods',
  182. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
  183. ],
  184. 'soap' => [
  185. 'service' => self::SERVICE_NAME,
  186. 'serviceVersion' => self::SERVICE_VERSION,
  187. 'operation' => self::SERVICE_NAME . 'getList',
  188. ],
  189. ];
  190. $requestData = ["cartId" => $cartId];
  191. $requestResponse = $this->_webApiCall($serviceInfo, $requestData);
  192. $expectedResponse = [
  193. 'code' => 'checkmo',
  194. 'title' => 'Check / Money order',
  195. ];
  196. $this->assertGreaterThan(0, count($requestResponse));
  197. $this->assertContains($expectedResponse, $requestResponse);
  198. }
  199. /**
  200. * @magentoApiDataFixture Magento/Checkout/_files/quote_with_payment_saved.php
  201. */
  202. public function testGet()
  203. {
  204. /** @var \Magento\Quote\Model\Quote $quote */
  205. $quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
  206. $quote->load('test_order_1_with_payment', 'reserved_order_id');
  207. $cartId = $this->getMaskedCartId($quote->getId());
  208. $serviceInfo = [
  209. 'rest' => [
  210. 'resourcePath' => self::RESOURCE_PATH . $cartId . '/selected-payment-method',
  211. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
  212. ],
  213. 'soap' => [
  214. 'service' => self::SERVICE_NAME,
  215. 'serviceVersion' => self::SERVICE_VERSION,
  216. 'operation' => self::SERVICE_NAME . 'get',
  217. ],
  218. ];
  219. $requestData = ["cartId" => $cartId];
  220. $requestResponse = $this->_webApiCall($serviceInfo, $requestData);
  221. foreach ($this->getPaymentMethodFieldsForAssert() as $field) {
  222. $this->assertArrayHasKey($field, $requestResponse);
  223. $this->assertNotNull($requestResponse[$field]);
  224. }
  225. $this->assertEquals('checkmo', $requestResponse['method']);
  226. }
  227. /**
  228. * @return array
  229. */
  230. protected function getPaymentMethodFieldsForAssert()
  231. {
  232. return ['method', 'po_number', 'additional_data'];
  233. }
  234. /**
  235. * Retrieve masked cart ID for guest cart.
  236. *
  237. * @param string $cartId
  238. * @return string
  239. */
  240. protected function getMaskedCartId($cartId)
  241. {
  242. /** @var \Magento\Quote\Model\QuoteIdMask $quoteIdMask */
  243. $quoteIdMask = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
  244. ->create(\Magento\Quote\Model\QuoteIdMaskFactory::class)
  245. ->create();
  246. $quoteIdMask->load($cartId, 'quote_id');
  247. return $quoteIdMask->getMaskedId();
  248. }
  249. }