Skip to content

Commit c5569e2

Browse files
authored
ENGCOM-3134: added config for newsletter in frontend #18407
2 parents 47acae6 + 18aa4ea commit c5569e2

File tree

9 files changed

+265
-8
lines changed

9 files changed

+265
-8
lines changed

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

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

88
use Magento\Customer\Model\AccountManagement;
9+
use Magento\Newsletter\Observer\PredispatchNewsletterObserver;
910

1011
/**
1112
* Customer register form block
@@ -86,6 +87,8 @@ public function getConfig($path)
8687
}
8788

8889
/**
90+
* Prepare layout
91+
*
8992
* @return $this
9093
*/
9194
protected function _prepareLayout()
@@ -177,11 +180,13 @@ public function getRegion()
177180
*/
178181
public function isNewsletterEnabled()
179182
{
180-
return $this->_moduleManager->isOutputEnabled('Magento_Newsletter');
183+
return $this->_moduleManager->isOutputEnabled('Magento_Newsletter')
184+
&& $this->getConfig(PredispatchNewsletterObserver::XML_PATH_NEWSLETTER_ACTIVE);
181185
}
182186

183187
/**
184188
* Restore entity data from session
189+
*
185190
* Entity and form code must be defined for the form
186191
*
187192
* @param \Magento\Customer\Model\Metadata\Form $form

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

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

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

1112
/**
1213
* Test class for \Magento\Customer\Block\Form\Register.
@@ -274,12 +275,13 @@ public function testGetRegionNull()
274275
}
275276

276277
/**
277-
* @param $isNewsletterEnabled
278-
* @param $expectedValue
278+
* @param boolean $isNewsletterEnabled
279+
* @param string $isNewsletterActive
280+
* @param boolean $expectedValue
279281
*
280282
* @dataProvider isNewsletterEnabledProvider
281283
*/
282-
public function testIsNewsletterEnabled($isNewsletterEnabled, $expectedValue)
284+
public function testIsNewsletterEnabled($isNewsletterEnabled, $isNewsletterActive, $expectedValue)
283285
{
284286
$this->_moduleManager->expects(
285287
$this->once()
@@ -290,6 +292,17 @@ public function testIsNewsletterEnabled($isNewsletterEnabled, $expectedValue)
290292
)->will(
291293
$this->returnValue($isNewsletterEnabled)
292294
);
295+
296+
$this->_scopeConfig->expects(
297+
$this->any()
298+
)->method(
299+
'getValue'
300+
)->with(
301+
PredispatchNewsletterObserver::XML_PATH_NEWSLETTER_ACTIVE
302+
)->will(
303+
$this->returnValue($isNewsletterActive)
304+
);
305+
293306
$this->assertEquals($expectedValue, $this->_block->isNewsletterEnabled());
294307
}
295308

@@ -298,7 +311,7 @@ public function testIsNewsletterEnabled($isNewsletterEnabled, $expectedValue)
298311
*/
299312
public function isNewsletterEnabledProvider()
300313
{
301-
return [[true, true], [false, false]];
314+
return [[true, true, true], [true, false, false], [false, true, false], [false, false, false]];
302315
}
303316

304317
/**
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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\Observer;
9+
10+
use Magento\Framework\App\Config\ScopeConfigInterface;
11+
use Magento\Framework\Event\Observer;
12+
use Magento\Framework\Event\ObserverInterface;
13+
use Magento\Framework\UrlInterface;
14+
use Magento\Store\Model\ScopeInterface;
15+
16+
/**
17+
* Class PredispatchNewsletterObserver
18+
*/
19+
class PredispatchNewsletterObserver implements ObserverInterface
20+
{
21+
/**
22+
* Configuration path to newsletter active setting
23+
*/
24+
const XML_PATH_NEWSLETTER_ACTIVE = 'newsletter/general/active';
25+
26+
/**
27+
* @var ScopeConfigInterface
28+
*/
29+
private $scopeConfig;
30+
31+
/**
32+
* @var UrlInterface
33+
*/
34+
private $url;
35+
36+
/**
37+
* PredispatchNewsletterObserver constructor.
38+
*
39+
* @param ScopeConfigInterface $scopeConfig
40+
* @param UrlInterface $url
41+
*/
42+
public function __construct(ScopeConfigInterface $scopeConfig, UrlInterface $url)
43+
{
44+
$this->scopeConfig = $scopeConfig;
45+
$this->url = $url;
46+
}
47+
48+
/**
49+
* Redirect newsletter routes to 404 when newsletter module is disabled.
50+
*
51+
* @param Observer $observer
52+
*/
53+
public function execute(Observer $observer) : void
54+
{
55+
if (!$this->scopeConfig->getValue(
56+
self::XML_PATH_NEWSLETTER_ACTIVE,
57+
ScopeInterface::SCOPE_STORE
58+
)
59+
) {
60+
$defaultNoRouteUrl = $this->scopeConfig->getValue(
61+
'web/default/no_route',
62+
ScopeInterface::SCOPE_STORE
63+
);
64+
$redirectUrl = $this->url->getUrl($defaultNoRouteUrl);
65+
$observer->getControllerAction()
66+
->getResponse()
67+
->setRedirect($redirectUrl);
68+
}
69+
}
70+
}
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
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\Test\Unit\Observer;
9+
10+
use Magento\Framework\App\Config\ScopeConfigInterface;
11+
use Magento\Framework\App\Response\RedirectInterface;
12+
use Magento\Framework\App\ResponseInterface;
13+
use Magento\Framework\Event\Observer;
14+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
15+
use Magento\Framework\UrlInterface;
16+
use Magento\Newsletter\Observer\PredispatchNewsletterObserver;
17+
use Magento\Store\Model\ScopeInterface;
18+
use PHPUnit\Framework\TestCase;
19+
20+
/**
21+
* Test class for \Magento\Newsletter\Observer\PredispatchNewsletterObserver
22+
*/
23+
class PredispatchNewsletterObserverTest extends TestCase
24+
{
25+
/**
26+
* @var Observer|\PHPUnit_Framework_MockObject_MockObject
27+
*/
28+
private $mockObject;
29+
30+
/**
31+
* @var ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject
32+
*/
33+
private $configMock;
34+
35+
/**
36+
* @var UrlInterface|\PHPUnit_Framework_MockObject_MockObject
37+
*/
38+
private $urlMock;
39+
40+
/**
41+
* @var \Magento\Framework\App\Response\RedirectInterface|\PHPUnit_Framework_MockObject_MockObject
42+
*/
43+
private $redirectMock;
44+
45+
/**
46+
* @var ResponseInterface|\PHPUnit_Framework_MockObject_MockObject
47+
*/
48+
private $responseMock;
49+
50+
/**
51+
* @var ObjectManager
52+
*/
53+
private $objectManager;
54+
55+
/**
56+
* @inheritdoc
57+
*/
58+
protected function setUp() : void
59+
{
60+
$this->configMock = $this->getMockBuilder(ScopeConfigInterface::class)
61+
->disableOriginalConstructor()
62+
->getMock();
63+
$this->urlMock = $this->getMockBuilder(UrlInterface::class)
64+
->disableOriginalConstructor()
65+
->getMock();
66+
$this->responseMock = $this->getMockBuilder(ResponseInterface::class)
67+
->disableOriginalConstructor()
68+
->setMethods(['setRedirect'])
69+
->getMockForAbstractClass();
70+
$this->redirectMock = $this->getMockBuilder(RedirectInterface::class)
71+
->getMock();
72+
$this->objectManager = new ObjectManager($this);
73+
$this->mockObject = $this->objectManager->getObject(
74+
PredispatchNewsletterObserver::class,
75+
[
76+
'scopeConfig' => $this->configMock,
77+
'url' => $this->urlMock
78+
]
79+
);
80+
}
81+
82+
/**
83+
* Test with enabled newsletter active config.
84+
*/
85+
public function testNewsletterEnabled() : void
86+
{
87+
$observerMock = $this->getMockBuilder(Observer::class)
88+
->disableOriginalConstructor()
89+
->setMethods(['getResponse', 'getData', 'setRedirect'])
90+
->getMockForAbstractClass();
91+
92+
$this->configMock->method('getValue')
93+
->with(PredispatchNewsletterObserver::XML_PATH_NEWSLETTER_ACTIVE, ScopeInterface::SCOPE_STORE)
94+
->willReturn(true);
95+
$observerMock->expects($this->never())
96+
->method('getData')
97+
->with('controller_action')
98+
->willReturnSelf();
99+
100+
$observerMock->expects($this->never())
101+
->method('getResponse')
102+
->willReturnSelf();
103+
104+
$this->assertNull($this->mockObject->execute($observerMock));
105+
}
106+
107+
/**
108+
* Test with disabled newsletter active config.
109+
*/
110+
public function testNewsletterDisabled() : void
111+
{
112+
$observerMock = $this->getMockBuilder(Observer::class)
113+
->disableOriginalConstructor()
114+
->setMethods(['getControllerAction', 'getResponse'])
115+
->getMockForAbstractClass();
116+
117+
$this->configMock->expects($this->at(0))
118+
->method('getValue')
119+
->with(PredispatchNewsletterObserver::XML_PATH_NEWSLETTER_ACTIVE, ScopeInterface::SCOPE_STORE)
120+
->willReturn(false);
121+
122+
$expectedRedirectUrl = 'https://test.com/index';
123+
124+
$this->configMock->expects($this->at(1))
125+
->method('getValue')
126+
->with('web/default/no_route', ScopeInterface::SCOPE_STORE)
127+
->willReturn($expectedRedirectUrl);
128+
129+
$this->urlMock->expects($this->once())
130+
->method('getUrl')
131+
->willReturn($expectedRedirectUrl);
132+
133+
$observerMock->expects($this->once())
134+
->method('getControllerAction')
135+
->willReturnSelf();
136+
137+
$observerMock->expects($this->once())
138+
->method('getResponse')
139+
->willReturn($this->responseMock);
140+
141+
$this->responseMock->expects($this->once())
142+
->method('setRedirect')
143+
->with($expectedRedirectUrl);
144+
145+
$this->assertNull($this->mockObject->execute($observerMock));
146+
}
147+
}

app/code/Magento/Newsletter/etc/adminhtml/system.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@
1111
<label>Newsletter</label>
1212
<tab>customer</tab>
1313
<resource>Magento_Newsletter::newsletter</resource>
14+
<group id="general" translate="label" type="text" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
15+
<label>General Options</label>
16+
<field id="active" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
17+
<label>Enabled</label>
18+
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
19+
</field>
20+
</group>
1421
<group id="subscription" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
1522
<label>Subscription Options</label>
1623
<field id="allow_guest_subscribe" translate="label" type="select" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">

app/code/Magento/Newsletter/etc/config.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
99
<default>
1010
<newsletter>
11+
<general>
12+
<active>1</active>
13+
</general>
1114
<subscription>
1215
<allow_guest_subscribe>1</allow_guest_subscribe>
1316
<confirm>0</confirm>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
9+
<event name="controller_action_predispatch_newsletter">
10+
<observer name="newsletter_enabled" instance="Magento\Newsletter\Observer\PredispatchNewsletterObserver" />
11+
</event>
12+
</config>

app/code/Magento/Newsletter/view/frontend/layout/customer_account.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
99
<body>
1010
<referenceBlock name="customer_account_navigation">
11-
<block class="Magento\Customer\Block\Account\SortLinkInterface" name="customer-account-navigation-newsletter-subscriptions-link">
11+
<block class="Magento\Customer\Block\Account\SortLinkInterface" ifconfig="newsletter/general/active" name="customer-account-navigation-newsletter-subscriptions-link">
1212
<arguments>
1313
<argument name="path" xsi:type="string">newsletter/manage</argument>
1414
<argument name="label" xsi:type="string" translate="true">Newsletter Subscriptions</argument>

app/code/Magento/Newsletter/view/frontend/layout/default.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
99
<body>
1010
<referenceBlock name="head.components">
11-
<block class="Magento\Framework\View\Element\Js\Components" name="newsletter_head_components" template="Magento_Newsletter::js/components.phtml"/>
11+
<block class="Magento\Framework\View\Element\Js\Components" name="newsletter_head_components" template="Magento_Newsletter::js/components.phtml" ifconfig="newsletter/general/active"/>
1212
</referenceBlock>
1313
<referenceContainer name="footer">
14-
<block class="Magento\Newsletter\Block\Subscribe" name="form.subscribe" as="subscribe" before="-" template="Magento_Newsletter::subscribe.phtml"/>
14+
<block class="Magento\Newsletter\Block\Subscribe" name="form.subscribe" as="subscribe" before="-" template="Magento_Newsletter::subscribe.phtml" ifconfig="newsletter/general/active"/>
1515
</referenceContainer>
1616
</body>
1717
</page>

0 commit comments

Comments
 (0)