2010-11-15 03:20:29 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* ExceptionRendererTest file
|
|
|
|
*
|
|
|
|
* PHP 5
|
|
|
|
*
|
2012-04-27 02:49:18 +00:00
|
|
|
* CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
|
2013-02-08 11:59:49 +00:00
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2010-11-15 03:20:29 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
2013-02-08 12:22:51 +00:00
|
|
|
* For full copyright and license information, please see the LICENSE.txt
|
2010-11-15 03:20:29 +00:00
|
|
|
* Redistributions of files must retain the above copyright notice
|
|
|
|
*
|
2013-02-08 11:59:49 +00:00
|
|
|
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2012-04-27 02:49:18 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Test.Case.Error
|
2010-11-15 03:20:29 +00:00
|
|
|
* @since CakePHP(tm) v 2.0
|
2013-05-30 22:11:14 +00:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
2010-11-15 03:20:29 +00:00
|
|
|
*/
|
|
|
|
|
2010-12-09 05:55:24 +00:00
|
|
|
App::uses('ExceptionRenderer', 'Error');
|
|
|
|
App::uses('Controller', 'Controller');
|
|
|
|
App::uses('Component', 'Controller');
|
2011-01-28 06:25:07 +00:00
|
|
|
App::uses('Router', 'Routing');
|
2010-11-15 03:20:29 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Short description for class.
|
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Test.Case.Error
|
2010-11-15 03:20:29 +00:00
|
|
|
*/
|
|
|
|
class AuthBlueberryUser extends CakeTestModel {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* useTable property
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $useTable = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* BlueberryComponent class
|
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Test.Case.Error
|
2010-11-15 03:20:29 +00:00
|
|
|
*/
|
|
|
|
class BlueberryComponent extends Component {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* testName property
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public $testName = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* initialize method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2012-02-23 14:06:25 +00:00
|
|
|
public function initialize(Controller $controller) {
|
2010-11-15 03:20:29 +00:00
|
|
|
$this->testName = 'BlueberryComponent';
|
|
|
|
}
|
2012-03-13 01:21:02 +00:00
|
|
|
|
2010-11-15 03:20:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* TestErrorController class
|
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Test.Case.Error
|
2010-11-15 03:20:29 +00:00
|
|
|
*/
|
|
|
|
class TestErrorController extends Controller {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* uses property
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
public $uses = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* components property
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public $components = array('Blueberry');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* beforeRender method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function beforeRender() {
|
2010-11-15 03:20:29 +00:00
|
|
|
echo $this->Blueberry->testName;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* index method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function index() {
|
2010-11-15 03:20:29 +00:00
|
|
|
$this->autoRender = false;
|
|
|
|
return 'what up';
|
|
|
|
}
|
2012-03-13 01:21:02 +00:00
|
|
|
|
2010-11-15 03:20:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* MyCustomExceptionRenderer class
|
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Test.Case.Error
|
2010-11-15 03:20:29 +00:00
|
|
|
*/
|
|
|
|
class MyCustomExceptionRenderer extends ExceptionRenderer {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* custom error message type.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function missingWidgetThing() {
|
2010-11-15 03:20:29 +00:00
|
|
|
echo 'widget thing is missing';
|
|
|
|
}
|
2012-03-13 01:21:02 +00:00
|
|
|
|
2010-11-15 03:20:29 +00:00
|
|
|
}
|
2011-12-06 20:52:48 +00:00
|
|
|
|
2010-11-15 03:20:29 +00:00
|
|
|
/**
|
|
|
|
* Exception class for testing app error handlers and custom errors.
|
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Test.Case.Error
|
2010-11-15 03:20:29 +00:00
|
|
|
*/
|
2012-03-13 01:21:02 +00:00
|
|
|
class MissingWidgetThingException extends NotFoundException {
|
|
|
|
}
|
2010-11-15 03:20:29 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* ExceptionRendererTest class
|
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Test.Case.Error
|
2010-11-15 03:20:29 +00:00
|
|
|
*/
|
|
|
|
class ExceptionRendererTest extends CakeTestCase {
|
|
|
|
|
2012-03-13 01:21:02 +00:00
|
|
|
protected $_restoreError = false;
|
2011-12-04 21:27:51 +00:00
|
|
|
|
2010-11-15 03:20:29 +00:00
|
|
|
/**
|
|
|
|
* setup create a request object to get out of router later.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function setUp() {
|
2011-12-04 21:27:51 +00:00
|
|
|
parent::setUp();
|
2013-06-11 18:03:05 +00:00
|
|
|
Configure::write('Config.language', 'eng');
|
2010-11-15 03:20:29 +00:00
|
|
|
App::build(array(
|
2012-02-18 12:31:29 +00:00
|
|
|
'View' => array(
|
2012-03-13 01:21:02 +00:00
|
|
|
CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS
|
2010-11-15 03:20:29 +00:00
|
|
|
)
|
2012-02-18 12:04:54 +00:00
|
|
|
), App::RESET);
|
2010-11-15 03:20:29 +00:00
|
|
|
Router::reload();
|
|
|
|
|
|
|
|
$request = new CakeRequest(null, false);
|
|
|
|
$request->base = '';
|
|
|
|
Router::setRequestInfo($request);
|
|
|
|
Configure::write('debug', 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-12-04 21:27:51 +00:00
|
|
|
* tearDown
|
2010-11-15 03:20:29 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-12-04 21:27:51 +00:00
|
|
|
public function tearDown() {
|
2012-04-16 02:20:34 +00:00
|
|
|
parent::tearDown();
|
2010-11-15 03:20:29 +00:00
|
|
|
if ($this->_restoreError) {
|
|
|
|
restore_error_handler();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Mocks out the response on the ExceptionRenderer object so headers aren't modified.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function _mockResponse($error) {
|
|
|
|
$error->controller->response = $this->getMock('CakeResponse', array('_sendHeader'));
|
|
|
|
return $error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test that methods declared in an ExceptionRenderer subclass are not converted
|
|
|
|
* into error400 when debug > 0
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testSubclassMethodsNotBeingConvertedToError() {
|
2010-11-15 03:20:29 +00:00
|
|
|
Configure::write('debug', 2);
|
2011-04-17 10:35:21 +00:00
|
|
|
|
2010-11-15 03:20:29 +00:00
|
|
|
$exception = new MissingWidgetThingException('Widget not found');
|
|
|
|
$ExceptionRenderer = $this->_mockResponse(new MyCustomExceptionRenderer($exception));
|
|
|
|
|
|
|
|
ob_start();
|
|
|
|
$ExceptionRenderer->render();
|
|
|
|
$result = ob_get_clean();
|
|
|
|
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals('widget thing is missing', $result);
|
2010-11-15 03:20:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test that subclass methods are not converted when debug = 0
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testSubclassMethodsNotBeingConvertedDebug0() {
|
2010-11-15 03:20:29 +00:00
|
|
|
Configure::write('debug', 0);
|
|
|
|
$exception = new MissingWidgetThingException('Widget not found');
|
|
|
|
$ExceptionRenderer = $this->_mockResponse(new MyCustomExceptionRenderer($exception));
|
|
|
|
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals('missingWidgetThing', $ExceptionRenderer->method);
|
2010-11-15 03:20:29 +00:00
|
|
|
|
|
|
|
ob_start();
|
|
|
|
$ExceptionRenderer->render();
|
|
|
|
$result = ob_get_clean();
|
|
|
|
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals('widget thing is missing', $result, 'Method declared in subclass converted to error400');
|
2010-11-15 03:20:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test that ExceptionRenderer subclasses properly convert framework errors.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testSubclassConvertingFrameworkErrors() {
|
2010-11-15 03:20:29 +00:00
|
|
|
Configure::write('debug', 0);
|
2011-04-17 10:35:21 +00:00
|
|
|
|
2010-11-15 03:20:29 +00:00
|
|
|
$exception = new MissingControllerException('PostsController');
|
|
|
|
$ExceptionRenderer = $this->_mockResponse(new MyCustomExceptionRenderer($exception));
|
2011-04-17 10:35:21 +00:00
|
|
|
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals('error400', $ExceptionRenderer->method);
|
2010-11-15 03:20:29 +00:00
|
|
|
|
|
|
|
ob_start();
|
|
|
|
$ExceptionRenderer->render();
|
|
|
|
$result = ob_get_clean();
|
|
|
|
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertRegExp('/Not Found/', $result, 'Method declared in error handler not converted to error400. %s');
|
2010-11-15 03:20:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test things in the constructor.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testConstruction() {
|
2010-11-15 03:20:29 +00:00
|
|
|
$exception = new NotFoundException('Page not found');
|
|
|
|
$ExceptionRenderer = new ExceptionRenderer($exception);
|
|
|
|
|
2010-12-24 17:54:04 +00:00
|
|
|
$this->assertInstanceOf('CakeErrorController', $ExceptionRenderer->controller);
|
2010-11-15 03:20:29 +00:00
|
|
|
$this->assertEquals('error400', $ExceptionRenderer->method);
|
|
|
|
$this->assertEquals($exception, $ExceptionRenderer->error);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test that method gets coerced when debug = 0
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testErrorMethodCoercion() {
|
2010-11-15 03:20:29 +00:00
|
|
|
Configure::write('debug', 0);
|
|
|
|
$exception = new MissingActionException('Page not found');
|
|
|
|
$ExceptionRenderer = new ExceptionRenderer($exception);
|
|
|
|
|
2010-12-24 17:54:04 +00:00
|
|
|
$this->assertInstanceOf('CakeErrorController', $ExceptionRenderer->controller);
|
2010-11-15 03:20:29 +00:00
|
|
|
$this->assertEquals('error400', $ExceptionRenderer->method);
|
|
|
|
$this->assertEquals($exception, $ExceptionRenderer->error);
|
|
|
|
}
|
|
|
|
|
2012-06-01 04:42:55 +00:00
|
|
|
/**
|
|
|
|
* test that helpers in custom CakeErrorController are not lost
|
|
|
|
*/
|
|
|
|
public function testCakeErrorHelpersNotLost() {
|
|
|
|
$testApp = CAKE . 'Test' . DS . 'test_app' . DS;
|
|
|
|
App::build(array(
|
|
|
|
'Controller' => array(
|
|
|
|
$testApp . 'Controller' . DS
|
|
|
|
),
|
|
|
|
'View/Helper' => array(
|
|
|
|
$testApp . 'View' . DS . 'Helper' . DS
|
|
|
|
),
|
|
|
|
'View/Layouts' => array(
|
|
|
|
$testApp . 'View' . DS . 'Layouts' . DS
|
|
|
|
),
|
|
|
|
'Error' => array(
|
|
|
|
$testApp . 'Error' . DS
|
|
|
|
),
|
|
|
|
), App::RESET);
|
|
|
|
|
2012-06-04 02:17:05 +00:00
|
|
|
App::uses('TestAppsExceptionRenderer', 'Error');
|
2012-06-01 04:42:55 +00:00
|
|
|
$exception = new SocketException('socket exception');
|
|
|
|
$renderer = new TestAppsExceptionRenderer($exception);
|
|
|
|
|
|
|
|
ob_start();
|
|
|
|
$renderer->render();
|
|
|
|
$result = ob_get_clean();
|
|
|
|
$this->assertContains('<b>peeled</b>', $result);
|
|
|
|
}
|
|
|
|
|
2010-11-15 03:20:29 +00:00
|
|
|
/**
|
|
|
|
* test that unknown exception types with valid status codes are treated correctly.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testUnknownExceptionTypeWithExceptionThatHasA400Code() {
|
2010-11-15 03:20:29 +00:00
|
|
|
$exception = new MissingWidgetThingException('coding fail.');
|
|
|
|
$ExceptionRenderer = new ExceptionRenderer($exception);
|
|
|
|
$ExceptionRenderer->controller->response = $this->getMock('CakeResponse', array('statusCode', '_sendHeader'));
|
|
|
|
$ExceptionRenderer->controller->response->expects($this->once())->method('statusCode')->with(404);
|
|
|
|
|
|
|
|
ob_start();
|
|
|
|
$ExceptionRenderer->render();
|
2011-11-03 01:21:48 +00:00
|
|
|
$result = ob_get_clean();
|
2010-11-15 03:20:29 +00:00
|
|
|
|
|
|
|
$this->assertFalse(method_exists($ExceptionRenderer, 'missingWidgetThing'), 'no method should exist.');
|
|
|
|
$this->assertEquals('error400', $ExceptionRenderer->method, 'incorrect method coercion.');
|
2011-11-03 01:21:48 +00:00
|
|
|
$this->assertContains('coding fail', $result, 'Text should show up.');
|
2010-11-15 03:20:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test that unknown exception types with valid status codes are treated correctly.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testUnknownExceptionTypeWithNoCodeIsA500() {
|
2010-11-15 03:20:29 +00:00
|
|
|
$exception = new OutOfBoundsException('foul ball.');
|
|
|
|
$ExceptionRenderer = new ExceptionRenderer($exception);
|
|
|
|
$ExceptionRenderer->controller->response = $this->getMock('CakeResponse', array('statusCode', '_sendHeader'));
|
2011-11-03 01:21:48 +00:00
|
|
|
$ExceptionRenderer->controller->response->expects($this->once())
|
|
|
|
->method('statusCode')
|
|
|
|
->with(500);
|
|
|
|
|
|
|
|
ob_start();
|
|
|
|
$ExceptionRenderer->render();
|
|
|
|
$result = ob_get_clean();
|
|
|
|
|
|
|
|
$this->assertEquals('error500', $ExceptionRenderer->method, 'incorrect method coercion.');
|
|
|
|
$this->assertContains('foul ball.', $result, 'Text should show up as its debug mode.');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test that unknown exceptions have messages ignored.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testUnknownExceptionInProduction() {
|
|
|
|
Configure::write('debug', 0);
|
|
|
|
|
|
|
|
$exception = new OutOfBoundsException('foul ball.');
|
|
|
|
$ExceptionRenderer = new ExceptionRenderer($exception);
|
|
|
|
$ExceptionRenderer->controller->response = $this->getMock('CakeResponse', array('statusCode', '_sendHeader'));
|
|
|
|
$ExceptionRenderer->controller->response->expects($this->once())
|
|
|
|
->method('statusCode')
|
|
|
|
->with(500);
|
2010-11-15 03:20:29 +00:00
|
|
|
|
|
|
|
ob_start();
|
|
|
|
$ExceptionRenderer->render();
|
2011-11-03 01:21:48 +00:00
|
|
|
$result = ob_get_clean();
|
2010-11-15 03:20:29 +00:00
|
|
|
|
|
|
|
$this->assertEquals('error500', $ExceptionRenderer->method, 'incorrect method coercion.');
|
2011-11-03 01:21:48 +00:00
|
|
|
$this->assertNotContains('foul ball.', $result, 'Text should no show up.');
|
|
|
|
$this->assertContains('Internal Error', $result, 'Generic message only.');
|
2010-11-15 03:20:29 +00:00
|
|
|
}
|
|
|
|
|
2010-12-11 18:30:29 +00:00
|
|
|
/**
|
|
|
|
* test that unknown exception types with valid status codes are treated correctly.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testUnknownExceptionTypeWithCodeHigherThan500() {
|
2010-12-11 18:30:29 +00:00
|
|
|
$exception = new OutOfBoundsException('foul ball.', 501);
|
|
|
|
$ExceptionRenderer = new ExceptionRenderer($exception);
|
|
|
|
$ExceptionRenderer->controller->response = $this->getMock('CakeResponse', array('statusCode', '_sendHeader'));
|
|
|
|
$ExceptionRenderer->controller->response->expects($this->once())->method('statusCode')->with(501);
|
|
|
|
|
|
|
|
ob_start();
|
|
|
|
$ExceptionRenderer->render();
|
2011-11-03 01:21:48 +00:00
|
|
|
$result = ob_get_clean();
|
2010-12-11 18:30:29 +00:00
|
|
|
|
|
|
|
$this->assertEquals('error500', $ExceptionRenderer->method, 'incorrect method coercion.');
|
2011-11-03 01:21:48 +00:00
|
|
|
$this->assertContains('foul ball.', $result, 'Text should show up as its debug mode.');
|
2010-12-11 18:30:29 +00:00
|
|
|
}
|
|
|
|
|
2010-11-15 03:20:29 +00:00
|
|
|
/**
|
|
|
|
* testerror400 method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testError400() {
|
2010-11-15 03:20:29 +00:00
|
|
|
Router::reload();
|
|
|
|
|
|
|
|
$request = new CakeRequest('posts/view/1000', false);
|
|
|
|
Router::setRequestInfo($request);
|
|
|
|
|
|
|
|
$exception = new NotFoundException('Custom message');
|
|
|
|
$ExceptionRenderer = new ExceptionRenderer($exception);
|
|
|
|
$ExceptionRenderer->controller->response = $this->getMock('CakeResponse', array('statusCode', '_sendHeader'));
|
|
|
|
$ExceptionRenderer->controller->response->expects($this->once())->method('statusCode')->with(404);
|
|
|
|
|
|
|
|
ob_start();
|
|
|
|
$ExceptionRenderer->render();
|
|
|
|
$result = ob_get_clean();
|
|
|
|
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertRegExp('/<h2>Custom message<\/h2>/', $result);
|
|
|
|
$this->assertRegExp("/<strong>'.*?\/posts\/view\/1000'<\/strong>/", $result);
|
2010-11-15 03:20:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test that error400 only modifies the messages on CakeExceptions.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testerror400OnlyChangingCakeException() {
|
2010-11-15 03:20:29 +00:00
|
|
|
Configure::write('debug', 0);
|
|
|
|
|
|
|
|
$exception = new NotFoundException('Custom message');
|
|
|
|
$ExceptionRenderer = $this->_mockResponse(new ExceptionRenderer($exception));
|
|
|
|
|
|
|
|
ob_start();
|
|
|
|
$ExceptionRenderer->render();
|
|
|
|
$result = ob_get_clean();
|
|
|
|
$this->assertContains('Custom message', $result);
|
|
|
|
|
|
|
|
$exception = new MissingActionException(array('controller' => 'PostsController', 'action' => 'index'));
|
|
|
|
$ExceptionRenderer = $this->_mockResponse(new ExceptionRenderer($exception));
|
|
|
|
|
|
|
|
ob_start();
|
|
|
|
$ExceptionRenderer->render();
|
|
|
|
$result = ob_get_clean();
|
|
|
|
$this->assertContains('Not Found', $result);
|
|
|
|
}
|
2011-12-06 20:52:48 +00:00
|
|
|
|
2010-11-15 03:20:29 +00:00
|
|
|
/**
|
|
|
|
* test that error400 doesn't expose XSS
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testError400NoInjection() {
|
2010-11-15 03:20:29 +00:00
|
|
|
Router::reload();
|
|
|
|
|
|
|
|
$request = new CakeRequest('pages/<span id=333>pink</span></id><script>document.body.style.background = t=document.getElementById(333).innerHTML;window.alert(t);</script>', false);
|
|
|
|
Router::setRequestInfo($request);
|
|
|
|
|
|
|
|
$exception = new NotFoundException('Custom message');
|
|
|
|
$ExceptionRenderer = $this->_mockResponse(new ExceptionRenderer($exception));
|
|
|
|
|
|
|
|
ob_start();
|
|
|
|
$ExceptionRenderer->render();
|
|
|
|
$result = ob_get_clean();
|
|
|
|
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertNotRegExp('#<script>document#', $result);
|
|
|
|
$this->assertNotRegExp('#alert\(t\);</script>#', $result);
|
2010-11-15 03:20:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* testError500 method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testError500Message() {
|
2010-11-15 03:20:29 +00:00
|
|
|
$exception = new InternalErrorException('An Internal Error Has Occurred');
|
|
|
|
$ExceptionRenderer = new ExceptionRenderer($exception);
|
|
|
|
$ExceptionRenderer->controller->response = $this->getMock('CakeResponse', array('statusCode', '_sendHeader'));
|
|
|
|
$ExceptionRenderer->controller->response->expects($this->once())->method('statusCode')->with(500);
|
|
|
|
|
|
|
|
ob_start();
|
|
|
|
$ExceptionRenderer->render();
|
|
|
|
$result = ob_get_clean();
|
|
|
|
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertRegExp('/<h2>An Internal Error Has Occurred<\/h2>/', $result);
|
2010-11-15 03:20:29 +00:00
|
|
|
}
|
|
|
|
|
2012-08-08 11:13:28 +00:00
|
|
|
/**
|
|
|
|
* testExceptionResponseHeader method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testExceptionResponseHeader() {
|
|
|
|
$exception = new MethodNotAllowedException('Only allowing POST and DELETE');
|
|
|
|
$exception->responseHeader(array('Allow: POST, DELETE'));
|
|
|
|
$ExceptionRenderer = new ExceptionRenderer($exception);
|
|
|
|
|
|
|
|
//Replace response object with mocked object add back the original headers which had been set in ExceptionRenderer constructor
|
|
|
|
$headers = $ExceptionRenderer->controller->response->header();
|
|
|
|
$ExceptionRenderer->controller->response = $this->getMock('CakeResponse', array('_sendHeader'));
|
|
|
|
$ExceptionRenderer->controller->response->header($headers);
|
|
|
|
|
|
|
|
$ExceptionRenderer->controller->response->expects($this->at(1))->method('_sendHeader')->with('Allow', 'POST, DELETE');
|
|
|
|
ob_start();
|
|
|
|
$ExceptionRenderer->render();
|
2013-01-23 12:45:50 +00:00
|
|
|
ob_get_clean();
|
2012-08-08 11:13:28 +00:00
|
|
|
}
|
|
|
|
|
2010-11-15 03:20:29 +00:00
|
|
|
/**
|
|
|
|
* testMissingController method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testMissingController() {
|
2011-10-16 11:40:36 +00:00
|
|
|
$exception = new MissingControllerException(array('class' => 'PostsController'));
|
2010-11-15 03:20:29 +00:00
|
|
|
$ExceptionRenderer = $this->_mockResponse(new ExceptionRenderer($exception));
|
|
|
|
|
|
|
|
ob_start();
|
|
|
|
$ExceptionRenderer->render();
|
|
|
|
$result = ob_get_clean();
|
|
|
|
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertRegExp('/<h2>Missing Controller<\/h2>/', $result);
|
|
|
|
$this->assertRegExp('/<em>PostsController<\/em>/', $result);
|
2010-11-15 03:20:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an array of tests to run for the various CakeException classes.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public static function testProvider() {
|
|
|
|
return array(
|
|
|
|
array(
|
|
|
|
new MissingActionException(array('controller' => 'PostsController', 'action' => 'index')),
|
|
|
|
array(
|
|
|
|
'/<h2>Missing Method in PostsController<\/h2>/',
|
|
|
|
'/<em>PostsController::<\/em><em>index\(\)<\/em>/'
|
|
|
|
),
|
|
|
|
404
|
|
|
|
),
|
|
|
|
array(
|
2013-08-07 23:03:21 +00:00
|
|
|
new PrivateActionException(array('controller' => 'PostsController', 'action' => '_secretSauce')),
|
2010-11-15 03:20:29 +00:00
|
|
|
array(
|
|
|
|
'/<h2>Private Method in PostsController<\/h2>/',
|
|
|
|
'/<em>PostsController::<\/em><em>_secretSauce\(\)<\/em>/'
|
|
|
|
),
|
|
|
|
404
|
|
|
|
),
|
|
|
|
array(
|
2011-11-05 06:50:11 +00:00
|
|
|
new MissingTableException(array('table' => 'articles', 'class' => 'Article', 'ds' => 'test')),
|
2010-11-15 03:20:29 +00:00
|
|
|
array(
|
|
|
|
'/<h2>Missing Database Table<\/h2>/',
|
2011-11-05 06:50:11 +00:00
|
|
|
'/Table <em>articles<\/em> for model <em>Article<\/em> was not found in datasource <em>test<\/em>/'
|
2010-11-15 03:20:29 +00:00
|
|
|
),
|
|
|
|
500
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
new MissingDatabaseException(array('connection' => 'default')),
|
|
|
|
array(
|
|
|
|
'/<h2>Missing Database Connection<\/h2>/',
|
|
|
|
'/Confirm you have created the file/'
|
|
|
|
),
|
|
|
|
500
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
new MissingViewException(array('file' => '/posts/about.ctp')),
|
|
|
|
array(
|
|
|
|
"/posts\/about.ctp/"
|
|
|
|
),
|
|
|
|
500
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
new MissingLayoutException(array('file' => 'layouts/my_layout.ctp')),
|
|
|
|
array(
|
|
|
|
"/Missing Layout/",
|
|
|
|
"/layouts\/my_layout.ctp/"
|
|
|
|
),
|
|
|
|
500
|
|
|
|
),
|
|
|
|
array(
|
2012-09-13 02:31:07 +00:00
|
|
|
new MissingConnectionException(array('class' => 'Mysql')),
|
2010-11-15 03:20:29 +00:00
|
|
|
array(
|
|
|
|
'/<h2>Missing Database Connection<\/h2>/',
|
2012-09-13 02:31:07 +00:00
|
|
|
'/A Database connection using "Mysql" was missing or unable to connect./',
|
2010-11-15 03:20:29 +00:00
|
|
|
),
|
|
|
|
500
|
|
|
|
),
|
2012-06-15 12:51:36 +00:00
|
|
|
array(
|
|
|
|
new MissingConnectionException(array('class' => 'Mysql', 'enabled' => false)),
|
|
|
|
array(
|
|
|
|
'/<h2>Missing Database Connection<\/h2>/',
|
2012-09-13 02:31:07 +00:00
|
|
|
'/A Database connection using "Mysql" was missing or unable to connect./',
|
2012-06-15 12:51:36 +00:00
|
|
|
'/Mysql driver is NOT enabled/'
|
|
|
|
),
|
|
|
|
500
|
|
|
|
),
|
2011-07-07 17:36:21 +00:00
|
|
|
array(
|
|
|
|
new MissingDatasourceConfigException(array('config' => 'default')),
|
|
|
|
array(
|
|
|
|
'/<h2>Missing Datasource Configuration<\/h2>/',
|
|
|
|
'/The datasource configuration <em>default<\/em> was not found in database.php/'
|
|
|
|
),
|
|
|
|
500
|
|
|
|
),
|
|
|
|
array(
|
2011-10-15 18:06:31 +00:00
|
|
|
new MissingDatasourceException(array('class' => 'MyDatasource', 'plugin' => 'MyPlugin')),
|
2011-07-07 17:36:21 +00:00
|
|
|
array(
|
2011-10-15 18:06:31 +00:00
|
|
|
'/<h2>Missing Datasource<\/h2>/',
|
2011-10-16 11:40:36 +00:00
|
|
|
'/Datasource class <em>MyPlugin.MyDatasource<\/em> could not be found/'
|
2011-07-07 17:36:21 +00:00
|
|
|
),
|
|
|
|
500
|
|
|
|
),
|
2010-11-15 03:20:29 +00:00
|
|
|
array(
|
2011-10-15 18:06:31 +00:00
|
|
|
new MissingHelperException(array('class' => 'MyCustomHelper')),
|
2010-11-15 03:20:29 +00:00
|
|
|
array(
|
2011-10-15 18:06:31 +00:00
|
|
|
'/<h2>Missing Helper<\/h2>/',
|
2011-10-16 11:40:36 +00:00
|
|
|
'/<em>MyCustomHelper<\/em> could not be found./',
|
2011-10-15 18:06:31 +00:00
|
|
|
'/Create the class <em>MyCustomHelper<\/em> below in file:/',
|
2011-06-15 02:47:43 +00:00
|
|
|
'/(\/|\\\)MyCustomHelper.php/'
|
2010-11-15 03:20:29 +00:00
|
|
|
),
|
|
|
|
500
|
|
|
|
),
|
|
|
|
array(
|
2011-10-15 18:06:31 +00:00
|
|
|
new MissingBehaviorException(array('class' => 'MyCustomBehavior')),
|
2010-11-15 03:20:29 +00:00
|
|
|
array(
|
2011-10-15 18:06:31 +00:00
|
|
|
'/<h2>Missing Behavior<\/h2>/',
|
|
|
|
'/Create the class <em>MyCustomBehavior<\/em> below in file:/',
|
2011-06-15 02:47:43 +00:00
|
|
|
'/(\/|\\\)MyCustomBehavior.php/'
|
2010-11-15 03:20:29 +00:00
|
|
|
),
|
|
|
|
500
|
|
|
|
),
|
|
|
|
array(
|
2011-10-15 18:06:31 +00:00
|
|
|
new MissingComponentException(array('class' => 'SideboxComponent')),
|
2010-11-15 03:20:29 +00:00
|
|
|
array(
|
2011-10-15 18:06:31 +00:00
|
|
|
'/<h2>Missing Component<\/h2>/',
|
|
|
|
'/Create the class <em>SideboxComponent<\/em> below in file:/',
|
2011-06-15 02:47:43 +00:00
|
|
|
'/(\/|\\\)SideboxComponent.php/'
|
2010-11-15 03:20:29 +00:00
|
|
|
),
|
|
|
|
500
|
2011-02-13 20:13:52 +00:00
|
|
|
),
|
|
|
|
array(
|
|
|
|
new Exception('boom'),
|
|
|
|
array(
|
|
|
|
'/Internal Error/'
|
|
|
|
),
|
|
|
|
500
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
new RuntimeException('another boom'),
|
|
|
|
array(
|
|
|
|
'/Internal Error/'
|
|
|
|
),
|
|
|
|
500
|
2011-04-16 21:47:39 +00:00
|
|
|
),
|
|
|
|
array(
|
|
|
|
new CakeException('base class'),
|
|
|
|
array('/Internal Error/'),
|
|
|
|
500
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
new ConfigureException('No file'),
|
|
|
|
array('/Internal Error/'),
|
|
|
|
500
|
2010-11-15 03:20:29 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test the various CakeException sub classes
|
|
|
|
*
|
|
|
|
* @dataProvider testProvider
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testCakeExceptionHandling($exception, $patterns, $code) {
|
2010-11-15 03:20:29 +00:00
|
|
|
$ExceptionRenderer = new ExceptionRenderer($exception);
|
|
|
|
$ExceptionRenderer->controller->response = $this->getMock('CakeResponse', array('statusCode', '_sendHeader'));
|
|
|
|
$ExceptionRenderer->controller->response->expects($this->once())
|
|
|
|
->method('statusCode')
|
|
|
|
->with($code);
|
|
|
|
|
|
|
|
ob_start();
|
|
|
|
$ExceptionRenderer->render();
|
|
|
|
$result = ob_get_clean();
|
|
|
|
|
|
|
|
foreach ($patterns as $pattern) {
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertRegExp($pattern, $result);
|
2010-11-15 03:20:29 +00:00
|
|
|
}
|
|
|
|
}
|
2011-04-26 01:04:50 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test exceptions being raised when helpers are missing.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testMissingRenderSafe() {
|
2011-10-15 18:06:31 +00:00
|
|
|
$exception = new MissingHelperException(array('class' => 'Fail'));
|
2011-04-26 01:04:50 +00:00
|
|
|
$ExceptionRenderer = new ExceptionRenderer($exception);
|
|
|
|
|
2012-09-28 01:38:20 +00:00
|
|
|
$ExceptionRenderer->controller = $this->getMock('Controller', array('render'));
|
2011-04-26 01:04:50 +00:00
|
|
|
$ExceptionRenderer->controller->helpers = array('Fail', 'Boom');
|
|
|
|
$ExceptionRenderer->controller->request = $this->getMock('CakeRequest');
|
2012-09-28 01:38:20 +00:00
|
|
|
$ExceptionRenderer->controller->expects($this->at(0))
|
2011-04-26 01:04:50 +00:00
|
|
|
->method('render')
|
2011-10-15 18:06:31 +00:00
|
|
|
->with('missingHelper')
|
2011-04-26 01:04:50 +00:00
|
|
|
->will($this->throwException($exception));
|
|
|
|
|
2012-09-28 01:38:20 +00:00
|
|
|
$response = $this->getMock('CakeResponse');
|
|
|
|
$response->expects($this->once())
|
|
|
|
->method('body')
|
|
|
|
->with($this->stringContains('Helper class Fail'));
|
2011-04-26 01:04:50 +00:00
|
|
|
|
2012-09-28 01:38:20 +00:00
|
|
|
$ExceptionRenderer->controller->response = $response;
|
2011-04-26 01:04:50 +00:00
|
|
|
$ExceptionRenderer->render();
|
|
|
|
sort($ExceptionRenderer->controller->helpers);
|
|
|
|
$this->assertEquals(array('Form', 'Html', 'Session'), $ExceptionRenderer->controller->helpers);
|
|
|
|
}
|
2011-05-17 01:12:28 +00:00
|
|
|
|
2012-09-28 01:38:20 +00:00
|
|
|
/**
|
|
|
|
* Test that exceptions in beforeRender() are handled by outputMessageSafe
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testRenderExceptionInBeforeRender() {
|
|
|
|
$exception = new NotFoundException('Not there, sorry');
|
|
|
|
$ExceptionRenderer = new ExceptionRenderer($exception);
|
|
|
|
|
|
|
|
$ExceptionRenderer->controller = $this->getMock('Controller', array('beforeRender'));
|
|
|
|
$ExceptionRenderer->controller->request = $this->getMock('CakeRequest');
|
|
|
|
$ExceptionRenderer->controller->expects($this->any())
|
|
|
|
->method('beforeRender')
|
|
|
|
->will($this->throwException($exception));
|
|
|
|
|
|
|
|
$response = $this->getMock('CakeResponse');
|
|
|
|
$response->expects($this->once())
|
|
|
|
->method('body')
|
|
|
|
->with($this->stringContains('Not there, sorry'));
|
|
|
|
|
|
|
|
$ExceptionRenderer->controller->response = $response;
|
|
|
|
$ExceptionRenderer->render();
|
|
|
|
}
|
|
|
|
|
2012-02-03 02:24:36 +00:00
|
|
|
/**
|
|
|
|
* Test that missing subDir/layoutPath don't cause other fatal errors.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testMissingSubdirRenderSafe() {
|
|
|
|
$exception = new NotFoundException();
|
|
|
|
$ExceptionRenderer = new ExceptionRenderer($exception);
|
|
|
|
|
2012-09-28 01:38:20 +00:00
|
|
|
$ExceptionRenderer->controller = $this->getMock('Controller', array('render'));
|
2012-02-03 02:24:36 +00:00
|
|
|
$ExceptionRenderer->controller->helpers = array('Fail', 'Boom');
|
|
|
|
$ExceptionRenderer->controller->layoutPath = 'json';
|
|
|
|
$ExceptionRenderer->controller->subDir = 'json';
|
|
|
|
$ExceptionRenderer->controller->viewClass = 'Json';
|
|
|
|
$ExceptionRenderer->controller->request = $this->getMock('CakeRequest');
|
|
|
|
|
2012-09-28 01:38:20 +00:00
|
|
|
$ExceptionRenderer->controller->expects($this->once())
|
2012-02-03 02:24:36 +00:00
|
|
|
->method('render')
|
|
|
|
->with('error400')
|
|
|
|
->will($this->throwException($exception));
|
|
|
|
|
2012-09-28 01:38:20 +00:00
|
|
|
$response = $this->getMock('CakeResponse');
|
|
|
|
$response->expects($this->once())
|
|
|
|
->method('body')
|
|
|
|
->with($this->stringContains('Not Found'));
|
|
|
|
$response->expects($this->once())
|
2012-02-03 02:24:36 +00:00
|
|
|
->method('type')
|
|
|
|
->with('html');
|
|
|
|
|
2012-09-28 01:38:20 +00:00
|
|
|
$ExceptionRenderer->controller->response = $response;
|
|
|
|
|
2012-02-03 02:24:36 +00:00
|
|
|
$ExceptionRenderer->render();
|
|
|
|
$this->assertEquals('', $ExceptionRenderer->controller->layoutPath);
|
|
|
|
$this->assertEquals('', $ExceptionRenderer->controller->subDir);
|
2013-09-15 22:49:01 +00:00
|
|
|
$this->assertEquals('Errors', $ExceptionRenderer->controller->viewPath);
|
2012-02-03 02:24:36 +00:00
|
|
|
}
|
|
|
|
|
2011-05-17 01:12:28 +00:00
|
|
|
/**
|
|
|
|
* Test that exceptions can be rendered when an request hasn't been registered
|
|
|
|
* with Router
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testRenderWithNoRequest() {
|
2011-05-17 01:12:28 +00:00
|
|
|
Router::reload();
|
|
|
|
$this->assertNull(Router::getRequest(false));
|
|
|
|
|
|
|
|
$exception = new Exception('Terrible');
|
|
|
|
$ExceptionRenderer = new ExceptionRenderer($exception);
|
|
|
|
$ExceptionRenderer->controller->response = $this->getMock('CakeResponse', array('statusCode', '_sendHeader'));
|
|
|
|
$ExceptionRenderer->controller->response->expects($this->once())
|
|
|
|
->method('statusCode')
|
|
|
|
->with(500);
|
|
|
|
|
|
|
|
ob_start();
|
|
|
|
$ExceptionRenderer->render();
|
|
|
|
$result = ob_get_clean();
|
|
|
|
|
|
|
|
$this->assertContains('Internal Error', $result);
|
|
|
|
}
|
2011-09-04 09:20:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests the output of rendering a PDOException
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testPDOException() {
|
|
|
|
$exception = new PDOException('There was an error in the SQL query');
|
|
|
|
$exception->queryString = 'SELECT * from poo_query < 5 and :seven';
|
|
|
|
$exception->params = array('seven' => 7);
|
|
|
|
$ExceptionRenderer = new ExceptionRenderer($exception);
|
|
|
|
$ExceptionRenderer->controller->response = $this->getMock('CakeResponse', array('statusCode', '_sendHeader'));
|
|
|
|
$ExceptionRenderer->controller->response->expects($this->once())->method('statusCode')->with(500);
|
|
|
|
|
|
|
|
ob_start();
|
|
|
|
$ExceptionRenderer->render();
|
|
|
|
$result = ob_get_clean();
|
|
|
|
|
2011-10-21 01:42:14 +00:00
|
|
|
$this->assertContains('<h2>Database Error</h2>', $result);
|
|
|
|
$this->assertContains('There was an error in the SQL query', $result);
|
2012-08-04 20:46:09 +00:00
|
|
|
$this->assertContains(h('SELECT * from poo_query < 5 and :seven'), $result);
|
2011-10-21 01:42:14 +00:00
|
|
|
$this->assertContains("'seven' => (int) 7", $result);
|
2011-09-04 09:20:19 +00:00
|
|
|
}
|
2010-11-15 03:20:29 +00:00
|
|
|
}
|