diff --git a/app/code/Magento/Catalog/Model/Product/Gallery/GalleryManagement.php b/app/code/Magento/Catalog/Model/Product/Gallery/GalleryManagement.php
index a9afb7cec45e2..6a078a915119c 100644
--- a/app/code/Magento/Catalog/Model/Product/Gallery/GalleryManagement.php
+++ b/app/code/Magento/Catalog/Model/Product/Gallery/GalleryManagement.php
@@ -61,6 +61,10 @@ public function create($sku, ProductAttributeMediaGalleryEntryInterface $entry)
$existingMediaGalleryEntries = $product->getMediaGalleryEntries();
$existingEntryIds = [];
if ($existingMediaGalleryEntries == null) {
+ // set all media types if not specified
+ if ($entry->getTypes() == null) {
+ $entry->setTypes(array_keys($product->getMediaAttributes()));
+ }
$existingMediaGalleryEntries = [$entry];
} else {
foreach ($existingMediaGalleryEntries as $existingEntries) {
diff --git a/app/code/Magento/Catalog/Test/Mftf/ActionGroup/AdminOpenProductImagesSectionActionGroup.xml b/app/code/Magento/Catalog/Test/Mftf/ActionGroup/AdminOpenProductImagesSectionActionGroup.xml
new file mode 100644
index 0000000000000..4d49b13a8bf5a
--- /dev/null
+++ b/app/code/Magento/Catalog/Test/Mftf/ActionGroup/AdminOpenProductImagesSectionActionGroup.xml
@@ -0,0 +1,18 @@
+
+
+
+
+
+
+ Requires the navigation to the Product page. Opens 'Image and Videos' section.
+
+
+
+
+
diff --git a/app/code/Magento/Catalog/Test/Mftf/ActionGroup/AssertAdminProductImageRolesSelectedActionGroup.xml b/app/code/Magento/Catalog/Test/Mftf/ActionGroup/AssertAdminProductImageRolesSelectedActionGroup.xml
new file mode 100644
index 0000000000000..3bb6210d6b824
--- /dev/null
+++ b/app/code/Magento/Catalog/Test/Mftf/ActionGroup/AssertAdminProductImageRolesSelectedActionGroup.xml
@@ -0,0 +1,26 @@
+
+
+
+
+
+
+ Requires the navigation to the Product page and opened 'Image and Videos' section.
+ Checks the Base, Small, Thumbnail and Swatch Roles are selected for provided image.
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/app/code/Magento/Catalog/Test/Mftf/Data/ProductAttributeMediaGalleryEntryData.xml b/app/code/Magento/Catalog/Test/Mftf/Data/ProductAttributeMediaGalleryEntryData.xml
index 75b4ef773a934..7016a1c1d0358 100644
--- a/app/code/Magento/Catalog/Test/Mftf/Data/ProductAttributeMediaGalleryEntryData.xml
+++ b/app/code/Magento/Catalog/Test/Mftf/Data/ProductAttributeMediaGalleryEntryData.xml
@@ -35,4 +35,11 @@
Magento Logo
MagentoLogoImageContentExportImport
+
+ image
+ Test Image
+ 0
+ false
+ TestImageContent
+
diff --git a/app/code/Magento/Catalog/Test/Mftf/Test/AdminCheckMediaRolesForFirstAddedImageViaApiTest.xml b/app/code/Magento/Catalog/Test/Mftf/Test/AdminCheckMediaRolesForFirstAddedImageViaApiTest.xml
new file mode 100644
index 0000000000000..c31054e3dc192
--- /dev/null
+++ b/app/code/Magento/Catalog/Test/Mftf/Test/AdminCheckMediaRolesForFirstAddedImageViaApiTest.xml
@@ -0,0 +1,41 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/app/code/Magento/Catalog/Test/Unit/Model/Product/Gallery/GalleryManagementTest.php b/app/code/Magento/Catalog/Test/Unit/Model/Product/Gallery/GalleryManagementTest.php
index 6d4e98b60ad18..30994eda87273 100644
--- a/app/code/Magento/Catalog/Test/Unit/Model/Product/Gallery/GalleryManagementTest.php
+++ b/app/code/Magento/Catalog/Test/Unit/Model/Product/Gallery/GalleryManagementTest.php
@@ -59,6 +59,7 @@ protected function setUp()
'getCustomAttribute',
'getMediaGalleryEntries',
'setMediaGalleryEntries',
+ 'getMediaAttributes',
]
);
$this->mediaGalleryEntryMock =
@@ -99,6 +100,9 @@ public function testCreateWithCannotSaveException()
$entryContentMock = $this->getMockBuilder(\Magento\Framework\Api\Data\ImageContentInterface::class)
->disableOriginalConstructor()
->getMock();
+ $attributeMock = $this->getMockBuilder(\Magento\Eav\Model\Entity\Attribute\AbstractAttribute::class)
+ ->disableOriginalConstructor()
+ ->getMock();
$this->mediaGalleryEntryMock->expects($this->any())->method('getContent')->willReturn($entryContentMock);
$this->productRepositoryMock->expects($this->once())
->method('get')
@@ -108,6 +112,10 @@ public function testCreateWithCannotSaveException()
$this->contentValidatorMock->expects($this->once())->method('isValid')->with($entryContentMock)
->willReturn(true);
+ $this->productMock->expects($this->any())
+ ->method('getMediaAttributes')
+ ->willReturn(['small_image' => $attributeMock]);
+
$this->productRepositoryMock->expects($this->once())->method('save')->with($this->productMock)
->willThrowException(new \Exception());
$this->model->create($productSku, $this->mediaGalleryEntryMock);
@@ -133,6 +141,8 @@ public function testCreate()
$this->contentValidatorMock->expects($this->once())->method('isValid')->with($entryContentMock)
->willReturn(true);
+ $this->mediaGalleryEntryMock->expects($this->any())->method('getTypes')->willReturn(['small_image']);
+
$newEntryMock = $this->createMock(\Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterface::class);
$newEntryMock->expects($this->exactly(2))->method('getId')->willReturn(42);
$this->productMock->expects($this->at(2))->method('getMediaGalleryEntries')