diff --git a/CHANGELOG.md b/CHANGELOG.md index 50b336bc..2c3a2e93 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] +### Fixed +- Fix integration test +- Fix potential issues with products only in Root Category ## [3.7.8] - 3 February 2024 ### Fixed diff --git a/Test/Integration/Page/AddToWishlistTest.php b/Test/Integration/Page/AddToWishlistTest.php index 9284d17a..a57f789d 100644 --- a/Test/Integration/Page/AddToWishlistTest.php +++ b/Test/Integration/Page/AddToWishlistTest.php @@ -4,7 +4,6 @@ use Magento\Catalog\Api\ProductRepositoryInterface; use Magento\Customer\CustomerData\SectionPool; -use Magento\Customer\Model\Session as CustomerSession; use Magento\Framework\App\Request\Http as HttpRequest; use Magento\Framework\Data\Form\FormKey; use Magento\Framework\Exception\LocalizedException; diff --git a/Util/CategoryProvider.php b/Util/CategoryProvider.php index 08280105..7c721d9c 100644 --- a/Util/CategoryProvider.php +++ b/Util/CategoryProvider.php @@ -54,15 +54,11 @@ public function __construct( */ public function addCategoryIds(array $categoryIds) { + $categoryIds = $this->filterRootCategoryIdFromCategoryIds($categoryIds); if (empty($categoryIds)) { return; } - $rootCategoryId = $this->getRootCategoryId(); - $categoryIds = array_filter($categoryIds, function ($categoryId) use ($rootCategoryId) { - return (int)$categoryId !== $rootCategoryId; - }); - $this->categoryIds = array_unique(array_merge($this->categoryIds, $categoryIds)); } @@ -111,15 +107,23 @@ public function getLoadedCategories(): array */ public function getFirstByProduct(ProductInterface $product): CategoryInterface { - $productCategoryIds = $product->getCategoryIds(); + $productCategoryIds = $this->filterRootCategoryIdFromCategoryIds($product->getCategoryIds()); if (empty($productCategoryIds)) { throw new NoSuchEntityException(__('Product "' . $product->getSku() . '" has no categories')); } $productCategoryId = array_shift($productCategoryIds); $this->addCategoryIds([$productCategoryId]); + if (empty($this->categoryIds)) { + throw new NoSuchEntityException(__('Product "' . $product->getSku() . '" has no categories')); + } + + $categories = $this->getLoadedCategories(); + if (false === array_key_exists($productCategoryId, $categories)) { + throw new NoSuchEntityException(__('Product "' . $product->getSku() . '" has no categories')); + } - return $this->getLoadedCategories()[$productCategoryId]; + return $categories[$productCategoryId]; } /** @@ -129,7 +133,7 @@ public function getFirstByProduct(ProductInterface $product): CategoryInterface */ public function getAllByProduct(ProductInterface $product): array { - $productCategoryIds = $product->getCategoryIds(); + $productCategoryIds = $this->filterRootCategoryIdFromCategoryIds($product->getCategoryIds()); if (empty($productCategoryIds)) { throw new NoSuchEntityException(__('Product "' . $product->getSku() . '" has no categories')); } @@ -181,6 +185,19 @@ private function loadCategoriesByIds(array $categoryIds): array return $this->categoryListRepository->getList($searchCriteria)->getItems(); } + /** + * @param array $categoryIds + * @return array + * @throws NoSuchEntityException + */ + private function filterRootCategoryIdFromCategoryIds(array $categoryIds): array + { + $rootCategoryId = $this->getRootCategoryId(); + return array_filter($categoryIds, function ($categoryId) use ($rootCategoryId) { + return (int)$categoryId !== $rootCategoryId; + }); + } + /** * @return int * @throws NoSuchEntityException