Merge branch '1.3-misc' of git@github.com:cakephp/cakephp1x into 1.3-misc

This commit is contained in:
Mark Story 2009-12-16 23:08:00 -05:00
commit 8960a01649
5 changed files with 189 additions and 49 deletions

View file

@ -349,6 +349,16 @@ class Controller extends Object {
*/
var $validationErrors = null;
/**
* Contains a list of the HTTP codes that CakePHP recognizes. These may be
* queried and/or modified through Controller::httpCodes(), which is also
* tasked with their lazy-loading.
*
* @var array Associative array of HTTP codes and their associated messages.
* @access private
*/
var $__httpCodes = null;
/**
* Constructor.
*
@ -504,6 +514,62 @@ class Controller extends Object {
return true;
}
/**
* Queries & sets valid HTTP response codes & messages.
*
* @param mixed $code If $code is an integer, then the corresponding code/message is
* returned if it exists, null if it does not exist. If $code is an array,
* then the 'code' and 'message' keys of each nested array are added to the default
* HTTP codes. Example:
*
* httpCodes(404); // returns array(404 => 'Not Found')
*
* httpCodes(array(
* 701 => 'Unicorn Moved',
* 800 => 'Unexpected Minotaur'
* )); // sets these new values, and returns true
*
* @return mixed Associative array of the HTTP codes as keys, and the message
* strings as values, or null of the given $code does not exist.
*/
function httpCodes($code = null) {
if (empty($this->__httpCodes)) {
$this->__httpCodes = array(
100 => 'Continue', 101 => 'Switching Protocols',
200 => 'OK', 201 => 'Created', 202 => 'Accepted',
203 => 'Non-Authoritative Information', 204 => 'No Content',
205 => 'Reset Content', 206 => 'Partial Content',
300 => 'Multiple Choices', 301 => 'Moved Permanently',
302 => 'Found', 303 => 'See Other',
304 => 'Not Modified', 305 => 'Use Proxy', 307 => 'Temporary Redirect',
400 => 'Bad Request', 401 => 'Unauthorized', 402 => 'Payment Required',
403 => 'Forbidden', 404 => 'Not Found', 405 => 'Method Not Allowed',
406 => 'Not Acceptable', 407 => 'Proxy Authentication Required',
408 => 'Request Time-out', 409 => 'Conflict', 410 => 'Gone',
411 => 'Length Required', 412 => 'Precondition Failed',
413 => 'Request Entity Too Large', 414 => 'Request-URI Too Large',
415 => 'Unsupported Media Type', 416 => 'Requested range not satisfiable',
417 => 'Expectation Failed', 500 => 'Internal Server Error',
501 => 'Not Implemented', 502 => 'Bad Gateway',
503 => 'Service Unavailable', 504 => 'Gateway Time-out'
);
}
if (empty($code)) {
return $this->__httpCodes;
}
if (is_array($code)) {
$this->__httpCodes = $code + $this->__httpCodes;
return true;
}
if (!isset($this->__httpCodes[$code])) {
return null;
}
return array($code => $this->__httpCodes[$code]);
}
/**
* Loads and instantiates models required by this controller.
* If Controller::persistModel; is true, controller will cache model instances on first request,
@ -603,47 +669,8 @@ class Controller extends Object {
}
if (!empty($status)) {
$codes = array(
100 => 'Continue',
101 => 'Switching Protocols',
200 => 'OK',
201 => 'Created',
202 => 'Accepted',
203 => 'Non-Authoritative Information',
204 => 'No Content',
205 => 'Reset Content',
206 => 'Partial Content',
300 => 'Multiple Choices',
301 => 'Moved Permanently',
302 => 'Found',
303 => 'See Other',
304 => 'Not Modified',
305 => 'Use Proxy',
307 => 'Temporary Redirect',
400 => 'Bad Request',
401 => 'Unauthorized',
402 => 'Payment Required',
403 => 'Forbidden',
404 => 'Not Found',
405 => 'Method Not Allowed',
406 => 'Not Acceptable',
407 => 'Proxy Authentication Required',
408 => 'Request Time-out',
409 => 'Conflict',
410 => 'Gone',
411 => 'Length Required',
412 => 'Precondition Failed',
413 => 'Request Entity Too Large',
414 => 'Request-URI Too Large',
415 => 'Unsupported Media Type',
416 => 'Requested range not satisfiable',
417 => 'Expectation Failed',
500 => 'Internal Server Error',
501 => 'Not Implemented',
502 => 'Bad Gateway',
503 => 'Service Unavailable',
504 => 'Gateway Time-out'
);
$codes = $this->httpCodes();
if (is_string($status)) {
$codes = array_flip($codes);
}
@ -657,14 +684,13 @@ class Controller extends Object {
$msg = $status;
}
$status = "HTTP/1.1 {$code} {$msg}";
} else {
$status = null;
}
}
if (!empty($status)) {
$this->header($status);
}
if ($url !== null) {
$this->header('Location: ' . Router::url($url, true));
}

View file

@ -92,7 +92,6 @@ class ErrorHandler extends Object {
$this->controller =& new Controller();
$this->controller->viewPath = 'errors';
}
$options = array('escape' => false);
$messages = Sanitize::clean($messages, $options);
@ -156,7 +155,7 @@ class ErrorHandler extends Object {
$url = $this->controller->here;
}
$url = Router::normalize($url);
header("HTTP/1.0 404 Not Found");
$this->controller->header("HTTP/1.0 404 Not Found");
$this->controller->set(array(
'code' => '404',
'name' => __('Not Found', true),
@ -166,6 +165,28 @@ class ErrorHandler extends Object {
$this->_outputMessage('error404');
}
/**
* Convenience method to display a 500 page.
*
* @param array $params Parameters for controller
* @access public
*/
function error500($params) {
extract($params, EXTR_OVERWRITE);
if (!isset($url)) {
$url = $this->controller->here;
}
$url = Router::normalize($url);
$this->controller->header("HTTP/1.0 500 Internal Server Error");
$this->controller->set(array(
'code' => '500',
'name' => __('An Internal Error Has Occurred', true),
'message' => h($url),
'base' => $this->controller->base
));
$this->_outputMessage('error500');
}
/**
* Renders the Missing Controller web page.
*
@ -229,7 +250,9 @@ class ErrorHandler extends Object {
function missingTable($params) {
extract($params, EXTR_OVERWRITE);
$this->controller->header("HTTP/1.0 500 Internal Server Error");
$this->controller->set(array(
'code' => '500',
'model' => $className,
'table' => $table,
'title' => __('Missing Database Table', true)
@ -244,7 +267,9 @@ class ErrorHandler extends Object {
* @access public
*/
function missingDatabase($params = array()) {
$this->controller->header("HTTP/1.0 500 Internal Server Error");
$this->controller->set(array(
'code' => '500',
'title' => __('Scaffold Missing Database Connection', true)
));
$this->_outputMessage('missingScaffolddb');
@ -294,7 +319,9 @@ class ErrorHandler extends Object {
function missingConnection($params) {
extract($params, EXTR_OVERWRITE);
$this->controller->header("HTTP/1.0 500 Internal Server Error");
$this->controller->set(array(
'code' => '500',
'model' => $className,
'title' => __('Missing Database Connection', true)
));

View file

@ -0,0 +1,24 @@
<?php
/**
*
* PHP versions 4 and 5
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright 2005-2009, Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2005-2009, Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @package cake
* @subpackage cake.cake.libs.view.templates.errors
* @since CakePHP(tm) v 0.10.0.1076
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
?>
<h2><?php echo $name; ?></h2>
<p class="error">
<strong><?php __('Error'); ?>: </strong>
<?php echo sprintf(__('An Internal Error Has Occurred.', true), "<strong>'{$message}'</strong>")?>
</p>

View file

@ -1243,5 +1243,43 @@ class ControllerTest extends CakeTestCase {
$this->assertEqual($Controller->RequestHandler->prefers(), 'rss');
unset($Controller);
}
/**
* testControllerHttpCodes method
*
* @access public
* @return void
*/
function testControllerHttpCodes() {
$Controller =& new Controller();
$result = $Controller->httpCodes();
$this->assertEqual(count($result), 39);
$result = $Controller->httpCodes(100);
$expected = array(100 => 'Continue');
$this->assertEqual($result, $expected);
$codes = array(
1337 => 'Undefined Unicorn',
1729 => 'Hardy-Ramanujan Located'
);
$result = $Controller->httpCodes($codes);
$this->assertTrue($result);
$this->assertEqual(count($Controller->httpCodes()), 41);
$result = $Controller->httpCodes(1337);
$expected = array(1337 => 'Undefined Unicorn');
$this->assertEqual($result, $expected);
$codes = array(404 => 'Sorry Bro');
$result = $Controller->httpCodes($codes);
$this->assertTrue($result);
$this->assertEqual(count($Controller->httpCodes()), 41);
$result = $Controller->httpCodes(404);
$expected = array(404 => 'Sorry Bro');
$this->assertEqual($result, $expected);
}
}
?>

View file

@ -345,6 +345,29 @@ class ErrorHandlerTest extends CakeTestCase {
$this->assertNoPattern('#</script>#', $result);
}
/**
* testError500 method
*
* @access public
* @return void
*/
function testError500() {
ob_start();
$TestErrorHandler = new TestErrorHandler('error500', array(
'message' => 'An Internal Error Has Occurred'
));
$result = ob_get_clean();
$this->assertPattern('/<h2>An Internal Error Has Occurred<\/h2>/', $result);
ob_start();
$TestErrorHandler = new TestErrorHandler('error500', array(
'message' => 'An Internal Error Has Occurred',
'code' => '500'
));
$result = ob_get_clean();
$this->assertPattern('/<h2>An Internal Error Has Occurred<\/h2>/', $result);
}
/**
* testMissingController method
*
@ -408,6 +431,7 @@ class ErrorHandlerTest extends CakeTestCase {
ob_start();
$TestErrorHandler = new TestErrorHandler('missingTable', array('className' => 'Article', 'table' => 'articles'));
$result = ob_get_clean();
$this->assertPattern('/HTTP\/1\.0 500 Internal Server Error/', $result);
$this->assertPattern('/<h2>Missing Database Table<\/h2>/', $result);
$this->assertPattern('/table <em>articles<\/em> for model <em>Article<\/em>/', $result);
}
@ -422,6 +446,7 @@ class ErrorHandlerTest extends CakeTestCase {
ob_start();
$TestErrorHandler = new TestErrorHandler('missingDatabase', array());
$result = ob_get_clean();
$this->assertPattern('/HTTP\/1\.0 500 Internal Server Error/', $result);
$this->assertPattern('/<h2>Missing Database Connection<\/h2>/', $result);
$this->assertPattern('/Confirm you have created the file/', $result);
}