-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added config for catalog review in frontend
- Loading branch information
Showing
19 changed files
with
288 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
app/code/Magento/Review/Observer/PredispatchReviewObserver.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
147 changes: 147 additions & 0 deletions
147
app/code/Magento/Review/Test/Unit/Observer/PredispatchReviewObserverTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ | |
<default> | ||
<catalog> | ||
<review> | ||
<active>1</active> | ||
<allow_guest>1</allow_guest> | ||
</review> | ||
</catalog> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.