AttributeSetManagementTest.php 8.1 KB

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