Skip to content

Commit 154590b

Browse files
authored
ENGCOM-3572: added config for catalog review in frontend #18895
2 parents 827edbd + d45f2e3 commit 154590b

19 files changed

+285
-24
lines changed

app/code/Magento/Review/Block/Product/ReviewRenderer.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@
99

1010
use Magento\Catalog\Block\Product\ReviewRendererInterface;
1111
use Magento\Catalog\Model\Product;
12+
use Magento\Review\Observer\PredispatchReviewObserver;
1213

14+
/**
15+
* Class ReviewRenderer
16+
*/
1317
class ReviewRenderer extends \Magento\Framework\View\Element\Template implements ReviewRendererInterface
1418
{
1519
/**
@@ -43,6 +47,19 @@ public function __construct(
4347
parent::__construct($context, $data);
4448
}
4549

50+
/**
51+
* Review module availability
52+
*
53+
* @return string
54+
*/
55+
public function isReviewEnabled() : string
56+
{
57+
return $this->_scopeConfig->getValue(
58+
PredispatchReviewObserver::XML_PATH_REVIEW_ACTIVE,
59+
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
60+
);
61+
}
62+
4663
/**
4764
* Get review summary html
4865
*

app/code/Magento/Review/Model/ResourceModel/Rating.php

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,17 @@
55
*/
66
namespace Magento\Review\Model\ResourceModel;
77

8+
use Magento\Framework\App\Config\ScopeConfigInterface;
9+
use Magento\Framework\App\ObjectManager;
10+
811
/**
912
* Rating resource model
1013
*
1114
* @api
1215
*
1316
* @author Magento Core Team <[email protected]>
1417
* @since 100.0.2
18+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1519
*/
1620
class Rating extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
1721
{
@@ -34,26 +38,34 @@ class Rating extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
3438
*/
3539
protected $_logger;
3640

41+
/**
42+
* @var ScopeConfigInterface
43+
*/
44+
private $scopeConfig;
45+
3746
/**
3847
* @param \Magento\Framework\Model\ResourceModel\Db\Context $context
3948
* @param \Psr\Log\LoggerInterface $logger
4049
* @param \Magento\Framework\Module\Manager $moduleManager
4150
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
42-
* @param \Magento\Review\Model\ResourceModel\Review\Summary $reviewSummary
51+
* @param Review\Summary $reviewSummary
4352
* @param string $connectionName
53+
* @param ScopeConfigInterface|null $scopeConfig
4454
*/
4555
public function __construct(
4656
\Magento\Framework\Model\ResourceModel\Db\Context $context,
4757
\Psr\Log\LoggerInterface $logger,
4858
\Magento\Framework\Module\Manager $moduleManager,
4959
\Magento\Store\Model\StoreManagerInterface $storeManager,
5060
\Magento\Review\Model\ResourceModel\Review\Summary $reviewSummary,
51-
$connectionName = null
61+
$connectionName = null,
62+
ScopeConfigInterface $scopeConfig = null
5263
) {
5364
$this->moduleManager = $moduleManager;
5465
$this->_storeManager = $storeManager;
5566
$this->_logger = $logger;
5667
$this->_reviewSummary = $reviewSummary;
68+
$this->scopeConfig = $scopeConfig ?: ObjectManager::getInstance()->get(ScopeConfigInterface::class);
5769
parent::__construct($context, $connectionName);
5870
}
5971

@@ -286,7 +298,12 @@ protected function insertRatingData($table, array $data)
286298
protected function _afterDelete(\Magento\Framework\Model\AbstractModel $object)
287299
{
288300
parent::_afterDelete($object);
289-
if (!$this->moduleManager->isEnabled('Magento_Review')) {
301+
if (!$this->moduleManager->isEnabled('Magento_Review') &&
302+
!$this->scopeConfig->getValue(
303+
\Magento\Review\Observer\PredispatchReviewObserver::XML_PATH_REVIEW_ACTIVE,
304+
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
305+
)
306+
) {
290307
return $this;
291308
}
292309
$data = $this->_getEntitySummaryData($object);
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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\Observer;
9+
10+
use Magento\Framework\App\Config\ScopeConfigInterface;
11+
use Magento\Framework\Event\Observer;
12+
use Magento\Framework\Event\ObserverInterface;
13+
use Magento\Framework\UrlInterface;
14+
use Magento\Review\Block\Product\ReviewRenderer;
15+
use Magento\Store\Model\ScopeInterface;
16+
17+
/**
18+
* Class PredispatchReviewObserver
19+
*/
20+
class PredispatchReviewObserver implements ObserverInterface
21+
{
22+
/**
23+
* Configuration path to review active setting
24+
*/
25+
const XML_PATH_REVIEW_ACTIVE = 'catalog/review/active';
26+
27+
/**
28+
* @var ScopeConfigInterface
29+
*/
30+
private $scopeConfig;
31+
32+
/**
33+
* @var UrlInterface
34+
*/
35+
private $url;
36+
37+
/**
38+
* PredispatchReviewObserver constructor.
39+
*
40+
* @param ScopeConfigInterface $scopeConfig
41+
* @param UrlInterface $url
42+
*/
43+
public function __construct(
44+
ScopeConfigInterface $scopeConfig,
45+
UrlInterface $url
46+
) {
47+
$this->scopeConfig = $scopeConfig;
48+
$this->url = $url;
49+
}
50+
/**
51+
* Redirect review routes to 404 when review module is disabled.
52+
*
53+
* @param Observer $observer
54+
*/
55+
public function execute(Observer $observer)
56+
{
57+
if (!$this->scopeConfig->getValue(
58+
self::XML_PATH_REVIEW_ACTIVE,
59+
ScopeInterface::SCOPE_STORE
60+
)
61+
) {
62+
$defaultNoRouteUrl = $this->scopeConfig->getValue(
63+
'web/default/no_route',
64+
ScopeInterface::SCOPE_STORE
65+
);
66+
$redirectUrl = $this->url->getUrl($defaultNoRouteUrl);
67+
$observer->getControllerAction()
68+
->getResponse()
69+
->setRedirect($redirectUrl);
70+
}
71+
}
72+
}
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
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\Test\Unit\Observer;
9+
10+
use Magento\Framework\App\Config\ScopeConfigInterface;
11+
use Magento\Framework\App\Response\RedirectInterface;
12+
use Magento\Framework\App\ResponseInterface;
13+
use Magento\Framework\Event\Observer;
14+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
15+
use Magento\Framework\UrlInterface;
16+
use Magento\Review\Observer\PredispatchReviewObserver;
17+
use Magento\Store\Model\ScopeInterface;
18+
use PHPUnit\Framework\TestCase;
19+
20+
/**
21+
* Test class for \Magento\Review\Observer\PredispatchReviewObserver
22+
*/
23+
class PredispatchReviewObserverTest extends TestCase
24+
{
25+
/**
26+
* @var Observer|\PHPUnit_Framework_MockObject_MockObject
27+
*/
28+
private $mockObject;
29+
30+
/**
31+
* @var ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject
32+
*/
33+
private $configMock;
34+
35+
/**
36+
* @var UrlInterface|\PHPUnit_Framework_MockObject_MockObject
37+
*/
38+
private $urlMock;
39+
40+
/**
41+
* @var \Magento\Framework\App\Response\RedirectInterface|\PHPUnit_Framework_MockObject_MockObject
42+
*/
43+
private $redirectMock;
44+
45+
/**
46+
* @var ResponseInterface|\PHPUnit_Framework_MockObject_MockObject
47+
*/
48+
private $responseMock;
49+
50+
/**
51+
* @var ObjectManager
52+
*/
53+
private $objectManager;
54+
55+
/**
56+
* @inheritdoc
57+
*/
58+
protected function setUp() : void
59+
{
60+
$this->configMock = $this->getMockBuilder(ScopeConfigInterface::class)
61+
->disableOriginalConstructor()
62+
->getMock();
63+
$this->urlMock = $this->getMockBuilder(UrlInterface::class)
64+
->disableOriginalConstructor()
65+
->getMock();
66+
$this->responseMock = $this->getMockBuilder(ResponseInterface::class)
67+
->disableOriginalConstructor()
68+
->setMethods(['setRedirect'])
69+
->getMockForAbstractClass();
70+
$this->redirectMock = $this->getMockBuilder(RedirectInterface::class)
71+
->getMock();
72+
$this->objectManager = new ObjectManager($this);
73+
$this->mockObject = $this->objectManager->getObject(
74+
PredispatchReviewObserver::class,
75+
[
76+
'scopeConfig' => $this->configMock,
77+
'url' => $this->urlMock
78+
]
79+
);
80+
}
81+
82+
/**
83+
* Test with enabled review active config.
84+
*/
85+
public function testReviewEnabled() : void
86+
{
87+
$observerMock = $this->getMockBuilder(Observer::class)
88+
->disableOriginalConstructor()
89+
->setMethods(['getResponse', 'getData', 'setRedirect'])
90+
->getMockForAbstractClass();
91+
92+
$this->configMock->method('getValue')
93+
->with(PredispatchReviewObserver::XML_PATH_REVIEW_ACTIVE, ScopeInterface::SCOPE_STORE)
94+
->willReturn(true);
95+
$observerMock->expects($this->never())
96+
->method('getData')
97+
->with('controller_action')
98+
->willReturnSelf();
99+
100+
$observerMock->expects($this->never())
101+
->method('getResponse')
102+
->willReturnSelf();
103+
104+
$this->assertNull($this->mockObject->execute($observerMock));
105+
}
106+
107+
/**
108+
* Test with disabled review active config.
109+
*/
110+
public function testReviewDisabled() : void
111+
{
112+
$observerMock = $this->getMockBuilder(Observer::class)
113+
->disableOriginalConstructor()
114+
->setMethods(['getControllerAction', 'getResponse'])
115+
->getMockForAbstractClass();
116+
117+
$this->configMock->expects($this->at(0))
118+
->method('getValue')
119+
->with(PredispatchReviewObserver::XML_PATH_REVIEW_ACTIVE, ScopeInterface::SCOPE_STORE)
120+
->willReturn(false);
121+
122+
$expectedRedirectUrl = 'https://test.com/index';
123+
124+
$this->configMock->expects($this->at(1))
125+
->method('getValue')
126+
->with('web/default/no_route', ScopeInterface::SCOPE_STORE)
127+
->willReturn($expectedRedirectUrl);
128+
129+
$this->urlMock->expects($this->once())
130+
->method('getUrl')
131+
->willReturn($expectedRedirectUrl);
132+
133+
$observerMock->expects($this->once())
134+
->method('getControllerAction')
135+
->willReturnSelf();
136+
137+
$observerMock->expects($this->once())
138+
->method('getResponse')
139+
->willReturn($this->responseMock);
140+
141+
$this->responseMock->expects($this->once())
142+
->method('setRedirect')
143+
->with($expectedRedirectUrl);
144+
145+
$this->assertNull($this->mockObject->execute($observerMock));
146+
}
147+
}

app/code/Magento/Review/etc/adminhtml/system.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@
1010
<section id="catalog">
1111
<group id="review" translate="label" type="text" sortOrder="100" showInDefault="1" showInWebsite="1" showInStore="0">
1212
<label>Product Reviews</label>
13-
<field id="allow_guest" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="0" canRestore="1">
13+
<field id="active" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
14+
<label>Enabled</label>
15+
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
16+
</field>
17+
<field id="allow_guest" translate="label" type="select" sortOrder="2" showInDefault="1" showInWebsite="1" showInStore="0" canRestore="1">
1418
<label>Allow Guests to Write Reviews</label>
1519
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
1620
</field>

app/code/Magento/Review/etc/config.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
<default>
1010
<catalog>
1111
<review>
12+
<active>1</active>
1213
<allow_guest>1</allow_guest>
1314
</review>
1415
</catalog>

app/code/Magento/Review/etc/frontend/events.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,7 @@
1212
<event name="catalog_block_product_list_collection">
1313
<observer name="review" instance="Magento\Review\Observer\CatalogBlockProductCollectionBeforeToHtmlObserver" shared="false" />
1414
</event>
15+
<event name="controller_action_predispatch_review">
16+
<observer name="catalog_review_enabled" instance="Magento\Review\Observer\PredispatchReviewObserver" />
17+
</event>
1518
</config>

app/code/Magento/Review/view/frontend/layout/catalog_product_view.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<update handle="review_product_form_component"/>
1010
<body>
1111
<referenceContainer name="content">
12-
<block class="Magento\Cookie\Block\RequireCookie" name="require-cookie" template="Magento_Cookie::require_cookie.phtml">
12+
<block class="Magento\Cookie\Block\RequireCookie" name="require-cookie" template="Magento_Cookie::require_cookie.phtml" ifconfig="catalog/review/active">
1313
<arguments>
1414
<argument name="triggers" xsi:type="array">
1515
<item name="submitReviewButton" xsi:type="string">.review .action.submit</item>
@@ -18,8 +18,8 @@
1818
</block>
1919
</referenceContainer>
2020
<referenceBlock name="product.info.details">
21-
<block class="Magento\Review\Block\Product\Review" name="reviews.tab" as="reviews" template="Magento_Review::review.phtml" group="detailed_info">
22-
<block class="Magento\Review\Block\Form" name="product.review.form" as="review_form">
21+
<block class="Magento\Review\Block\Product\Review" name="reviews.tab" as="reviews" template="Magento_Review::review.phtml" group="detailed_info" ifconfig="catalog/review/active">
22+
<block class="Magento\Review\Block\Form" name="product.review.form" as="review_form" ifconfig="catalog/review/active">
2323
<container name="product.review.form.fields.before" as="form_fields_before" label="Review Form Fields Before"/>
2424
</block>
2525
</block>

app/code/Magento/Review/view/frontend/layout/checkout_cart_configure.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<update handle="catalog_product_view"/>
1010
<body>
1111
<referenceBlock name="reviews.tab">
12-
<block class="Magento\Review\Block\Form\Configure" name="product.review.form" as="review_form">
12+
<block class="Magento\Review\Block\Form\Configure" name="product.review.form" as="review_form" ifconfig="catalog/review/active">
1313
<arguments>
1414
<argument name="jsLayout" xsi:type="array">
1515
<item name="components" xsi:type="array">

app/code/Magento/Review/view/frontend/layout/customer_account.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
99
<body>
1010
<referenceBlock name="customer_account_navigation">
11-
<block class="Magento\Customer\Block\Account\SortLinkInterface" name="customer-account-navigation-product-reviews-link">
11+
<block class="Magento\Customer\Block\Account\SortLinkInterface" name="customer-account-navigation-product-reviews-link" ifconfig="catalog/review/active">
1212
<arguments>
1313
<argument name="path" xsi:type="string">review/customer</argument>
1414
<argument name="label" xsi:type="string" translate="true">My Product Reviews</argument>

app/code/Magento/Review/view/frontend/layout/customer_account_index.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
99
<body>
1010
<referenceContainer name="content">
11-
<block class="Magento\Review\Block\Customer\Recent" name="customer_account_dashboard_info1" template="Magento_Review::customer/recent.phtml" after="customer_account_dashboard_address" cacheable="false"/>
11+
<block class="Magento\Review\Block\Customer\Recent" name="customer_account_dashboard_info1" template="Magento_Review::customer/recent.phtml" after="customer_account_dashboard_address" cacheable="false" ifconfig="catalog/review/active"/>
1212
</referenceContainer>
1313
</body>
1414
</page>

0 commit comments

Comments
 (0)