AttributeManagementTest.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Eav\Test\Unit\Model;
  7. use Magento\Eav\Model\AttributeManagement;
  8. use Magento\Framework\Exception\NoSuchEntityException;
  9. use Magento\Eav\Model\ResourceModel\Entity\Attribute\CollectionFactory;
  10. use Magento\Eav\Api\AttributeSetRepositoryInterface;
  11. use Magento\Eav\Model\ResourceModel\Entity\Attribute\Collection;
  12. use Magento\Eav\Model\Config;
  13. use Magento\Eav\Model\ConfigFactory;
  14. use Magento\Eav\Api\AttributeGroupRepositoryInterface;
  15. use Magento\Eav\Api\AttributeRepositoryInterface;
  16. use Magento\Eav\Model\ResourceModel\Entity\Attribute;
  17. /**
  18. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  19. */
  20. class AttributeManagementTest extends \PHPUnit\Framework\TestCase
  21. {
  22. /**
  23. * @var AttributeManagement
  24. */
  25. private $attributeManagement;
  26. /**
  27. * @var AttributeSetRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
  28. */
  29. private $setRepositoryMock;
  30. /**
  31. * @var Collection|\PHPUnit_Framework_MockObject_MockObject
  32. */
  33. private $attributeCollectionMock;
  34. /**
  35. * @var Config|\PHPUnit_Framework_MockObject_MockObject
  36. */
  37. private $eavConfigMock;
  38. /**
  39. * @var ConfigFactory|\PHPUnit_Framework_MockObject_MockObject
  40. */
  41. private $entityTypeFactoryMock;
  42. /**
  43. * @var AttributeGroupRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
  44. */
  45. private $groupRepositoryMock;
  46. /**
  47. * @var AttributeRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
  48. */
  49. private $attributeRepositoryMock;
  50. /**
  51. * @var Attribute|\PHPUnit_Framework_MockObject_MockObject
  52. */
  53. private $attributeResourceMock;
  54. /**
  55. * @var CollectionFactory|\PHPUnit_Framework_MockObject_MockObject
  56. */
  57. private $attributeCollectionFactoryMock;
  58. protected function setUp()
  59. {
  60. $this->setRepositoryMock =
  61. $this->createMock(AttributeSetRepositoryInterface::class);
  62. $this->attributeCollectionMock =
  63. $this->createMock(Collection::class);
  64. $this->eavConfigMock =
  65. $this->createMock(Config::class);
  66. $this->entityTypeFactoryMock =
  67. $this->createPartialMock(ConfigFactory::class, ['create', '__wakeup']);
  68. $this->groupRepositoryMock =
  69. $this->createMock(AttributeGroupRepositoryInterface::class);
  70. $this->attributeRepositoryMock =
  71. $this->createMock(AttributeRepositoryInterface::class);
  72. $this->attributeResourceMock =
  73. $this->createMock(Attribute::class);
  74. $this->attributeCollectionFactoryMock = $this->getMockBuilder(CollectionFactory::class)
  75. ->disableOriginalConstructor()
  76. ->getMock();
  77. $this->attributeManagement = new AttributeManagement(
  78. $this->setRepositoryMock,
  79. $this->attributeCollectionMock,
  80. $this->eavConfigMock,
  81. $this->entityTypeFactoryMock,
  82. $this->groupRepositoryMock,
  83. $this->attributeRepositoryMock,
  84. $this->attributeResourceMock,
  85. $this->attributeCollectionFactoryMock
  86. );
  87. }
  88. /**
  89. *
  90. * @expectedException \Magento\Framework\Exception\NoSuchEntityException
  91. * @expectedExceptionMessage The AttributeSet with a "2" ID doesn't exist. Verify the attributeSet and try again.
  92. */
  93. public function testAssignNoSuchEntityException()
  94. {
  95. $entityTypeCode = 1;
  96. $attributeSetId = 2;
  97. $attributeGroupId = 3;
  98. $attributeCode = 4;
  99. $sortOrder = 5;
  100. $this->setRepositoryMock->expects($this->once())
  101. ->method('get')
  102. ->with($attributeSetId)
  103. ->will($this->throwException(new \Magento\Framework\Exception\NoSuchEntityException()));
  104. $this->attributeManagement->assign(
  105. $entityTypeCode,
  106. $attributeSetId,
  107. $attributeGroupId,
  108. $attributeCode,
  109. $sortOrder
  110. );
  111. }
  112. /**
  113. *
  114. * @expectedException \Magento\Framework\Exception\InputException
  115. * @expectedExceptionMessage The attribute set ID is incorrect. Verify the ID and try again.
  116. */
  117. public function testAssignInputException()
  118. {
  119. $entityTypeCode = 1;
  120. $attributeSetId = 2;
  121. $attributeGroupId = 3;
  122. $attributeCode = 4;
  123. $sortOrder = 5;
  124. $attributeSetMock = $this->createMock(\Magento\Eav\Api\Data\AttributeSetInterface::class);
  125. $this->setRepositoryMock->expects($this->once())
  126. ->method('get')
  127. ->with($attributeSetId)
  128. ->willReturn($attributeSetMock);
  129. $this->entityTypeFactoryMock->expects($this->once())->method('create')->willReturn($this->eavConfigMock);
  130. $attributeSetMock->expects($this->once())->method('getEntityTypeId')->willReturn(66);
  131. $entityTypeMock = $this->createMock(\Magento\Eav\Model\Entity\Type::class);
  132. $this->eavConfigMock->expects($this->once())->method('getEntityType')->with(66)->willReturn($entityTypeMock);
  133. $entityTypeMock->expects($this->once())->method('getEntityTypeCode')->willReturn($entityTypeCode+1);
  134. $this->attributeManagement->assign(
  135. $entityTypeCode,
  136. $attributeSetId,
  137. $attributeGroupId,
  138. $attributeCode,
  139. $sortOrder
  140. );
  141. }
  142. /**
  143. *
  144. * @expectedException \Magento\Framework\Exception\InputException
  145. * @expectedExceptionMessage The attribute group doesn't belong to the attribute set.
  146. */
  147. public function testAssignInputExceptionGroupInSet()
  148. {
  149. $entityTypeCode = 1;
  150. $attributeSetId = 2;
  151. $attributeGroupId = 3;
  152. $attributeCode = 4;
  153. $sortOrder = 5;
  154. $attributeSetMock = $this->createMock(\Magento\Eav\Api\Data\AttributeSetInterface::class);
  155. $this->setRepositoryMock->expects($this->once())
  156. ->method('get')
  157. ->with($attributeSetId)
  158. ->willReturn($attributeSetMock);
  159. $this->entityTypeFactoryMock->expects($this->once())->method('create')->willReturn($this->eavConfigMock);
  160. $attributeSetMock->expects($this->once())->method('getEntityTypeId')->willReturn(66);
  161. $entityTypeMock = $this->createMock(\Magento\Eav\Model\Entity\Type::class);
  162. $this->eavConfigMock->expects($this->once())->method('getEntityType')->with(66)->willReturn($entityTypeMock);
  163. $entityTypeMock->expects($this->once())->method('getEntityTypeCode')->willReturn($entityTypeCode);
  164. $attributeGroup = $this->getMockBuilder(\Magento\Eav\Api\Data\AttributeGroupInterface::class)
  165. ->setMethods(['getAttributeSetId'])
  166. ->disableOriginalConstructor()
  167. ->getMockForAbstractClass();
  168. $this->groupRepositoryMock->expects($this->once())->method('get')->willReturn($attributeGroup);
  169. $attributeGroup->expects($this->once())->method('getAttributeSetId')->willReturn($attributeSetId + 1);
  170. $this->attributeManagement->assign(
  171. $entityTypeCode,
  172. $attributeSetId,
  173. $attributeGroupId,
  174. $attributeCode,
  175. $sortOrder
  176. );
  177. }
  178. public function testAssign()
  179. {
  180. $entityTypeCode = 1;
  181. $attributeSetId = 2;
  182. $attributeGroupId = 3;
  183. $attributeCode = 4;
  184. $sortOrder = 5;
  185. $attributeSetMock = $this->createMock(\Magento\Eav\Api\Data\AttributeSetInterface::class);
  186. $this->setRepositoryMock->expects($this->once())
  187. ->method('get')
  188. ->with($attributeSetId)
  189. ->willReturn($attributeSetMock);
  190. $this->entityTypeFactoryMock->expects($this->once())->method('create')->willReturn($this->eavConfigMock);
  191. $attributeSetMock->expects($this->once())->method('getEntityTypeId')->willReturn(66);
  192. $entityTypeMock = $this->createMock(\Magento\Eav\Model\Entity\Type::class);
  193. $this->eavConfigMock->expects($this->once())->method('getEntityType')->with(66)->willReturn($entityTypeMock);
  194. $entityTypeMock->expects($this->once())->method('getEntityTypeCode')->willReturn($entityTypeCode);
  195. $attributeMock = $this->createMock(\Magento\Eav\Model\Attribute::class);
  196. $this->attributeRepositoryMock->expects($this->once())
  197. ->method('get')
  198. ->with($entityTypeCode, $attributeCode)
  199. ->willReturn($attributeMock);
  200. $attributeMock->expects($this->once())->method('getAttributeId')->willReturn(16);
  201. $this->attributeResourceMock->expects($this->once())->method('saveInSetIncluding')
  202. ->with(
  203. $attributeMock,
  204. 16,
  205. $attributeSetId,
  206. $attributeGroupId,
  207. $sortOrder
  208. )
  209. ->willReturn($attributeMock);
  210. $attributeMock->expects($this->once())->method('setAttributeSetId')->with($attributeSetId)->willReturnSelf();
  211. $attributeMock->expects($this->once())->method('loadEntityAttributeIdBySet')->willReturnSelf();
  212. $attributeMock->expects($this->once())->method('getData')->with('entity_attribute_id')->willReturnSelf();
  213. $attributeGroup = $this->getMockBuilder(\Magento\Eav\Api\Data\AttributeGroupInterface::class)
  214. ->setMethods(['getAttributeSetId'])
  215. ->disableOriginalConstructor()
  216. ->getMockForAbstractClass();
  217. $this->groupRepositoryMock->expects($this->once())->method('get')->willReturn($attributeGroup);
  218. $attributeGroup->expects($this->once())->method('getAttributeSetId')->willReturn($attributeSetId);
  219. $this->assertEquals(
  220. $attributeMock,
  221. $this->attributeManagement->assign(
  222. $entityTypeCode,
  223. $attributeSetId,
  224. $attributeGroupId,
  225. $attributeCode,
  226. $sortOrder
  227. )
  228. );
  229. }
  230. public function testUnassign()
  231. {
  232. $attributeSetId = 1;
  233. $attributeCode = 'code';
  234. $attributeSetMock = $this->createMock(\Magento\Eav\Api\Data\AttributeSetInterface::class);
  235. $this->setRepositoryMock->expects($this->once())
  236. ->method('get')
  237. ->with($attributeSetId)
  238. ->willReturn($attributeSetMock);
  239. $this->entityTypeFactoryMock->expects($this->once())->method('create')->willReturn($this->eavConfigMock);
  240. $attributeSetMock->expects($this->once())->method('getEntityTypeId')->willReturn(66);
  241. $entityTypeMock = $this->createMock(\Magento\Eav\Model\Entity\Type::class);
  242. $this->eavConfigMock->expects($this->once())->method('getEntityType')->with(66)->willReturn($entityTypeMock);
  243. $attributeMock = $this->createPartialMock(
  244. \Magento\Eav\Model\Entity\Attribute::class,
  245. [
  246. 'getEntityAttributeId',
  247. 'setAttributeSetId',
  248. 'loadEntityAttributeIdBySet',
  249. 'getIsUserDefined',
  250. 'deleteEntity',
  251. '__wakeup'
  252. ]
  253. );
  254. $entityTypeMock->expects($this->once())->method('getEntityTypeCode')->willReturn('entity type code');
  255. $this->attributeRepositoryMock->expects($this->once())
  256. ->method('get')
  257. ->with('entity type code', $attributeCode)
  258. ->willReturn($attributeMock);
  259. $attributeSetMock->expects($this->once())->method('getAttributeSetId')->willReturn(33);
  260. $attributeMock->expects($this->once())->method('setAttributeSetId')->with(33)->willReturnSelf();
  261. $attributeMock->expects($this->once())->method('loadEntityAttributeIdBySet')->willReturnSelf();
  262. $attributeMock->expects($this->once())->method('getEntityAttributeId')->willReturn(12);
  263. $attributeMock->expects($this->once())->method('getIsUserDefined')->willReturn(true);
  264. $attributeMock->expects($this->once())->method('deleteEntity')->willReturnSelf();
  265. $this->assertTrue($this->attributeManagement->unassign($attributeSetId, $attributeCode));
  266. }
  267. /**
  268. * @expectedException \Magento\Framework\Exception\InputException
  269. */
  270. public function testUnassignInputException()
  271. {
  272. $attributeSetId = 1;
  273. $attributeCode = 'code';
  274. $attributeSetMock = $this->createMock(\Magento\Eav\Api\Data\AttributeSetInterface::class);
  275. $this->setRepositoryMock->expects($this->once())
  276. ->method('get')
  277. ->with($attributeSetId)
  278. ->willReturn($attributeSetMock);
  279. $this->entityTypeFactoryMock->expects($this->once())->method('create')->willReturn($this->eavConfigMock);
  280. $attributeSetMock->expects($this->once())->method('getEntityTypeId')->willReturn(66);
  281. $entityTypeMock = $this->createMock(\Magento\Eav\Model\Entity\Type::class);
  282. $this->eavConfigMock->expects($this->once())->method('getEntityType')->with(66)->willReturn($entityTypeMock);
  283. $attributeMock = $this->createPartialMock(
  284. \Magento\Eav\Model\Entity\Attribute::class,
  285. [
  286. 'getEntityAttributeId',
  287. 'setAttributeSetId',
  288. 'loadEntityAttributeIdBySet',
  289. 'getIsUserDefined',
  290. 'deleteEntity',
  291. '__wakeup'
  292. ]
  293. );
  294. $entityTypeMock->expects($this->once())->method('getEntityTypeCode')->willReturn('entity type code');
  295. $this->attributeRepositoryMock->expects($this->once())
  296. ->method('get')
  297. ->with('entity type code', $attributeCode)
  298. ->willReturn($attributeMock);
  299. $attributeSetMock->expects($this->once())->method('getAttributeSetId')->willReturn($attributeSetId);
  300. $attributeMock->expects($this->once())->method('setAttributeSetId')->with($attributeSetId)->willReturnSelf();
  301. $attributeMock->expects($this->once())->method('loadEntityAttributeIdBySet')->willReturnSelf();
  302. $attributeMock->expects($this->once())->method('getEntityAttributeId')->willReturn(null);
  303. $attributeMock->expects($this->never())->method('getIsUserDefined');
  304. $attributeMock->expects($this->never())->method('deleteEntity');
  305. $this->attributeManagement->unassign($attributeSetId, $attributeCode);
  306. $this->expectExceptionMessage(
  307. 'The "code" attribute wasn\'t found in the "1" attribute set. Enter the attribute and try again.'
  308. );
  309. }
  310. /**
  311. * @expectedException \Magento\Framework\Exception\NoSuchEntityException
  312. * @expectedExceptionMessage The "1234567" attribute set wasn't found. Verify and try again.
  313. */
  314. public function testUnassignWithWrongAttributeSet()
  315. {
  316. $attributeSetId = 1234567;
  317. $attributeCode = 'code';
  318. $this->setRepositoryMock->expects($this->once())
  319. ->method('get')
  320. ->with($attributeSetId)
  321. ->willThrowException(new NoSuchEntityException(__('hello')));
  322. $this->attributeManagement->unassign($attributeSetId, $attributeCode);
  323. }
  324. /**
  325. * @expectedException \Magento\Framework\Exception\StateException
  326. * @expectedExceptionMessage The system attribute can't be deleted.
  327. */
  328. public function testUnassignStateException()
  329. {
  330. $attributeSetId = 1;
  331. $attributeCode = 'code';
  332. $attributeSetMock = $this->createMock(\Magento\Eav\Api\Data\AttributeSetInterface::class);
  333. $this->setRepositoryMock->expects($this->once())
  334. ->method('get')
  335. ->with($attributeSetId)
  336. ->willReturn($attributeSetMock);
  337. $this->entityTypeFactoryMock->expects($this->once())->method('create')->willReturn($this->eavConfigMock);
  338. $attributeSetMock->expects($this->once())->method('getEntityTypeId')->willReturn(66);
  339. $entityTypeMock = $this->createMock(\Magento\Eav\Model\Entity\Type::class);
  340. $this->eavConfigMock->expects($this->once())->method('getEntityType')->with(66)->willReturn($entityTypeMock);
  341. $attributeMock = $this->createPartialMock(
  342. \Magento\Eav\Model\Entity\Attribute::class,
  343. [
  344. 'getEntityAttributeId',
  345. 'setAttributeSetId',
  346. 'loadEntityAttributeIdBySet',
  347. 'getIsUserDefined',
  348. 'deleteEntity',
  349. '__wakeup'
  350. ]
  351. );
  352. $entityTypeMock->expects($this->once())->method('getEntityTypeCode')->willReturn('entity type code');
  353. $this->attributeRepositoryMock->expects($this->once())
  354. ->method('get')
  355. ->with('entity type code', $attributeCode)
  356. ->willReturn($attributeMock);
  357. $attributeSetMock->expects($this->once())->method('getAttributeSetId')->willReturn($attributeSetId);
  358. $attributeMock->expects($this->once())->method('setAttributeSetId')->with($attributeSetId)->willReturnSelf();
  359. $attributeMock->expects($this->once())->method('loadEntityAttributeIdBySet')->willReturnSelf();
  360. $attributeMock->expects($this->once())->method('getEntityAttributeId')->willReturn(12);
  361. $attributeMock->expects($this->once())->method('getIsUserDefined')->willReturn(null);
  362. $attributeMock->expects($this->never())->method('deleteEntity');
  363. $this->attributeManagement->unassign($attributeSetId, $attributeCode);
  364. }
  365. public function testGetAttributes()
  366. {
  367. $entityType = 'type';
  368. $attributeSetId = 148;
  369. $attributeCollectionFactoryMock = $this->createPartialMock(
  370. \Magento\Eav\Model\ResourceModel\Entity\Attribute\CollectionFactory::class,
  371. ['create']
  372. );
  373. $attributeCollectionFactoryMock->expects($this->once())
  374. ->method('create')
  375. ->willReturn($this->attributeCollectionMock);
  376. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  377. $objectManager->setBackwardCompatibleProperty(
  378. $this->attributeManagement,
  379. 'attributeCollectionFactory',
  380. $attributeCollectionFactoryMock
  381. );
  382. $attributeSetMock = $this->createMock(\Magento\Eav\Api\Data\AttributeSetInterface::class);
  383. $this->setRepositoryMock->expects($this->once())
  384. ->method('get')
  385. ->with($attributeSetId)
  386. ->willReturn($attributeSetMock);
  387. $entityTypeMock = $this->createMock(\Magento\Eav\Model\Entity\Type::class);
  388. $this->eavConfigMock->expects($this->once())
  389. ->method('getEntityType')
  390. ->with($entityType)
  391. ->willReturn($entityTypeMock);
  392. $entityTypeMock->expects($this->once())->method('getId')->willReturn(88);
  393. $attributeSetMock->expects($this->exactly(2))->method('getAttributeSetId')->willReturn(88);
  394. $attributeSetMock->expects($this->once())->method('getEntityTypeId')->willReturn(88);
  395. $this->attributeCollectionMock->expects($this->once())
  396. ->method('setAttributeSetFilter')
  397. ->with(88)
  398. ->willReturnSelf();
  399. $attributeMock = $this->createMock(\Magento\Eav\Model\Entity\Attribute::class);
  400. $this->attributeCollectionMock->expects($this->once())->method('load')->willReturnSelf();
  401. $this->attributeCollectionMock->expects($this->once())->method('getItems')->willReturn([$attributeMock]);
  402. $this->assertEquals([$attributeMock], $this->attributeManagement->getAttributes($entityType, $attributeSetId));
  403. }
  404. /**
  405. * @expectedException \Magento\Framework\Exception\NoSuchEntityException
  406. * @expectedExceptionMessage No such entity with attributeSetId = 148
  407. */
  408. public function testGetAttributesNoSuchEntityException()
  409. {
  410. $entityType = 'type';
  411. $attributeSetId = 148;
  412. $attributeSetMock = $this->createMock(\Magento\Eav\Api\Data\AttributeSetInterface::class);
  413. $this->setRepositoryMock->expects($this->once())
  414. ->method('get')
  415. ->with($attributeSetId)
  416. ->willReturn($attributeSetMock);
  417. $entityTypeMock = $this->createMock(\Magento\Eav\Model\Entity\Type::class);
  418. $this->eavConfigMock->expects($this->once())
  419. ->method('getEntityType')
  420. ->with($entityType)
  421. ->willReturn($entityTypeMock);
  422. $entityTypeMock->expects($this->once())->method('getId')->willReturn(77);
  423. $attributeSetMock->expects($this->once())->method('getAttributeSetId')->willReturn(88);
  424. $attributeSetMock->expects($this->once())->method('getEntityTypeId')->willReturn(88);
  425. $this->attributeManagement->getAttributes($entityType, $attributeSetId);
  426. }
  427. }