Skip to content

Commit c7b7585

Browse files
committed
magento/graphql-ce#762: Subscribe for newsletter even when not allowed
1 parent 3f90ebd commit c7b7585

File tree

5 files changed

+50
-35
lines changed

5 files changed

+50
-35
lines changed

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

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

88
use Magento\Customer\Model\AccountManagement;
9+
use Magento\Framework\App\ObjectManager;
10+
use Magento\Newsletter\Model\Config;
911

1012
/**
1113
* Customer register form block
@@ -31,6 +33,11 @@ class Register extends \Magento\Directory\Block\Data
3133
*/
3234
protected $_customerUrl;
3335

36+
/**
37+
* @var Config
38+
*/
39+
private $newsLetterConfig;
40+
3441
/**
3542
* Constructor
3643
*
@@ -44,6 +51,7 @@ class Register extends \Magento\Directory\Block\Data
4451
* @param \Magento\Customer\Model\Session $customerSession
4552
* @param \Magento\Customer\Model\Url $customerUrl
4653
* @param array $data
54+
* @param Config $newsLetterConfig
4755
*
4856
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
4957
*/
@@ -57,11 +65,13 @@ public function __construct(
5765
\Magento\Framework\Module\ModuleManagerInterface $moduleManager,
5866
\Magento\Customer\Model\Session $customerSession,
5967
\Magento\Customer\Model\Url $customerUrl,
60-
array $data = []
68+
array $data = [],
69+
Config $newsLetterConfig = null
6170
) {
6271
$this->_customerUrl = $customerUrl;
6372
$this->_moduleManager = $moduleManager;
6473
$this->_customerSession = $customerSession;
74+
$this->newsLetterConfig = $newsLetterConfig ?: ObjectManager::getInstance()->get(Config::class);
6575
parent::__construct(
6676
$context,
6777
$directoryHelper,
@@ -169,7 +179,7 @@ public function getRegion()
169179
public function isNewsletterEnabled()
170180
{
171181
return $this->_moduleManager->isOutputEnabled('Magento_Newsletter')
172-
&& $this->getConfig(\Magento\Newsletter\Model\Config::XML_PATH_NEWSLETTER_ACTIVE);
182+
&& $this->newsLetterConfig->isActive();
173183
}
174184

175185
/**

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ class RegisterTest extends \PHPUnit\Framework\TestCase
4848
/** @var Register */
4949
private $_block;
5050

51+
/** @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Newsletter\Model\Config */
52+
private $newsletterConfig;
53+
5154
protected function setUp()
5255
{
5356
$this->_scopeConfig = $this->createMock(\Magento\Framework\App\Config\ScopeConfigInterface::class);
@@ -58,6 +61,7 @@ protected function setUp()
5861
\Magento\Customer\Model\Session::class,
5962
['getCustomerFormData']
6063
);
64+
$this->newsletterConfig = $this->createMock(\Magento\Newsletter\Model\Config::class);
6165
$context = $this->createMock(\Magento\Framework\View\Element\Template\Context::class);
6266
$context->expects($this->any())->method('getScopeConfig')->will($this->returnValue($this->_scopeConfig));
6367

@@ -70,7 +74,9 @@ protected function setUp()
7074
$this->createMock(\Magento\Directory\Model\ResourceModel\Country\CollectionFactory::class),
7175
$this->_moduleManager,
7276
$this->_customerSession,
73-
$this->_customerUrl
77+
$this->_customerUrl,
78+
[],
79+
$this->newsletterConfig
7480
);
7581
}
7682

@@ -292,12 +298,10 @@ public function testIsNewsletterEnabled($isNewsletterEnabled, $isNewsletterActiv
292298
$this->returnValue($isNewsletterEnabled)
293299
);
294300

295-
$this->_scopeConfig->expects(
301+
$this->newsletterConfig->expects(
296302
$this->any()
297303
)->method(
298-
'getValue'
299-
)->with(
300-
\Magento\Newsletter\Model\Config::XML_PATH_NEWSLETTER_ACTIVE
304+
'isActive'
301305
)->will(
302306
$this->returnValue($isNewsletterActive)
303307
);

app/code/Magento/Newsletter/Model/Config.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ class Config
1717
/**
1818
* Configuration path to newsletter active setting
1919
*/
20-
const XML_PATH_NEWSLETTER_ACTIVE = 'newsletter/general/active';
20+
private const XML_PATH_NEWSLETTER_ACTIVE = 'newsletter/general/active';
2121

2222
/**
2323
* @var ScopeConfigInterface
2424
*/
25-
protected $scopeConfig;
25+
private $scopeConfig;
2626

2727
/**
2828
* Config constructor.
@@ -41,7 +41,7 @@ public function __construct(
4141
* @param string $scopeType
4242
* @return bool
4343
*/
44-
public function isActive($scopeType = ScopeConfigInterface::SCOPE_TYPE_DEFAULT): bool
44+
public function isActive(string $scopeType = ScopeConfigInterface::SCOPE_TYPE_DEFAULT): bool
4545
{
4646
return $this->scopeConfig->isSetFlag(self::XML_PATH_NEWSLETTER_ACTIVE, $scopeType);
4747
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ class PredispatchNewsletterObserver implements ObserverInterface
2222
{
2323
/**
2424
* @deprecated
25-
* use Magento\Newsletter\Model\Config::XML_PATH_NEWSLETTER_ACTIVE instead
25+
* @see \Magento\Newsletter\Model\Config::isActive()
2626
*/
2727
const XML_PATH_NEWSLETTER_ACTIVE = 'newsletter/general/active';
2828

2929
/**
3030
* @var Newsletter Config
3131
*/
32-
private $config;
32+
private $newsletterConfig;
3333

3434
/**
3535
* @var ScopeConfigInterface
@@ -51,11 +51,11 @@ class PredispatchNewsletterObserver implements ObserverInterface
5151
public function __construct(
5252
ScopeConfigInterface $scopeConfig,
5353
UrlInterface $url,
54-
Config $config = null
54+
Config $newsletterConfig = null
5555
) {
5656
$this->scopeConfig = $scopeConfig;
5757
$this->url = $url;
58-
$this->config = $config ?: ObjectManager::getInstance()->get(Config::class);
58+
$this->newsletterConfig = $newsletterConfig ?: ObjectManager::getInstance()->get(Config::class);
5959
}
6060

6161
/**
@@ -65,7 +65,7 @@ public function __construct(
6565
*/
6666
public function execute(Observer $observer) : void
6767
{
68-
if (!$this->config->isActive(ScopeInterface::SCOPE_STORE)) {
68+
if (!$this->newsletterConfig->isActive(ScopeInterface::SCOPE_STORE)) {
6969
$defaultNoRouteUrl = $this->scopeConfig->getValue(
7070
'web/default/no_route',
7171
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(\Magento\Newsletter\Model\Config::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(\Magento\Newsletter\Model\Config::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)