2010-11-15 03:20:29 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* ExceptionRendererTest file
|
|
|
|
*
|
|
|
|
* PHP 5
|
|
|
|
*
|
|
|
|
* CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
|
2011-05-29 21:31:39 +00:00
|
|
|
* Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2010-11-15 03:20:29 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice
|
|
|
|
*
|
2011-05-29 21:31:39 +00:00
|
|
|
* @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2010-11-15 03:20:29 +00:00
|
|
|
* @link http://book.cakephp.org/view/1196/Testing 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
|
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
|
|
|
*/
|
|
|
|
|
2010-12-09 05:55:24 +00:00
|
|
|
App::uses('ExceptionRenderer', 'Error');
|
|
|
|
App::uses('Controller', 'Controller');
|
2011-01-28 06:25:07 +00:00
|
|
|
App::uses('AppController', 'Controller');
|
2010-12-09 05:55:24 +00:00
|
|
|
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 {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* name property
|
|
|
|
*
|
|
|
|
* @var string 'AuthBlueberryUser'
|
|
|
|
*/
|
|
|
|
public $name = 'AuthBlueberryUser';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2011-11-13 01:43:55 +00:00
|
|
|
public function initialize($controller) {
|
2010-11-15 03:20:29 +00:00
|
|
|
$this->testName = 'BlueberryComponent';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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';
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
*/
|
|
|
|
class MissingWidgetThingException extends NotFoundException { }
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 {
|
|
|
|
|
2011-10-10 21:18:48 +00:00
|
|
|
public $_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();
|
2010-11-15 03:20:29 +00:00
|
|
|
App::build(array(
|
|
|
|
'views' => array(
|
2011-04-17 10:35:21 +00:00
|
|
|
CAKE . 'Test' . DS . 'test_app' . DS . 'View'. DS
|
2010-11-15 03:20:29 +00:00
|
|
|
)
|
|
|
|
), true);
|
|
|
|
Router::reload();
|
|
|
|
|
|
|
|
$request = new CakeRequest(null, false);
|
|
|
|
$request->base = '';
|
|
|
|
Router::setRequestInfo($request);
|
|
|
|
$this->_debug = Configure::read('debug');
|
|
|
|
$this->_error = Configure::read('Error');
|
|
|
|
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() {
|
2010-11-15 03:20:29 +00:00
|
|
|
Configure::write('debug', $this->_debug);
|
|
|
|
Configure::write('Error', $this->_error);
|
|
|
|
App::build();
|
|
|
|
if ($this->_restoreError) {
|
|
|
|
restore_error_handler();
|
|
|
|
}
|
2011-12-04 21:27:51 +00:00
|
|
|
parent::tearDown();
|
2010-11-15 03:20:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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();
|
|
|
|
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($result, 'widget thing is missing');
|
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();
|
|
|
|
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($result, 'widget thing is missing', '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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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(
|
|
|
|
new PrivateActionException(array('controller' => 'PostsController' , 'action' => '_secretSauce')),
|
|
|
|
array(
|
|
|
|
'/<h2>Private Method in PostsController<\/h2>/',
|
|
|
|
'/<em>PostsController::<\/em><em>_secretSauce\(\)<\/em>/'
|
|
|
|
),
|
|
|
|
404
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
new MissingTableException(array('table' => 'articles', 'class' => 'Article')),
|
|
|
|
array(
|
|
|
|
'/<h2>Missing Database Table<\/h2>/',
|
|
|
|
'/table <em>articles<\/em> for model <em>Article<\/em>/'
|
|
|
|
),
|
|
|
|
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(
|
|
|
|
new MissingConnectionException(array('class' => 'Article')),
|
|
|
|
array(
|
|
|
|
'/<h2>Missing Database Connection<\/h2>/',
|
|
|
|
'/Article requires a database connection/'
|
|
|
|
),
|
|
|
|
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);
|
|
|
|
|
|
|
|
$ExceptionRenderer->controller = $this->getMock('Controller');
|
|
|
|
$ExceptionRenderer->controller->helpers = array('Fail', 'Boom');
|
|
|
|
$ExceptionRenderer->controller->request = $this->getMock('CakeRequest');
|
|
|
|
$ExceptionRenderer->controller->expects($this->at(2))
|
|
|
|
->method('render')
|
2011-10-15 18:06:31 +00:00
|
|
|
->with('missingHelper')
|
2011-04-26 01:04:50 +00:00
|
|
|
->will($this->throwException($exception));
|
|
|
|
|
|
|
|
$ExceptionRenderer->controller->expects($this->at(3))
|
|
|
|
->method('render')
|
|
|
|
->with('error500')
|
|
|
|
->will($this->returnValue(true));
|
|
|
|
|
|
|
|
$ExceptionRenderer->controller->response = $this->getMock('CakeResponse');
|
|
|
|
$ExceptionRenderer->render();
|
|
|
|
sort($ExceptionRenderer->controller->helpers);
|
|
|
|
$this->assertEquals(array('Form', 'Html', 'Session'), $ExceptionRenderer->controller->helpers);
|
|
|
|
}
|
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-11-16 00:07:56 +00:00
|
|
|
$this->assertRegExp('/<h2>Database Error<\/h2>/', $result);
|
|
|
|
$this->assertRegExp('/There was an error in the SQL query/', $result);
|
|
|
|
$this->assertRegExp('/SELECT \* from poo_query < 5 and :seven/', $result);
|
|
|
|
$this->assertRegExp('/"seven" => 7/', $result);
|
2011-09-04 09:20:19 +00:00
|
|
|
}
|
2010-11-15 03:20:29 +00:00
|
|
|
}
|