Skip to content

Commit e1dd62c

Browse files
committed
Merge remote-tracking branch 'origin/MC-41093' into 2.4-develop-pr135
2 parents 9f83674 + 4c200a5 commit e1dd62c

File tree

3 files changed

+47
-5
lines changed

3 files changed

+47
-5
lines changed

app/code/Magento/CatalogInventory/Model/Stock/StockItemRepository.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
use Magento\Framework\Stdlib\DateTime\DateTime;
2626
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
2727
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
28+
use Psr\Log\LoggerInterface as PsrLogger;
2829

2930
/**
3031
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
@@ -98,8 +99,11 @@ class StockItemRepository implements StockItemRepositoryInterface
9899
protected $productCollectionFactory;
99100

100101
/**
101-
* Constructor
102-
*
102+
* @var PsrLogger
103+
*/
104+
private $psrLogger;
105+
106+
/**
103107
* @param StockConfigurationInterface $stockConfiguration
104108
* @param StockStateProviderInterface $stockStateProvider
105109
* @param StockItemResource $resource
@@ -111,7 +115,8 @@ class StockItemRepository implements StockItemRepositoryInterface
111115
* @param TimezoneInterface $localeDate
112116
* @param Processor $indexProcessor
113117
* @param DateTime $dateTime
114-
* @param \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory|null $collectionFactory
118+
* @param CollectionFactory|null $productCollectionFactory
119+
* @param PsrLogger|null $psrLogger
115120
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
116121
*/
117122
public function __construct(
@@ -126,7 +131,8 @@ public function __construct(
126131
TimezoneInterface $localeDate,
127132
Processor $indexProcessor,
128133
DateTime $dateTime,
129-
\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory = null
134+
\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory = null,
135+
PsrLogger $psrLogger = null
130136
) {
131137
$this->stockConfiguration = $stockConfiguration;
132138
$this->stockStateProvider = $stockStateProvider;
@@ -141,6 +147,8 @@ public function __construct(
141147
$this->dateTime = $dateTime;
142148
$this->productCollectionFactory = $productCollectionFactory ?: ObjectManager::getInstance()
143149
->get(CollectionFactory::class);
150+
$this->psrLogger = $psrLogger ?: ObjectManager::getInstance()
151+
->get(PsrLogger::class);
144152
}
145153

146154
/**
@@ -184,6 +192,7 @@ public function save(\Magento\CatalogInventory\Api\Data\StockItemInterface $stoc
184192

185193
$this->resource->save($stockItem);
186194
} catch (\Exception $exception) {
195+
$this->psrLogger->error($exception->getMessage());
187196
throw new CouldNotSaveException(__('The stock item was unable to be saved. Please try again.'), $exception);
188197
}
189198
return $stockItem;

app/code/Magento/Elasticsearch/Model/ResourceModel/Index.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
namespace Magento\Elasticsearch\Model\ResourceModel;
77

88
use Magento\Catalog\Model\Indexer\Product\Price\DimensionCollectionFactory;
9+
use Magento\Framework\Exception\NoSuchEntityException;
910
use Magento\Framework\Model\ResourceModel\Db\Context;
1011
use Magento\Store\Model\StoreManagerInterface;
1112
use Magento\Catalog\Api\ProductRepositoryInterface;
@@ -137,7 +138,11 @@ public function getFullCategoryProductIndexData($storeId = null, $productIds = n
137138

138139
foreach ($categoryPositions as $productId => $positions) {
139140
foreach ($positions as $categoryId => $position) {
140-
$category = $this->categoryRepository->get($categoryId, $storeId);
141+
try {
142+
$category = $this->categoryRepository->get($categoryId, $storeId);
143+
} catch (NoSuchEntityException $e) {
144+
continue;
145+
}
141146
$categoryName = $category->getName();
142147
$categoryData[$productId][] = [
143148
'id' => $categoryId,

dev/tests/integration/testsuite/Magento/Catalog/Controller/Adminhtml/ProductTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
use Magento\TestFramework\TestCase\AbstractBackendController;
2323
use Magento\Catalog\Model\Product\Attribute\LayoutUpdateManager;
2424
use Magento\Catalog\Model\Product\Type;
25+
use Magento\Catalog\Api\ProductRepositoryInterface;
26+
use Magento\Catalog\Model\Category;
2527

2628
/**
2729
* Test class for Product adminhtml actions
@@ -46,6 +48,11 @@ class ProductTest extends AbstractBackendController
4648
*/
4749
private $resourceModel;
4850

51+
/**
52+
* @var ProductRepositoryInterface
53+
*/
54+
private $productRepository;
55+
4956
/**
5057
* @inheritDoc
5158
*/
@@ -62,6 +69,7 @@ protected function setUp(): void
6269
$this->aclBuilder = Bootstrap::getObjectManager()->get(Builder::class);
6370
$this->repositoryFactory = Bootstrap::getObjectManager()->get(ProductRepositoryFactory::class);
6471
$this->resourceModel = Bootstrap::getObjectManager()->get(ProductResource::class);
72+
$this->productRepository = $this->_objectManager->get(ProductRepositoryInterface::class);
6573
}
6674

6775
/**
@@ -677,4 +685,24 @@ public function testSaveActionWithInvalidUrlKey(array $postData)
677685
);
678686
$this->assertRedirect($this->stringContains('/backend/catalog/product/new'));
679687
}
688+
689+
/**
690+
* @magentoDataFixture Magento/Catalog/_files/category_product.php
691+
* @magentoDbIsolation disabled
692+
* @magentoAppArea adminhtml
693+
*/
694+
public function testSaveProductWithDeletedCategory(): void
695+
{
696+
$category = $this->_objectManager->get(Category::class);
697+
$category->load(333);
698+
$category->delete();
699+
$product = $this->productRepository->get('simple333');
700+
$this->productRepository->save($product);
701+
$this->getRequest()->setMethod(HttpRequest::METHOD_POST);
702+
$this->dispatch('backend/catalog/product/save/id/' . $product->getEntityId());
703+
$this->assertSessionMessages(
704+
$this->equalTo([(string)__('You saved the product.')]),
705+
MessageInterface::TYPE_SUCCESS
706+
);
707+
}
680708
}

0 commit comments

Comments
 (0)