Skip to content

Commit 960732a

Browse files
author
Vaha
committed
added improvements to galleryManagement 'create' method to set all image roles for first product entity
1 parent 5a9fcab commit 960732a

6 files changed

+106
-0
lines changed

app/code/Magento/Catalog/Model/Product/Gallery/GalleryManagement.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ public function create($sku, ProductAttributeMediaGalleryEntryInterface $entry)
6161
$existingMediaGalleryEntries = $product->getMediaGalleryEntries();
6262
$existingEntryIds = [];
6363
if ($existingMediaGalleryEntries == null) {
64+
// set all media types if not specified
65+
if ($entry->getTypes() == null) {
66+
$entry->setTypes(array_keys($product->getMediaAttributes()));
67+
}
6468
$existingMediaGalleryEntries = [$entry];
6569
} else {
6670
foreach ($existingMediaGalleryEntries as $existingEntries) {
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
9+
<actionGroups xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
10+
xsi:noNamespaceSchemaLocation="urn:magento:mftf:Test/etc/actionGroupSchema.xsd">
11+
<actionGroup name="AdminOpenProductImagesSectionActionGroup">
12+
<annotations>
13+
<description>Requires the navigation to the Product page. Opens 'Image and Videos' section.</description>
14+
</annotations>
15+
<conditionalClick selector="{{AdminProductImagesSection.productImagesToggle}}" dependentSelector="{{AdminProductImagesSection.imageUploadButton}}" visible="false" stepKey="openProductImagesSection"/>
16+
<waitForElementVisible selector="{{AdminProductImagesSection.imageUploadButton}}" stepKey="waitForImageUploadButton"/>
17+
</actionGroup>
18+
</actionGroups>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
9+
<actionGroups xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
10+
xsi:noNamespaceSchemaLocation="urn:magento:mftf:Test/etc/actionGroupSchema.xsd">
11+
<actionGroup name="AssertAdminProductImageRolesSelectedActionGroup">
12+
<annotations>
13+
<description>Requires the navigation to the Product page and opened 'Image and Videos' section.
14+
Checks the Base, Small, Thumbnail and Swatch Roles are selected for provided image.</description>
15+
</annotations>
16+
<arguments>
17+
<argument name="imageFileName" type="string" defaultValue="test_image"/>
18+
</arguments>
19+
<waitForElementVisible selector="{{AdminProductImagesSection.imageFile(imageFileName)}}" stepKey="seeProductImageName"/>
20+
<click selector="{{AdminProductImagesSection.imageFile(imageFileName)}}" stepKey="clickProductImage"/>
21+
<waitForElementVisible selector="{{AdminProductImagesSection.isBaseSelected}}" stepKey="checkRoleBaseSelected"/>
22+
<waitForElementVisible selector="{{AdminProductImagesSection.isSmallSelected}}" stepKey="checkRoleSmallSelected"/>
23+
<waitForElementVisible selector="{{AdminProductImagesSection.isThumbnailSelected}}" stepKey="checkRoleThumbnailSelected"/>
24+
<waitForElementVisible selector="{{AdminProductImagesSection.isSwatchSelected}}" stepKey="checkRoleSwatchSelected"/>
25+
</actionGroup>
26+
</actionGroups>

app/code/Magento/Catalog/Test/Mftf/Data/ProductAttributeMediaGalleryEntryData.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,11 @@
3535
<data key="label">Magento Logo</data>
3636
<requiredEntity type="ImageContent">MagentoLogoImageContentExportImport</requiredEntity>
3737
</entity>
38+
<entity name="ApiProductAttributeMediaGalleryEntryWithoutTypesTestImage" type="ProductAttributeMediaGalleryEntry">
39+
<data key="media_type">image</data>
40+
<data key="label" unique="suffix">Test Image</data>
41+
<data key="position">0</data>
42+
<data key="disabled">false</data>
43+
<requiredEntity type="ImageContent">TestImageContent</requiredEntity>
44+
</entity>
3845
</entities>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
9+
<tests xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
10+
xsi:noNamespaceSchemaLocation="urn:magento:mftf:Test/etc/testSchema.xsd">
11+
<test name="AdminCheckMediaRolesForFirstAddedImageViaApiTest">
12+
<annotations>
13+
<stories value="Add Simple Product with image via API"/>
14+
<title value="Check that added image for created product has selected image roles."/>
15+
<description value="Login as admin, create simple product, add image to created product (via API).Go to
16+
Admin Product Edit page for created product to check that added image has selected image roles."/>
17+
<group value="catalog"/>
18+
</annotations>
19+
<before>
20+
<actionGroup ref="AdminLoginActionGroup" stepKey="loginToAdminPanel"/>
21+
<createData entity="SimpleOutOfStockProduct" stepKey="createSimpleProduct"/>
22+
<createData entity="ApiProductAttributeMediaGalleryEntryWithoutTypesTestImage" stepKey="createSimpleProductImage">
23+
<requiredEntity createDataKey="createSimpleProduct"/>
24+
</createData>
25+
</before>
26+
<after>
27+
<deleteData createDataKey="createSimpleProduct" stepKey="deleteSimpleProduct"/>
28+
<actionGroup ref="logout" stepKey="logout"/>
29+
<!-- Reindex invalidated indices after product attribute has been created/deleted -->
30+
<actionGroup ref="CliRunReindexUsingCronJobsActionGroup" stepKey="reindexInvalidatedIndices"/>
31+
</after>
32+
33+
<actionGroup ref="GoToProductPageViaIDActionGroup" stepKey="goToSimpleProduct">
34+
<argument name="productId" value="$$createSimpleProduct.id$$"/>
35+
</actionGroup>
36+
<actionGroup ref="AdminOpenProductImagesSectionActionGroup" stepKey="openProductImagesSection"/>
37+
<actionGroup ref="AssertAdminProductImageRolesSelectedActionGroup" stepKey="checkImageRolesSelected">
38+
<argument name="imageFileName" value="$createSimpleProductImage.entry[content][name]$"/>
39+
</actionGroup>
40+
</test>
41+
</tests>

app/code/Magento/Catalog/Test/Unit/Model/Product/Gallery/GalleryManagementTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ protected function setUp()
5959
'getCustomAttribute',
6060
'getMediaGalleryEntries',
6161
'setMediaGalleryEntries',
62+
'getMediaAttributes',
6263
]
6364
);
6465
$this->mediaGalleryEntryMock =
@@ -99,6 +100,9 @@ public function testCreateWithCannotSaveException()
99100
$entryContentMock = $this->getMockBuilder(\Magento\Framework\Api\Data\ImageContentInterface::class)
100101
->disableOriginalConstructor()
101102
->getMock();
103+
$attributeMock = $this->getMockBuilder(\Magento\Eav\Model\Entity\Attribute\AbstractAttribute::class)
104+
->disableOriginalConstructor()
105+
->getMock();
102106
$this->mediaGalleryEntryMock->expects($this->any())->method('getContent')->willReturn($entryContentMock);
103107
$this->productRepositoryMock->expects($this->once())
104108
->method('get')
@@ -108,6 +112,10 @@ public function testCreateWithCannotSaveException()
108112
$this->contentValidatorMock->expects($this->once())->method('isValid')->with($entryContentMock)
109113
->willReturn(true);
110114

115+
$this->productMock->expects($this->any())
116+
->method('getMediaAttributes')
117+
->willReturn(['small_image' => $attributeMock]);
118+
111119
$this->productRepositoryMock->expects($this->once())->method('save')->with($this->productMock)
112120
->willThrowException(new \Exception());
113121
$this->model->create($productSku, $this->mediaGalleryEntryMock);
@@ -133,6 +141,8 @@ public function testCreate()
133141
$this->contentValidatorMock->expects($this->once())->method('isValid')->with($entryContentMock)
134142
->willReturn(true);
135143

144+
$this->mediaGalleryEntryMock->expects($this->any())->method('getTypes')->willReturn(['small_image']);
145+
136146
$newEntryMock = $this->createMock(\Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterface::class);
137147
$newEntryMock->expects($this->exactly(2))->method('getId')->willReturn(42);
138148
$this->productMock->expects($this->at(2))->method('getMediaGalleryEntries')

0 commit comments

Comments
 (0)