Skip to content
This repository was archived by the owner on Dec 19, 2019. It is now read-only.
Merged
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
15 changes: 12 additions & 3 deletions app/code/Magento/Customer/Block/Form/Register.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
namespace Magento\Customer\Block\Form;

use Magento\Customer\Model\AccountManagement;
use Magento\Newsletter\Observer\PredispatchNewsletterObserver;
use Magento\Framework\App\ObjectManager;
use Magento\Newsletter\Model\Config;

/**
* Customer register form block
Expand All @@ -32,6 +33,11 @@ class Register extends \Magento\Directory\Block\Data
*/
protected $_customerUrl;

/**
* @var Config
*/
private $newsLetterConfig;

/**
* Constructor
*
Expand All @@ -45,6 +51,7 @@ class Register extends \Magento\Directory\Block\Data
* @param \Magento\Customer\Model\Session $customerSession
* @param \Magento\Customer\Model\Url $customerUrl
* @param array $data
* @param Config $newsLetterConfig
*
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
Expand All @@ -58,11 +65,13 @@ public function __construct(
\Magento\Framework\Module\ModuleManagerInterface $moduleManager,
\Magento\Customer\Model\Session $customerSession,
\Magento\Customer\Model\Url $customerUrl,
array $data = []
array $data = [],
Config $newsLetterConfig = null
) {
$this->_customerUrl = $customerUrl;
$this->_moduleManager = $moduleManager;
$this->_customerSession = $customerSession;
$this->newsLetterConfig = $newsLetterConfig ?: ObjectManager::getInstance()->get(Config::class);
parent::__construct(
$context,
$directoryHelper,
Expand Down Expand Up @@ -170,7 +179,7 @@ public function getRegion()
public function isNewsletterEnabled()
{
return $this->_moduleManager->isOutputEnabled('Magento_Newsletter')
&& $this->getConfig(PredispatchNewsletterObserver::XML_PATH_NEWSLETTER_ACTIVE);
&& $this->newsLetterConfig->isActive();
}

/**
Expand Down
15 changes: 9 additions & 6 deletions app/code/Magento/Customer/Test/Unit/Block/Form/RegisterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

use Magento\Customer\Block\Form\Register;
use Magento\Customer\Model\AccountManagement;
use Magento\Newsletter\Observer\PredispatchNewsletterObserver;

/**
* Test class for \Magento\Customer\Block\Form\Register.
Expand Down Expand Up @@ -49,6 +48,9 @@ class RegisterTest extends \PHPUnit\Framework\TestCase
/** @var Register */
private $_block;

/** @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Newsletter\Model\Config */
private $newsletterConfig;

protected function setUp()
{
$this->_scopeConfig = $this->createMock(\Magento\Framework\App\Config\ScopeConfigInterface::class);
Expand All @@ -59,6 +61,7 @@ protected function setUp()
\Magento\Customer\Model\Session::class,
['getCustomerFormData']
);
$this->newsletterConfig = $this->createMock(\Magento\Newsletter\Model\Config::class);
$context = $this->createMock(\Magento\Framework\View\Element\Template\Context::class);
$context->expects($this->any())->method('getScopeConfig')->will($this->returnValue($this->_scopeConfig));

Expand All @@ -71,7 +74,9 @@ protected function setUp()
$this->createMock(\Magento\Directory\Model\ResourceModel\Country\CollectionFactory::class),
$this->_moduleManager,
$this->_customerSession,
$this->_customerUrl
$this->_customerUrl,
[],
$this->newsletterConfig
);
}

Expand Down Expand Up @@ -293,12 +298,10 @@ public function testIsNewsletterEnabled($isNewsletterEnabled, $isNewsletterActiv
$this->returnValue($isNewsletterEnabled)
);

$this->_scopeConfig->expects(
$this->newsletterConfig->expects(
$this->any()
)->method(
'getValue'
)->with(
PredispatchNewsletterObserver::XML_PATH_NEWSLETTER_ACTIVE
'isActive'
)->will(
$this->returnValue($isNewsletterActive)
);
Expand Down
48 changes: 48 additions & 0 deletions app/code/Magento/Newsletter/Model/Config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Newsletter\Model;

use Magento\Framework\App\Config\ScopeConfigInterface;

/**
* Newsletter configuration model
*/
class Config
{
/**
* Configuration path to newsletter active setting
*/
private const XML_PATH_NEWSLETTER_ACTIVE = 'newsletter/general/active';

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

/**
* Config constructor.
*
* @param ScopeConfigInterface $scopeConfig
*/
public function __construct(
ScopeConfigInterface $scopeConfig
) {
$this->scopeConfig = $scopeConfig;
}

/**
* Returns newsletter's enabled status
*
* @param string $scopeType
* @return bool
*/
public function isActive(string $scopeType = ScopeConfigInterface::SCOPE_TYPE_DEFAULT): bool
{
return $this->scopeConfig->isSetFlag(self::XML_PATH_NEWSLETTER_ACTIVE, $scopeType);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,25 @@
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\UrlInterface;
use Magento\Store\Model\ScopeInterface;
use Magento\Newsletter\Model\Config;
use Magento\Framework\App\ObjectManager;

/**
* Class PredispatchNewsletterObserver
*/
class PredispatchNewsletterObserver implements ObserverInterface
{
/**
* Configuration path to newsletter active setting
* @deprecated
* @see \Magento\Newsletter\Model\Config::isActive()
*/
const XML_PATH_NEWSLETTER_ACTIVE = 'newsletter/general/active';

/**
* @var Config
*/
private $newsletterConfig;

/**
* @var ScopeConfigInterface
*/
Expand All @@ -38,11 +46,16 @@ class PredispatchNewsletterObserver implements ObserverInterface
*
* @param ScopeConfigInterface $scopeConfig
* @param UrlInterface $url
* @param Config|null $newsletterConfig
*/
public function __construct(ScopeConfigInterface $scopeConfig, UrlInterface $url)
{
public function __construct(
ScopeConfigInterface $scopeConfig,
UrlInterface $url,
Config $newsletterConfig = null
) {
$this->scopeConfig = $scopeConfig;
$this->url = $url;
$this->newsletterConfig = $newsletterConfig ?: ObjectManager::getInstance()->get(Config::class);
}

/**
Expand All @@ -52,11 +65,7 @@ public function __construct(ScopeConfigInterface $scopeConfig, UrlInterface $url
*/
public function execute(Observer $observer) : void
{
if (!$this->scopeConfig->getValue(
self::XML_PATH_NEWSLETTER_ACTIVE,
ScopeInterface::SCOPE_STORE
)
) {
if (!$this->newsletterConfig->isActive(ScopeInterface::SCOPE_STORE)) {
$defaultNoRouteUrl = $this->scopeConfig->getValue(
'web/default/no_route',
ScopeInterface::SCOPE_STORE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Magento\Framework\Event\Observer;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use Magento\Framework\UrlInterface;
use Magento\Newsletter\Model\Config;
use Magento\Newsletter\Observer\PredispatchNewsletterObserver;
use Magento\Store\Model\ScopeInterface;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -52,30 +53,29 @@ class PredispatchNewsletterObserverTest extends TestCase
*/
private $objectManager;

/**
* @var Config
*/
private $newsletterConfig;

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

Expand All @@ -89,8 +89,9 @@ public function testNewsletterEnabled() : void
->setMethods(['getResponse', 'getData', 'setRedirect'])
->getMockForAbstractClass();

$this->configMock->method('getValue')
->with(PredispatchNewsletterObserver::XML_PATH_NEWSLETTER_ACTIVE, ScopeInterface::SCOPE_STORE)
$this->newsletterConfig->expects($this->once())
->method('isActive')
->with(ScopeInterface::SCOPE_STORE)
->willReturn(true);
$observerMock->expects($this->never())
->method('getData')
Expand All @@ -114,14 +115,14 @@ public function testNewsletterDisabled() : void
->setMethods(['getControllerAction', 'getResponse'])
->getMockForAbstractClass();

$this->configMock->expects($this->at(0))
->method('getValue')
->with(PredispatchNewsletterObserver::XML_PATH_NEWSLETTER_ACTIVE, ScopeInterface::SCOPE_STORE)
$this->newsletterConfig->expects($this->once())
->method('isActive')
->with(ScopeInterface::SCOPE_STORE)
->willReturn(false);

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

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