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
11 changes: 7 additions & 4 deletions app/code/Magento/Review/Model/Review.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
107 changes: 106 additions & 1 deletion app/code/Magento/Review/Test/Unit/Model/ReviewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -219,6 +219,111 @@ public function testGetPendingStatus()
$this->assertSame(Review::STATUS_PENDING, $this->review->getPendingStatus());
}

/**
* @param array<int, array{entity_id:int}> $productData
* @param array<int, array{entity_pk_value:int, value?:string}> $summaryData
* @param array<int, string> $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<string, array{
* 0: array<int, array{entity_id:int}>,
* 1: array<int, array{entity_pk_value:int, value?:string}>,
* 2: array<int, string>
* }>
*/
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';
Expand Down