ItemRepositoryTest.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\GiftMessage\Api;
  7. use Magento\TestFramework\TestCase\WebapiAbstract;
  8. class ItemRepositoryTest extends WebapiAbstract
  9. {
  10. const SERVICE_VERSION = 'V1';
  11. const SERVICE_NAME = 'giftMessageItemRepositoryV1';
  12. const RESOURCE_PATH = '/V1/carts/';
  13. /**
  14. * @var \Magento\TestFramework\ObjectManager
  15. */
  16. protected $objectManager;
  17. protected function setUp()
  18. {
  19. $this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  20. }
  21. /**
  22. * @magentoApiDataFixture Magento/GiftMessage/_files/quote_with_item_message.php
  23. * @magentoAppIsolation enabled
  24. * @magentoDbIsolation disabled
  25. */
  26. public function testGet()
  27. {
  28. /** @var \Magento\Quote\Model\Quote $quote */
  29. $quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
  30. $quote->load('test_order_item_with_message', 'reserved_order_id');
  31. $product = $this->objectManager->create(\Magento\Catalog\Model\Product::class);
  32. $product->load($product->getIdBySku('simple_with_message'));
  33. $itemId = $quote->getItemByProduct($product)->getId();
  34. /** @var \Magento\Catalog\Model\Product $product */
  35. $cartId = $quote->getId();
  36. $serviceInfo = [
  37. 'rest' => [
  38. 'resourcePath' => self::RESOURCE_PATH . $cartId . '/gift-message/' . $itemId,
  39. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
  40. ],
  41. 'soap' => [
  42. 'service' => self::SERVICE_NAME,
  43. 'serviceVersion' => self::SERVICE_VERSION,
  44. 'operation' => self::SERVICE_NAME . 'Get',
  45. ],
  46. ];
  47. $expectedMessage = [
  48. 'recipient' => 'Jane Roe',
  49. 'sender' => 'John Doe',
  50. 'message' => 'Gift Message Text',
  51. ];
  52. $requestData = ["cartId" => $cartId, "itemId" => $itemId];
  53. $resultMessage = $this->_webApiCall($serviceInfo, $requestData);
  54. $this->assertCount(5, $resultMessage);
  55. unset($resultMessage['gift_message_id']);
  56. unset($resultMessage['customer_id']);
  57. $this->assertEquals($expectedMessage, $resultMessage);
  58. }
  59. /**
  60. * @magentoApiDataFixture Magento/GiftMessage/_files/quote_with_item_message.php
  61. */
  62. public function testGetForMyCart()
  63. {
  64. $this->_markTestAsRestOnly();
  65. // get customer ID token
  66. /** @var \Magento\Integration\Api\CustomerTokenServiceInterface $customerTokenService */
  67. $customerTokenService = $this->objectManager->create(
  68. \Magento\Integration\Api\CustomerTokenServiceInterface::class
  69. );
  70. $token = $customerTokenService->createCustomerAccessToken('customer@example.com', 'password');
  71. /** @var \Magento\Quote\Model\Quote $quote */
  72. $quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
  73. $quote->load('test_order_item_with_message', 'reserved_order_id');
  74. $product = $this->objectManager->create(\Magento\Catalog\Model\Product::class);
  75. $product->load($product->getIdBySku('simple_with_message'));
  76. $itemId = $quote->getItemByProduct($product)->getId();
  77. /** @var \Magento\Catalog\Model\Product $product */
  78. $serviceInfo = [
  79. 'rest' => [
  80. 'resourcePath' => self::RESOURCE_PATH . 'mine/gift-message/' . $itemId,
  81. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
  82. 'token' => $token,
  83. ],
  84. ];
  85. $expectedMessage = [
  86. 'recipient' => 'Jane Roe',
  87. 'sender' => 'John Doe',
  88. 'message' => 'Gift Message Text',
  89. ];
  90. $requestData = ["itemId" => $itemId];
  91. $resultMessage = $this->_webApiCall($serviceInfo, $requestData);
  92. $this->assertCount(5, $resultMessage);
  93. unset($resultMessage['gift_message_id']);
  94. unset($resultMessage['customer_id']);
  95. $this->assertEquals($expectedMessage, $resultMessage);
  96. }
  97. /**
  98. * @magentoApiDataFixture Magento/GiftMessage/_files/quote_with_item_message.php
  99. */
  100. public function testSave()
  101. {
  102. // sales/gift_options/allow_items must be set to 1 in system configuration
  103. // @todo remove next statement when \Magento\TestFramework\TestCase\WebapiAbstract::_updateAppConfig is fixed
  104. $this->markTestIncomplete('This test relies on system configuration state.');
  105. /** @var \Magento\Quote\Model\Quote $quote */
  106. $quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
  107. $quote->load('test_order_item_with_message', 'reserved_order_id');
  108. $cartId = $quote->getId();
  109. $product = $this->objectManager->create(\Magento\Catalog\Model\Product::class);
  110. $product->load($product->getIdBySku('simple_with_message'));
  111. $itemId = $quote->getItemByProduct($product)->getId();
  112. $serviceInfo = [
  113. 'rest' => [
  114. 'resourcePath' => self::RESOURCE_PATH . $cartId . '/gift-message/' . $itemId,
  115. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST,
  116. ],
  117. 'soap' => [
  118. 'service' => self::SERVICE_NAME,
  119. 'serviceVersion' => self::SERVICE_VERSION,
  120. 'operation' => self::SERVICE_NAME . 'Save',
  121. ],
  122. ];
  123. $requestData = [
  124. 'cartId' => $cartId,
  125. 'itemId' => $itemId,
  126. 'giftMessage' => [
  127. 'recipient' => 'John Doe',
  128. 'sender' => 'Jane Roe',
  129. 'message' => 'Gift Message Text New',
  130. ],
  131. ];
  132. $this->assertTrue($this->_webApiCall($serviceInfo, $requestData));
  133. $messageId = $quote->getItemByProduct($product)->getGiftMessageId();
  134. /** @var \Magento\GiftMessage\Model\Message $message */
  135. $message = $this->objectManager->create(\Magento\GiftMessage\Model\Message::class)->load($messageId);
  136. $this->assertEquals('John Doe', $message->getRecipient());
  137. $this->assertEquals('Jane Roe', $message->getSender());
  138. $this->assertEquals('Gift Message Text New', $message->getMessage());
  139. }
  140. /**
  141. * @magentoApiDataFixture Magento/GiftMessage/_files/quote_with_item_message.php
  142. */
  143. public function testSaveForMyCart()
  144. {
  145. $this->_markTestAsRestOnly();
  146. // get customer ID token
  147. /** @var \Magento\Integration\Api\CustomerTokenServiceInterface $customerTokenService */
  148. $customerTokenService = $this->objectManager->create(
  149. \Magento\Integration\Api\CustomerTokenServiceInterface::class
  150. );
  151. $token = $customerTokenService->createCustomerAccessToken('customer@example.com', 'password');
  152. // sales/gift_options/allow_items must be set to 1 in system configuration
  153. // @todo remove next statement when \Magento\TestFramework\TestCase\WebapiAbstract::_updateAppConfig is fixed
  154. $this->markTestIncomplete('This test relies on system configuration state.');
  155. /** @var \Magento\Quote\Model\Quote $quote */
  156. $quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
  157. $quote->load('test_order_item_with_message', 'reserved_order_id');
  158. $product = $this->objectManager->create(\Magento\Catalog\Model\Product::class);
  159. $product->load($product->getIdBySku('simple_with_message'));
  160. $itemId = $quote->getItemByProduct($product)->getId();
  161. $serviceInfo = [
  162. 'rest' => [
  163. 'resourcePath' => self::RESOURCE_PATH . 'mine/gift-message/' . $itemId,
  164. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST,
  165. 'token' => $token,
  166. ],
  167. ];
  168. $requestData = [
  169. 'itemId' => $itemId,
  170. 'giftMessage' => [
  171. 'recipient' => 'John Doe',
  172. 'sender' => 'Jane Roe',
  173. 'message' => 'Gift Message Text New',
  174. ],
  175. ];
  176. $this->assertTrue($this->_webApiCall($serviceInfo, $requestData));
  177. $messageId = $quote->getItemByProduct($product)->getGiftMessageId();
  178. /** @var \Magento\GiftMessage\Model\Message $message */
  179. $message = $this->objectManager->create(\Magento\GiftMessage\Model\Message::class)->load($messageId);
  180. $this->assertEquals('John Doe', $message->getRecipient());
  181. $this->assertEquals('Jane Roe', $message->getSender());
  182. $this->assertEquals('Gift Message Text New', $message->getMessage());
  183. }
  184. }