Skip to content

fix Can't remove samples link product downloadable #31895

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 12 commits into
base: 2.4-develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions app/code/Magento/Downloadable/Model/Sample/UpdateHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->

<actionGroups xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:mftf:Test/etc/actionGroupSchema.xsd">
<actionGroup name="RemoveSampleActionGroup">
<annotations>
<description>Delete Downloadable Product Sample by row id.</description>
</annotations>
<arguments>
<argument name="index" type="string" defaultValue="0"/>
</arguments>

<waitForElementVisible selector="{{AdminProductDownloadableSection.addSampleRemoveRowButton(index)}}" stepKey="waitForSample"/>
<click selector="{{AdminProductDownloadableSection.addSampleRemoveRowButton(index)}}" stepKey="deleteSample"/>
</actionGroup>
</actionGroups>
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->

<actionGroups xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:mftf:Test/etc/actionGroupSchema.xsd">
<actionGroup name="VerifySampleAbsentActionGroup">
<annotations>
<description>Verify Samples are absent on Downloadable Product page.</description>
</annotations>
<arguments>
<argument name="index" type="string" defaultValue="0"/>
</arguments>

<dontSeeElement selector="{{AdminProductDownloadableSection.addSampleTitleInput(index)}}" stepKey="verifySamplesAbsent"/>
</actionGroup>
</actionGroups>
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->

<actionGroups xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:mftf:Test/etc/actionGroupSchema.xsd">
<actionGroup name="VerifySamplePresentActionGroup">
<annotations>
<description>Verify Sample is present on Downloadable Product page.</description>
</annotations>
<arguments>
<argument name="index" type="string" defaultValue="0"/>
</arguments>

<seeElement selector="{{AdminProductDownloadableSection.addSampleTitleInput(index)}}" stepKey="verifySampleVisible"/>
</actionGroup>
</actionGroups>
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,13 @@
<element name="addLinkSampleUrlInput" type="input" selector="input[name='downloadable[link][{{var1}}][sample][url]']" parameterized="true" />
<element name="linkFileNameOrUrl" type="text" parameterized="true" selector="//div[@data-index='container_links']//tr[@data-repeat-index='{{index}}']//fieldset[@data-index='container_file']//div[@class='file-uploader-filename']//a|//input[@name='downloadable[link][{{index}}][link_url]']"/>
<element name="linkSampleFileNameOrUrl" type="text" parameterized="true" selector="//div[@data-index='container_links']//tr[@data-repeat-index='{{index}}']//fieldset[@data-index='container_sample']//div[@class='file-uploader-filename']//a|//input[@name='downloadable[link][{{index}}][sample][url]']"/>

<element name="samplesTitleInput" type="input" selector="input[name='product[samples_title]']"/>
<element name="samplesAddLinkButton" type="button" selector="div[data-index='container_samples'] button[data-action='add_new_row']" />

<!-- Parameterized selectors for elements in an individual sample row -->
<element name="addSampleTitleInput" type="input" selector="input[name='downloadable[sample][{{var1}}][title]']" parameterized="true" />
<element name="addSampleFileTypeSelector" type="select" selector="select[name='downloadable[sample][{{var1}}][type]']" parameterized="true" />
<element name="addSampleRemoveRowButton" type="button" selector="div[data-index='container_links'] tr[data-repeat-index='{{var1}}'] button[data-action='remove_row']" parameterized="true" />
<element name="addSampleRemoveRowButton" type="button" selector="div[data-index='container_samples'] tr[data-repeat-index='{{var1}}'] button[data-action='remove_row']" parameterized="true" />
<element name="addSampleFileUploadFile" type="file" selector="div[data-index='container_samples'] tr[data-repeat-index='{{var1}}'] input[type='file']" parameterized="true" />
<element name="addSampleFileUrlInput" type="input" selector="input[name='downloadable[sample][{{var1}}][sample_url]']" parameterized="true" />
<element name="sampleFileNameOrUrl" type="text" parameterized="true" selector="//div[@data-index='sample']//tr[@data-repeat-index='{{index}}']//fieldset[@data-index='container_sample']//div[@class='file-uploader-filename']//a|//input[@name='downloadable[sample][{{index}}][sample_url]']"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->

<tests xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:mftf:Test/etc/testSchema.xsd">
<test name="AdminDeleteDownloadableProductSampleTest">
<annotations>
<features value="Catalog"/>
<stories value="Delete Downloadable Product"/>
<title value="Delete Downloadable Product Samples"/>
<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>
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets double check if we need to remove these assertions

->method('getExtensionAttributes')
->willReturn($this->productExtensionMock);
$this->entityMock->expects($this->never())
->method('getSku');
$this->entityMock->expects($this->never())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets double check if we need to remove these assertions

$this->assertArrayHasKey(self::PRODUCT_LINKS, $response[ProductInterface::EXTENSION_ATTRIBUTES_KEY]);

$this->assertCount(2, $response[ProductInterface::EXTENSION_ATTRIBUTES_KEY][self::PRODUCT_SAMPLES]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets double check if we need to remove these assertions

$this->assertCount(2, $response[ProductInterface::EXTENSION_ATTRIBUTES_KEY][self::PRODUCT_LINKS]);
}

Expand Down