Skip to content

Commit

Permalink
added config for catalog review in frontend
Browse files Browse the repository at this point in the history
  • Loading branch information
torhoehn committed Dec 22, 2018
1 parent 6b109f4 commit 4249ed8
Show file tree
Hide file tree
Showing 19 changed files with 288 additions and 28 deletions.
17 changes: 17 additions & 0 deletions app/code/Magento/Review/Block/Product/ReviewRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@

use Magento\Catalog\Block\Product\ReviewRendererInterface;
use Magento\Catalog\Model\Product;
use Magento\Review\Observer\PredispatchReviewObserver;

/**
* Class ReviewRenderer
*/
class ReviewRenderer extends \Magento\Framework\View\Element\Template implements ReviewRendererInterface
{
/**
Expand Down Expand Up @@ -43,6 +47,19 @@ public function __construct(
parent::__construct($context, $data);
}

/**
* Review module availability
*
* @return string
*/
public function isReviewEnabled() : string
{
return $this->_scopeConfig->getValue(
PredispatchReviewObserver::XML_PATH_REVIEW_ACTIVE,
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
);
}

/**
* Get review summary html
*
Expand Down
30 changes: 23 additions & 7 deletions app/code/Magento/Review/Model/ResourceModel/Rating.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
*/
namespace Magento\Review\Model\ResourceModel;

use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\ObjectManager;

/**
* Rating resource model
*
Expand Down Expand Up @@ -35,25 +38,33 @@ class Rating extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
protected $_logger;

/**
* @param \Magento\Framework\Model\ResourceModel\Db\Context $context
* @param \Psr\Log\LoggerInterface $logger
* @param \Magento\Framework\Module\Manager $moduleManager
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
* @var ScopeConfigInterface
*/
private $scopeConfig;

/**
* @param \Magento\Framework\Model\ResourceModel\Db\Context $context
* @param \Psr\Log\LoggerInterface $logger
* @param \Magento\Framework\Module\Manager $moduleManager
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
* @param \Magento\Review\Model\ResourceModel\Review\Summary $reviewSummary
* @param string $connectionName
* @param string $connectionName
* @param ScopeConfigInterface|null $scopeConfig
*/
public function __construct(
\Magento\Framework\Model\ResourceModel\Db\Context $context,
\Psr\Log\LoggerInterface $logger,
\Magento\Framework\Module\Manager $moduleManager,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Review\Model\ResourceModel\Review\Summary $reviewSummary,
$connectionName = null
$connectionName = null,
ScopeConfigInterface $scopeConfig = null
) {
$this->moduleManager = $moduleManager;
$this->_storeManager = $storeManager;
$this->_logger = $logger;
$this->_reviewSummary = $reviewSummary;
$this->scopeConfig = $scopeConfig ?: ObjectManager::getInstance()->get(ScopeConfigInterface::class);
parent::__construct($context, $connectionName);
}

Expand Down Expand Up @@ -277,7 +288,12 @@ protected function insertRatingData($table, array $data)
protected function _afterDelete(\Magento\Framework\Model\AbstractModel $object)
{
parent::_afterDelete($object);
if (!$this->moduleManager->isEnabled('Magento_Review')) {
if (!$this->moduleManager->isEnabled('Magento_Review') &&
!$this->scopeConfig->getValue(
\Magento\Review\Observer\PredispatchReviewObserver::XML_PATH_REVIEW_ACTIVE,
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
)
) {
return $this;
}
$data = $this->_getEntitySummaryData($object);
Expand Down
72 changes: 72 additions & 0 deletions app/code/Magento/Review/Observer/PredispatchReviewObserver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types = 1);

namespace Magento\Review\Observer;

use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\UrlInterface;
use Magento\Review\Block\Product\ReviewRenderer;
use Magento\Store\Model\ScopeInterface;

/**
* Class PredispatchReviewObserver
*/
class PredispatchReviewObserver implements ObserverInterface
{
/**
* Configuration path to review active setting
*/
const XML_PATH_REVIEW_ACTIVE = 'catalog/review/active';

/**
* @var ScopeConfigInterface
*/
private $scopeConfig;

/**
* @var UrlInterface
*/
private $url;

/**
* PredispatchReviewObserver constructor.
*
* @param ScopeConfigInterface $scopeConfig
* @param UrlInterface $url
*/
public function __construct(
ScopeConfigInterface $scopeConfig,
UrlInterface $url
) {
$this->scopeConfig = $scopeConfig;
$this->url = $url;
}
/**
* Redirect review routes to 404 when review module is disabled.
*
* @param Observer $observer
*/
public function execute(Observer $observer)
{
if (!$this->scopeConfig->getValue(
self::XML_PATH_REVIEW_ACTIVE,
ScopeInterface::SCOPE_STORE
)
) {
$defaultNoRouteUrl = $this->scopeConfig->getValue(
'web/default/no_route',
ScopeInterface::SCOPE_STORE
);
$redirectUrl = $this->url->getUrl($defaultNoRouteUrl);
$observer->getControllerAction()
->getResponse()
->setRedirect($redirectUrl);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types = 1);

namespace Magento\Review\Test\Unit\Observer;

use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\Response\RedirectInterface;
use Magento\Framework\App\ResponseInterface;
use Magento\Framework\Event\Observer;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use Magento\Framework\UrlInterface;
use Magento\Review\Observer\PredispatchReviewObserver;
use Magento\Store\Model\ScopeInterface;
use PHPUnit\Framework\TestCase;

/**
* Test class for \Magento\Review\Observer\PredispatchReviewObserver
*/
class PredispatchReviewObserverTest extends TestCase
{
/**
* @var Observer|\PHPUnit_Framework_MockObject_MockObject
*/
private $mockObject;

/**
* @var ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $configMock;

/**
* @var UrlInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $urlMock;

/**
* @var \Magento\Framework\App\Response\RedirectInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $redirectMock;

/**
* @var ResponseInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $responseMock;

/**
* @var ObjectManager
*/
private $objectManager;

/**
* @inheritdoc
*/
protected function setUp() : void
{
$this->configMock = $this->getMockBuilder(ScopeConfigInterface::class)
->disableOriginalConstructor()
->getMock();
$this->urlMock = $this->getMockBuilder(UrlInterface::class)
->disableOriginalConstructor()
->getMock();
$this->responseMock = $this->getMockBuilder(ResponseInterface::class)
->disableOriginalConstructor()
->setMethods(['setRedirect'])
->getMockForAbstractClass();
$this->redirectMock = $this->getMockBuilder(RedirectInterface::class)
->getMock();
$this->objectManager = new ObjectManager($this);
$this->mockObject = $this->objectManager->getObject(
PredispatchReviewObserver::class,
[
'scopeConfig' => $this->configMock,
'url' => $this->urlMock
]
);
}

/**
* Test with enabled review active config.
*/
public function testReviewEnabled() : void
{
$observerMock = $this->getMockBuilder(Observer::class)
->disableOriginalConstructor()
->setMethods(['getResponse', 'getData', 'setRedirect'])
->getMockForAbstractClass();

$this->configMock->method('getValue')
->with(PredispatchReviewObserver::XML_PATH_REVIEW_ACTIVE, ScopeInterface::SCOPE_STORE)
->willReturn(true);
$observerMock->expects($this->never())
->method('getData')
->with('controller_action')
->willReturnSelf();

$observerMock->expects($this->never())
->method('getResponse')
->willReturnSelf();

$this->assertNull($this->mockObject->execute($observerMock));
}

/**
* Test with disabled review active config.
*/
public function testReviewDisabled() : void
{
$observerMock = $this->getMockBuilder(Observer::class)
->disableOriginalConstructor()
->setMethods(['getControllerAction', 'getResponse'])
->getMockForAbstractClass();

$this->configMock->expects($this->at(0))
->method('getValue')
->with(PredispatchReviewObserver::XML_PATH_REVIEW_ACTIVE, ScopeInterface::SCOPE_STORE)
->willReturn(false);

$expectedRedirectUrl = 'https://test.com/index';

$this->configMock->expects($this->at(1))
->method('getValue')
->with('web/default/no_route', ScopeInterface::SCOPE_STORE)
->willReturn($expectedRedirectUrl);

$this->urlMock->expects($this->once())
->method('getUrl')
->willReturn($expectedRedirectUrl);

$observerMock->expects($this->once())
->method('getControllerAction')
->willReturnSelf();

$observerMock->expects($this->once())
->method('getResponse')
->willReturn($this->responseMock);

$this->responseMock->expects($this->once())
->method('setRedirect')
->with($expectedRedirectUrl);

$this->assertNull($this->mockObject->execute($observerMock));
}
}
6 changes: 5 additions & 1 deletion app/code/Magento/Review/etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
<section id="catalog">
<group id="review" translate="label" type="text" sortOrder="100" showInDefault="1" showInWebsite="1" showInStore="0">
<label>Product Reviews</label>
<field id="allow_guest" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="0" canRestore="1">
<field id="active" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
<label>Enabled</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
<field id="allow_guest" translate="label" type="select" sortOrder="2" showInDefault="1" showInWebsite="1" showInStore="0" canRestore="1">
<label>Allow Guests to Write Reviews</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
Expand Down
1 change: 1 addition & 0 deletions app/code/Magento/Review/etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<default>
<catalog>
<review>
<active>1</active>
<allow_guest>1</allow_guest>
</review>
</catalog>
Expand Down
3 changes: 3 additions & 0 deletions app/code/Magento/Review/etc/frontend/events.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,7 @@
<event name="catalog_block_product_list_collection">
<observer name="review" instance="Magento\Review\Observer\CatalogBlockProductCollectionBeforeToHtmlObserver" shared="false" />
</event>
<event name="controller_action_predispatch_review">
<observer name="catalog_review_enabled" instance="Magento\Review\Observer\PredispatchReviewObserver" />
</event>
</config>
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<update handle="review_product_form_component"/>
<body>
<referenceContainer name="content">
<block class="Magento\Cookie\Block\RequireCookie" name="require-cookie" template="Magento_Cookie::require_cookie.phtml">
<block class="Magento\Cookie\Block\RequireCookie" name="require-cookie" template="Magento_Cookie::require_cookie.phtml" ifconfig="catalog/review/active">
<arguments>
<argument name="triggers" xsi:type="array">
<item name="submitReviewButton" xsi:type="string">.review .action.submit</item>
Expand All @@ -18,8 +18,8 @@
</block>
</referenceContainer>
<referenceBlock name="product.info.details">
<block class="Magento\Review\Block\Product\Review" name="reviews.tab" as="reviews" template="Magento_Review::review.phtml" group="detailed_info">
<block class="Magento\Review\Block\Form" name="product.review.form" as="review_form">
<block class="Magento\Review\Block\Product\Review" name="reviews.tab" as="reviews" template="Magento_Review::review.phtml" group="detailed_info" ifconfig="catalog/review/active">
<block class="Magento\Review\Block\Form" name="product.review.form" as="review_form" ifconfig="catalog/review/active">
<container name="product.review.form.fields.before" as="form_fields_before" label="Review Form Fields Before"/>
</block>
</block>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<update handle="catalog_product_view"/>
<body>
<referenceBlock name="reviews.tab">
<block class="Magento\Review\Block\Form\Configure" name="product.review.form" as="review_form">
<block class="Magento\Review\Block\Form\Configure" name="product.review.form" as="review_form" ifconfig="catalog/review/active">
<arguments>
<argument name="jsLayout" xsi:type="array">
<item name="components" xsi:type="array">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceBlock name="customer_account_navigation">
<block class="Magento\Customer\Block\Account\SortLinkInterface" name="customer-account-navigation-product-reviews-link">
<block class="Magento\Customer\Block\Account\SortLinkInterface" name="customer-account-navigation-product-reviews-link" ifconfig="catalog/review/active">
<arguments>
<argument name="path" xsi:type="string">review/customer</argument>
<argument name="label" xsi:type="string" translate="true">My Product Reviews</argument>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="content">
<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"/>
<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"/>
</referenceContainer>
</body>
</page>
Loading

0 comments on commit 4249ed8

Please sign in to comment.