Skip to content

Commit d9aa9bf

Browse files
authored
MAGETWO-65376: [GitHub][PR] Throw exception for invalid template in dev mode #8119
2 parents d0bb11d + 2182d87 commit d9aa9bf

File tree

3 files changed

+79
-10
lines changed

3 files changed

+79
-10
lines changed

dev/tests/integration/testsuite/Magento/Framework/View/Element/TemplateTest.php

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,43 @@ class TemplateTest extends \PHPUnit_Framework_TestCase
1414
*/
1515
protected $_block;
1616

17+
/**
18+
* @var \Magento\TestFramework\ObjectManager
19+
*/
20+
protected $objectManager;
21+
22+
/**
23+
* @var string
24+
*/
25+
private $origMode;
26+
1727
protected function setUp()
1828
{
19-
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
20-
$params = ['layout' => $objectManager->create(\Magento\Framework\View\Layout::class, [])];
21-
$context = $objectManager->create(\Magento\Framework\View\Element\Template\Context::class, $params);
29+
$this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
30+
$params = ['layout' => $this->objectManager->create(\Magento\Framework\View\Layout::class, [])];
31+
$context = $this->objectManager->create(\Magento\Framework\View\Element\Template\Context::class, $params);
2232
$this->_block = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
2333
\Magento\Framework\View\LayoutInterface::class
2434
)->createBlock(
2535
\Magento\Framework\View\Element\Template::class,
2636
'',
2737
['context' => $context]
2838
);
39+
40+
/** @var \Magento\TestFramework\App\State $appState */
41+
$appState = $this->objectManager->get(\Magento\TestFramework\App\State::class);
42+
$this->origMode = $appState->getMode();
43+
}
44+
45+
/**
46+
* {@inheritDoc}
47+
*/
48+
protected function tearDown()
49+
{
50+
/** @var \Magento\TestFramework\App\State $appState */
51+
$appState = $this->objectManager->get(\Magento\TestFramework\App\State::class);
52+
$appState->setMode($this->origMode);
53+
parent::tearDown();
2954
}
3055

3156
public function testConstruct()
@@ -70,6 +95,9 @@ public function testToHtml()
7095
{
7196
\Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(\Magento\Framework\App\State::class)
7297
->setAreaCode(Area::AREA_GLOBAL);
98+
/** @var \Magento\TestFramework\App\State $appState */
99+
$appState = $this->objectManager->get(\Magento\TestFramework\App\State::class);
100+
$appState->setMode(\Magento\TestFramework\App\State::MODE_DEFAULT);
73101
$this->assertEmpty($this->_block->toHtml());
74102
$this->_block->setTemplate(uniqid('invalid_filename.phtml'));
75103
$this->assertEmpty($this->_block->toHtml());

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -256,10 +256,16 @@ 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 \Magento\Framework\Exception\ValidatorException(
263+
new \Magento\Framework\Phrase(
264+
$errorMessage
265+
)
266+
);
267+
}
268+
$this->_logger->critical($errorMessage);
263269
}
264270

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

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

Lines changed: 38 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,14 @@ 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(
99+
\Magento\Framework\App\State::class,
100+
['getAreaCode', 'getMode'],
101+
[],
102+
'',
103+
false
104+
);
105+
$this->appState->expects($this->any())->method('getAreaCode')->willReturn('frontend');
95106
$storeManagerMock = $this->getMock(StoreManager::class, [], [], '', false);
96107
$storeMock = $this->getMock(Store::class, [], [], '', false);
97108
$storeManagerMock->expects($this->any())
@@ -112,7 +123,7 @@ protected function setUp()
112123
'enginePool' => $this->templateEngine,
113124
'resolver' => $this->resolver,
114125
'validator' => $this->validator,
115-
'appState' => $appState,
126+
'appState' => $this->appState,
116127
'logger' => $this->loggerMock,
117128
'storeManager' => $storeManagerMock,
118129
'urlBuilder' => $urlBuilderMock,
@@ -165,6 +176,30 @@ public function testFetchViewWithNoFileName()
165176
$this->assertEquals($output, $this->block->fetchView($template));
166177
}
167178

179+
public function testFetchViewWithNoFileNameDeveloperMode()
180+
{
181+
$template = false;
182+
$templatePath = 'wrong_template_path.pthml';
183+
$moduleName = 'Acme';
184+
$blockName = 'acme_test_module_test_block';
185+
$exception = "Invalid template file: '{$templatePath}' in module: '{$moduleName}' block's name: '{$blockName}'";
186+
$this->block->setTemplate($templatePath);
187+
$this->block->setData('module_name', $moduleName);
188+
$this->block->setNameInLayout($blockName);
189+
$this->validator->expects($this->once())
190+
->method('isValid')
191+
->with($template)
192+
->willReturn(false);
193+
$this->loggerMock->expects($this->never())
194+
->method('critical');
195+
$this->appState->expects($this->once())
196+
->method('getMode')
197+
->willReturn(\Magento\Framework\App\State::MODE_DEVELOPER);
198+
199+
$this->setExpectedException(\Magento\Framework\Exception\ValidatorException::class, $exception);
200+
$this->block->fetchView($template);
201+
}
202+
168203
public function testSetTemplateContext()
169204
{
170205
$template = 'themedir/template.phtml';

0 commit comments

Comments
 (0)