Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw exception for invalid (missing) template in dev mode #8119

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,43 @@ class TemplateTest extends \PHPUnit_Framework_TestCase
*/
protected $_block;

/**
* @var \Magento\TestFramework\ObjectManager
*/
protected $objectManager;

/**
* @var string
*/
private $origMode;

protected function setUp()
{
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
$params = ['layout' => $objectManager->create(\Magento\Framework\View\Layout::class, [])];
$context = $objectManager->create(\Magento\Framework\View\Element\Template\Context::class, $params);
$this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
$params = ['layout' => $this->objectManager->create(\Magento\Framework\View\Layout::class, [])];
$context = $this->objectManager->create(\Magento\Framework\View\Element\Template\Context::class, $params);
$this->_block = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
\Magento\Framework\View\LayoutInterface::class
)->createBlock(
\Magento\Framework\View\Element\Template::class,
'',
['context' => $context]
);

/** @var \Magento\TestFramework\App\State $appState */
$appState = $this->objectManager->get(\Magento\TestFramework\App\State::class);
$this->origMode = $appState->getMode();
}

/**
* {@inheritDoc}
*/
protected function tearDown()
{
/** @var \Magento\TestFramework\App\State $appState */
$appState = $this->objectManager->get(\Magento\TestFramework\App\State::class);
$appState->setMode($this->origMode);
parent::tearDown();
}

public function testConstruct()
Expand Down Expand Up @@ -70,6 +95,9 @@ public function testToHtml()
{
\Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(\Magento\Framework\App\State::class)
->setAreaCode(Area::AREA_GLOBAL);
/** @var \Magento\TestFramework\App\State $appState */
$appState = $this->objectManager->get(\Magento\TestFramework\App\State::class);
$appState->setMode(\Magento\TestFramework\App\State::MODE_DEFAULT);
$this->assertEmpty($this->_block->toHtml());
$this->_block->setTemplate(uniqid('invalid_filename.phtml'));
$this->assertEmpty($this->_block->toHtml());
Expand Down
14 changes: 10 additions & 4 deletions lib/internal/Magento/Framework/View/Element/Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -256,10 +256,16 @@ public function fetchView($fileName)
} else {
$html = '';
$templatePath = $fileName ?: $this->getTemplate();
$this->_logger->critical(
"Invalid template file: '{$templatePath}' in module: '{$this->getModuleName()}'"
. " block's name: '{$this->getNameInLayout()}'"
);
$errorMessage = "Invalid template file: '{$templatePath}' in module: '{$this->getModuleName()}'"
. " block's name: '{$this->getNameInLayout()}'";
if ($this->_appState->getMode() === \Magento\Framework\App\State::MODE_DEVELOPER) {
throw new \Magento\Framework\Exception\ValidatorException(
new \Magento\Framework\Phrase(
$errorMessage
)
);
}
$this->_logger->critical($errorMessage);
}

\Magento\Framework\Profiler::stop('TEMPLATE:' . $fileName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ class TemplateTest extends \PHPUnit_Framework_TestCase
*/
protected $loggerMock;

/**
* @var \Magento\Framework\App\State|\PHPUnit_Framework_MockObject_MockObject
*/
protected $appState;

protected function setUp()
{
$this->resolver = $this->getMock(
Expand Down Expand Up @@ -90,8 +95,14 @@ protected function setUp()
$this->loggerMock = $this->getMock(\Psr\Log\LoggerInterface::class);
$this->templateEngine->expects($this->any())->method('get')->willReturn($this->templateEngine);

$appState = $this->getMock(\Magento\Framework\App\State::class, ['getAreaCode'], [], '', false);
$appState->expects($this->any())->method('getAreaCode')->willReturn('frontend');
$this->appState = $this->getMock(
\Magento\Framework\App\State::class,
['getAreaCode', 'getMode'],
[],
'',
false
);
$this->appState->expects($this->any())->method('getAreaCode')->willReturn('frontend');
$storeManagerMock = $this->getMock(StoreManager::class, [], [], '', false);
$storeMock = $this->getMock(Store::class, [], [], '', false);
$storeManagerMock->expects($this->any())
Expand All @@ -112,7 +123,7 @@ protected function setUp()
'enginePool' => $this->templateEngine,
'resolver' => $this->resolver,
'validator' => $this->validator,
'appState' => $appState,
'appState' => $this->appState,
'logger' => $this->loggerMock,
'storeManager' => $storeManagerMock,
'urlBuilder' => $urlBuilderMock,
Expand Down Expand Up @@ -165,6 +176,30 @@ public function testFetchViewWithNoFileName()
$this->assertEquals($output, $this->block->fetchView($template));
}

public function testFetchViewWithNoFileNameDeveloperMode()
{
$template = false;
$templatePath = 'wrong_template_path.pthml';
$moduleName = 'Acme';
$blockName = 'acme_test_module_test_block';
$exception = "Invalid template file: '{$templatePath}' in module: '{$moduleName}' block's name: '{$blockName}'";
$this->block->setTemplate($templatePath);
$this->block->setData('module_name', $moduleName);
$this->block->setNameInLayout($blockName);
$this->validator->expects($this->once())
->method('isValid')
->with($template)
->willReturn(false);
$this->loggerMock->expects($this->never())
->method('critical');
$this->appState->expects($this->once())
->method('getMode')
->willReturn(\Magento\Framework\App\State::MODE_DEVELOPER);

$this->setExpectedException(\Magento\Framework\Exception\ValidatorException::class, $exception);
$this->block->fetchView($template);
}

public function testSetTemplateContext()
{
$template = 'themedir/template.phtml';
Expand Down