Skip to content

Commit e467545

Browse files
committed
Throw exception for invalid template in dev mode
In developer mode php notices/warnings etc are promoted to being exceptions. This means the developer is a bit more diligent with cleaning up any messes as they occur. I noticed this error on a Magento 1 client which is filled with "Not valid template file", I checked in M2 and this error seems to persist. This change should make sure developers do not lead any dead templates in their layout.xml.
1 parent dd124b7 commit e467545

File tree

2 files changed

+38
-7
lines changed

2 files changed

+38
-7
lines changed

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -256,10 +256,12 @@ public function fetchView($fileName)
256256
} else {
257257
$html = '';
258258
$templatePath = $fileName ?: $this->getTemplate();
259-
$this->_logger->critical(
260-
"Invalid template file: '{$templatePath}' in module: '{$this->getModuleName()}'"
261-
. " block's name: '{$this->getNameInLayout()}'"
262-
);
259+
$errorMessage = "Invalid template file: '{$templatePath}' in module: '{$this->getModuleName()}'"
260+
. " block's name: '{$this->getNameInLayout()}'";
261+
if ($this->_appState->getMode() === \Magento\Framework\App\State::MODE_DEVELOPER) {
262+
throw new \InvalidArgumentException($errorMessage);
263+
}
264+
$this->_logger->critical($errorMessage);
263265
}
264266

265267
\Magento\Framework\Profiler::stop('TEMPLATE:' . $fileName);

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

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ class TemplateTest extends \PHPUnit_Framework_TestCase
5151
*/
5252
protected $loggerMock;
5353

54+
/**
55+
* @var \Magento\Framework\App\State|\PHPUnit_Framework_MockObject_MockObject
56+
*/
57+
protected $appState;
58+
5459
protected function setUp()
5560
{
5661
$this->resolver = $this->getMock(
@@ -90,8 +95,8 @@ protected function setUp()
9095
$this->loggerMock = $this->getMock(\Psr\Log\LoggerInterface::class);
9196
$this->templateEngine->expects($this->any())->method('get')->willReturn($this->templateEngine);
9297

93-
$appState = $this->getMock(\Magento\Framework\App\State::class, ['getAreaCode'], [], '', false);
94-
$appState->expects($this->any())->method('getAreaCode')->willReturn('frontend');
98+
$this->appState = $this->getMock(\Magento\Framework\App\State::class, ['getAreaCode', 'getMode'], [], '', false);
99+
$this->appState->expects($this->any())->method('getAreaCode')->willReturn('frontend');
95100
$storeManagerMock = $this->getMock(StoreManager::class, [], [], '', false);
96101
$storeMock = $this->getMock(Store::class, [], [], '', false);
97102
$storeManagerMock->expects($this->any())
@@ -112,7 +117,7 @@ protected function setUp()
112117
'enginePool' => $this->templateEngine,
113118
'resolver' => $this->resolver,
114119
'validator' => $this->validator,
115-
'appState' => $appState,
120+
'appState' => $this->appState,
116121
'logger' => $this->loggerMock,
117122
'storeManager' => $storeManagerMock,
118123
'urlBuilder' => $urlBuilderMock,
@@ -165,6 +170,30 @@ public function testFetchViewWithNoFileName()
165170
$this->assertEquals($output, $this->block->fetchView($template));
166171
}
167172

173+
public function testFetchViewWithNoFileNameDeveloperMode()
174+
{
175+
$template = false;
176+
$templatePath = 'wrong_template_path.pthml';
177+
$moduleName = 'Acme';
178+
$blockName = 'acme_test_module_test_block';
179+
$exception = "Invalid template file: '{$templatePath}' in module: '{$moduleName}' block's name: '{$blockName}'";
180+
$this->block->setTemplate($templatePath);
181+
$this->block->setData('module_name', $moduleName);
182+
$this->block->setNameInLayout($blockName);
183+
$this->validator->expects($this->once())
184+
->method('isValid')
185+
->with($template)
186+
->willReturn(false);
187+
$this->loggerMock->expects($this->never())
188+
->method('critical');
189+
$this->appState->expects($this->once())
190+
->method('getMode')
191+
->willReturn(\Magento\Framework\App\State::MODE_DEVELOPER);
192+
193+
$this->setExpectedException('\InvalidArgumentException', $exception);
194+
$this->block->fetchView($template);
195+
}
196+
168197
public function testSetTemplateContext()
169198
{
170199
$template = 'themedir/template.phtml';

0 commit comments

Comments
 (0)