diff --git a/app/code/Magento/Catalog/Model/CategoryRepository.php b/app/code/Magento/Catalog/Model/CategoryRepository.php index 7082fa4747fdc..be14e48619e46 100644 --- a/app/code/Magento/Catalog/Model/CategoryRepository.php +++ b/app/code/Magento/Catalog/Model/CategoryRepository.php @@ -14,9 +14,11 @@ use Magento\Framework\EntityManager\MetadataPool; use Magento\Framework\Exception\CouldNotSaveException; use Magento\Framework\Exception\NoSuchEntityException; +use Magento\Framework\Exception\SerializationException; use Magento\Framework\Exception\StateException; use Magento\Catalog\Api\Data\CategoryInterface; use Magento\Store\Model\StoreManagerInterface; +use Magento\Framework\Phrase; /** * Repository for categories. @@ -144,10 +146,12 @@ public function save(CategoryInterface $category) /** * @inheritdoc + * + * @throws SerializationException */ public function get($categoryId, $storeId = null) { - $cacheKey = $storeId ?? 'all'; + $cacheKey = $this->getScopeCacheKey($storeId); if (!isset($this->instances[$categoryId][$cacheKey])) { /** @var Category $category */ $category = $this->categoryFactory->create(); @@ -254,4 +258,26 @@ private function getMetadataPool() } return $this->metadataPool; } + + /** + * Returns a cache key based on scope + * + * @param string|int|null $storeId + * + * @throws SerializationException + * @return int|string + */ + private function getScopeCacheKey($storeId = null) + { + if (null !== $storeId && !is_numeric($storeId)) { + throw new SerializationException( + new Phrase( + 'The "%value" value\'s type is invalid. The "%type" type was expected. ' + . 'Verify and try again.', + ['value' => $storeId, 'type' => 'int'] + ) + ); + } + return $storeId === null ? 'all' : (int)$storeId; + } } diff --git a/app/code/Magento/Catalog/Test/Unit/Model/CategoryRepositoryTest.php b/app/code/Magento/Catalog/Test/Unit/Model/CategoryRepositoryTest.php index 8274ed9da5f32..c64fbd06f8592 100644 --- a/app/code/Magento/Catalog/Test/Unit/Model/CategoryRepositoryTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Model/CategoryRepositoryTest.php @@ -168,6 +168,17 @@ public function testGetWhenCategoryDoesNotExist() $this->assertEquals($categoryMock, $this->model->get($categoryId)); } + public function testGetWithStoreCodeException() + { + $categoryId = 5; + $categoryMock = $this->createMock(CategoryModel::class); + $this->expectException('\Magento\Framework\Exception\SerializationException'); + $this->expectExceptionMessage( + 'The "default" value\'s type is invalid. The "int" type was expected. Verify and try again.' + ); + $this->assertEquals($categoryMock, $this->model->get($categoryId, 'default')); + } + /** * @return array */ diff --git a/dev/tests/integration/testsuite/Magento/Catalog/Model/CategoryRepositoryTest.php b/dev/tests/integration/testsuite/Magento/Catalog/Model/CategoryRepositoryTest.php index e829801d60e1a..d02835956d8be 100644 --- a/dev/tests/integration/testsuite/Magento/Catalog/Model/CategoryRepositoryTest.php +++ b/dev/tests/integration/testsuite/Magento/Catalog/Model/CategoryRepositoryTest.php @@ -13,6 +13,7 @@ use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory; use Magento\Cms\Api\GetBlockByIdentifierInterface; use Magento\Framework\Exception\LocalizedException; +use Magento\Framework\Exception\SerializationException; use Magento\Framework\ObjectManagerInterface; use Magento\Store\Api\StoreManagementInterface; use Magento\Store\Model\StoreManagerInterface; @@ -143,6 +144,14 @@ public function testGetCategoryForProvidedStore(): void $fixtureStoreId = $this->storeManager->getStore('fixturestore')->getId(); $categorySecondStore = $this->categoryRepository->get($categoryId, $fixtureStoreId); $this->assertSame('category-fixturestore', $categorySecondStore->getUrlKey()); + + $caughtException = false; + try { + $this->categoryRepository->get($categoryId, 'default'); + } catch (SerializationException $exception) { + $caughtException = true; + } + $this->assertTrue($caughtException); } /**