AttributeSetManagementTest.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Catalog\Api;
  7. use Magento\TestFramework\Helper\Bootstrap;
  8. use Magento\TestFramework\TestCase\WebapiAbstract;
  9. use Magento\Framework\Webapi\Exception as HTTPExceptionCodes;
  10. class AttributeSetManagementTest extends WebapiAbstract
  11. {
  12. /**
  13. * @var array
  14. */
  15. private $createServiceInfo;
  16. protected function setUp()
  17. {
  18. $this->createServiceInfo = [
  19. 'rest' => [
  20. 'resourcePath' => '/V1/products/attribute-sets',
  21. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST,
  22. ],
  23. 'soap' => [
  24. 'service' => 'catalogAttributeSetManagementV1',
  25. 'serviceVersion' => 'V1',
  26. 'operation' => 'catalogAttributeSetManagementV1Create',
  27. ],
  28. ];
  29. }
  30. public function testCreate()
  31. {
  32. $entityTypeCode = 'catalog_product';
  33. $entityType = $this->getEntityTypeByCode($entityTypeCode);
  34. $attributeSetName = 'new_attribute_set';
  35. $arguments = [
  36. 'attributeSet' => [
  37. 'attribute_set_name' => $attributeSetName,
  38. 'sort_order' => 500,
  39. ],
  40. 'skeletonId' => $entityType->getDefaultAttributeSetId(),
  41. ];
  42. $result = $this->_webApiCall($this->createServiceInfo, $arguments);
  43. $this->assertNotNull($result);
  44. $attributeSet = $this->getAttributeSetByName($attributeSetName);
  45. $this->assertNotNull($attributeSet);
  46. $this->assertEquals($attributeSet->getId(), $result['attribute_set_id']);
  47. $this->assertEquals($attributeSet->getAttributeSetName(), $result['attribute_set_name']);
  48. $this->assertEquals($attributeSet->getEntityTypeId(), $result['entity_type_id']);
  49. $this->assertEquals($attributeSet->getEntityTypeId(), $entityType->getId());
  50. $this->assertEquals($attributeSet->getSortOrder(), $result['sort_order']);
  51. $this->assertEquals($attributeSet->getSortOrder(), 500);
  52. // Clean up database
  53. $attributeSet->delete();
  54. }
  55. /**
  56. * @expectedException \Exception
  57. * @expectedExceptionMessage Invalid value
  58. */
  59. public function testCreateThrowsExceptionIfGivenAttributeSetAlreadyHasId()
  60. {
  61. $entityTypeCode = 'catalog_product';
  62. $entityType = $this->getEntityTypeByCode($entityTypeCode);
  63. $attributeSetName = 'new_attribute_set';
  64. $arguments = [
  65. 'attributeSet' => [
  66. 'attribute_set_id' => 1,
  67. 'attribute_set_name' => $attributeSetName,
  68. 'sort_order' => 100,
  69. ],
  70. 'skeletonId' => $entityType->getDefaultAttributeSetId(),
  71. ];
  72. $this->_webApiCall($this->createServiceInfo, $arguments);
  73. }
  74. /**
  75. * @expectedException \Exception
  76. */
  77. public function testCreateThrowsExceptionIfGivenSkeletonIdIsInvalid()
  78. {
  79. $attributeSetName = 'new_attribute_set';
  80. $arguments = [
  81. 'attributeSet' => [
  82. 'attribute_set_name' => $attributeSetName,
  83. 'sort_order' => 200,
  84. ],
  85. 'skeletonId' => 0,
  86. ];
  87. $this->_webApiCall($this->createServiceInfo, $arguments);
  88. $this->expectExceptionMessage(
  89. "The attribute set couldn't be created because it's based on a non-existing attribute set."
  90. );
  91. }
  92. /**
  93. * @expectedException \Exception
  94. */
  95. public function testCreateThrowsExceptionIfGivenSkeletonIdHasWrongEntityType()
  96. {
  97. $attributeSetName = 'new_attribute_set';
  98. $arguments = [
  99. 'attributeSet' => [
  100. 'attribute_set_name' => $attributeSetName,
  101. 'sort_order' => 200,
  102. ],
  103. 'skeletonId' => 7,
  104. ];
  105. $this->_webApiCall($this->createServiceInfo, $arguments);
  106. $this->expectExceptionMessage(
  107. "The attribute set couldn't be created because it's based on a non-product attribute set."
  108. );
  109. }
  110. /**
  111. * @expectedException \Exception
  112. */
  113. public function testCreateThrowsExceptionIfGivenSkeletonAttributeSetDoesNotExist()
  114. {
  115. $attributeSetName = 'new_attribute_set';
  116. $arguments = [
  117. 'attributeSet' => [
  118. 'attribute_set_name' => $attributeSetName,
  119. 'sort_order' => 300,
  120. ],
  121. 'skeletonId' => 9999,
  122. ];
  123. $this->_webApiCall($this->createServiceInfo, $arguments);
  124. $this->expectExceptionMessage(
  125. "The attribute set couldn't be created because it's based on a non-existing attribute set."
  126. );
  127. }
  128. /**
  129. * @expectedException \Exception
  130. * @expectedExceptionMessage The attribute set name is empty. Enter the name and try again.
  131. */
  132. public function testCreateThrowsExceptionIfAttributeSetNameIsEmpty()
  133. {
  134. $entityTypeCode = 'catalog_product';
  135. $entityType = $this->getEntityTypeByCode($entityTypeCode);
  136. $attributeSetName = '';
  137. $arguments = [
  138. 'attributeSet' => [
  139. 'attribute_set_name' => $attributeSetName,
  140. 'sort_order' => 500,
  141. ],
  142. 'skeletonId' => $entityType->getDefaultAttributeSetId(),
  143. ];
  144. $this->_webApiCall($this->createServiceInfo, $arguments);
  145. }
  146. public function testCreateThrowsExceptionIfAttributeSetWithGivenNameAlreadyExists()
  147. {
  148. $entityTypeCode = 'catalog_product';
  149. $entityType = $this->getEntityTypeByCode($entityTypeCode);
  150. $attributeSetName = 'Default';
  151. $expectedMessage = 'A "Default" attribute set name already exists. Create a new name and try again.';
  152. $arguments = [
  153. 'attributeSet' => [
  154. 'attribute_set_name' => $attributeSetName,
  155. 'sort_order' => 550,
  156. ],
  157. 'skeletonId' => $entityType->getDefaultAttributeSetId(),
  158. ];
  159. try {
  160. $this->_webApiCall($this->createServiceInfo, $arguments);
  161. $this->fail("Expected exception");
  162. } catch (\SoapFault $e) {
  163. $this->assertContains(
  164. $expectedMessage,
  165. $e->getMessage(),
  166. "SoapFault does not contain expected message."
  167. );
  168. } catch (\Exception $e) {
  169. $errorObj = $this->processRestExceptionResult($e);
  170. $this->assertEquals(
  171. $expectedMessage,
  172. $errorObj['message']
  173. );
  174. $this->assertEquals(HTTPExceptionCodes::HTTP_BAD_REQUEST, $e->getCode());
  175. }
  176. }
  177. /**
  178. * Retrieve attribute set based on given name.
  179. * This utility methods assumes that there is only one attribute set with given name,
  180. *
  181. * @param string $attributeSetName
  182. * @return \Magento\Eav\Model\Entity\Attribute\Set|null
  183. */
  184. protected function getAttributeSetByName($attributeSetName)
  185. {
  186. $objectManager = Bootstrap::getObjectManager();
  187. /** @var \Magento\Eav\Model\Entity\Attribute\Set $attributeSet */
  188. $attributeSet = $objectManager->create(\Magento\Eav\Model\Entity\Attribute\Set::class)
  189. ->load($attributeSetName, 'attribute_set_name');
  190. if ($attributeSet->getId() === null) {
  191. return null;
  192. }
  193. return $attributeSet;
  194. }
  195. /**
  196. * Retrieve entity type based on given code.
  197. *
  198. * @param string $entityTypeCode
  199. * @return \Magento\Eav\Model\Entity\Type|null
  200. */
  201. protected function getEntityTypeByCode($entityTypeCode)
  202. {
  203. $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  204. /** @var \Magento\Eav\Model\Entity\Type $entityType */
  205. $entityType = $objectManager->create(\Magento\Eav\Model\Config::class)
  206. ->getEntityType($entityTypeCode);
  207. return $entityType;
  208. }
  209. }