Skip to content

Commit bd65ca4

Browse files
author
Eric Bohanon
committed
MAGETWO-71257: Need to disable module output by configuration
1 parent 980159b commit bd65ca4

File tree

8 files changed

+182
-59
lines changed

8 files changed

+182
-59
lines changed

app/code/Magento/Backend/Block/Template.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,17 @@ public function getFormKey()
8484
*
8585
* @param string $moduleName Full module name
8686
* @return boolean
87-
* @deprecated 100.2.0 Magento does not support custom disabling/enabling module output since 2.2.0 version
88-
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
8987
*/
9088
public function isOutputEnabled($moduleName = null)
9189
{
92-
return true;
90+
if ($moduleName === null) {
91+
$moduleName = $this->getModuleName();
92+
}
93+
94+
return !$this->_scopeConfig->isSetFlag(
95+
'advanced/modules_disable_output/' . $moduleName,
96+
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
97+
);
9398
}
9499

95100
/**

app/code/Magento/Payment/Test/Unit/Block/Info/SubstitutionTest.php

Lines changed: 110 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
7+
// @codingStandardsIgnoreFile
8+
69
namespace Magento\Payment\Test\Unit\Block\Info;
710

811
/**
@@ -31,23 +34,71 @@ class SubstitutionTest extends \PHPUnit\Framework\TestCase
3134
protected function setUp()
3235
{
3336
$this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
34-
$this->layout = $this->getMockBuilder(\Magento\Framework\View\LayoutInterface::class)
35-
->disableOriginalConstructor()
36-
->setMethods([])
37-
->getMock();
38-
$eventManager = $this->getMockBuilder(\Magento\Framework\Event\ManagerInterface::class)
39-
->disableOriginalConstructor()
40-
->setMethods([])
41-
->getMock();
42-
$context = $this->getMockBuilder(\Magento\Framework\View\Element\Template\Context::class)
43-
->disableOriginalConstructor()
44-
->setMethods(['getLayout', 'getEventManager', 'getScopeConfig'])
45-
->getMock();
46-
$context->expects($this->any())
47-
->method('getLayout')
48-
->willReturn($this->layout);
49-
$context->expects($this->any())->method('getEventManager')
50-
->willReturn($eventManager);
37+
38+
$this->layout = $this->getMockBuilder(
39+
\Magento\Framework\View\LayoutInterface::class
40+
)->disableOriginalConstructor()->setMethods(
41+
[]
42+
)->getMock();
43+
44+
$eventManager = $this->getMockBuilder(
45+
\Magento\Framework\Event\ManagerInterface::class
46+
)->disableOriginalConstructor()->setMethods(
47+
[]
48+
)->getMock();
49+
50+
$scopeConfig = $this->getMockBuilder(
51+
\Magento\Framework\App\Config\ScopeConfigInterface::class
52+
)->disableOriginalConstructor()->setMethods(
53+
[]
54+
)->getMock();
55+
$scopeConfig->expects(
56+
$this->any()
57+
)->method(
58+
'getValue'
59+
)->with(
60+
$this->stringContains(
61+
'advanced/modules_disable_output/'
62+
),
63+
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
64+
)->will(
65+
$this->returnValue(
66+
false
67+
)
68+
);
69+
70+
$context = $this->getMockBuilder(
71+
\Magento\Framework\View\Element\Template\Context::class
72+
)->disableOriginalConstructor()->setMethods(
73+
['getLayout', 'getEventManager', 'getScopeConfig']
74+
)->getMock();
75+
$context->expects(
76+
$this->any()
77+
)->method(
78+
'getLayout'
79+
)->will(
80+
$this->returnValue(
81+
$this->layout
82+
)
83+
);
84+
$context->expects(
85+
$this->any()
86+
)->method(
87+
'getEventManager'
88+
)->will(
89+
$this->returnValue(
90+
$eventManager
91+
)
92+
);
93+
$context->expects(
94+
$this->any()
95+
)->method(
96+
'getScopeConfig'
97+
)->will(
98+
$this->returnValue(
99+
$scopeConfig
100+
)
101+
);
51102

52103
$this->block = $this->objectManager->getObject(
53104
\Magento\Payment\Block\Info\Substitution::class,
@@ -62,44 +113,52 @@ protected function setUp()
62113

63114
public function testBeforeToHtml()
64115
{
65-
$abstractBlock = $this->getMockBuilder(\Magento\Framework\View\Element\AbstractBlock::class)
66-
->disableOriginalConstructor()
67-
->setMethods([])
68-
->getMock();
69-
$childAbstractBlock = clone $abstractBlock;
70-
71-
$abstractBlock->expects($this->any())
72-
->method('getParentBlock')
73-
->willReturn($childAbstractBlock);
74-
$this->layout->expects($this->any())
75-
->method('getParentName')
76-
->willReturn('parentName');
77-
$this->layout->expects($this->any())
78-
->method('getBlock')
79-
->willReturn($abstractBlock);
80-
81-
$infoMock = $this->getMockBuilder(\Magento\Payment\Model\Info::class)
82-
->disableOriginalConstructor()->setMethods([])
83-
->getMock();
84-
$methodMock = $this->getMockBuilder(\Magento\Payment\Model\MethodInterface::class)
85-
->getMockForAbstractClass();
86-
$infoMock->expects($this->once())
87-
->method('getMethodInstance')
88-
->willReturn($methodMock);
116+
$abstractBlock = $this->getMockBuilder(
117+
\Magento\Framework\View\Element\AbstractBlock::class
118+
)->disableOriginalConstructor()->setMethods(
119+
[]
120+
)->getMock();
121+
$childAbstractBlock = clone($abstractBlock);
122+
123+
$abstractBlock->expects($this->any())->method('getParentBlock')->will($this->returnValue($childAbstractBlock));
89124

125+
$this->layout->expects($this->any())->method('getParentName')->will($this->returnValue('parentName'));
126+
$this->layout->expects($this->any())->method('getBlock')->will($this->returnValue($abstractBlock));
127+
128+
$infoMock = $this->getMockBuilder(
129+
\Magento\Payment\Model\Info::class
130+
)->disableOriginalConstructor()->setMethods(
131+
[]
132+
)->getMock();
133+
$methodMock = $this->getMockBuilder(
134+
\Magento\Payment\Model\MethodInterface::class
135+
)->getMockForAbstractClass();
136+
$infoMock->expects($this->once())->method('getMethodInstance')->will($this->returnValue($methodMock));
90137
$this->block->setInfo($infoMock);
91138

92139
$fakeBlock = new \StdClass();
93-
$this->layout->expects($this->any())
94-
->method('createBlock')
95-
->with(
96-
\Magento\Framework\View\Element\Template::class, '',
97-
['data' => ['method' => $methodMock, 'template' => 'Magento_Payment::info/substitution.phtml']]
98-
)->willReturn($fakeBlock);
99-
100-
$childAbstractBlock->expects($this->any())
101-
->method('setChild')
102-
->with('order_payment_additional', $fakeBlock);
140+
$this->layout->expects(
141+
$this->any()
142+
)->method(
143+
'createBlock'
144+
)->with(
145+
\Magento\Framework\View\Element\Template::class,
146+
'',
147+
['data' => ['method' => $methodMock, 'template' => 'Magento_Payment::info/substitution.phtml']]
148+
)->will(
149+
$this->returnValue(
150+
$fakeBlock
151+
)
152+
);
153+
154+
$childAbstractBlock->expects(
155+
$this->any()
156+
)->method(
157+
'setChild'
158+
)->with(
159+
'order_payment_additional',
160+
$fakeBlock
161+
);
103162

104163
$this->block->toHtml();
105164
}

app/code/Magento/Paypal/Test/Unit/Block/Express/ReviewTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
67
namespace Magento\Paypal\Test\Unit\Block\Express;
78

89
use Magento\Paypal\Block\Express\Review;
10+
use Magento\Quote\Model\Quote\Address\Rate;
911

1012
/**
1113
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
@@ -36,6 +38,14 @@ protected function setUp()
3638

3739
$layout = $this->createMock(\Magento\Framework\View\LayoutInterface::class);
3840
$eventManager = $this->createMock(\Magento\Framework\Event\ManagerInterface::class);
41+
$scopeConfig = $this->getMock(\Magento\Framework\App\Config\ScopeConfigInterface::class, [], [], '', false);
42+
43+
$scopeConfig->expects($this->any())
44+
->method('getValue')
45+
->with(
46+
$this->stringContains('advanced/modules_disable_output/'),
47+
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
48+
)->will($this->returnValue(false));
3949

4050
$urlBuilder = $this->createMock(\Magento\Framework\UrlInterface::class);
4151
$urlBuilder->expects($this->any())->method('getUrl')->will($this->returnArgument(0));
@@ -50,6 +60,7 @@ protected function setUp()
5060

5161
$context->expects($this->any())->method('getLayout')->will($this->returnValue($layout));
5262
$context->expects($this->any())->method('getEventManager')->will($this->returnValue($eventManager));
63+
$context->expects($this->any())->method('getScopeConfig')->will($this->returnValue($scopeConfig));
5364
$context->expects($this->any())->method('getRequest')->will($this->returnValue($this->request));
5465
$context->expects($this->any())->method('getAssetRepository')->will($this->returnValue($this->assetRepo));
5566
$context->expects($this->any())->method('getUrlBuilder')->will($this->returnValue($urlBuilder));

app/code/Magento/Persistent/Test/Unit/Block/Header/AdditionalTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,12 @@ public function testToHtml($customerId)
252252
$this->eventManagerMock->expects($this->at(1))
253253
->method('dispatch')
254254
->with('view_block_abstract_to_html_after');
255+
$this->scopeConfigMock->expects($this->once())
256+
->method('getValue')
257+
->with(
258+
'advanced/modules_disable_output/Magento_Persistent',
259+
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
260+
)->willReturn(false);
255261

256262
// get cache
257263
$this->cacheStateMock->expects($this->at(0))

app/code/Magento/Wishlist/Test/Unit/Model/Rss/WishlistTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,12 @@ public function testGetRssData()
156156
->method('getValue')
157157
->will($this->returnValueMap(
158158
[
159+
[
160+
'advanced/modules_disable_output/Magento_Rss',
161+
\Magento\Store\Model\ScopeInterface::SCOPE_STORE,
162+
null,
163+
null,
164+
],
159165
[
160166
Data::XML_PATH_DEFAULT_LOCALE,
161167
\Magento\Store\Model\ScopeInterface::SCOPE_STORE,

dev/tests/integration/testsuite/Magento/Backend/Block/TemplateTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,26 @@ public function testGetFormKey()
3434
{
3535
$this->assertGreaterThan(15, strlen($this->_block->getFormKey()));
3636
}
37+
38+
/**
39+
* @magentoAppArea adminhtml
40+
* @covers \Magento\Backend\Block\Template::isOutputEnabled
41+
* @magentoConfigFixture current_store advanced/modules_disable_output/dummy 1
42+
*/
43+
public function testIsOutputEnabledTrue()
44+
{
45+
$this->_block->setData('module_name', 'dummy');
46+
$this->assertFalse($this->_block->isOutputEnabled('dummy'));
47+
}
48+
49+
/**
50+
* @magentoAppArea adminhtml
51+
* @covers \Magento\Backend\Block\Template::isOutputEnabled
52+
* @magentoConfigFixture current_store advanced/modules_disable_output/dummy 0
53+
*/
54+
public function testIsOutputEnabledFalse()
55+
{
56+
$this->_block->setData('module_name', 'dummy');
57+
$this->assertTrue($this->_block->isOutputEnabled('dummy'));
58+
}
3759
}

lib/internal/Magento/Framework/View/Element/AbstractBlock.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,12 @@ protected function _beforeToHtml()
650650
public function toHtml()
651651
{
652652
$this->_eventManager->dispatch('view_block_abstract_to_html_before', ['block' => $this]);
653+
if ($this->_scopeConfig->getValue(
654+
'advanced/modules_disable_output/' . $this->getModuleName(),
655+
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
656+
)) {
657+
return '';
658+
}
653659

654660
$html = $this->_loadCache();
655661
if ($html === false) {

lib/internal/Magento/Framework/View/Test/Unit/Element/AbstractBlockTest.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
7+
// @codingStandardsIgnoreFile
8+
69
namespace Magento\Framework\View\Test\Unit\Element;
710

811
use Magento\Framework\View\Element\AbstractBlock;
@@ -204,12 +207,13 @@ public function testToHtmlWhenModuleIsDisabled()
204207
$moduleName = 'Test';
205208
$this->block->setData('module_name', $moduleName);
206209

207-
$this->eventManagerMock->expects($this->exactly(2))
210+
$this->eventManagerMock->expects($this->any())
208211
->method('dispatch')
209-
->willReturnMap([
210-
['view_block_abstract_to_html_before', ['block' => $this->block]],
211-
['view_block_abstract_to_html_after', ['block' => $this->block]],
212-
]);
212+
->with('view_block_abstract_to_html_before', ['block' => $this->block]);
213+
$this->scopeConfigMock->expects($this->once())
214+
->method('getValue')
215+
->with('advanced/modules_disable_output/' . $moduleName, \Magento\Store\Model\ScopeInterface::SCOPE_STORE)
216+
->willReturn(true);
213217

214218
$this->assertSame('', $this->block->toHtml());
215219
}
@@ -242,6 +246,10 @@ public function testGetCacheLifetimeViaToHtml(
242246

243247
$this->eventManagerMock->expects($expectsDispatchEvent)
244248
->method('dispatch');
249+
$this->scopeConfigMock->expects($this->once())
250+
->method('getValue')
251+
->with('advanced/modules_disable_output/' . $moduleName, \Magento\Store\Model\ScopeInterface::SCOPE_STORE)
252+
->willReturn(false);
245253
$this->cacheStateMock->expects($this->any())
246254
->method('isEnabled')
247255
->with(AbstractBlock::CACHE_GROUP)

0 commit comments

Comments
 (0)