AttributeSetRepositoryTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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\Framework\Api\FilterBuilder;
  8. use Magento\Framework\Api\SearchCriteriaBuilder;
  9. use Magento\Framework\Api\SortOrder;
  10. use Magento\Framework\Api\SortOrderBuilder;
  11. use Magento\TestFramework\Helper\Bootstrap;
  12. use Magento\TestFramework\TestCase\WebapiAbstract;
  13. class AttributeSetRepositoryTest extends WebapiAbstract
  14. {
  15. /**
  16. * @magentoApiDataFixture Magento/Eav/_files/empty_attribute_set.php
  17. */
  18. public function testGet()
  19. {
  20. $attributeSetName = 'empty_attribute_set';
  21. $attributeSet = $this->getAttributeSetByName($attributeSetName);
  22. $attributeSetId = $attributeSet->getId();
  23. $serviceInfo = [
  24. 'rest' => [
  25. 'resourcePath' => '/V1/eav/attribute-sets/' . $attributeSetId,
  26. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
  27. ],
  28. 'soap' => [
  29. 'service' => 'eavAttributeSetRepositoryV1',
  30. 'serviceVersion' => 'V1',
  31. 'operation' => 'eavAttributeSetRepositoryV1Get',
  32. ],
  33. ];
  34. $arguments = [
  35. 'attributeSetId' => $attributeSetId,
  36. ];
  37. $result = $this->_webApiCall($serviceInfo, $arguments);
  38. $this->assertNotNull($result);
  39. $this->assertEquals($attributeSet->getId(), $result['attribute_set_id']);
  40. $this->assertEquals($attributeSet->getAttributeSetName(), $result['attribute_set_name']);
  41. $this->assertEquals($attributeSet->getEntityTypeId(), $result['entity_type_id']);
  42. $this->assertEquals($attributeSet->getSortOrder(), $result['sort_order']);
  43. }
  44. /**
  45. * @expectedException \Exception
  46. */
  47. public function testGetThrowsExceptionIfRequestedAttributeSetDoesNotExist()
  48. {
  49. $attributeSetId = 9999;
  50. $serviceInfo = [
  51. 'rest' => [
  52. 'resourcePath' => '/V1/eav/attribute-sets/' . $attributeSetId,
  53. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
  54. ],
  55. 'soap' => [
  56. 'service' => 'eavAttributeSetRepositoryV1',
  57. 'serviceVersion' => 'V1',
  58. 'operation' => 'eavAttributeSetRepositoryV1Get',
  59. ],
  60. ];
  61. $arguments = [
  62. 'attributeSetId' => $attributeSetId,
  63. ];
  64. $this->_webApiCall($serviceInfo, $arguments);
  65. }
  66. /**
  67. * @magentoApiDataFixture Magento/Eav/_files/empty_attribute_set.php
  68. */
  69. public function testSave()
  70. {
  71. $attributeSetName = 'empty_attribute_set';
  72. $attributeSet = $this->getAttributeSetByName($attributeSetName);
  73. $serviceInfo = [
  74. 'rest' => [
  75. 'resourcePath' => '/V1/eav/attribute-sets/' . $attributeSet->getId(),
  76. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT,
  77. ],
  78. 'soap' => [
  79. 'service' => 'eavAttributeSetRepositoryV1',
  80. 'serviceVersion' => 'V1',
  81. 'operation' => 'eavAttributeSetRepositoryV1Save',
  82. ],
  83. ];
  84. $updatedSortOrder = $attributeSet->getSortOrder() + 200;
  85. $arguments = [
  86. 'attributeSet' => [
  87. 'attribute_set_id' => $attributeSet->getId(),
  88. // name is the same, because it is used by fixture rollback script
  89. 'attribute_set_name' => $attributeSet->getAttributeSetName(),
  90. 'entity_type_id' => $attributeSet->getEntityTypeId(),
  91. 'sort_order' => $updatedSortOrder,
  92. ],
  93. ];
  94. $result = $this->_webApiCall($serviceInfo, $arguments);
  95. $this->assertNotNull($result);
  96. // Reload attribute set data
  97. $attributeSet = $this->getAttributeSetByName($attributeSetName);
  98. $this->assertEquals($attributeSet->getAttributeSetId(), $result['attribute_set_id']);
  99. $this->assertEquals($attributeSet->getAttributeSetName(), $result['attribute_set_name']);
  100. $this->assertEquals($attributeSet->getEntityTypeId(), $result['entity_type_id']);
  101. $this->assertEquals($updatedSortOrder, $result['sort_order']);
  102. $this->assertEquals($attributeSet->getSortOrder(), $result['sort_order']);
  103. }
  104. /**
  105. * @magentoApiDataFixture Magento/Eav/_files/empty_attribute_set.php
  106. */
  107. public function testDeleteById()
  108. {
  109. $attributeSetName = 'empty_attribute_set';
  110. $attributeSet = $this->getAttributeSetByName($attributeSetName);
  111. $attributeSetId = $attributeSet->getId();
  112. $serviceInfo = [
  113. 'rest' => [
  114. 'resourcePath' => '/V1/eav/attribute-sets/' . $attributeSetId,
  115. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_DELETE,
  116. ],
  117. 'soap' => [
  118. 'service' => 'eavAttributeSetRepositoryV1',
  119. 'serviceVersion' => 'V1',
  120. 'operation' => 'eavAttributeSetRepositoryV1DeleteById',
  121. ],
  122. ];
  123. $arguments = [
  124. 'attributeSetId' => $attributeSetId,
  125. ];
  126. $this->assertTrue($this->_webApiCall($serviceInfo, $arguments));
  127. $this->assertNull($this->getAttributeSetByName($attributeSetName));
  128. }
  129. /**
  130. * @expectedException \Exception
  131. * @expectedExceptionMessage The default attribute set can't be deleted.
  132. */
  133. public function testDeleteByIdDefaultAttributeSet()
  134. {
  135. $objectManager = Bootstrap::getObjectManager();
  136. /** @var \Magento\Eav\Model\Config */
  137. $eavConfig = $objectManager->create(\Magento\Eav\Model\Config::class);
  138. $defaultAttributeSetId = $eavConfig
  139. ->getEntityType(\Magento\Catalog\Api\Data\ProductAttributeInterface::ENTITY_TYPE_CODE)
  140. ->getDefaultAttributeSetId();
  141. $serviceInfo = [
  142. 'rest' => [
  143. 'resourcePath' => '/V1/eav/attribute-sets/' . $defaultAttributeSetId,
  144. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_DELETE,
  145. ],
  146. 'soap' => [
  147. 'service' => 'eavAttributeSetRepositoryV1',
  148. 'serviceVersion' => 'V1',
  149. 'operation' => 'eavAttributeSetRepositoryV1DeleteById',
  150. ],
  151. ];
  152. $arguments = [
  153. 'attributeSetId' => $defaultAttributeSetId,
  154. ];
  155. $this->assertTrue($this->_webApiCall($serviceInfo, $arguments));
  156. }
  157. /**
  158. * @expectedException \Exception
  159. */
  160. public function testDeleteByIdThrowsExceptionIfRequestedAttributeSetDoesNotExist()
  161. {
  162. $attributeSetId = 9999;
  163. $serviceInfo = [
  164. 'rest' => [
  165. 'resourcePath' => '/V1/eav/attribute-sets/' . $attributeSetId,
  166. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_DELETE,
  167. ],
  168. 'soap' => [
  169. 'service' => 'eavAttributeSetRepositoryV1',
  170. 'serviceVersion' => 'V1',
  171. 'operation' => 'eavAttributeSetRepositoryV1DeleteById',
  172. ],
  173. ];
  174. $arguments = [
  175. 'attributeSetId' => $attributeSetId,
  176. ];
  177. $this->_webApiCall($serviceInfo, $arguments);
  178. }
  179. /**
  180. * @magentoApiDataFixture Magento/Eav/_files/attribute_set_for_search.php
  181. */
  182. public function testGetList()
  183. {
  184. /** @var SearchCriteriaBuilder $searchCriteriaBuilder */
  185. $searchCriteriaBuilder = Bootstrap::getObjectManager()
  186. ->create(SearchCriteriaBuilder::class);
  187. /** @var FilterBuilder $filterBuilder */
  188. $filterBuilder = Bootstrap::getObjectManager()->create(FilterBuilder::class);
  189. $filter1 = $filterBuilder
  190. ->setField('entity_type_code')
  191. ->setValue('catalog_product')
  192. ->create();
  193. $filter2 = $filterBuilder
  194. ->setField('sort_order')
  195. ->setValue(200)
  196. ->setConditionType('gteq')
  197. ->create();
  198. $filter3 = $filterBuilder
  199. ->setField('sort_order')
  200. ->setValue(300)
  201. ->setConditionType('lteq')
  202. ->create();
  203. $searchCriteriaBuilder->addFilters([$filter1, $filter2]);
  204. $searchCriteriaBuilder->addFilters([$filter3]);
  205. /** @var SortOrderBuilder $sortOrderBuilder */
  206. $sortOrderBuilder = Bootstrap::getObjectManager()->create(SortOrderBuilder::class);
  207. /** @var SortOrder $sortOrder */
  208. $sortOrder = $sortOrderBuilder->setField('sort_order')
  209. ->setDirection(SortOrder::SORT_ASC)
  210. ->create();
  211. $searchCriteriaBuilder->setSortOrders([$sortOrder]);
  212. $searchCriteriaBuilder->setPageSize(1);
  213. $searchCriteriaBuilder->setCurrentPage(2);
  214. $searchData = $searchCriteriaBuilder->create()->__toArray();
  215. $requestData = ['searchCriteria' => $searchData];
  216. $serviceInfo = [
  217. 'rest' => [
  218. 'resourcePath' => '/V1/eav/attribute-sets/list' . '?' . http_build_query($requestData),
  219. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
  220. ],
  221. 'soap' => [
  222. 'service' => 'eavAttributeSetRepositoryV1',
  223. 'serviceVersion' => 'V1',
  224. 'operation' => 'eavAttributeSetRepositoryV1GetList',
  225. ],
  226. ];
  227. $searchResult = $this->_webApiCall($serviceInfo, $requestData);
  228. $this->assertEquals(2, $searchResult['total_count']);
  229. $this->assertEquals(1, count($searchResult['items']));
  230. $this->assertEquals(
  231. $searchResult['items'][0]['attribute_set_name'],
  232. 'attribute_set_3_for_search'
  233. );
  234. }
  235. /**
  236. * Retrieve attribute set based on given name.
  237. * This utility methods assumes that there is only one attribute set with given name,
  238. *
  239. * @param string $attributeSetName
  240. * @return \Magento\Eav\Model\Entity\Attribute\Set|null
  241. */
  242. protected function getAttributeSetByName($attributeSetName)
  243. {
  244. $objectManager = Bootstrap::getObjectManager();
  245. /** @var \Magento\Eav\Model\Entity\Attribute\Set $attributeSet */
  246. $attributeSet = $objectManager->create(\Magento\Eav\Model\Entity\Attribute\Set::class)
  247. ->load($attributeSetName, 'attribute_set_name');
  248. if ($attributeSet->getId() === null) {
  249. return null;
  250. }
  251. return $attributeSet;
  252. }
  253. }