LinkManagementTest.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\ConfigurableProduct\Api;
  8. use Magento\Eav\Model\AttributeRepository;
  9. class LinkManagementTest extends \Magento\TestFramework\TestCase\WebapiAbstract
  10. {
  11. const SERVICE_NAME = 'configurableProductLinkManagementV1';
  12. const SERVICE_VERSION = 'V1';
  13. const RESOURCE_PATH = '/V1/configurable-products';
  14. /**
  15. * @var \Magento\Framework\ObjectManagerInterface
  16. */
  17. protected $objectManager;
  18. /**
  19. * @var AttributeRepository
  20. */
  21. protected $attributeRepository;
  22. /**
  23. * Execute per test initialization
  24. */
  25. public function setUp()
  26. {
  27. $this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  28. $this->attributeRepository = $this->objectManager->get(\Magento\Eav\Model\AttributeRepository::class);
  29. }
  30. /**
  31. * @magentoApiDataFixture Magento/ConfigurableProduct/_files/product_configurable.php
  32. */
  33. public function testGetChildren()
  34. {
  35. $productSku = 'configurable';
  36. /** @var array $result */
  37. $result = $this->getChildren($productSku);
  38. $this->assertCount(2, $result);
  39. foreach ($result as $product) {
  40. $this->assertArrayHasKey('custom_attributes', $product);
  41. $this->assertArrayHasKey('price', $product);
  42. $this->assertArrayHasKey('updated_at', $product);
  43. $this->assertArrayHasKey('name', $product);
  44. $this->assertContains('Configurable Option', $product['name']);
  45. $this->assertArrayHasKey('sku', $product);
  46. $this->assertContains('simple_', $product['sku']);
  47. $this->assertArrayHasKey('status', $product);
  48. $this->assertEquals('1', $product['status']);
  49. }
  50. }
  51. /**
  52. * @magentoApiDataFixture Magento/ConfigurableProduct/_files/product_configurable.php
  53. * @magentoApiDataFixture Magento/ConfigurableProduct/_files/product_simple_77.php
  54. * @magentoApiDataFixture Magento/ConfigurableProduct/_files/delete_association.php
  55. */
  56. public function testAddChild()
  57. {
  58. $productSku = 'configurable';
  59. $childSku = 'simple_77';
  60. $res = $this->addChild($productSku, $childSku);
  61. $this->assertTrue($res);
  62. }
  63. /**
  64. * Test the full flow of creating a configurable product and adding a child via REST
  65. *
  66. * @magentoApiDataFixture Magento/ConfigurableProduct/_files/configurable_attribute.php
  67. */
  68. public function testAddChildFullRestCreation()
  69. {
  70. $productSku = 'configurable-product-sku';
  71. $childSku = 'simple-product-sku';
  72. $this->createConfigurableProduct($productSku);
  73. $attribute = $this->attributeRepository->get('catalog_product', 'test_configurable');
  74. $attributeValue = $attribute->getOptions()[1]->getValue();
  75. $this->addOptionToConfigurableProduct($productSku, $attribute->getAttributeId(), $attributeValue);
  76. $this->createSimpleProduct($childSku, $attributeValue);
  77. $res = $this->addChild($productSku, $childSku);
  78. $this->assertTrue($res);
  79. // confirm that the simple product was added
  80. $children = $this->getChildren($productSku);
  81. $added = false;
  82. foreach ($children as $child) {
  83. if ($child['sku'] == $childSku) {
  84. $added = true;
  85. break;
  86. }
  87. }
  88. $this->assertTrue($added);
  89. // clean up products
  90. $serviceInfo = [
  91. 'rest' => [
  92. 'resourcePath' => '/V1/products/' . $productSku,
  93. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_DELETE
  94. ],
  95. 'soap' => [
  96. 'service' => 'catalogProductRepositoryV1',
  97. 'serviceVersion' => self::SERVICE_VERSION,
  98. 'operation' => 'catalogProductRepositoryV1DeleteById',
  99. ],
  100. ];
  101. $this->_webApiCall($serviceInfo, ['sku' => $productSku]);
  102. $serviceInfo = [
  103. 'rest' => [
  104. 'resourcePath' => '/V1/products/' . $childSku,
  105. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_DELETE
  106. ],
  107. 'soap' => [
  108. 'service' => 'catalogProductRepositoryV1',
  109. 'serviceVersion' => self::SERVICE_VERSION,
  110. 'operation' => 'catalogProductRepositoryV1DeleteById',
  111. ],
  112. ];
  113. $this->_webApiCall($serviceInfo, ['sku' => $childSku]);
  114. }
  115. private function addChild($productSku, $childSku)
  116. {
  117. $serviceInfo = [
  118. 'rest' => [
  119. 'resourcePath' => self::RESOURCE_PATH . '/' . $productSku . '/child',
  120. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST
  121. ],
  122. 'soap' => [
  123. 'service' => self::SERVICE_NAME,
  124. 'serviceVersion' => self::SERVICE_VERSION,
  125. 'operation' => self::SERVICE_NAME . 'AddChild'
  126. ]
  127. ];
  128. return $this->_webApiCall($serviceInfo, ['sku' => $productSku, 'childSku' => $childSku]);
  129. }
  130. protected function createConfigurableProduct($productSku)
  131. {
  132. $requestData = [
  133. 'product' => [
  134. 'sku' => $productSku,
  135. 'name' => 'configurable-product-' . $productSku,
  136. 'type_id' => 'configurable',
  137. 'price' => 50,
  138. 'attribute_set_id' => 4
  139. ]
  140. ];
  141. $serviceInfo = [
  142. 'rest' => [
  143. 'resourcePath' => '/V1/products',
  144. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST
  145. ],
  146. 'soap' => [
  147. 'service' => 'catalogProductRepositoryV1',
  148. 'serviceVersion' => self::SERVICE_VERSION,
  149. 'operation' => 'catalogProductRepositoryV1Save',
  150. ],
  151. ];
  152. return $this->_webApiCall($serviceInfo, $requestData);
  153. }
  154. protected function addOptionToConfigurableProduct($productSku, $attributeId, $attributeValue)
  155. {
  156. $requestData = [
  157. 'sku' => $productSku,
  158. 'option' => [
  159. 'attribute_id' => $attributeId,
  160. 'label' => 'test_configurable',
  161. 'position' => 0,
  162. 'is_use_default' => true,
  163. 'values' => [
  164. ['value_index' => $attributeValue],
  165. ]
  166. ]
  167. ];
  168. $serviceInfo = [
  169. 'rest' => [
  170. 'resourcePath' => '/V1/configurable-products/'. $productSku .'/options',
  171. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST,
  172. ],
  173. 'soap' => [
  174. 'service' => 'configurableProductOptionRepositoryV1',
  175. 'serviceVersion' => self::SERVICE_VERSION,
  176. 'operation' => 'configurableProductOptionRepositoryV1Save',
  177. ],
  178. ];
  179. return $this->_webApiCall($serviceInfo, $requestData);
  180. }
  181. protected function createSimpleProduct($sku, $attributeValue)
  182. {
  183. $requestData = [
  184. 'product' => [
  185. 'sku' => $sku,
  186. 'name' => 'simple-product-' . $sku,
  187. 'type_id' => 'simple',
  188. 'attribute_set_id' => 4,
  189. 'price' => 3.62,
  190. 'status' => 1,
  191. 'visibility' => 4,
  192. 'custom_attributes' => [
  193. ['attribute_code' => 'test_configurable', 'value' => $attributeValue],
  194. ]
  195. ]
  196. ];
  197. $serviceInfo = [
  198. 'rest' => [
  199. 'resourcePath' => '/V1/products',
  200. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST,
  201. ],
  202. 'soap' => [
  203. 'service' => 'catalogProductRepositoryV1',
  204. 'serviceVersion' => self::SERVICE_VERSION,
  205. 'operation' => 'catalogProductRepositoryV1Save',
  206. ],
  207. ];
  208. return $this->_webApiCall($serviceInfo, $requestData);
  209. }
  210. /**
  211. * @magentoApiDataFixture Magento/ConfigurableProduct/_files/product_configurable.php
  212. */
  213. public function testRemoveChild()
  214. {
  215. $productSku = 'configurable';
  216. $childSku = 'simple_10';
  217. $this->assertTrue($this->removeChild($productSku, $childSku));
  218. }
  219. protected function removeChild($productSku, $childSku)
  220. {
  221. $resourcePath = self::RESOURCE_PATH . '/%s/children/%s';
  222. $serviceInfo = [
  223. 'rest' => [
  224. 'resourcePath' => sprintf($resourcePath, $productSku, $childSku),
  225. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_DELETE
  226. ],
  227. 'soap' => [
  228. 'service' => self::SERVICE_NAME,
  229. 'serviceVersion' => self::SERVICE_VERSION,
  230. 'operation' => self::SERVICE_NAME . 'RemoveChild'
  231. ]
  232. ];
  233. $requestData = ['sku' => $productSku, 'childSku' => $childSku];
  234. return $this->_webApiCall($serviceInfo, $requestData);
  235. }
  236. /**
  237. * @param string $productSku
  238. * @return string[]
  239. */
  240. protected function getChildren($productSku)
  241. {
  242. $serviceInfo = [
  243. 'rest' => [
  244. 'resourcePath' => self::RESOURCE_PATH . '/' . $productSku . '/children',
  245. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET
  246. ],
  247. 'soap' => [
  248. 'service' => self::SERVICE_NAME,
  249. 'serviceVersion' => self::SERVICE_VERSION,
  250. 'operation' => self::SERVICE_NAME . 'GetChildren'
  251. ]
  252. ];
  253. return $this->_webApiCall($serviceInfo, ['sku' => $productSku]);
  254. }
  255. }