2008-05-30 11:40:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2009-03-18 17:55:58 +00:00
|
|
|
* ErrorHandlerTest file
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2017-06-11 00:15:34 +02:00
|
|
|
* CakePHP(tm) Tests <https://book.cakephp.org/2.0/en/development/testing.html>
|
2017-06-11 00:10:52 +02:00
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-10-03 12:31:21 -04:00
|
|
|
* Licensed under The MIT License
|
2013-02-08 21:22:51 +09:00
|
|
|
* For full copyright and license information, please see the LICENSE.txt
|
2010-10-03 12:31:21 -04:00
|
|
|
* Redistributions of files must retain the above copyright notice
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2017-06-11 00:10:52 +02:00
|
|
|
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
|
2017-06-11 00:15:34 +02:00
|
|
|
* @link https://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
|
2011-07-26 01:46:14 -04:30
|
|
|
* @package Cake.Test.Case.Error
|
2008-10-30 17:30:26 +00:00
|
|
|
* @since CakePHP(tm) v 1.2.0.5432
|
2017-06-11 00:23:14 +02:00
|
|
|
* @license https://opensource.org/licenses/mit-license.php MIT License
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2009-07-24 21:18:37 +02:00
|
|
|
|
2010-12-09 01:25:24 -04:30
|
|
|
App::uses('ErrorHandler', 'Error');
|
|
|
|
App::uses('Controller', 'Controller');
|
|
|
|
App::uses('Router', 'Routing');
|
2015-06-13 12:48:29 -04:00
|
|
|
App::uses('Debugger', 'Utility');
|
2009-07-24 21:18:37 +02:00
|
|
|
|
2015-01-15 19:20:28 +01:00
|
|
|
/**
|
|
|
|
* A faulty ExceptionRenderer to test nesting.
|
|
|
|
*/
|
|
|
|
class FaultyExceptionRenderer extends ExceptionRenderer {
|
|
|
|
|
2015-01-15 20:04:06 +01:00
|
|
|
/**
|
|
|
|
* Dummy rendering implementation.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
2015-01-15 19:20:28 +01:00
|
|
|
public function render() {
|
|
|
|
throw new Exception('Error from renderer.');
|
|
|
|
}
|
2015-01-15 20:04:06 +01:00
|
|
|
|
2015-01-15 19:20:28 +01:00
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2009-03-18 17:55:58 +00:00
|
|
|
* ErrorHandlerTest class
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2011-07-26 01:46:14 -04:30
|
|
|
* @package Cake.Test.Case.Error
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2009-03-18 17:55:58 +00:00
|
|
|
class ErrorHandlerTest extends CakeTestCase {
|
2009-07-24 21:18:37 +02:00
|
|
|
|
2012-03-12 21:21:02 -04:00
|
|
|
protected $_restoreError = false;
|
2011-12-04 13:27:51 -08:00
|
|
|
|
2010-05-29 00:21:02 -04:00
|
|
|
/**
|
|
|
|
* setup create a request object to get out of router later.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 22:02:32 +02:00
|
|
|
public function setUp() {
|
2011-12-04 13:27:51 -08:00
|
|
|
parent::setUp();
|
2010-09-25 10:41:08 -04:00
|
|
|
App::build(array(
|
2011-04-22 08:47:28 -04:30
|
|
|
'View' => array(
|
2012-03-12 21:21:02 -04:00
|
|
|
CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS
|
2010-09-25 10:41:08 -04:00
|
|
|
)
|
2012-02-18 04:04:54 -08:00
|
|
|
), App::RESET);
|
2010-09-25 10:41:08 -04:00
|
|
|
Router::reload();
|
|
|
|
|
2010-05-29 00:21:02 -04:00
|
|
|
$request = new CakeRequest(null, false);
|
|
|
|
$request->base = '';
|
|
|
|
Router::setRequestInfo($request);
|
2010-11-14 22:06:18 -05:00
|
|
|
Configure::write('debug', 2);
|
2012-05-10 15:29:45 +07:00
|
|
|
|
|
|
|
CakeLog::disable('stdout');
|
|
|
|
CakeLog::disable('stderr');
|
2010-05-29 00:21:02 -04:00
|
|
|
}
|
|
|
|
|
2010-09-25 10:41:08 -04:00
|
|
|
/**
|
2011-12-04 13:27:51 -08:00
|
|
|
* tearDown
|
2010-09-25 10:41:08 -04:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-12-04 13:27:51 -08:00
|
|
|
public function tearDown() {
|
2012-04-15 22:20:34 -04:00
|
|
|
parent::tearDown();
|
2010-11-14 20:00:27 -05:00
|
|
|
if ($this->_restoreError) {
|
|
|
|
restore_error_handler();
|
|
|
|
}
|
2012-05-10 15:29:45 +07:00
|
|
|
CakeLog::enable('stdout');
|
|
|
|
CakeLog::enable('stderr');
|
2010-09-19 01:43:00 -04:00
|
|
|
}
|
|
|
|
|
2010-11-14 20:00:27 -05:00
|
|
|
/**
|
|
|
|
* test error handling when debug is on, an error should be printed from Debugger.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 22:02:32 +02:00
|
|
|
public function testHandleErrorDebugOn() {
|
2010-11-14 20:00:27 -05:00
|
|
|
set_error_handler('ErrorHandler::handleError');
|
|
|
|
$this->_restoreError = true;
|
|
|
|
|
2015-06-13 12:48:29 -04:00
|
|
|
Debugger::getInstance()->output('html');
|
|
|
|
|
2010-11-14 20:00:27 -05:00
|
|
|
ob_start();
|
|
|
|
$wrong .= '';
|
|
|
|
$result = ob_get_clean();
|
|
|
|
|
2011-11-15 16:07:56 -08:00
|
|
|
$this->assertRegExp('/<pre class="cake-error">/', $result);
|
|
|
|
$this->assertRegExp('/<b>Notice<\/b>/', $result);
|
|
|
|
$this->assertRegExp('/variable:\s+wrong/', $result);
|
2010-11-14 20:00:27 -05:00
|
|
|
}
|
|
|
|
|
2010-11-21 23:02:02 -05:00
|
|
|
/**
|
|
|
|
* provides errors for mapping tests.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public static function errorProvider() {
|
|
|
|
return array(
|
|
|
|
array(E_USER_NOTICE, 'Notice'),
|
|
|
|
array(E_USER_WARNING, 'Warning'),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test error mappings
|
|
|
|
*
|
|
|
|
* @dataProvider errorProvider
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 22:02:32 +02:00
|
|
|
public function testErrorMapping($error, $expected) {
|
2010-11-21 23:02:02 -05:00
|
|
|
set_error_handler('ErrorHandler::handleError');
|
|
|
|
$this->_restoreError = true;
|
|
|
|
|
2015-06-13 12:48:29 -04:00
|
|
|
Debugger::getInstance()->output('html');
|
|
|
|
|
2010-11-21 23:02:02 -05:00
|
|
|
ob_start();
|
|
|
|
trigger_error('Test error', $error);
|
|
|
|
|
|
|
|
$result = ob_get_clean();
|
2011-11-15 16:07:56 -08:00
|
|
|
$this->assertRegExp('/<b>' . $expected . '<\/b>/', $result);
|
2010-11-21 23:02:02 -05:00
|
|
|
}
|
|
|
|
|
2010-12-02 01:44:31 -02:00
|
|
|
/**
|
|
|
|
* test error prepended by @
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 22:02:32 +02:00
|
|
|
public function testErrorSuppressed() {
|
2010-12-02 01:44:31 -02:00
|
|
|
set_error_handler('ErrorHandler::handleError');
|
|
|
|
$this->_restoreError = true;
|
|
|
|
|
|
|
|
ob_start();
|
2012-11-14 10:00:15 +01:00
|
|
|
//@codingStandardsIgnoreStart
|
2010-12-02 01:44:31 -02:00
|
|
|
@include 'invalid.file';
|
2012-11-14 10:00:15 +01:00
|
|
|
//@codingStandardsIgnoreEnd
|
2010-12-02 01:44:31 -02:00
|
|
|
$result = ob_get_clean();
|
|
|
|
$this->assertTrue(empty($result));
|
|
|
|
}
|
|
|
|
|
2010-11-14 20:00:27 -05:00
|
|
|
/**
|
|
|
|
* Test that errors go into CakeLog when debug = 0.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 22:02:32 +02:00
|
|
|
public function testHandleErrorDebugOff() {
|
2010-11-14 20:00:27 -05:00
|
|
|
Configure::write('debug', 0);
|
2010-11-14 22:06:18 -05:00
|
|
|
Configure::write('Error.trace', false);
|
|
|
|
if (file_exists(LOGS . 'debug.log')) {
|
2012-11-14 10:00:15 +01:00
|
|
|
unlink(LOGS . 'debug.log');
|
2010-11-14 22:06:18 -05:00
|
|
|
}
|
2010-11-14 20:00:27 -05:00
|
|
|
|
|
|
|
set_error_handler('ErrorHandler::handleError');
|
|
|
|
$this->_restoreError = true;
|
|
|
|
|
|
|
|
$out .= '';
|
|
|
|
|
|
|
|
$result = file(LOGS . 'debug.log');
|
2012-03-22 23:37:12 -07:00
|
|
|
$this->assertEquals(1, count($result));
|
2011-11-15 16:07:56 -08:00
|
|
|
$this->assertRegExp(
|
2011-04-24 23:31:44 -04:00
|
|
|
'/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} (Notice|Debug): Notice \(8\): Undefined variable:\s+out in \[.+ line \d+\]$/',
|
2010-11-14 20:00:27 -05:00
|
|
|
$result[0]
|
|
|
|
);
|
2012-11-14 10:00:15 +01:00
|
|
|
if (file_exists(LOGS . 'debug.log')) {
|
|
|
|
unlink(LOGS . 'debug.log');
|
|
|
|
}
|
2010-11-14 20:00:27 -05:00
|
|
|
}
|
|
|
|
|
2010-11-14 22:06:18 -05:00
|
|
|
/**
|
|
|
|
* Test that errors going into CakeLog include traces.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 22:02:32 +02:00
|
|
|
public function testHandleErrorLoggingTrace() {
|
2010-11-14 22:06:18 -05:00
|
|
|
Configure::write('debug', 0);
|
|
|
|
Configure::write('Error.trace', true);
|
|
|
|
if (file_exists(LOGS . 'debug.log')) {
|
2012-11-14 10:00:15 +01:00
|
|
|
unlink(LOGS . 'debug.log');
|
2010-11-14 22:06:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
set_error_handler('ErrorHandler::handleError');
|
|
|
|
$this->_restoreError = true;
|
|
|
|
|
|
|
|
$out .= '';
|
|
|
|
|
|
|
|
$result = file(LOGS . 'debug.log');
|
2011-11-15 16:07:56 -08:00
|
|
|
$this->assertRegExp(
|
2011-04-24 23:31:44 -04:00
|
|
|
'/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} (Notice|Debug): Notice \(8\): Undefined variable:\s+out in \[.+ line \d+\]$/',
|
2010-11-14 22:06:18 -05:00
|
|
|
$result[0]
|
|
|
|
);
|
2011-11-15 16:07:56 -08:00
|
|
|
$this->assertRegExp('/^Trace:/', $result[1]);
|
2016-03-23 19:05:23 +00:00
|
|
|
$this->assertRegExp('/^ErrorHandlerTest\:\:testHandleErrorLoggingTrace\(\)/', $result[3]);
|
2012-11-14 10:00:15 +01:00
|
|
|
if (file_exists(LOGS . 'debug.log')) {
|
|
|
|
unlink(LOGS . 'debug.log');
|
|
|
|
}
|
2010-11-14 22:06:18 -05:00
|
|
|
}
|
|
|
|
|
2010-08-28 01:39:02 -04:00
|
|
|
/**
|
|
|
|
* test handleException generating a page.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 22:02:32 +02:00
|
|
|
public function testHandleException() {
|
2010-11-14 23:33:46 -05:00
|
|
|
$error = new NotFoundException('Kaboom!');
|
|
|
|
ob_start();
|
|
|
|
ErrorHandler::handleException($error);
|
|
|
|
$result = ob_get_clean();
|
2011-11-15 16:07:56 -08:00
|
|
|
$this->assertRegExp('/Kaboom!/', $result, 'message missing.');
|
2010-11-14 23:33:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-03-31 18:55:38 -04:00
|
|
|
* test handleException generating log.
|
2010-11-14 23:33:46 -05:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 22:02:32 +02:00
|
|
|
public function testHandleExceptionLog() {
|
2010-11-14 23:33:46 -05:00
|
|
|
if (file_exists(LOGS . 'error.log')) {
|
|
|
|
unlink(LOGS . 'error.log');
|
|
|
|
}
|
|
|
|
Configure::write('Exception.log', true);
|
2010-09-05 02:05:31 -04:00
|
|
|
$error = new NotFoundException('Kaboom!');
|
2010-11-14 23:33:46 -05:00
|
|
|
|
2010-08-28 01:39:02 -04:00
|
|
|
ob_start();
|
|
|
|
ErrorHandler::handleException($error);
|
|
|
|
$result = ob_get_clean();
|
2011-11-15 16:07:56 -08:00
|
|
|
$this->assertRegExp('/Kaboom!/', $result, 'message missing.');
|
2010-11-14 23:33:46 -05:00
|
|
|
|
|
|
|
$log = file(LOGS . 'error.log');
|
2013-03-18 16:16:03 +01:00
|
|
|
$this->assertContains('[NotFoundException] Kaboom!', $log[0], 'message missing.');
|
|
|
|
$this->assertContains('ErrorHandlerTest->testHandleExceptionLog', $log[2], 'Stack trace missing.');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test handleException generating log.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testHandleExceptionLogSkipping() {
|
|
|
|
if (file_exists(LOGS . 'error.log')) {
|
|
|
|
unlink(LOGS . 'error.log');
|
|
|
|
}
|
|
|
|
Configure::write('Exception.log', true);
|
|
|
|
Configure::write('Exception.skipLog', array('NotFoundException'));
|
|
|
|
$notFound = new NotFoundException('Kaboom!');
|
|
|
|
$forbidden = new ForbiddenException('Fooled you!');
|
|
|
|
|
|
|
|
ob_start();
|
|
|
|
ErrorHandler::handleException($notFound);
|
|
|
|
$result = ob_get_clean();
|
|
|
|
$this->assertRegExp('/Kaboom!/', $result, 'message missing.');
|
|
|
|
|
|
|
|
ob_start();
|
|
|
|
ErrorHandler::handleException($forbidden);
|
|
|
|
$result = ob_get_clean();
|
|
|
|
$this->assertRegExp('/Fooled you!/', $result, 'message missing.');
|
|
|
|
|
|
|
|
$log = file(LOGS . 'error.log');
|
|
|
|
$this->assertNotContains('[NotFoundException] Kaboom!', $log[0], 'message should not be logged.');
|
|
|
|
$this->assertContains('[ForbiddenException] Fooled you!', $log[0], 'message missing.');
|
2010-08-28 01:39:02 -04:00
|
|
|
}
|
|
|
|
|
2011-04-24 00:56:48 -04:30
|
|
|
/**
|
|
|
|
* tests it is possible to load a plugin exception renderer
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2012-05-10 15:29:45 +07:00
|
|
|
public function testLoadPluginHandler() {
|
2011-04-24 00:56:48 -04:30
|
|
|
App::build(array(
|
2012-02-18 04:31:29 -08:00
|
|
|
'Plugin' => array(
|
2011-04-17 12:35:21 +02:00
|
|
|
CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS
|
2011-04-24 00:56:48 -04:30
|
|
|
)
|
2012-02-18 04:04:54 -08:00
|
|
|
), App::RESET);
|
2011-05-09 23:47:29 -04:30
|
|
|
CakePlugin::load('TestPlugin');
|
2011-04-24 00:56:48 -04:30
|
|
|
Configure::write('Exception.renderer', 'TestPlugin.TestPluginExceptionRenderer');
|
|
|
|
$error = new NotFoundException('Kaboom!');
|
|
|
|
ob_start();
|
|
|
|
ErrorHandler::handleException($error);
|
|
|
|
$result = ob_get_clean();
|
2012-03-22 23:37:12 -07:00
|
|
|
$this->assertEquals('Rendered by test plugin', $result);
|
2011-05-09 23:47:29 -04:30
|
|
|
CakePlugin::unload();
|
2011-04-24 00:56:48 -04:30
|
|
|
}
|
|
|
|
|
2012-03-31 18:55:38 -04:00
|
|
|
/**
|
|
|
|
* test handleFatalError generating a page.
|
|
|
|
*
|
2013-02-25 14:19:13 -05:00
|
|
|
* These tests start two buffers as handleFatalError blows the outer one up.
|
|
|
|
*
|
2012-03-31 18:55:38 -04:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testHandleFatalErrorPage() {
|
|
|
|
$line = __LINE__;
|
|
|
|
|
2013-02-25 14:19:13 -05:00
|
|
|
ob_start();
|
2012-03-31 18:55:38 -04:00
|
|
|
ob_start();
|
|
|
|
Configure::write('debug', 1);
|
2012-04-01 23:49:40 -04:00
|
|
|
ErrorHandler::handleFatalError(E_ERROR, 'Something wrong', __FILE__, $line);
|
2012-03-31 18:55:38 -04:00
|
|
|
$result = ob_get_clean();
|
|
|
|
$this->assertContains('Something wrong', $result, 'message missing.');
|
|
|
|
$this->assertContains(__FILE__, $result, 'filename missing.');
|
|
|
|
$this->assertContains((string)$line, $result, 'line missing.');
|
|
|
|
|
2013-02-25 14:19:13 -05:00
|
|
|
ob_start();
|
2012-03-31 18:55:38 -04:00
|
|
|
ob_start();
|
|
|
|
Configure::write('debug', 0);
|
2012-04-01 23:49:40 -04:00
|
|
|
ErrorHandler::handleFatalError(E_ERROR, 'Something wrong', __FILE__, $line);
|
2012-03-31 18:55:38 -04:00
|
|
|
$result = ob_get_clean();
|
|
|
|
$this->assertNotContains('Something wrong', $result, 'message must not appear.');
|
|
|
|
$this->assertNotContains(__FILE__, $result, 'filename must not appear.');
|
|
|
|
$this->assertContains('An Internal Error Has Occurred', $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test handleException generating log.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testHandleFatalErrorLog() {
|
|
|
|
if (file_exists(LOGS . 'error.log')) {
|
|
|
|
unlink(LOGS . 'error.log');
|
|
|
|
}
|
|
|
|
|
|
|
|
ob_start();
|
2012-04-01 23:49:40 -04:00
|
|
|
ErrorHandler::handleFatalError(E_ERROR, 'Something wrong', __FILE__, __LINE__);
|
2012-03-31 18:55:38 -04:00
|
|
|
ob_clean();
|
|
|
|
|
|
|
|
$log = file(LOGS . 'error.log');
|
|
|
|
$this->assertContains(__FILE__, $log[0], 'missing filename');
|
|
|
|
$this->assertContains('[FatalErrorException] Something wrong', $log[1], 'message missing.');
|
|
|
|
}
|
|
|
|
|
2015-01-15 19:20:28 +01:00
|
|
|
/**
|
|
|
|
* testExceptionRendererNestingDebug method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testExceptionRendererNestingDebug() {
|
|
|
|
Configure::write('debug', 2);
|
|
|
|
Configure::write('Exception.renderer', 'FaultyExceptionRenderer');
|
2015-01-15 23:11:36 +01:00
|
|
|
|
|
|
|
$result = false;
|
|
|
|
try {
|
|
|
|
ob_start();
|
|
|
|
ob_start();
|
|
|
|
ErrorHandler::handleFatalError(E_USER_ERROR, 'Initial error', __FILE__, __LINE__);
|
|
|
|
} catch (Exception $e) {
|
|
|
|
$result = $e instanceof FatalErrorException;
|
|
|
|
}
|
|
|
|
|
|
|
|
restore_error_handler();
|
|
|
|
$this->assertTrue($result);
|
2015-01-15 19:20:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* testExceptionRendererNestingProduction method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testExceptionRendererNestingProduction() {
|
|
|
|
Configure::write('debug', 0);
|
|
|
|
Configure::write('Exception.renderer', 'FaultyExceptionRenderer');
|
2015-01-15 23:11:36 +01:00
|
|
|
|
|
|
|
$result = false;
|
|
|
|
try {
|
|
|
|
ob_start();
|
|
|
|
ob_start();
|
|
|
|
ErrorHandler::handleFatalError(E_USER_ERROR, 'Initial error', __FILE__, __LINE__);
|
|
|
|
} catch (Exception $e) {
|
|
|
|
$result = $e instanceof InternalErrorException;
|
|
|
|
}
|
|
|
|
|
|
|
|
restore_error_handler();
|
|
|
|
$this->assertTrue($result);
|
2015-01-15 19:20:28 +01:00
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|