Skip to content

Commit

Permalink
Fixes #13058: Fixed caught exception thrown during view file renderin…
Browse files Browse the repository at this point in the history
…g produces wrong output
  • Loading branch information
klimov-paul authored and samdark committed Jun 1, 2017
1 parent 17a1d91 commit 0beb593
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 3 deletions.
1 change: 1 addition & 0 deletions framework/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Yii Framework 2 Change Log
- Bug #11404: `yii\base\Model::loadMultiple()` returns true even if `yii\base\Model::load()` returns false (zvook)
- Bug #11719: Fixed `yii\db\Connection::$enableQueryCache` caused infinite loop when the same connection was used for `yii\caching\DbCache` (michaelarnauts)
- Bug #12715: Exception `SAVEPOINT LEVEL1 does not exist` instead of deadlock exception (Vovan-VE)
- Bug #13058: Fixed caught exception thrown during view file rendering produces wrong output (klimov-paul)
- Bug #13086, #13656: Fixed bug with optional parameters at the beginning of pattern in `yii\web\UrlRule` (rob006)
- Bug #13087: Fixed getting active validators for safe attribute (developeruz, klimov-paul)
- Bug #13306: Wildcard in `reloadableScripts` in `yii.js` allows 0 characters (arogachev)
Expand Down
22 changes: 19 additions & 3 deletions framework/base/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -322,12 +322,28 @@ public function afterRender($viewFile, $params, &$output)
*/
public function renderPhpFile($_file_, $_params_ = [])
{
$_obInitialLevel_ = ob_get_level();
ob_start();
ob_implicit_flush(false);
extract($_params_, EXTR_OVERWRITE);
require($_file_);

return ob_get_clean();
try {
require($_file_);
return ob_get_clean();
} catch (\Exception $e) {
while (ob_get_level() > $_obInitialLevel_) {
if (!@ob_end_clean()) {
ob_clean();
}
}
throw $e;
} catch (\Throwable $e) {
while (ob_get_level() > $_obInitialLevel_) {
if (!@ob_end_clean()) {
ob_clean();
}
}
throw $e;
}
}

/**
Expand Down
65 changes: 65 additions & 0 deletions tests/framework/base/ViewTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace yiiunit\framework\base;

use Yii;
use yii\base\View;
use yii\helpers\FileHelper;
use yiiunit\TestCase;

/**
* @group base
*/
class ViewTest extends TestCase
{
/**
* @var string path for the test files.
*/
protected $testViewPath = '';

public function setUp()
{
parent::setUp();

$this->mockApplication();
$this->testViewPath = Yii::getAlias('@yiiunit/runtime') . DIRECTORY_SEPARATOR . str_replace('\\', '_', get_class($this)) . uniqid();
FileHelper::createDirectory($this->testViewPath);
}

public function tearDown()
{
FileHelper::removeDirectory($this->testViewPath);
parent::tearDown();
}

/**
* @see https://github.com/yiisoft/yii2/issues/13058
*/
public function testExceptionOnRenderFile()
{
$view = new View();

$exceptionViewFile = $this->testViewPath . DIRECTORY_SEPARATOR . 'exception.php';
file_put_contents($exceptionViewFile, <<<PHP
<h1>Exception</h1>
<?php throw new Exception('Test Exception'); ?>
PHP
);
$normalViewFile = $this->testViewPath . DIRECTORY_SEPARATOR . 'no-exception.php';
file_put_contents($normalViewFile, <<<PHP
<h1>No Exception</h1>
PHP
);

$obInitialLevel = ob_get_level();

try {
$view->renderFile($exceptionViewFile);
} catch (\Exception $e) {
// shutdown exception
}
$view->renderFile($normalViewFile);

$this->assertEquals($obInitialLevel, ob_get_level());
}
}

0 comments on commit 0beb593

Please sign in to comment.