Skip to content

Commit 615642e

Browse files
ENGCOM-5636: Set Newsletter Config #807
- Merge Pull Request magento/graphql-ce#807 from sedonik/graphql-ce:predispatch-newsletter-config - Merged commits: 1. 8e07181 2. b185332 3. 2b12036 4. dc4cc89 5. 3f90ebd 6. c7b7585 7. 98dc804
2 parents 74aa9c6 + 98dc804 commit 615642e

File tree

5 files changed

+107
-37
lines changed

5 files changed

+107
-37
lines changed

app/code/Magento/Customer/Block/Form/Register.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
namespace Magento\Customer\Block\Form;
77

88
use Magento\Customer\Model\AccountManagement;
9-
use Magento\Newsletter\Observer\PredispatchNewsletterObserver;
9+
use Magento\Framework\App\ObjectManager;
10+
use Magento\Newsletter\Model\Config;
1011

1112
/**
1213
* Customer register form block
@@ -32,6 +33,11 @@ class Register extends \Magento\Directory\Block\Data
3233
*/
3334
protected $_customerUrl;
3435

36+
/**
37+
* @var Config
38+
*/
39+
private $newsLetterConfig;
40+
3541
/**
3642
* Constructor
3743
*
@@ -45,6 +51,7 @@ class Register extends \Magento\Directory\Block\Data
4551
* @param \Magento\Customer\Model\Session $customerSession
4652
* @param \Magento\Customer\Model\Url $customerUrl
4753
* @param array $data
54+
* @param Config $newsLetterConfig
4855
*
4956
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
5057
*/
@@ -58,11 +65,13 @@ public function __construct(
5865
\Magento\Framework\Module\ModuleManagerInterface $moduleManager,
5966
\Magento\Customer\Model\Session $customerSession,
6067
\Magento\Customer\Model\Url $customerUrl,
61-
array $data = []
68+
array $data = [],
69+
Config $newsLetterConfig = null
6270
) {
6371
$this->_customerUrl = $customerUrl;
6472
$this->_moduleManager = $moduleManager;
6573
$this->_customerSession = $customerSession;
74+
$this->newsLetterConfig = $newsLetterConfig ?: ObjectManager::getInstance()->get(Config::class);
6675
parent::__construct(
6776
$context,
6877
$directoryHelper,
@@ -170,7 +179,7 @@ public function getRegion()
170179
public function isNewsletterEnabled()
171180
{
172181
return $this->_moduleManager->isOutputEnabled('Magento_Newsletter')
173-
&& $this->getConfig(PredispatchNewsletterObserver::XML_PATH_NEWSLETTER_ACTIVE);
182+
&& $this->newsLetterConfig->isActive();
174183
}
175184

176185
/**

app/code/Magento/Customer/Test/Unit/Block/Form/RegisterTest.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
use Magento\Customer\Block\Form\Register;
99
use Magento\Customer\Model\AccountManagement;
10-
use Magento\Newsletter\Observer\PredispatchNewsletterObserver;
1110

1211
/**
1312
* Test class for \Magento\Customer\Block\Form\Register.
@@ -49,6 +48,9 @@ class RegisterTest extends \PHPUnit\Framework\TestCase
4948
/** @var Register */
5049
private $_block;
5150

51+
/** @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Newsletter\Model\Config */
52+
private $newsletterConfig;
53+
5254
protected function setUp()
5355
{
5456
$this->_scopeConfig = $this->createMock(\Magento\Framework\App\Config\ScopeConfigInterface::class);
@@ -59,6 +61,7 @@ protected function setUp()
5961
\Magento\Customer\Model\Session::class,
6062
['getCustomerFormData']
6163
);
64+
$this->newsletterConfig = $this->createMock(\Magento\Newsletter\Model\Config::class);
6265
$context = $this->createMock(\Magento\Framework\View\Element\Template\Context::class);
6366
$context->expects($this->any())->method('getScopeConfig')->will($this->returnValue($this->_scopeConfig));
6467

@@ -71,7 +74,9 @@ protected function setUp()
7174
$this->createMock(\Magento\Directory\Model\ResourceModel\Country\CollectionFactory::class),
7275
$this->_moduleManager,
7376
$this->_customerSession,
74-
$this->_customerUrl
77+
$this->_customerUrl,
78+
[],
79+
$this->newsletterConfig
7580
);
7681
}
7782

@@ -293,12 +298,10 @@ public function testIsNewsletterEnabled($isNewsletterEnabled, $isNewsletterActiv
293298
$this->returnValue($isNewsletterEnabled)
294299
);
295300

296-
$this->_scopeConfig->expects(
301+
$this->newsletterConfig->expects(
297302
$this->any()
298303
)->method(
299-
'getValue'
300-
)->with(
301-
PredispatchNewsletterObserver::XML_PATH_NEWSLETTER_ACTIVE
304+
'isActive'
302305
)->will(
303306
$this->returnValue($isNewsletterActive)
304307
);
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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\Newsletter\Model;
9+
10+
use Magento\Framework\App\Config\ScopeConfigInterface;
11+
12+
/**
13+
* Newsletter configuration model
14+
*/
15+
class Config
16+
{
17+
/**
18+
* Configuration path to newsletter active setting
19+
*/
20+
private const XML_PATH_NEWSLETTER_ACTIVE = 'newsletter/general/active';
21+
22+
/**
23+
* @var ScopeConfigInterface
24+
*/
25+
private $scopeConfig;
26+
27+
/**
28+
* Config constructor.
29+
*
30+
* @param ScopeConfigInterface $scopeConfig
31+
*/
32+
public function __construct(
33+
ScopeConfigInterface $scopeConfig
34+
) {
35+
$this->scopeConfig = $scopeConfig;
36+
}
37+
38+
/**
39+
* Returns newsletter's enabled status
40+
*
41+
* @param string $scopeType
42+
* @return bool
43+
*/
44+
public function isActive(string $scopeType = ScopeConfigInterface::SCOPE_TYPE_DEFAULT): bool
45+
{
46+
return $this->scopeConfig->isSetFlag(self::XML_PATH_NEWSLETTER_ACTIVE, $scopeType);
47+
}
48+
}

app/code/Magento/Newsletter/Observer/PredispatchNewsletterObserver.php

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,25 @@
1212
use Magento\Framework\Event\ObserverInterface;
1313
use Magento\Framework\UrlInterface;
1414
use Magento\Store\Model\ScopeInterface;
15+
use Magento\Newsletter\Model\Config;
16+
use Magento\Framework\App\ObjectManager;
1517

1618
/**
1719
* Class PredispatchNewsletterObserver
1820
*/
1921
class PredispatchNewsletterObserver implements ObserverInterface
2022
{
2123
/**
22-
* Configuration path to newsletter active setting
24+
* @deprecated
25+
* @see \Magento\Newsletter\Model\Config::isActive()
2326
*/
2427
const XML_PATH_NEWSLETTER_ACTIVE = 'newsletter/general/active';
2528

29+
/**
30+
* @var Config
31+
*/
32+
private $newsletterConfig;
33+
2634
/**
2735
* @var ScopeConfigInterface
2836
*/
@@ -38,11 +46,16 @@ class PredispatchNewsletterObserver implements ObserverInterface
3846
*
3947
* @param ScopeConfigInterface $scopeConfig
4048
* @param UrlInterface $url
49+
* @param Config|null $newsletterConfig
4150
*/
42-
public function __construct(ScopeConfigInterface $scopeConfig, UrlInterface $url)
43-
{
51+
public function __construct(
52+
ScopeConfigInterface $scopeConfig,
53+
UrlInterface $url,
54+
Config $newsletterConfig = null
55+
) {
4456
$this->scopeConfig = $scopeConfig;
4557
$this->url = $url;
58+
$this->newsletterConfig = $newsletterConfig ?: ObjectManager::getInstance()->get(Config::class);
4659
}
4760

4861
/**
@@ -52,11 +65,7 @@ public function __construct(ScopeConfigInterface $scopeConfig, UrlInterface $url
5265
*/
5366
public function execute(Observer $observer) : void
5467
{
55-
if (!$this->scopeConfig->getValue(
56-
self::XML_PATH_NEWSLETTER_ACTIVE,
57-
ScopeInterface::SCOPE_STORE
58-
)
59-
) {
68+
if (!$this->newsletterConfig->isActive(ScopeInterface::SCOPE_STORE)) {
6069
$defaultNoRouteUrl = $this->scopeConfig->getValue(
6170
'web/default/no_route',
6271
ScopeInterface::SCOPE_STORE

app/code/Magento/Newsletter/Test/Unit/Observer/PredispatchNewsletterObserverTest.php

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Magento\Framework\Event\Observer;
1414
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
1515
use Magento\Framework\UrlInterface;
16+
use Magento\Newsletter\Model\Config;
1617
use Magento\Newsletter\Observer\PredispatchNewsletterObserver;
1718
use Magento\Store\Model\ScopeInterface;
1819
use PHPUnit\Framework\TestCase;
@@ -52,30 +53,29 @@ class PredispatchNewsletterObserverTest extends TestCase
5253
*/
5354
private $objectManager;
5455

56+
/**
57+
* @var Config
58+
*/
59+
private $newsletterConfig;
60+
5561
/**
5662
* @inheritdoc
5763
*/
5864
protected function setUp() : void
5965
{
60-
$this->configMock = $this->getMockBuilder(ScopeConfigInterface::class)
61-
->disableOriginalConstructor()
62-
->getMock();
63-
$this->urlMock = $this->getMockBuilder(UrlInterface::class)
64-
->disableOriginalConstructor()
65-
->getMock();
66+
$this->configMock = $this->createMock(ScopeConfigInterface::class);
67+
$this->urlMock = $this->createMock(UrlInterface::class);
6668
$this->responseMock = $this->getMockBuilder(ResponseInterface::class)
6769
->disableOriginalConstructor()
6870
->setMethods(['setRedirect'])
6971
->getMockForAbstractClass();
70-
$this->redirectMock = $this->getMockBuilder(RedirectInterface::class)
71-
->getMock();
72+
$this->redirectMock = $this->createMock(RedirectInterface::class);
73+
$this->newsletterConfig = $this->createMock(Config::class);
7274
$this->objectManager = new ObjectManager($this);
73-
$this->mockObject = $this->objectManager->getObject(
74-
PredispatchNewsletterObserver::class,
75-
[
76-
'scopeConfig' => $this->configMock,
77-
'url' => $this->urlMock
78-
]
75+
$this->mockObject = new PredispatchNewsletterObserver(
76+
$this->configMock,
77+
$this->urlMock,
78+
$this->newsletterConfig
7979
);
8080
}
8181

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

92-
$this->configMock->method('getValue')
93-
->with(PredispatchNewsletterObserver::XML_PATH_NEWSLETTER_ACTIVE, ScopeInterface::SCOPE_STORE)
92+
$this->newsletterConfig->expects($this->once())
93+
->method('isActive')
94+
->with(ScopeInterface::SCOPE_STORE)
9495
->willReturn(true);
9596
$observerMock->expects($this->never())
9697
->method('getData')
@@ -114,14 +115,14 @@ public function testNewsletterDisabled() : void
114115
->setMethods(['getControllerAction', 'getResponse'])
115116
->getMockForAbstractClass();
116117

117-
$this->configMock->expects($this->at(0))
118-
->method('getValue')
119-
->with(PredispatchNewsletterObserver::XML_PATH_NEWSLETTER_ACTIVE, ScopeInterface::SCOPE_STORE)
118+
$this->newsletterConfig->expects($this->once())
119+
->method('isActive')
120+
->with(ScopeInterface::SCOPE_STORE)
120121
->willReturn(false);
121122

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

124-
$this->configMock->expects($this->at(1))
125+
$this->configMock->expects($this->once())
125126
->method('getValue')
126127
->with('web/default/no_route', ScopeInterface::SCOPE_STORE)
127128
->willReturn($expectedRedirectUrl);

0 commit comments

Comments
 (0)