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
|
|
|
*
|
|
|
|
* PHP versions 4 and 5
|
|
|
|
*
|
2010-05-19 01:15:13 +00:00
|
|
|
* CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
|
2010-01-26 19:18:20 +00:00
|
|
|
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* Licensed under The Open Group Test Suite License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2010-01-26 19:18:20 +00:00
|
|
|
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2010-05-19 01:15:13 +00:00
|
|
|
* @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
|
2009-03-18 17:55:58 +00:00
|
|
|
* @package cake
|
2008-10-30 17:30:26 +00:00
|
|
|
* @subpackage cake.tests.cases.libs
|
|
|
|
* @since CakePHP(tm) v 1.2.0.5432
|
|
|
|
* @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-08-31 02:59:10 +00:00
|
|
|
App::import('Core', array('ErrorHandler', 'Controller', 'Component'));
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-11-08 01:00:05 +00:00
|
|
|
/**
|
|
|
|
* Short description for class.
|
|
|
|
*
|
2009-03-18 17:55:58 +00:00
|
|
|
* @package cake
|
2008-11-08 01:00:05 +00:00
|
|
|
* @subpackage cake.tests.cases.libs
|
|
|
|
*/
|
|
|
|
class AuthBlueberryUser extends CakeTestModel {
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-11-08 01:00:05 +00:00
|
|
|
/**
|
|
|
|
* name property
|
|
|
|
*
|
|
|
|
* @var string 'AuthBlueberryUser'
|
|
|
|
* @access public
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $name = 'AuthBlueberryUser';
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-11-08 01:00:05 +00:00
|
|
|
/**
|
|
|
|
* useTable property
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
* @access public
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $useTable = false;
|
2008-11-08 01:00:05 +00:00
|
|
|
}
|
2010-09-19 05:43:00 +00:00
|
|
|
|
2010-08-29 05:01:36 +00:00
|
|
|
/**
|
|
|
|
* BlueberryComponent class
|
|
|
|
*
|
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.tests.cases.libs
|
|
|
|
*/
|
|
|
|
class BlueberryComponent extends Component {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* testName property
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public $testName = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* initialize method
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function initialize(&$controller) {
|
|
|
|
$this->testName = 'BlueberryComponent';
|
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* TestErrorController class
|
2008-06-11 09:02:54 +00:00
|
|
|
*
|
2009-03-18 17:55:58 +00:00
|
|
|
* @package cake
|
2008-10-30 17:30:26 +00:00
|
|
|
* @subpackage cake.tests.cases.libs
|
2008-06-02 19:22:55 +00:00
|
|
|
*/
|
2010-09-25 14:41:08 +00:00
|
|
|
class TestErrorController extends Controller {
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* uses property
|
2008-06-11 09:02:54 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @var array
|
|
|
|
* @access public
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $uses = array();
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
2010-09-25 14:41:08 +00:00
|
|
|
* components property
|
2008-06-11 09:02:54 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
2010-09-25 14:41:08 +00:00
|
|
|
public $components = array('Blueberry');
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-11-08 01:00:05 +00:00
|
|
|
/**
|
2010-09-25 14:41:08 +00:00
|
|
|
* beforeRender method
|
2008-11-08 01:00:05 +00:00
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
2010-09-25 14:41:08 +00:00
|
|
|
function beforeRender() {
|
|
|
|
echo $this->Blueberry->testName;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-11-08 01:00:05 +00:00
|
|
|
/**
|
2010-09-25 14:41:08 +00:00
|
|
|
* index method
|
2008-11-08 01:00:05 +00:00
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
2010-09-25 14:41:08 +00:00
|
|
|
function index() {
|
|
|
|
$this->autoRender = false;
|
|
|
|
return 'what up';
|
|
|
|
}
|
2008-11-08 01:00:05 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-10-09 04:13:08 +00:00
|
|
|
/**
|
|
|
|
* MyCustomErrorHandler class
|
|
|
|
*
|
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.tests.cases.libs
|
|
|
|
*/
|
|
|
|
class MyCustomErrorHandler extends ErrorHandler {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* custom error message type.
|
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2009-10-09 04:13:08 +00:00
|
|
|
function missingWidgetThing() {
|
|
|
|
echo 'widget thing is missing';
|
|
|
|
}
|
|
|
|
}
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
2010-08-29 17:49:10 +00:00
|
|
|
* Exception class for testing app error handlers and custom errors.
|
2008-06-11 09:02:54 +00:00
|
|
|
*
|
2010-08-29 17:49:10 +00:00
|
|
|
* @package cake.test.cases.libs
|
2008-06-02 19:22:55 +00:00
|
|
|
*/
|
2010-09-05 06:05:31 +00:00
|
|
|
class MissingWidgetThingException extends NotFoundException { }
|
2009-07-24 19:18:37 +00: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
|
|
|
*
|
2009-03-18 17:55:58 +00:00
|
|
|
* @package cake
|
2008-10-30 17:30:26 +00:00
|
|
|
* @subpackage cake.tests.cases.libs
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2009-03-18 17:55:58 +00:00
|
|
|
class ErrorHandlerTest extends CakeTestCase {
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-05-29 04:21:02 +00:00
|
|
|
/**
|
|
|
|
* setup create a request object to get out of router later.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function setUp() {
|
2010-09-25 14:41:08 +00:00
|
|
|
App::build(array(
|
|
|
|
'views' => array(
|
|
|
|
TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views'. DS,
|
|
|
|
TEST_CAKE_CORE_INCLUDE_PATH . 'libs' . DS . 'view' . DS
|
|
|
|
)
|
|
|
|
), true);
|
|
|
|
Router::reload();
|
|
|
|
|
2010-05-29 04:21:02 +00:00
|
|
|
$request = new CakeRequest(null, false);
|
|
|
|
$request->base = '';
|
|
|
|
Router::setRequestInfo($request);
|
2010-08-29 02:50:29 +00:00
|
|
|
$this->_debug = Configure::read('debug');
|
2010-05-29 04:21:02 +00:00
|
|
|
}
|
|
|
|
|
2010-09-25 14:41:08 +00:00
|
|
|
/**
|
|
|
|
* teardown
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2010-08-29 02:50:29 +00:00
|
|
|
function teardown() {
|
|
|
|
Configure::write('debug', $this->_debug);
|
2010-09-25 14:41:08 +00:00
|
|
|
App::build();
|
2010-09-19 05:43:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Mocks out the response on the errorhandler object so headers aren't modified.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function _mockResponse($error) {
|
|
|
|
$error->controller->response = $this->getMock('CakeResponse', array('_sendHeader'));
|
|
|
|
return $error;
|
|
|
|
}
|
2010-08-29 02:50:29 +00:00
|
|
|
|
2010-08-28 05:39:02 +00:00
|
|
|
/**
|
|
|
|
* test handleException generating a page.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function testHandleException() {
|
2010-09-03 19:35:11 +00:00
|
|
|
if ($this->skipIf(file_exists(APP . 'app_error.php'), 'App error exists cannot run.')) {
|
|
|
|
return;
|
|
|
|
}
|
2010-09-19 05:43:00 +00:00
|
|
|
if ($this->skipIf(PHP_SAPI == 'cli', 'This integration test can not be run in cli.')) {
|
|
|
|
return;
|
|
|
|
}
|
2010-09-05 06:05:31 +00:00
|
|
|
$error = new NotFoundException('Kaboom!');
|
2010-08-28 05:39:02 +00:00
|
|
|
ob_start();
|
|
|
|
ErrorHandler::handleException($error);
|
|
|
|
$result = ob_get_clean();
|
2010-08-29 03:32:14 +00:00
|
|
|
$this->assertPattern('/Kaboom!/', $result, 'message missing.');
|
2010-08-28 05:39:02 +00:00
|
|
|
}
|
|
|
|
|
2009-10-09 04:13:08 +00:00
|
|
|
/**
|
|
|
|
* test that methods declared in an ErrorHandler subclass are not converted
|
2010-09-04 19:38:10 +00:00
|
|
|
* into error400 when debug > 0
|
2009-10-09 04:13:08 +00:00
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2009-10-09 04:13:08 +00:00
|
|
|
function testSubclassMethodsNotBeingConvertedToError() {
|
|
|
|
Configure::write('debug', 2);
|
2010-08-29 17:49:10 +00:00
|
|
|
|
|
|
|
$exception = new MissingWidgetThingException('Widget not found');
|
2010-09-19 05:43:00 +00:00
|
|
|
$ErrorHandler = $this->_mockResponse(new MyCustomErrorHandler($exception));
|
2010-08-29 17:49:10 +00:00
|
|
|
|
2009-10-09 04:13:08 +00:00
|
|
|
ob_start();
|
2010-08-29 17:49:10 +00:00
|
|
|
$ErrorHandler->render();
|
2009-10-09 04:13:08 +00:00
|
|
|
$result = ob_get_clean();
|
2010-08-29 17:49:10 +00:00
|
|
|
|
2009-10-09 04:13:08 +00:00
|
|
|
$this->assertEqual($result, 'widget thing is missing');
|
2010-08-29 17:49:10 +00:00
|
|
|
}
|
2009-10-09 04:13:08 +00:00
|
|
|
|
2010-08-29 17:49:10 +00:00
|
|
|
/**
|
|
|
|
* test that subclass methods are not converted when debug = 0
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function testSubclassMethodsNotBeingConvertedDebug0() {
|
2009-10-09 04:13:08 +00:00
|
|
|
Configure::write('debug', 0);
|
2010-08-29 17:49:10 +00:00
|
|
|
$exception = new MissingWidgetThingException('Widget not found');
|
2010-09-19 05:43:00 +00:00
|
|
|
$ErrorHandler = $this->_mockResponse(new MyCustomErrorHandler($exception));
|
2010-08-29 17:49:10 +00:00
|
|
|
|
|
|
|
$this->assertEqual('missingWidgetThing', $ErrorHandler->method);
|
|
|
|
|
2009-10-09 04:13:08 +00:00
|
|
|
ob_start();
|
2010-08-29 17:49:10 +00:00
|
|
|
$ErrorHandler->render();
|
2009-10-09 04:13:08 +00:00
|
|
|
$result = ob_get_clean();
|
2009-12-13 20:35:51 +00:00
|
|
|
|
2010-09-04 19:38:10 +00:00
|
|
|
$this->assertEqual($result, 'widget thing is missing', 'Method declared in subclass converted to error400');
|
2010-08-29 17:49:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test that ErrorHandler subclasses properly convert framework errors.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function testSubclassConvertingFrameworkErrors() {
|
2009-10-09 04:13:08 +00:00
|
|
|
Configure::write('debug', 0);
|
2010-08-29 17:49:10 +00:00
|
|
|
|
|
|
|
$exception = new MissingControllerException('PostsController');
|
2010-09-19 05:43:00 +00:00
|
|
|
$ErrorHandler = $this->_mockResponse(new MyCustomErrorHandler($exception));
|
2010-08-29 17:49:10 +00:00
|
|
|
|
2010-09-04 19:38:10 +00:00
|
|
|
$this->assertEqual('error400', $ErrorHandler->method);
|
2010-08-29 17:49:10 +00:00
|
|
|
|
2009-10-09 04:13:08 +00:00
|
|
|
ob_start();
|
2010-08-29 17:49:10 +00:00
|
|
|
$ErrorHandler->render();
|
2009-10-09 04:13:08 +00:00
|
|
|
$result = ob_get_clean();
|
|
|
|
|
2010-09-04 19:38:10 +00:00
|
|
|
$this->assertPattern('/Not Found/', $result, 'Method declared in error handler not converted to error400. %s');
|
2009-10-09 04:13:08 +00:00
|
|
|
}
|
|
|
|
|
2010-08-29 02:50:29 +00:00
|
|
|
/**
|
|
|
|
* test things in the constructor.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function testConstruction() {
|
2010-09-05 06:05:31 +00:00
|
|
|
$exception = new NotFoundException('Page not found');
|
2010-08-29 02:50:29 +00:00
|
|
|
$ErrorHandler = new ErrorHandler($exception);
|
|
|
|
|
|
|
|
$this->assertType('CakeErrorController', $ErrorHandler->controller);
|
2010-09-04 19:38:10 +00:00
|
|
|
$this->assertEquals('error400', $ErrorHandler->method);
|
2010-08-29 02:50:29 +00:00
|
|
|
$this->assertEquals($exception, $ErrorHandler->error);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test that method gets coerced when debug = 0
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function testErrorMethodCoercion() {
|
|
|
|
Configure::write('debug', 0);
|
|
|
|
$exception = new MissingActionException('Page not found');
|
|
|
|
$ErrorHandler = new ErrorHandler($exception);
|
|
|
|
|
|
|
|
$this->assertType('CakeErrorController', $ErrorHandler->controller);
|
2010-09-04 19:38:10 +00:00
|
|
|
$this->assertEquals('error400', $ErrorHandler->method);
|
2010-08-29 02:50:29 +00:00
|
|
|
$this->assertEquals($exception, $ErrorHandler->error);
|
|
|
|
}
|
|
|
|
|
2010-09-02 00:29:55 +00:00
|
|
|
/**
|
2010-09-04 19:38:10 +00:00
|
|
|
* test that unknown exception types with valid status codes are treated correctly.
|
2010-09-02 00:29:55 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2010-09-04 19:38:10 +00:00
|
|
|
function testUnknownExceptionTypeWithExceptionThatHasA400Code() {
|
2010-09-02 00:29:55 +00:00
|
|
|
$exception = new MissingWidgetThingException('coding fail.');
|
|
|
|
$ErrorHandler = new ErrorHandler($exception);
|
2010-09-19 05:43:00 +00:00
|
|
|
$ErrorHandler->controller->response = $this->getMock('CakeResponse', array('statusCode', '_sendHeader'));
|
2010-09-04 23:06:10 +00:00
|
|
|
$ErrorHandler->controller->response->expects($this->once())->method('statusCode')->with(404);
|
|
|
|
|
|
|
|
ob_start();
|
|
|
|
$ErrorHandler->render();
|
|
|
|
$results = ob_get_clean();
|
2010-09-02 00:29:55 +00:00
|
|
|
|
|
|
|
$this->assertFalse(method_exists($ErrorHandler, 'missingWidgetThing'), 'no method should exist.');
|
2010-09-04 19:38:10 +00:00
|
|
|
$this->assertEquals('error400', $ErrorHandler->method, 'incorrect method coercion.');
|
2010-09-02 00:29:55 +00:00
|
|
|
}
|
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
2010-09-04 19:38:10 +00:00
|
|
|
* test that unknown exception types with valid status codes are treated correctly.
|
2008-06-11 09:02:54 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2010-09-04 19:38:10 +00:00
|
|
|
function testUnknownExceptionTypeWithNoCodeIsA500() {
|
|
|
|
$exception = new OutOfBoundsException('foul ball.');
|
2010-08-29 02:50:29 +00:00
|
|
|
$ErrorHandler = new ErrorHandler($exception);
|
2010-09-19 05:43:00 +00:00
|
|
|
$ErrorHandler->controller->response = $this->getMock('CakeResponse', array('statusCode', '_sendHeader'));
|
2010-09-04 23:06:10 +00:00
|
|
|
$ErrorHandler->controller->response->expects($this->once())->method('statusCode')->with(500);
|
|
|
|
|
|
|
|
ob_start();
|
|
|
|
$ErrorHandler->render();
|
|
|
|
$results = ob_get_clean();
|
2010-08-28 23:53:21 +00:00
|
|
|
|
2010-09-04 19:38:10 +00:00
|
|
|
$this->assertEquals('error500', $ErrorHandler->method, 'incorrect method coercion.');
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
2010-09-04 19:38:10 +00:00
|
|
|
* testerror400 method
|
2008-06-11 09:02:54 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
2010-09-25 14:41:08 +00:00
|
|
|
function testError400() {
|
2010-08-29 03:32:14 +00:00
|
|
|
Router::reload();
|
|
|
|
|
|
|
|
$request = new CakeRequest('posts/view/1000', false);
|
|
|
|
Router::setRequestInfo($request);
|
|
|
|
|
2010-09-05 06:05:31 +00:00
|
|
|
$exception = new NotFoundException('Custom message');
|
2010-08-29 03:32:14 +00:00
|
|
|
$ErrorHandler = new ErrorHandler($exception);
|
2010-09-19 05:43:00 +00:00
|
|
|
$ErrorHandler->controller->response = $this->getMock('CakeResponse', array('statusCode', '_sendHeader'));
|
2010-08-29 17:49:10 +00:00
|
|
|
$ErrorHandler->controller->response->expects($this->once())->method('statusCode')->with(404);
|
2010-02-28 18:44:04 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
ob_start();
|
2010-08-29 03:32:14 +00:00
|
|
|
$ErrorHandler->render();
|
2008-05-30 11:40:08 +00:00
|
|
|
$result = ob_get_clean();
|
2010-08-29 03:32:14 +00:00
|
|
|
|
|
|
|
$this->assertPattern('/<h2>Custom message<\/h2>/', $result);
|
|
|
|
$this->assertPattern("/<strong>'\/posts\/view\/1000'<\/strong>/", $result);
|
|
|
|
}
|
|
|
|
|
2010-08-30 03:39:28 +00:00
|
|
|
/**
|
2010-09-04 19:38:10 +00:00
|
|
|
* test that error400 only modifies the messages on CakeExceptions.
|
2010-08-30 03:39:28 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2010-09-04 19:38:10 +00:00
|
|
|
function testerror400OnlyChangingCakeException() {
|
2010-08-30 03:39:28 +00:00
|
|
|
Configure::write('debug', 0);
|
|
|
|
|
2010-09-05 06:05:31 +00:00
|
|
|
$exception = new NotFoundException('Custom message');
|
2010-09-19 05:43:00 +00:00
|
|
|
$ErrorHandler = $this->_mockResponse(new ErrorHandler($exception));
|
2010-08-30 03:39:28 +00:00
|
|
|
|
|
|
|
ob_start();
|
|
|
|
$ErrorHandler->render();
|
|
|
|
$result = ob_get_clean();
|
|
|
|
$this->assertContains('Custom message', $result);
|
|
|
|
|
|
|
|
$exception = new MissingActionException(array('controller' => 'PostsController', 'action' => 'index'));
|
2010-09-19 05:43:00 +00:00
|
|
|
$ErrorHandler = $this->_mockResponse(new ErrorHandler($exception));
|
2010-08-30 03:39:28 +00:00
|
|
|
|
|
|
|
ob_start();
|
|
|
|
$ErrorHandler->render();
|
|
|
|
$result = ob_get_clean();
|
|
|
|
$this->assertContains('Not Found', $result);
|
|
|
|
}
|
2010-08-29 03:32:14 +00:00
|
|
|
/**
|
2010-09-04 19:38:10 +00:00
|
|
|
* test that error400 doesn't expose XSS
|
2010-08-29 03:32:14 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2010-09-05 06:05:31 +00:00
|
|
|
function testError400NoInjection() {
|
2010-08-29 03:32:14 +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);
|
|
|
|
|
2010-09-05 06:05:31 +00:00
|
|
|
$exception = new NotFoundException('Custom message');
|
2010-09-19 05:43:00 +00:00
|
|
|
$ErrorHandler = $this->_mockResponse(new ErrorHandler($exception));
|
2009-03-18 17:55:58 +00:00
|
|
|
|
2009-02-13 14:56:00 +00:00
|
|
|
ob_start();
|
2010-08-29 03:32:14 +00:00
|
|
|
$ErrorHandler->render();
|
2009-02-13 14:56:00 +00:00
|
|
|
$result = ob_get_clean();
|
2010-02-28 18:44:04 +00:00
|
|
|
|
2010-08-29 03:32:14 +00:00
|
|
|
$this->assertNoPattern('#<script>document#', $result);
|
|
|
|
$this->assertNoPattern('#alert\(t\);</script>#', $result);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-12-13 20:35:51 +00:00
|
|
|
/**
|
|
|
|
* testError500 method
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
2010-08-29 17:49:10 +00:00
|
|
|
function testError500Message() {
|
2010-09-05 06:05:31 +00:00
|
|
|
$exception = new InternalErrorException('An Internal Error Has Occurred');
|
2010-08-29 17:49:10 +00:00
|
|
|
$ErrorHandler = new ErrorHandler($exception);
|
2010-09-19 05:43:00 +00:00
|
|
|
$ErrorHandler->controller->response = $this->getMock('CakeResponse', array('statusCode', '_sendHeader'));
|
2010-08-29 17:49:10 +00:00
|
|
|
$ErrorHandler->controller->response->expects($this->once())->method('statusCode')->with(500);
|
2009-12-13 20:35:51 +00:00
|
|
|
|
|
|
|
ob_start();
|
2010-08-29 17:49:10 +00:00
|
|
|
$ErrorHandler->render();
|
2009-12-13 20:35:51 +00:00
|
|
|
$result = ob_get_clean();
|
2010-08-29 17:49:10 +00:00
|
|
|
|
2009-12-13 20:35:51 +00:00
|
|
|
$this->assertPattern('/<h2>An Internal Error Has Occurred<\/h2>/', $result);
|
|
|
|
}
|
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* testMissingController method
|
2008-06-11 09:02:54 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
2008-05-30 11:40:08 +00:00
|
|
|
function testMissingController() {
|
2010-08-30 01:37:25 +00:00
|
|
|
$exception = new MissingControllerException(array('controller' => 'PostsController'));
|
2010-09-19 05:43:00 +00:00
|
|
|
$ErrorHandler = $this->_mockResponse(new ErrorHandler($exception));
|
2010-08-29 17:49:10 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
ob_start();
|
2010-08-29 17:49:10 +00:00
|
|
|
$ErrorHandler->render();
|
2008-05-30 11:40:08 +00:00
|
|
|
$result = ob_get_clean();
|
2010-08-29 17:49:10 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->assertPattern('/<h2>Missing Controller<\/h2>/', $result);
|
|
|
|
$this->assertPattern('/<em>PostsController<\/em>/', $result);
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-08-30 01:53:44 +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
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
new MissingHelperFileException(array('file' => 'my_custom.php', 'class' => 'MyCustomHelper')),
|
|
|
|
array(
|
|
|
|
'/<h2>Missing Helper File<\/h2>/',
|
|
|
|
'/Create the class below in file:/',
|
|
|
|
'/(\/|\\\)my_custom.php/'
|
|
|
|
),
|
|
|
|
500
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
new MissingHelperClassException(array('file' => 'my_custom.php', 'class' => 'MyCustomHelper')),
|
|
|
|
array(
|
|
|
|
'/<h2>Missing Helper Class<\/h2>/',
|
|
|
|
'/The helper class <em>MyCustomHelper<\/em> can not be found or does not exist./',
|
|
|
|
'/(\/|\\\)my_custom.php/',
|
|
|
|
),
|
|
|
|
500
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
new MissingBehaviorFileException(array('file' => 'my_custom.php', 'class' => 'MyCustomBehavior')),
|
|
|
|
array(
|
|
|
|
'/<h2>Missing Behavior File<\/h2>/',
|
|
|
|
'/Create the class below in file:/',
|
|
|
|
'/(\/|\\\)my_custom.php/',
|
|
|
|
),
|
|
|
|
500
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
new MissingBehaviorClassException(array('file' => 'my_custom.php', 'class' => 'MyCustomBehavior')),
|
|
|
|
array(
|
|
|
|
'/The behavior class <em>MyCustomBehavior<\/em> can not be found or does not exist./',
|
|
|
|
'/(\/|\\\)my_custom.php/'
|
|
|
|
),
|
|
|
|
500
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
new MissingComponentFileException(array('file' => 'sidebox.php', 'class' => 'SideboxComponent')),
|
|
|
|
array(
|
|
|
|
'/<h2>Missing Component File<\/h2>/',
|
|
|
|
'/Create the class <em>SideboxComponent<\/em> in file:/',
|
|
|
|
'/(\/|\\\)sidebox.php/'
|
|
|
|
),
|
|
|
|
500
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
new MissingComponentClassException(array('file' => 'sidebox.php', 'class' => 'SideboxComponent')),
|
|
|
|
array(
|
|
|
|
'/<h2>Missing Component Class<\/h2>/',
|
|
|
|
'/Create the class <em>SideboxComponent<\/em> in file:/',
|
|
|
|
'/(\/|\\\)sidebox.php/'
|
|
|
|
),
|
|
|
|
500
|
|
|
|
)
|
|
|
|
|
|
|
|
);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
2010-08-30 01:53:44 +00:00
|
|
|
* Test the various CakeException sub classes
|
2008-06-11 09:02:54 +00:00
|
|
|
*
|
2010-08-30 01:53:44 +00:00
|
|
|
* @dataProvider testProvider
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2010-08-30 01:53:44 +00:00
|
|
|
function testCakeExceptionHandling($exception, $patterns, $code) {
|
2010-08-29 05:01:36 +00:00
|
|
|
$ErrorHandler = new ErrorHandler($exception);
|
2010-09-19 05:43:00 +00:00
|
|
|
$ErrorHandler->controller->response = $this->getMock('CakeResponse', array('statusCode', '_sendHeader'));
|
2010-08-30 01:53:44 +00:00
|
|
|
$ErrorHandler->controller->response->expects($this->once())
|
|
|
|
->method('statusCode')
|
|
|
|
->with($code);
|
2010-08-29 05:26:21 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
ob_start();
|
2010-08-29 05:26:21 +00:00
|
|
|
$ErrorHandler->render();
|
2008-05-30 11:40:08 +00:00
|
|
|
$result = ob_get_clean();
|
2010-08-29 05:26:21 +00:00
|
|
|
|
2010-08-30 01:53:44 +00:00
|
|
|
foreach ($patterns as $pattern) {
|
|
|
|
$this->assertPattern($pattern, $result);
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|