diff --git a/app/code/Magento/Review/Model/Review.php b/app/code/Magento/Review/Model/Review.php index d041f0f56f68c..e5bd2ba493170 100644 --- a/app/code/Magento/Review/Model/Review.php +++ b/app/code/Magento/Review/Model/Review.php @@ -334,11 +334,14 @@ public function appendSummary($collection) ->addStoreFilter($this->_storeManager->getStore()->getId()) ->load(); + $summariesByEntityId = []; + foreach ($summaryData as $summary) { + $summariesByEntityId[$summary->getEntityPkValue()] = $summary; + } + foreach ($collection->getItems() as $item) { - foreach ($summaryData as $summary) { - if ($summary->getEntityPkValue() == $item->getEntityId()) { - $item->setRatingSummary($summary); - } + if (isset($summariesByEntityId[$item->getEntityId()])) { + $item->setRatingSummary($summariesByEntityId[$item->getEntityId()]); } if (!$item->getRatingSummary()) { $item->setRatingSummary(new DataObject()); diff --git a/app/code/Magento/Review/Test/Unit/Model/ReviewTest.php b/app/code/Magento/Review/Test/Unit/Model/ReviewTest.php index f910b6dd57fab..bf0b15ea9ca0b 100644 --- a/app/code/Magento/Review/Test/Unit/Model/ReviewTest.php +++ b/app/code/Magento/Review/Test/Unit/Model/ReviewTest.php @@ -26,9 +26,9 @@ use Magento\Store\Model\Store; use Magento\Store\Model\StoreManager; use Magento\Store\Model\StoreManagerInterface; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; -use PHPUnit\Framework\Attributes\DataProvider; /** * @SuppressWarnings(PHPMD.CouplingBetweenObjects) @@ -219,6 +219,111 @@ public function testGetPendingStatus() $this->assertSame(Review::STATUS_PENDING, $this->review->getPendingStatus()); } + /** + * @param array $productData + * @param array $summaryData + * @param array $expectedSummaryValues + */ + #[DataProvider('appendSummaryDataProvider')] + public function testAppendSummary(array $productData, array $summaryData, array $expectedSummaryValues): void + { + $storeId = 4; + $storeMock = $this->createConfiguredMock(Store::class, ['getId' => $storeId]); + $collectionMock = $this->createMock(Collection::class); + + $products = array_map( + static fn (array $data): DataObject => new DataObject($data), + $productData + ); + $summaries = array_map( + static fn (array $data): DataObject => new DataObject($data), + $summaryData + ); + + $entityIds = array_column($productData, 'entity_id'); + $summaryCollection = new class ($summaries) extends \ArrayObject { + public array $entityIds = []; + public int $storeId; + + public function addEntityFilter($entityIds): self + { + $this->entityIds = $entityIds; + return $this; + } + + public function addStoreFilter($storeId): self + { + $this->storeId = $storeId; + return $this; + } + + public function load(): self + { + return $this; + } + }; + + $this->reviewSummaryMock->expects($this->once()) + ->method('create') + ->willReturn($summaryCollection); + + $collectionMock->expects($this->exactly(2)) + ->method('getItems') + ->willReturn($products); + + $this->storeManagerMock->expects($this->once()) + ->method('getStore') + ->willReturn($storeMock); + + $this->assertSame($this->review, $this->review->appendSummary($collectionMock)); + $this->assertSame($entityIds, $summaryCollection->entityIds); + $this->assertSame($storeId, $summaryCollection->storeId); + + foreach ($products as $index => $product) { + $ratingSummary = $product->getRatingSummary(); + $this->assertInstanceOf(DataObject::class, $ratingSummary); + if ($expectedSummaryValues[$index] === '') { + $this->assertSame([], $ratingSummary->getData()); + continue; + } + + $this->assertSame($expectedSummaryValues[$index], $ratingSummary->getData('value')); + } + } + + /** + * @return array, + * 1: array, + * 2: array + * }> + */ + public static function appendSummaryDataProvider(): array + { + return [ + 'products with matching and missing summaries' => [ + [ + ['entity_id' => 10], + ['entity_id' => 20], + ['entity_id' => 30], + ], + [ + ['entity_pk_value' => 10, 'value' => 'first'], + ['entity_pk_value' => 30, 'value' => 'third'], + ], + ['first', '', 'third'], + ], + 'empty summary collection' => [ + [ + ['entity_id' => 10], + ['entity_id' => 20], + ], + [], + ['', ''], + ], + ]; + } + public function testGetReviewUrl() { $result = 'http://some.url';