Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

namespace Magento\Catalog\Model\Product\Price\Validation;

use Magento\Framework\Exception\NoSuchEntityException;

/**
* Class is responsible to detect list of invalid SKU values from list of provided skus and allowed product types.
*/
Expand Down Expand Up @@ -57,9 +59,18 @@ public function retrieveInvalidSkuList(array $skus, array $allowedProductTypes,

if ($allowedPriceTypeValue
&& $type == \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE
&& $this->productRepository->get($sku)->getPriceType() != $allowedPriceTypeValue
) {
$valueTypeIsAllowed = true;
try {
$product = $this->productRepository->get($sku);
if ($product->getPriceType() != $allowedPriceTypeValue) {
$valueTypeIsAllowed = true;
}
} catch (NoSuchEntityException $e) {
// Product was deleted between the ID lookup and repository fetch.
// Treat as invalid SKU so the rest of the batch continues.
$skuDiff[] = $sku;
break;
}
}

if (!in_array($type, $allowedProductTypes) || $valueTypeIsAllowed) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Magento\Catalog\Model\Product\Price\Validation\InvalidSkuProcessor;
use Magento\Catalog\Model\Product\Type;
use Magento\Catalog\Model\ProductIdLocatorInterface;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\TestFramework\Unit\Helper\MockCreationTrait;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use PHPUnit\Framework\MockObject\MockObject;
Expand Down Expand Up @@ -98,4 +99,73 @@ public function testRetrieveInvalidSkuList()
$this->invalidSkuProcessor->retrieveInvalidSkuList($skus, $allowedProductTypes, $allowedPriceTypeValue)
);
}

/**
* A bundle product can be deleted between the retrieveProductIdsBySkus() lookup and the
* productRepository->get() call a few lines later. Before this fix, the resulting
* NoSuchEntityException propagated uncaught and failed the whole batch instead of just
* this one SKU.
*
* @return void
*/
public function testRetrieveInvalidSkuListWhenBundleProductDeletedConcurrently()
{
$deletedSku = 'DELETED-BUNDLE-SKU';
$idsBySku = [$deletedSku => [235235235 => Type::TYPE_BUNDLE]];
$this->productIdLocator->expects($this->atLeastOnce())->method('retrieveProductIdsBySkus')
->willReturn($idsBySku);
$this->productRepository->expects($this->atLeastOnce())->method('get')
->willThrowException(new NoSuchEntityException());

// array_values(): retrieveInvalidSkuList() never guarantees zero-based
// keys (array_udiff()'s internal next-index counter isn't reset just
// because the filtered result is empty) and no caller relies on the
// keys, only the values -- same as filterSkuList()'s own array_diff().
$this->assertEquals(
[$deletedSku],
array_values($this->invalidSkuProcessor->retrieveInvalidSkuList(
[$deletedSku],
[Type::TYPE_BUNDLE],
true
))
);
}

/**
* One bundle product being deleted mid-batch must not stop the rest of the batch from
* being processed normally.
*
* @return void
*/
public function testRetrieveInvalidSkuListContinuesBatchWhenOneBundleProductIsDeletedConcurrently()
{
$deletedSku = 'DELETED-BUNDLE-SKU';
$validSku = 'VALID-BUNDLE-SKU';
$idsBySku = [
$deletedSku => [235235235 => Type::TYPE_BUNDLE],
$validSku => [235235236 => Type::TYPE_BUNDLE],
];
$this->productIdLocator->expects($this->atLeastOnce())->method('retrieveProductIdsBySkus')
->willReturn($idsBySku);

$validProduct = $this->createPartialMockWithReflection(Product::class, ['getPriceType']);
$validProduct->method('getPriceType')->willReturn(true);

$this->productRepository->method('get')
->willReturnCallback(function ($sku) use ($deletedSku, $validProduct) {
if ($sku === $deletedSku) {
throw new NoSuchEntityException();
}
return $validProduct;
});

$this->assertEquals(
[$deletedSku],
array_values($this->invalidSkuProcessor->retrieveInvalidSkuList(
[$deletedSku, $validSku],
[Type::TYPE_BUNDLE],
true
))
);
}
}