diff --git a/app/code/Magento/Downloadable/Model/Sample/UpdateHandler.php b/app/code/Magento/Downloadable/Model/Sample/UpdateHandler.php index cb7ff725a21d3..779f3caa2f6d2 100644 --- a/app/code/Magento/Downloadable/Model/Sample/UpdateHandler.php +++ b/app/code/Magento/Downloadable/Model/Sample/UpdateHandler.php @@ -43,10 +43,8 @@ public function __construct(SampleRepository $sampleRepository) */ public function execute($entity, $arguments = []): ProductInterface { - $samples = $entity->getExtensionAttributes()->getDownloadableProductSamples(); - - if ($samples && $entity->getTypeId() === Type::TYPE_DOWNLOADABLE) { - $this->updateSamples($entity, $samples); + if ($entity->getTypeId() === Type::TYPE_DOWNLOADABLE) { + $this->updateSamples($entity); } return $entity; @@ -56,20 +54,22 @@ public function execute($entity, $arguments = []): ProductInterface * Update product samples * * @param ProductInterface $entity - * @param array $samples * @return void */ - private function updateSamples(ProductInterface $entity, array $samples): void + private function updateSamples(ProductInterface $entity): void { $isGlobalScope = (int) $entity->getStoreId() === self::GLOBAL_SCOPE_ID; + $samples = $entity->getExtensionAttributes()->getDownloadableProductSamples(); $oldSamples = $this->sampleRepository->getList($entity->getSku()); - $updatedSamples = []; - foreach ($samples as $sample) { - if ($sample->getId()) { - $updatedSamples[$sample->getId()] = true; + if (!empty($samples)) { + $updatedSamples = []; + foreach ($samples as $sample) { + if ($sample->getId()) { + $updatedSamples[$sample->getId()] = true; + } + $this->sampleRepository->save($entity->getSku(), $sample, $isGlobalScope); } - $this->sampleRepository->save($entity->getSku(), $sample, $isGlobalScope); } foreach ($oldSamples as $sample) { diff --git a/app/code/Magento/Downloadable/Test/Mftf/ActionGroup/RemoveSampleActionGroup.xml b/app/code/Magento/Downloadable/Test/Mftf/ActionGroup/RemoveSampleActionGroup.xml new file mode 100644 index 0000000000000..2948300063abc --- /dev/null +++ b/app/code/Magento/Downloadable/Test/Mftf/ActionGroup/RemoveSampleActionGroup.xml @@ -0,0 +1,22 @@ + + + + + + + Delete Downloadable Product Sample by row id. + + + + + + + + + diff --git a/app/code/Magento/Downloadable/Test/Mftf/ActionGroup/VerifySampleAbsentActionGroup.xml b/app/code/Magento/Downloadable/Test/Mftf/ActionGroup/VerifySampleAbsentActionGroup.xml new file mode 100644 index 0000000000000..4755be327384a --- /dev/null +++ b/app/code/Magento/Downloadable/Test/Mftf/ActionGroup/VerifySampleAbsentActionGroup.xml @@ -0,0 +1,21 @@ + + + + + + + Verify Samples are absent on Downloadable Product page. + + + + + + + + diff --git a/app/code/Magento/Downloadable/Test/Mftf/ActionGroup/VerifySamplePresentActionGroup.xml b/app/code/Magento/Downloadable/Test/Mftf/ActionGroup/VerifySamplePresentActionGroup.xml new file mode 100644 index 0000000000000..9c0c64b86b412 --- /dev/null +++ b/app/code/Magento/Downloadable/Test/Mftf/ActionGroup/VerifySamplePresentActionGroup.xml @@ -0,0 +1,21 @@ + + + + + + + Verify Sample is present on Downloadable Product page. + + + + + + + + diff --git a/app/code/Magento/Downloadable/Test/Mftf/Section/AdminProductDownloadableSection.xml b/app/code/Magento/Downloadable/Test/Mftf/Section/AdminProductDownloadableSection.xml index fc21578f6c5ea..0e9baa02b02ad 100644 --- a/app/code/Magento/Downloadable/Test/Mftf/Section/AdminProductDownloadableSection.xml +++ b/app/code/Magento/Downloadable/Test/Mftf/Section/AdminProductDownloadableSection.xml @@ -33,14 +33,13 @@ - - + diff --git a/app/code/Magento/Downloadable/Test/Mftf/Test/AdminDeleteDownloadableProductSampleTest.xml b/app/code/Magento/Downloadable/Test/Mftf/Test/AdminDeleteDownloadableProductSampleTest.xml new file mode 100644 index 0000000000000..e575d86b7bed0 --- /dev/null +++ b/app/code/Magento/Downloadable/Test/Mftf/Test/AdminDeleteDownloadableProductSampleTest.xml @@ -0,0 +1,72 @@ + + + + + + + + + + <description value="Admin should be able to delete downloadable product samples"/> + <severity value="MAJOR"/> + <group value="Downloadable"/> + </annotations> + <before> + <magentoCLI command="downloadable:domains:add static.magento.com" stepKey="addDownloadableDomain"/> + <createData entity="SimpleSubCategory" stepKey="createCategory"/> + <actionGroup ref="AdminLoginActionGroup" stepKey="LoginAsAdmin"/> + </before> + <after> + <deleteData createDataKey="createCategory" stepKey="deleteCategory"/> + <actionGroup ref="DeleteProductUsingProductGridActionGroup" stepKey="deleteProduct"> + <argument name="product" value="DownloadableProduct"/> + </actionGroup> + <actionGroup ref="AdminLogoutActionGroup" stepKey="logout"/> + <magentoCLI command="downloadable:domains:remove static.magento.com" stepKey="removeDownloadableDomain"/> + </after> + <actionGroup ref="AdminOpenProductIndexPageActionGroup" stepKey="amOnProductGridPage"/> + <actionGroup ref="GoToSpecifiedCreateProductPageActionGroup" stepKey="createProduct"> + <argument name="productType" value="downloadable"/> + </actionGroup> + <actionGroup ref="FillMainProductFormNoWeightActionGroup" stepKey="fillDownloadableProductForm"> + <argument name="product" value="DownloadableProduct"/> + </actionGroup> + <searchAndMultiSelectOption selector="{{AdminProductFormSection.categoriesDropdown}}" + parameterArray="[$$createCategory.name$$]" stepKey="fillCategory"/> + <actionGroup ref="AdminAddDownloadableLinkInformationActionGroup" stepKey="addDownloadableLinkInformation"/> + <checkOption selector="{{AdminProductDownloadableSection.isLinksPurchasedSeparately}}" + stepKey="checkOptionPurchaseSeparately"/> + <actionGroup ref="AddDownloadableProductLinkWithMaxDownloadsActionGroup" stepKey="addDownloadableProductLink"> + <argument name="link" value="downloadableLinkWithMaxDownloads"/> + </actionGroup> + <actionGroup ref="AddDownloadableSampleFileActionGroup" stepKey="addDownloadSampleFile"> + <argument name="sample" value="downloadableSampleFile"/> + </actionGroup> + <actionGroup ref="AddDownloadableSampleUrlActionGroup" stepKey="addDownloadableSampleUrl"> + <argument name="sample" value="downloadableSampleUrl"/> + </actionGroup> + <actionGroup ref="SaveProductFormActionGroup" stepKey="saveProduct"/> + <actionGroup ref="VerifySamplePresentActionGroup" stepKey="verifySampleFilePresent"> + <argument name="index" value="0"/> + </actionGroup> + <actionGroup ref="VerifySamplePresentActionGroup" stepKey="verifySampleUrlPresent"> + <argument name="index" value="1"/> + </actionGroup> + <actionGroup ref="RemoveSampleActionGroup" stepKey="removeSampleUrl"> + <argument name="index" value="1"/> + </actionGroup> + <actionGroup ref="RemoveSampleActionGroup" stepKey="removeSampleFile"> + <argument name="index" value="0"/> + </actionGroup> + <actionGroup ref="SaveProductFormActionGroup" stepKey="saveProductAfterSamplesRemoved"/> + <actionGroup ref="VerifySampleAbsentActionGroup" stepKey="verifySamplesAbsent"> + <argument name="index" value="0"/> + </actionGroup> + </test> +</tests> diff --git a/app/code/Magento/Downloadable/Test/Unit/Model/Sample/UpdateHandlerTest.php b/app/code/Magento/Downloadable/Test/Unit/Model/Sample/UpdateHandlerTest.php index 727ce4b0af0a6..48cd8ba1aa405 100644 --- a/app/code/Magento/Downloadable/Test/Unit/Model/Sample/UpdateHandlerTest.php +++ b/app/code/Magento/Downloadable/Test/Unit/Model/Sample/UpdateHandlerTest.php @@ -128,9 +128,6 @@ public function testExecuteNonDownloadable(): void $this->entityMock->expects($this->once()) ->method('getTypeId') ->willReturn(Type::TYPE_DOWNLOADABLE . 'some'); - $this->entityMock->expects($this->once()) - ->method('getExtensionAttributes') - ->willReturn($this->productExtensionMock); $this->entityMock->expects($this->never()) ->method('getSku'); $this->entityMock->expects($this->never()) diff --git a/dev/tests/api-functional/testsuite/Magento/Downloadable/Api/ProductRepositoryTest.php b/dev/tests/api-functional/testsuite/Magento/Downloadable/Api/ProductRepositoryTest.php index 00bbb3f435cae..f8245d934db1b 100644 --- a/dev/tests/api-functional/testsuite/Magento/Downloadable/Api/ProductRepositoryTest.php +++ b/dev/tests/api-functional/testsuite/Magento/Downloadable/Api/ProductRepositoryTest.php @@ -326,10 +326,8 @@ public function testUpdateDownloadableProductData(): void $response = $this->saveProduct($productData); $this->assertArrayHasKey(ProductInterface::EXTENSION_ATTRIBUTES_KEY, $response); - $this->assertArrayHasKey(self::PRODUCT_SAMPLES, $response[ProductInterface::EXTENSION_ATTRIBUTES_KEY]); $this->assertArrayHasKey(self::PRODUCT_LINKS, $response[ProductInterface::EXTENSION_ATTRIBUTES_KEY]); - $this->assertCount(2, $response[ProductInterface::EXTENSION_ATTRIBUTES_KEY][self::PRODUCT_SAMPLES]); $this->assertCount(2, $response[ProductInterface::EXTENSION_ATTRIBUTES_KEY][self::PRODUCT_LINKS]); }