mockWebApplication([ 'controllerNamespace' => 'yiiunit\\data\\controllers', 'components' => [ 'errorHandler' => [ 'class' => 'yiiunit\framework\web\ErrorHandler', 'errorView' => '@yiiunit/data/views/errorHandler.php', 'exceptionView' => '@yiiunit/data/views/errorHandlerForAssetFiles.php', ], ], ]); } public function testCorrectResponseCodeInErrorView() { /** @var ErrorHandler $handler */ $handler = Yii::$app->getErrorHandler(); ob_start(); // suppress response output $this->invokeMethod($handler, 'renderException', [new NotFoundHttpException('This message is displayed to end user')]); ob_get_clean(); $out = Yii::$app->response->data; $this->assertEquals('Code: 404 Message: This message is displayed to end user Exception: yii\web\NotFoundHttpException', $out); } public function testClearAssetFilesInErrorView() { Yii::$app->getView()->registerJsFile('somefile.js'); /** @var ErrorHandler $handler */ $handler = Yii::$app->getErrorHandler(); ob_start(); // suppress response output $this->invokeMethod($handler, 'renderException', [new \Exception('Some Exception')]); ob_get_clean(); $out = Yii::$app->response->data; $this->assertEquals('Exception View ', $out); } public function testClearAssetFilesInErrorActionView() { Yii::$app->getErrorHandler()->errorAction = 'test/error'; Yii::$app->getView()->registerJs("alert('hide me')", View::POS_END); /** @var ErrorHandler $handler */ $handler = Yii::$app->getErrorHandler(); ob_start(); // suppress response output $this->invokeMethod($handler, 'renderException', [new NotFoundHttpException()]); ob_get_clean(); $out = Yii::$app->response->data; $this->assertNotContains('getErrorHandler(); $handler->traceLine = '{html}'; $file = \yii\BaseYii::getAlias('@yii/web/Application.php'); $out = $handler->renderCallStackItem($file, 63, \yii\web\Application::className(), null, null, null); $this->assertContains('', $out); } public function dataHtmlEncode() { return [ [ "a \t=<>&\"'\x80`\n", "a \t=<>&\"'�`\n", ], [ 'test', '<b>test</b>', ], [ '"hello"', '"hello"', ], [ "'hello world'", "'hello world'", ], [ 'Chip&Dale', 'Chip&amp;Dale', ], [ "\t\$x=24;", "\t\$x=24;", ], ]; } /** * @dataProvider dataHtmlEncode */ public function testHtmlEncode($text, $expected) { $handler = Yii::$app->getErrorHandler(); $this->assertSame($expected, $handler->htmlEncode($text)); } public function testHtmlEncodeWithUnicodeSequence() { if (PHP_VERSION_ID < 70000) { $this->markTestSkipped('Can not be tested on PHP < 7.0'); return; } $handler = Yii::$app->getErrorHandler(); $text = "a \t=<>&\"'\x80\u{20bd}`\u{000a}\u{000c}\u{0000}"; $expected = "a \t=<>&\"'�₽`\n\u{000c}\u{0000}"; $this->assertSame($expected, $handler->htmlEncode($text)); } } class ErrorHandler extends \yii\web\ErrorHandler { /** * @return bool if simple HTML should be rendered */ protected function shouldRenderSimpleHtml() { return false; } }