Skip to content

Commit d2745c5

Browse files
committed
MC-39763: Ratings data not rendering on review detail page
- Adding review id with rating
1 parent 285b397 commit d2745c5

File tree

5 files changed

+259
-8
lines changed

5 files changed

+259
-8
lines changed

app/code/Magento/Review/Block/View.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,10 @@ public function getBackUrl()
103103
*/
104104
public function getRating()
105105
{
106+
$reviewId = $this->getReviewId() ?: $this->getReviewData()->getId();
106107
if (!$this->getRatingCollection()) {
107108
$ratingCollection = $this->_voteFactory->create()->getResourceCollection()->setReviewFilter(
108-
$this->getReviewId()
109+
$reviewId
109110
)->setStoreFilter(
110111
$this->_storeManager->getStore()->getId()
111112
)->addRatingInfo(

app/code/Magento/Review/view/frontend/templates/view.phtml

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,26 @@
3333
<caption class="table-caption"><?= $block->escapeHtml(__('Product Rating')) ?></caption>
3434
<?php foreach ($block->getRating() as $_rating): ?>
3535
<?php if ($_rating->getPercent()): ?>
36+
<?php $rating = ceil($_rating->getPercent()) ?>
3637
<tr>
37-
<td class="label"><?= $block->escapeHtml(__($_rating->getRatingCode())) ?></td>
38+
<td class="label" width="10%">
39+
<?= $block->escapeHtml(__($_rating->getRatingCode())) ?>
40+
</td>
3841
<td class="value">
39-
<div class="rating-box">
40-
<div class="rating"/>
42+
<?php $ratingId = $_rating->getRatingId() ?>
43+
<div class="rating-summary item"
44+
id="rating-div-<?= $block->escapeHtml($ratingId) ?>">
45+
<div class="rating-result" title="<?= /* @noEscape */ $rating ?>%">
46+
<span>
47+
<span><?= /* @noEscape */ $rating ?>%</span>
48+
</span>
49+
</div>
50+
<?= /* @noEscape */ $secureRenderer->renderStyleAsTag(
51+
"width:" . /* @noEscape */ $rating . "%",
52+
'div#rating-div-'.$_rating->getRatingId().
53+
'>div.rating-result>span:first-child'
54+
) ?>
4155
</div>
42-
<?= /* @noEscape */ $secureRenderer->renderStyleAsTag(
43-
"width:" . /* @noEscape */ ceil($_rating->getPercent()) . "%;",
44-
'div.rating-box div.rating'
45-
) ?>
4656
</td>
4757
</tr>
4858
<?php endif; ?>
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Review\Block;
9+
10+
use Magento\Catalog\Api\Data\ProductInterface;
11+
use Magento\Catalog\Api\ProductRepositoryInterface;
12+
use Magento\Catalog\Model\Product;
13+
use Magento\Customer\Model\Session;
14+
use Magento\Framework\Exception\NoSuchEntityException;
15+
use Magento\Framework\ObjectManagerInterface;
16+
use Magento\Framework\Registry;
17+
use Magento\Framework\View\LayoutInterface;
18+
use Magento\Review\Model\ResourceModel\Review\Product\CollectionFactory;
19+
use Magento\TestFramework\Helper\Bootstrap;
20+
use Magento\TestFramework\Helper\Xpath;
21+
use PHPUnit\Framework\TestCase;
22+
23+
/**
24+
* Test for displaying product review block.
25+
*
26+
* @magentoAppArea frontend
27+
* @magentoDbIsolation enabled
28+
*/
29+
class ViewTest extends TestCase
30+
{
31+
/** @var ObjectManagerInterface */
32+
private $objectManager;
33+
34+
/** @var Session */
35+
private $customerSession;
36+
37+
/** @var CollectionFactory */
38+
private $collectionFactory;
39+
40+
/** @var Registry */
41+
private $registry;
42+
43+
/** @var View */
44+
private $block;
45+
46+
/**
47+
* @inheritdoc
48+
*/
49+
protected function setUp(): void
50+
{
51+
parent::setUp();
52+
53+
$this->objectManager = Bootstrap::getObjectManager();
54+
$this->customerSession = $this->objectManager->get(Session::class);
55+
$this->collectionFactory = $this->objectManager->get(CollectionFactory::class);
56+
$this->registry = $this->objectManager->get(Registry::class);
57+
$this->block = $this->objectManager->get(LayoutInterface::class)->createBlock(View::class);
58+
}
59+
60+
/**
61+
* @inheritdoc
62+
*/
63+
protected function tearDown(): void
64+
{
65+
$this->registry->unregister('current_review');
66+
$this->registry->unregister('current_product');
67+
$this->registry->unregister('product');
68+
$this->customerSession->setCustomerId(null);
69+
70+
parent::tearDown();
71+
}
72+
73+
/**
74+
* Test product review block
75+
*
76+
* @magentoDataFixture Magento/Review/_files/product_review_with_rating.php
77+
*
78+
* @return void
79+
* @throws NoSuchEntityException
80+
*/
81+
public function testProductReviewBlock(): void
82+
{
83+
$this->customerSession->setCustomerId(1);
84+
$review = $this->collectionFactory->create()->addCustomerFilter(1)->getFirstItem();
85+
$this->registerReview($review);
86+
$this->assertNotNull($review->getReviewId());
87+
88+
/** @var ProductRepositoryInterface $productRepository */
89+
$productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
90+
/** @var ProductInterface $product */
91+
$product = $productRepository->get('simple', false, null, true);
92+
$this->registerProduct($product);
93+
94+
$blockHtml = $this->block->setReviewId($review->getReviewId())->toHtml();
95+
$this->assertEquals(
96+
1,
97+
Xpath::getElementsCountForXpath(
98+
sprintf("//div[contains(@class, 'details')]/h3[contains(text(), '%s')]", $review->getName()),
99+
$blockHtml
100+
),
101+
'Product name wasn\'t found.'
102+
);
103+
$ratings = $this->block->getRating();
104+
$this->assertCount(2, $ratings);
105+
$this->assertEquals(
106+
1,
107+
Xpath::getElementsCountForXpath(
108+
sprintf(
109+
"//a[contains(@class, 'action back')]/span[contains(text(), '%s')]",
110+
__('Back to Product Reviews')
111+
),
112+
$blockHtml
113+
),
114+
sprintf('%s button wasn\'t found.', __('Back to Product Reviews'))
115+
);
116+
}
117+
118+
/**
119+
* Register the product
120+
*
121+
* @param ProductInterface $product
122+
* @return void
123+
*/
124+
private function registerProduct(ProductInterface $product): void
125+
{
126+
$this->registry->unregister('current_product');
127+
$this->registry->unregister('product');
128+
$this->registry->register('current_product', $product);
129+
$this->registry->register('product', $product);
130+
}
131+
132+
/**
133+
* Register the current review
134+
*
135+
* @param Product $review
136+
* @return void
137+
*/
138+
private function registerReview(Product $review): void
139+
{
140+
$this->registry->unregister('current_review');
141+
$this->registry->register('current_review', $review);
142+
}
143+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
use Magento\Backend\App\Area\FrontNameResolver;
9+
use Magento\Catalog\Api\ProductRepositoryInterface;
10+
use Magento\Customer\Model\CustomerRegistry;
11+
use Magento\Framework\Registry;
12+
use Magento\Review\Model\Rating;
13+
use Magento\Review\Model\Rating\Option;
14+
use Magento\Review\Model\ResourceModel\Review\Collection;
15+
use Magento\Review\Model\Review;
16+
use Magento\Store\Model\StoreManagerInterface;
17+
use Magento\TestFramework\Helper\Bootstrap;
18+
use Magento\TestFramework\Workaround\Override\Fixture\Resolver;
19+
20+
Bootstrap::getInstance()->loadArea(
21+
FrontNameResolver::AREA_CODE
22+
);
23+
Resolver::getInstance()->requireDataFixture('Magento/Customer/_files/customer.php');
24+
Resolver::getInstance()->requireDataFixture('Magento/Catalog/_files/product_simple.php');
25+
26+
$objectManager = Bootstrap::getObjectManager();
27+
/** @var CustomerRegistry $customerRegistry */
28+
$customerRegistry = $objectManager->create(CustomerRegistry::class);
29+
$customer = $customerRegistry->retrieve(1);
30+
/** @var ProductRepositoryInterface $productRepository */
31+
$productRepository = $objectManager->create(ProductRepositoryInterface::class);
32+
$product = $productRepository->get('simple');
33+
$storeId = $objectManager->get(
34+
StoreManagerInterface::class
35+
)->getStore()->getId();
36+
37+
$review = $objectManager->create(
38+
Review::class,
39+
['data' => [
40+
'customer_id' => $customer->getId(),
41+
'title' => 'Review Summary',
42+
'detail' => 'Review text',
43+
'nickname' => 'Nickname',
44+
]]
45+
);
46+
47+
$review
48+
->setEntityId($review->getEntityIdByCode(Review::ENTITY_PRODUCT_CODE))
49+
->setEntityPkValue($product->getId())
50+
->setStatusId(Review::STATUS_APPROVED)
51+
->setStoreId($storeId)
52+
->setStores([$storeId])
53+
->save();
54+
55+
$objectManager->get(Registry::class)->register(
56+
'review_data',
57+
$review
58+
);
59+
60+
/** @var Collection $ratingCollection */
61+
$ratingCollection = $objectManager->create(
62+
Rating::class
63+
)->getCollection()
64+
->setPageSize(2)
65+
->setCurPage(1);
66+
67+
foreach ($ratingCollection as $rating) {
68+
$rating->setStores([$storeId])->setIsActive(1)->save();
69+
}
70+
71+
foreach ($ratingCollection as $rating) {
72+
$ratingOption = $objectManager
73+
->create(Option::class)
74+
->getCollection()
75+
->setPageSize(1)
76+
->setCurPage(2)
77+
->addRatingFilter($rating->getId())
78+
->getFirstItem();
79+
$rating->setReviewId($review->getId())
80+
->addOptionVote($ratingOption->getId(), $product->getId());
81+
}
82+
83+
$objectManager->get(Registry::class)->register(
84+
'rating_data',
85+
$ratingCollection->getFirstItem()
86+
);
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
use Magento\TestFramework\Workaround\Override\Fixture\Resolver;
9+
10+
Resolver::getInstance()->requireDataFixture('Magento/Customer/_files/customer_rollback.php');
11+
Resolver::getInstance()->requireDataFixture('Magento/Catalog/_files/product_simple_rollback.php');

0 commit comments

Comments
 (0)