PageRenderTime 57ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/dev/tests/api-functional/testsuite/Magento/Downloadable/Api/CartItemRepositoryTest.php

https://gitlab.com/svillegas/magento2
PHP | 327 lines | 259 code | 23 blank | 45 comment | 0 complexity | 0d87e2e43b179efe1095b916c332597f MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Downloadable\Api;
  7. use Magento\TestFramework\TestCase\WebapiAbstract;
  8. class CartItemRepositoryTest extends WebapiAbstract
  9. {
  10. const SERVICE_VERSION = 'V1';
  11. const SERVICE_NAME = 'quoteCartItemRepositoryV1';
  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. /**
  21. * @magentoApiDataFixture Magento/Quote/_files/empty_quote.php
  22. * @magentoApiDataFixture Magento/Downloadable/_files/product_downloadable.php
  23. */
  24. public function testAddItem()
  25. {
  26. /** @var \Magento\Catalog\Model\Product $product */
  27. $product = $this->objectManager->create(\Magento\Catalog\Model\Product::class)->load(1);
  28. $productSku = $product->getSku();
  29. /** @var \Magento\Quote\Model\Quote $quote */
  30. $quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
  31. $quote->load('reserved_order_id', 'reserved_order_id');
  32. $cartId = $quote->getId();
  33. $serviceInfo = [
  34. 'rest' => [
  35. 'resourcePath' => '/V1/carts/' . $cartId . '/items',
  36. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST,
  37. ],
  38. 'soap' => [
  39. 'service' => self::SERVICE_NAME,
  40. 'serviceVersion' => self::SERVICE_VERSION,
  41. 'operation' => self::SERVICE_NAME . 'Save',
  42. ],
  43. ];
  44. // use ID of the first downloadable link
  45. $linkId = array_values($product->getDownloadableLinks())[0]->getId();
  46. $requestData = [
  47. 'cartItem' => [
  48. 'sku' => $productSku,
  49. 'qty' => 1,
  50. 'quote_id' => $cartId,
  51. 'product_option' => [
  52. 'extension_attributes' => [
  53. 'downloadable_option' => [
  54. 'downloadable_links' => [$linkId]
  55. ]
  56. ]
  57. ]
  58. ],
  59. ];
  60. $response = $this->_webApiCall($serviceInfo, $requestData);
  61. $this->assertNotEmpty($response);
  62. $this->assertEquals('downloadable-product', $response['sku']);
  63. $this->assertEquals(1, $response['qty']);
  64. $this->assertCount(
  65. 1,
  66. $response['product_option']['extension_attributes']['downloadable_option']['downloadable_links']
  67. );
  68. $this->assertContains(
  69. $linkId,
  70. $response['product_option']['extension_attributes']['downloadable_option']['downloadable_links']
  71. );
  72. }
  73. /**
  74. * @magentoApiDataFixture Magento/Quote/_files/empty_quote.php
  75. * @magentoApiDataFixture Magento/Downloadable/_files/product_downloadable.php
  76. * @expectedException \Exception
  77. */
  78. public function testAddItemWithInvalidLinkId()
  79. {
  80. /** @var \Magento\Catalog\Model\Product $product */
  81. $product = $this->objectManager->create(\Magento\Catalog\Model\Product::class)->load(1);
  82. /** @var \Magento\Quote\Model\Quote $quote */
  83. $quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
  84. $quote->load('reserved_order_id', 'reserved_order_id');
  85. $cartId = $quote->getId();
  86. $productSku = $product->getSku();
  87. $serviceInfo = [
  88. 'rest' => [
  89. 'resourcePath' => '/V1/carts/' . $cartId . '/items',
  90. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST,
  91. ],
  92. 'soap' => [
  93. 'service' => self::SERVICE_NAME,
  94. 'serviceVersion' => self::SERVICE_VERSION,
  95. 'operation' => self::SERVICE_NAME . 'Save',
  96. ],
  97. ];
  98. $linkId = 9999;
  99. $requestData = [
  100. 'cartItem' => [
  101. 'sku' => $productSku,
  102. 'qty' => 1,
  103. 'quote_id' => $cartId,
  104. 'product_option' => [
  105. 'extension_attributes' => [
  106. 'downloadable_option' => [
  107. 'downloadable_links' => [$linkId]
  108. ]
  109. ]
  110. ]
  111. ],
  112. ];
  113. $this->_webApiCall($serviceInfo, $requestData);
  114. }
  115. /**
  116. * @magentoApiDataFixture Magento/Downloadable/_files/quote_with_downloadable_product.php
  117. */
  118. public function testUpdateItem()
  119. {
  120. /** @var \Magento\Quote\Model\Quote $quote */
  121. $quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
  122. $quote->load('reserved_order_id_1', 'reserved_order_id');
  123. $cartId = $quote->getId();
  124. $product = $this->objectManager->create(\Magento\Catalog\Model\Product::class);
  125. $product->load($product->getIdBySku('downloadable-product'));
  126. // use ID of the first quote item
  127. $itemId = $quote->getAllItems()[0]->getId();
  128. $serviceInfo = [
  129. 'rest' => [
  130. 'resourcePath' => '/V1/carts/' . $cartId . '/items/' . $itemId,
  131. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT,
  132. ],
  133. 'soap' => [
  134. 'service' => self::SERVICE_NAME,
  135. 'serviceVersion' => self::SERVICE_VERSION,
  136. 'operation' => self::SERVICE_NAME . 'Save',
  137. ],
  138. ];
  139. // use ID of the first downloadable link
  140. $linkId = array_values($product->getDownloadableLinks())[0]->getId();
  141. $requestData = [
  142. 'cartItem' => [
  143. 'qty' => 2,
  144. 'quote_id' => $cartId,
  145. 'item_id' => $itemId,
  146. 'sku' => 'downloadable-product',
  147. 'product_option' => [
  148. 'extension_attributes' => [
  149. 'downloadable_option' => [
  150. 'downloadable_links' => [$linkId]
  151. ]
  152. ]
  153. ]
  154. ],
  155. ];
  156. $response = $this->_webApiCall($serviceInfo, $requestData);
  157. $this->assertNotEmpty($response);
  158. $this->assertEquals('downloadable-product', $response['sku']);
  159. $this->assertEquals(2, $response['qty']);
  160. $this->assertCount(
  161. 1,
  162. $response['product_option']['extension_attributes']['downloadable_option']['downloadable_links']
  163. );
  164. $this->assertContains(
  165. $linkId,
  166. $response['product_option']['extension_attributes']['downloadable_option']['downloadable_links']
  167. );
  168. }
  169. /**
  170. * @magentoApiDataFixture Magento/Downloadable/_files/quote_with_downloadable_product.php
  171. * @expectedException \Exception
  172. */
  173. public function testUpdateItemWithInvalidLinkId()
  174. {
  175. /** @var \Magento\Quote\Model\Quote $quote */
  176. $quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
  177. $quote->load('reserved_order_id_1', 'reserved_order_id');
  178. $cartId = $quote->getId();
  179. $product = $this->objectManager->create(\Magento\Catalog\Model\Product::class);
  180. $product->load($product->getIdBySku('downloadable-product'));
  181. // use ID of the first quote item
  182. $itemId = $quote->getAllItems()[0]->getId();
  183. $serviceInfo = [
  184. 'soap' => [
  185. 'service' => self::SERVICE_NAME,
  186. 'serviceVersion' => self::SERVICE_VERSION,
  187. 'operation' => self::SERVICE_NAME . 'Save',
  188. ],
  189. 'rest' => [
  190. 'resourcePath' => '/V1/carts/' . $cartId . '/items/' . $itemId,
  191. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT,
  192. ],
  193. ];
  194. $linkId = 9999;
  195. $requestData = [
  196. 'cartItem' => [
  197. 'qty' => 2,
  198. 'quote_id' => $cartId,
  199. 'item_id' => $itemId,
  200. 'sku' => 'downloadable-product',
  201. 'product_option' => [
  202. 'extension_attributes' => [
  203. 'downloadable_option' => [
  204. 'downloadable_links' => [$linkId]
  205. ]
  206. ]
  207. ]
  208. ],
  209. ];
  210. $this->_webApiCall($serviceInfo, $requestData);
  211. }
  212. /**
  213. * @magentoApiDataFixture Magento/Downloadable/_files/quote_with_downloadable_product.php
  214. */
  215. public function testGetList()
  216. {
  217. /** @var \Magento\Quote\Model\Quote $quote */
  218. $quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
  219. $quote->load('reserved_order_id_1', 'reserved_order_id');
  220. $cartId = $quote->getId();
  221. $product = $this->objectManager->create(\Magento\Catalog\Model\Product::class);
  222. $product->load($product->getIdBySku('downloadable-product'));
  223. // use ID of the first downloadable link
  224. $linkId = array_values($product->getDownloadableLinks())[0]->getId();
  225. /** @var \Magento\Quote\Api\Data\CartItemInterface $item */
  226. $item = $quote->getAllItems()[0];
  227. $expectedResult = [[
  228. 'item_id' => $item->getItemId(),
  229. 'sku' => $item->getSku(),
  230. 'name' => $item->getName(),
  231. 'price' => $item->getPrice(),
  232. 'qty' => $item->getQty(),
  233. 'product_type' => $item->getProductType(),
  234. 'quote_id' => $item->getQuoteId(),
  235. 'product_option' => [
  236. 'extension_attributes' => [
  237. 'downloadable_option' => [
  238. 'downloadable_links' => [$linkId]
  239. ]
  240. ]
  241. ]
  242. ]];
  243. $serviceInfo = [
  244. 'rest' => [
  245. 'resourcePath' => '/V1/carts/' . $cartId . '/items',
  246. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
  247. ],
  248. 'soap' => [
  249. 'service' => self::SERVICE_NAME,
  250. 'serviceVersion' => self::SERVICE_VERSION,
  251. 'operation' => self::SERVICE_NAME . 'GetList',
  252. ],
  253. ];
  254. $requestData = ["cartId" => $cartId];
  255. $this->assertEquals($expectedResult, $this->_webApiCall($serviceInfo, $requestData));
  256. }
  257. /**
  258. * @magentoApiDataFixture Magento/Downloadable/_files/quote_with_downloadable_product.php
  259. */
  260. public function testUpdateItemQty()
  261. {
  262. /** @var \Magento\Quote\Model\Quote $quote */
  263. $quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
  264. $quote->load('reserved_order_id_1', 'reserved_order_id');
  265. $product = $this->objectManager->create(\Magento\Catalog\Model\Product::class);
  266. $product->load($product->getIdBySku('downloadable-product'));
  267. $cartId = $quote->getId();
  268. // use ID of the first quote item
  269. $itemId = $quote->getAllItems()[0]->getId();
  270. $serviceInfo = [
  271. 'rest' => [
  272. 'resourcePath' => '/V1/carts/' . $cartId . '/items/' . $itemId,
  273. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT,
  274. ],
  275. 'soap' => [
  276. 'service' => self::SERVICE_NAME,
  277. 'serviceVersion' => self::SERVICE_VERSION,
  278. 'operation' => self::SERVICE_NAME . 'Save',
  279. ],
  280. ];
  281. // use ID of the first downloadable link
  282. $linkId = array_values($product->getDownloadableLinks())[0]->getId();
  283. $requestData = [
  284. 'cartItem' => [
  285. 'qty' => 2,
  286. 'quote_id' => $cartId,
  287. 'item_id' => $itemId,
  288. 'sku' => 'downloadable-product',
  289. ],
  290. ];
  291. $response = $this->_webApiCall($serviceInfo, $requestData);
  292. $this->assertNotEmpty($response);
  293. $this->assertEquals('downloadable-product', $response['sku']);
  294. $this->assertEquals(2, $response['qty']);
  295. $this->assertCount(
  296. 1,
  297. $response['product_option']['extension_attributes']['downloadable_option']['downloadable_links']
  298. );
  299. $this->assertContains(
  300. $linkId,
  301. $response['product_option']['extension_attributes']['downloadable_option']['downloadable_links']
  302. );
  303. }
  304. }