mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 03:18:26 +00:00
refactoring component handling, closes #4795, tests added. deprecated Controller::_initComponents();
git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@7070 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
parent
3a643bad92
commit
cf896eaa9b
9 changed files with 259 additions and 179 deletions
|
@ -114,7 +114,7 @@ class Dispatcher extends Object {
|
|||
}
|
||||
|
||||
if (is_array($url)) {
|
||||
$url = $this->extractParams($url, $additionalParams);
|
||||
$url = $this->__extractParams($url, $additionalParams);
|
||||
$parse = false;
|
||||
}
|
||||
|
||||
|
@ -216,7 +216,6 @@ class Dispatcher extends Object {
|
|||
}
|
||||
|
||||
Router::setRequestInfo(array($this->params, array('base' => $this->base, 'here' => $this->here, 'webroot' => $this->webroot)));
|
||||
$controller->constructClasses();
|
||||
$this->start($controller);
|
||||
|
||||
if ($privateAction) {
|
||||
|
@ -268,15 +267,8 @@ class Dispatcher extends Object {
|
|||
$controller->output = $output;
|
||||
}
|
||||
|
||||
foreach ($controller->components as $c) {
|
||||
$path = preg_split('/\/|\./', $c);
|
||||
$c = $path[count($path) - 1];
|
||||
if (isset($controller->{$c}) && is_object($controller->{$c}) && is_callable(array($controller->{$c}, 'shutdown'))) {
|
||||
if (!array_key_exists('enabled', get_object_vars($controller->{$c})) || $controller->{$c}->enabled == true) {
|
||||
$controller->{$c}->shutdown($controller);
|
||||
}
|
||||
}
|
||||
}
|
||||
$controller->Component->shutdown($controller);
|
||||
|
||||
$controller->afterFilter();
|
||||
if (isset($params['return'])) {
|
||||
return $controller->output;
|
||||
|
@ -291,6 +283,10 @@ class Dispatcher extends Object {
|
|||
* @access public
|
||||
*/
|
||||
function start(&$controller) {
|
||||
$controller->constructClasses();
|
||||
|
||||
$controller->Component->initialize($controller);
|
||||
|
||||
if (!empty($controller->beforeFilter)) {
|
||||
trigger_error(sprintf(__('Dispatcher::start - Controller::$beforeFilter property usage is deprecated and will no longer be supported. Use Controller::beforeFilter().', true)), E_USER_WARNING);
|
||||
|
||||
|
@ -308,15 +304,7 @@ class Dispatcher extends Object {
|
|||
}
|
||||
$controller->beforeFilter();
|
||||
|
||||
foreach ($controller->components as $c) {
|
||||
$path = preg_split('/\/|\./', $c);
|
||||
$c = $path[count($path) - 1];
|
||||
if (isset($controller->{$c}) && is_object($controller->{$c}) && is_callable(array($controller->{$c}, 'startup'))) {
|
||||
if (!array_key_exists('enabled', get_object_vars($controller->{$c})) || $controller->{$c}->enabled == true) {
|
||||
$controller->{$c}->startup($controller);
|
||||
}
|
||||
}
|
||||
}
|
||||
$controller->Component->startup($controller);
|
||||
}
|
||||
/**
|
||||
* Sets the params when $url is passed as an array to Object::requestAction();
|
||||
|
@ -328,7 +316,7 @@ class Dispatcher extends Object {
|
|||
* @todo commented Router::url(). this improved performance,
|
||||
* will work on this more later.
|
||||
*/
|
||||
function extractParams($url, $additionalParams = array()) {
|
||||
function __extractParams($url, $additionalParams = array()) {
|
||||
$defaults = array('pass' => array(), 'named' => array(), 'form' => array());
|
||||
$this->params = array_merge($defaults, $url, $additionalParams);
|
||||
//$url = Router::url($url);
|
||||
|
|
|
@ -24,122 +24,182 @@
|
|||
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
|
||||
*/
|
||||
/**
|
||||
* Base class for all CakePHP Components.
|
||||
* Handler for Controller::$components
|
||||
*
|
||||
* @package cake
|
||||
* @subpackage cake.cake.libs.controller
|
||||
*/
|
||||
class Component extends Object {
|
||||
/**
|
||||
* Components used by this component.
|
||||
*
|
||||
* @var array
|
||||
* @access public
|
||||
*/
|
||||
var $components = array();
|
||||
/**
|
||||
* Controller to which this component is linked.
|
||||
* Some vars from controller (plugin, name, base)
|
||||
*
|
||||
* @var object
|
||||
* @access public
|
||||
* @access private
|
||||
*/
|
||||
var $controller = null;
|
||||
var $__controllerVars = array('plugin' => null, 'name' => null, 'base' => null);
|
||||
/**
|
||||
* All loaded components
|
||||
*
|
||||
* @var object
|
||||
* @access private
|
||||
*/
|
||||
var $__loaded = array();
|
||||
/**
|
||||
* Used to initialize the components for current controller
|
||||
*
|
||||
* @param object $controller Controller using this component.
|
||||
* @param object $controller Controller with components to load
|
||||
* @access public
|
||||
*/
|
||||
function init(&$controller) {
|
||||
$this->controller =& $controller;
|
||||
if ($this->controller->components !== false) {
|
||||
$loaded = array();
|
||||
if (!in_array('Session', $this->controller->components)) {
|
||||
array_unshift($this->controller->components, 'Session');
|
||||
}
|
||||
$loaded = $this->_loadComponents($loaded, $this->controller->components);
|
||||
if ($controller->components !== false && is_array($controller->components)) {
|
||||
$this->__controllerVars = array(
|
||||
'plugin' => $controller->plugin, 'name' => $controller->name, 'base' => $controller->base
|
||||
);
|
||||
|
||||
foreach (array_keys($loaded) as $component) {
|
||||
$tempComponent =& $loaded[$component];
|
||||
if (is_callable(array($tempComponent, 'initialize'))) {
|
||||
$tempComponent->initialize($controller);
|
||||
if (!in_array('Session', $controller->components)) {
|
||||
array_unshift($controller->components, 'Session');
|
||||
}
|
||||
$this->_loadComponents($controller);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Called before the Controller::beforeFilter()
|
||||
*
|
||||
* @param object $controller Controller with components to initialize
|
||||
* @access public
|
||||
*/
|
||||
function initialize(&$controller) {
|
||||
foreach ($this->__loaded as $name => $component) {
|
||||
if (is_callable(array($component, 'initialize')) && $component->enabled === true) {
|
||||
$component->initialize($controller);
|
||||
}
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Called after the Controller::beforeFilter() and before the controller action
|
||||
*
|
||||
* @param object $controller Controller with components to startup
|
||||
* @access public
|
||||
*/
|
||||
function startup(&$controller) {
|
||||
foreach ($this->__loaded as $name => $component) {
|
||||
if (is_callable(array($component, 'startup')) && $component->enabled === true) {
|
||||
$component->startup($controller);
|
||||
}
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Called after the Controller::beforeRender(), after the view class is loaded, and before the Controller::render()
|
||||
*
|
||||
* @param object $controller Controller with components to beforeRender
|
||||
* @access public
|
||||
*/
|
||||
function beforeRender(&$controller) {
|
||||
foreach ($this->__loaded as $name => $component) {
|
||||
if (is_callable(array($component, 'beforeRender')) && $component->enabled === true) {
|
||||
$component->beforeRender($controller);
|
||||
}
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Called before Controller::redirect();
|
||||
*
|
||||
* @param object $controller Controller with components to beforeRedirect
|
||||
* @access public
|
||||
*/
|
||||
function beforeRedirect(&$controller, $url, $status = null, $exit = true) {
|
||||
$response = array();
|
||||
foreach ($this->__loaded as $name => $component) {
|
||||
if (is_callable(array($component, 'beforeRender')) && $component->enabled === true) {
|
||||
$resp = $component->beforeRedirect($controller, $url, $status, $exit);
|
||||
if ($resp === false) {
|
||||
return false;
|
||||
}
|
||||
$response[] = $resp;
|
||||
}
|
||||
}
|
||||
return $response;
|
||||
}
|
||||
/**
|
||||
* Called after Controller::render() and before the output is printed to the browser
|
||||
*
|
||||
* @param object $controller Controller with components to shutdown
|
||||
* @access public
|
||||
*/
|
||||
function shutdown(&$controller) {
|
||||
foreach ($this->__loaded as $name => $component) {
|
||||
if (is_callable(array($component, 'shutdown')) && $component->enabled === true) {
|
||||
$component->shutdown($controller);
|
||||
}
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Load components used by this component.
|
||||
*
|
||||
* @param array $loaded Components already loaded (indexed by component name)
|
||||
* @param array $components Components to load
|
||||
* @return array Components loaded
|
||||
* @param object $object Object with a Components array
|
||||
* @param object $parent the parent of the current object
|
||||
* @return void
|
||||
* @access protected
|
||||
*/
|
||||
function &_loadComponents(&$loaded, $components, $parent = null) {
|
||||
foreach ($components as $component) {
|
||||
$parts = preg_split('/\/|\./', $component);
|
||||
function _loadComponents(&$object, $parent = null) {
|
||||
$components = $object->components;
|
||||
$base = $this->__controllerVars['base'];
|
||||
|
||||
if (count($parts) === 1) {
|
||||
$plugin = $this->controller->plugin;
|
||||
} else {
|
||||
$plugin = Inflector::underscore($parts['0']);
|
||||
$component = $parts[count($parts) - 1];
|
||||
}
|
||||
if (is_array($object->components)) {
|
||||
foreach ($object->components as $component) {
|
||||
$parts = preg_split('/\/|\./', $component);
|
||||
|
||||
$componentCn = $component . 'Component';
|
||||
if (count($parts) === 1) {
|
||||
$plugin = $this->__controllerVars['plugin'] . '.';
|
||||
} else {
|
||||
$plugin = Inflector::underscore($parts['0']) . '.';
|
||||
$component = array_pop($parts);
|
||||
}
|
||||
$componentCn = $component . 'Component';
|
||||
|
||||
if (!class_exists($componentCn)) {
|
||||
if (is_null($plugin) || !App::import('Component', $plugin . '.' . $component)) {
|
||||
if (!App::import('Component', $component)) {
|
||||
$this->cakeError('missingComponentFile', array(array(
|
||||
'className' => $this->controller->name,
|
||||
if (!class_exists($componentCn)) {
|
||||
if (is_null($plugin) || !App::import('Component', $plugin . $component)) {
|
||||
if (!App::import('Component', $component)) {
|
||||
$this->cakeError('missingComponentFile', array(array(
|
||||
'className' => $this->__controllerVars['name'],
|
||||
'component' => $component,
|
||||
'file' => Inflector::underscore($component) . '.php',
|
||||
'base' => $base,
|
||||
'code' => 500
|
||||
)));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (!class_exists($componentCn)) {
|
||||
$this->cakeError('missingComponentClass', array(array(
|
||||
'className' => $this->__controllerVars['name'],
|
||||
'component' => $component,
|
||||
'file' => Inflector::underscore($component) . '.php',
|
||||
'base' => $this->controller->base,
|
||||
'base' => $base,
|
||||
'code' => 500
|
||||
)));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (!class_exists($componentCn)) {
|
||||
$this->cakeError('missingComponentClass', array(array(
|
||||
'className' => $this->controller->name,
|
||||
'component' => $component,
|
||||
'file' => Inflector::underscore($component) . '.php',
|
||||
'base' => $this->controller->base,
|
||||
'code' => 500
|
||||
)));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
$base = null;
|
||||
|
||||
if ($componentCn == 'SessionComponent') {
|
||||
$base = $this->controller->base;
|
||||
}
|
||||
|
||||
if ($parent === null) {
|
||||
$this->controller->{$component} =& new $componentCn($base);
|
||||
$loaded[$component] =& $this->controller->{$component};
|
||||
} elseif ($parent !== null) {
|
||||
if (isset($loaded[$component])) {
|
||||
$this->controller->{$parent}->{$component} =& $loaded[$component];
|
||||
if ($componentCn == 'SessionComponent') {
|
||||
$object->{$component} =& new $componentCn($base);
|
||||
} else {
|
||||
$this->controller->{$parent}->{$component} =& new $componentCn($base);
|
||||
$loaded[$component] =& $this->controller->{$parent}->{$component};
|
||||
$object->{$component} =& new $componentCn();
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($this->controller->{$component}->components) && is_array($this->controller->{$component}->components)) {
|
||||
$loaded =& $this->_loadComponents($loaded, $this->controller->{$component}->components, $component);
|
||||
}
|
||||
$object->{$component}->enabled = true;
|
||||
|
||||
if (isset($loaded[$parent])) {
|
||||
$loaded[$parent]->{$component} =& $loaded[$component];
|
||||
if (!isset($this->__loaded[$component])) {
|
||||
$this->__loaded[$component] =& $object->{$component};
|
||||
}
|
||||
|
||||
if (isset($object->{$component}->components) && is_array($object->{$component}->components)) {
|
||||
$this->_loadComponents($object->{$component});
|
||||
}
|
||||
}
|
||||
}
|
||||
return $loaded;
|
||||
}
|
||||
}
|
||||
?>
|
||||
?>
|
|
@ -221,11 +221,6 @@ class RequestHandlerComponent extends Object {
|
|||
foreach ($_POST as $key => $val) {
|
||||
unset($_POST[$key]);
|
||||
}
|
||||
Router::reload();
|
||||
|
||||
if (is_array($url)) {
|
||||
$url = Router::url(array_merge(array('base' => false), $url));
|
||||
}
|
||||
echo $this->requestAction($url, array('return'));
|
||||
$this->stop();
|
||||
}
|
||||
|
|
|
@ -42,7 +42,7 @@ App::import('Core', array('Component', 'View'));
|
|||
*/
|
||||
class Controller extends Object {
|
||||
/**
|
||||
* Tshe name of this controller. Controller names are plural, named after the model they manipulate.
|
||||
* The name of this controller. Controller names are plural, named after the model they manipulate.
|
||||
*
|
||||
* @var string
|
||||
* @access public
|
||||
|
@ -190,6 +190,13 @@ class Controller extends Object {
|
|||
* @access public
|
||||
*/
|
||||
var $autoLayout = true;
|
||||
/**
|
||||
* Instance of Component use to handle callbacks
|
||||
*
|
||||
* @var string
|
||||
* @access public
|
||||
*/
|
||||
var $Component = null;
|
||||
/**
|
||||
* Array containing the names of components this controller uses. Component names
|
||||
* should not contain the -Component portion of the classname.
|
||||
|
@ -214,13 +221,6 @@ class Controller extends Object {
|
|||
* @access public
|
||||
*/
|
||||
var $ext = '.ctp';
|
||||
/**
|
||||
* Instance of $view class create by a controller
|
||||
*
|
||||
* @var object
|
||||
* @access private
|
||||
*/
|
||||
var $__viewClass = null;
|
||||
/**
|
||||
* The output of the requested action. Contains either a variable
|
||||
* returned from the action, or the data of the rendered view;
|
||||
|
@ -277,7 +277,6 @@ class Controller extends Object {
|
|||
function __construct() {
|
||||
if ($this->name === null) {
|
||||
$r = null;
|
||||
|
||||
if (!preg_match('/(.*)Controller/i', get_class($this), $r)) {
|
||||
die (__("Controller::__construct() : Can not get or parse my own class name, exiting."));
|
||||
}
|
||||
|
@ -289,17 +288,17 @@ class Controller extends Object {
|
|||
}
|
||||
$this->modelClass = Inflector::classify($this->name);
|
||||
$this->modelKey = Inflector::underscore($this->modelClass);
|
||||
$this->Component =& new Component();
|
||||
parent::__construct();
|
||||
}
|
||||
/**
|
||||
* Starts the components linked to this controller.
|
||||
*
|
||||
* @access protected
|
||||
* @deprecated 1.2.0.7070
|
||||
* @see Component::init()
|
||||
*/
|
||||
function _initComponents() {
|
||||
$component = new Component();
|
||||
$component->init($this);
|
||||
$this->Component->init($this);
|
||||
}
|
||||
/**
|
||||
* Merge components, helpers, and uses vars from AppController and PluginAppController
|
||||
|
@ -371,7 +370,8 @@ class Controller extends Object {
|
|||
*/
|
||||
function constructClasses() {
|
||||
$this->__mergeVars();
|
||||
$this->_initComponents();
|
||||
$this->Component->init($this);
|
||||
|
||||
if ($this->uses === null || ($this->uses === array())) {
|
||||
return false;
|
||||
}
|
||||
|
@ -464,21 +464,17 @@ class Controller extends Object {
|
|||
if (is_array($status)) {
|
||||
extract($status, EXTR_OVERWRITE);
|
||||
}
|
||||
$response = $this->Component->beforeRedirect($this, $url, $status, $exit);
|
||||
|
||||
foreach ($this->components as $c) {
|
||||
$path = preg_split('/\/|\./', $c);
|
||||
$c = $path[count($path) - 1];
|
||||
|
||||
if (isset($this->{$c}) && is_object($this->{$c}) && is_callable(array($this->{$c}, 'beforeRedirect'))) {
|
||||
if (!array_key_exists('enabled', get_object_vars($this->{$c})) || $this->{$c}->enabled == true) {
|
||||
$resp = $this->{$c}->beforeRedirect($this, $url, $status, $exit);
|
||||
if ($resp === false) {
|
||||
return;
|
||||
} elseif (is_array($resp) && isset($resp['url'])) {
|
||||
extract($resp, EXTR_OVERWRITE);
|
||||
} elseif ($resp !== null) {
|
||||
$url = $resp;
|
||||
}
|
||||
if ($response === false) {
|
||||
return;
|
||||
}
|
||||
if (is_array($response)) {
|
||||
foreach ($response as $resp) {
|
||||
if (is_array($resp) && isset($resp['url'])) {
|
||||
extract($resp, EXTR_OVERWRITE);
|
||||
} elseif ($resp !== null) {
|
||||
$url = $resp;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -689,22 +685,16 @@ class Controller extends Object {
|
|||
App::import('View', $this->view);
|
||||
}
|
||||
|
||||
foreach ($this->components as $c) {
|
||||
$path = preg_split('/\/|\./', $c);
|
||||
$c = $path[count($path) - 1];
|
||||
if (isset($this->{$c}) && is_object($this->{$c}) && is_callable(array($this->{$c}, 'beforeRender'))) {
|
||||
if (!array_key_exists('enabled', get_object_vars($this->{$c})) || $this->{$c}->enabled == true) {
|
||||
$this->{$c}->beforeRender($this);
|
||||
}
|
||||
}
|
||||
}
|
||||
$this->Component->beforeRender($this);
|
||||
|
||||
$this->params['models'] = $this->modelNames;
|
||||
|
||||
if (Configure::read() > 2) {
|
||||
$this->set('cakeDebug', $this);
|
||||
}
|
||||
|
||||
$this->__viewClass =& new $viewClass($this);
|
||||
$View =& new $viewClass($this);
|
||||
|
||||
if (!empty($this->modelNames)) {
|
||||
$models = array();
|
||||
foreach ($this->modelNames as $currentModel) {
|
||||
|
@ -712,7 +702,7 @@ class Controller extends Object {
|
|||
$models[] = Inflector::underscore($currentModel);
|
||||
}
|
||||
if (isset($this->$currentModel) && is_a($this->$currentModel, 'Model') && !empty($this->$currentModel->validationErrors)) {
|
||||
$this->__viewClass->validationErrors[Inflector::camelize($currentModel)] =& $this->$currentModel->validationErrors;
|
||||
$View->validationErrors[Inflector::camelize($currentModel)] =& $this->$currentModel->validationErrors;
|
||||
}
|
||||
}
|
||||
$models = array_diff(ClassRegistry::keys(), $models);
|
||||
|
@ -720,14 +710,15 @@ class Controller extends Object {
|
|||
if (ClassRegistry::isKeySet($currentModel)) {
|
||||
$currentObject =& ClassRegistry::getObject($currentModel);
|
||||
if (is_a($currentObject, 'Model') && !empty($currentObject->validationErrors)) {
|
||||
$this->__viewClass->validationErrors[Inflector::camelize($currentModel)] =& $currentObject->validationErrors;
|
||||
$View->validationErrors[Inflector::camelize($currentModel)] =& $currentObject->validationErrors;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$this->autoRender = false;
|
||||
$this->output .= $this->__viewClass->render($action, $layout, $file);
|
||||
$this->output .= $View->render($action, $layout, $file);
|
||||
|
||||
return $this->output;
|
||||
}
|
||||
/**
|
||||
|
|
|
@ -26,7 +26,7 @@
|
|||
* @lastmodified $Date$
|
||||
* @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
|
||||
*/
|
||||
uses('controller' . DS . 'component', 'controller' . DS . 'app_controller');
|
||||
App::import('Core', array('Component', 'AppController'));
|
||||
/**
|
||||
* Short description for class.
|
||||
*
|
||||
|
@ -34,9 +34,29 @@ uses('controller' . DS . 'component', 'controller' . DS . 'app_controller');
|
|||
* @subpackage cake.tests.cases.libs.controller
|
||||
*/
|
||||
class ComponentTestController extends AppController {
|
||||
var $name = 'ComponentTestController';
|
||||
var $name = 'ComponentTest';
|
||||
var $uses = array();
|
||||
}
|
||||
class AppleComponent extends Object {
|
||||
|
||||
var $components = array('Orange');
|
||||
|
||||
var $name = null;
|
||||
|
||||
function startup(&$controller) {
|
||||
$this->name = $controller->name;
|
||||
}
|
||||
|
||||
}
|
||||
class OrangeComponent extends Object {
|
||||
|
||||
var $components = array('Banana');
|
||||
|
||||
}
|
||||
class BananaComponent extends Object {
|
||||
|
||||
}
|
||||
|
||||
class ComponentTest extends CakeTestCase {
|
||||
|
||||
function setUp() {
|
||||
|
@ -44,19 +64,19 @@ class ComponentTest extends CakeTestCase {
|
|||
}
|
||||
|
||||
function testLoadComponents() {
|
||||
$Controller = new ComponentTestController();
|
||||
$Controller =& new ComponentTestController();
|
||||
$Controller->components = array('RequestHandler');
|
||||
|
||||
$Component = new Component();
|
||||
$Component =& new Component();
|
||||
$Component->init($Controller);
|
||||
|
||||
$this->assertTrue(is_a($Controller->RequestHandler, 'RequestHandlerComponent'));
|
||||
|
||||
$Controller = new ComponentTestController();
|
||||
$Controller =& new ComponentTestController();
|
||||
$Controller->plugin = 'test_plugin';
|
||||
$Controller->components = array('RequestHandler', 'TestPluginComponent');
|
||||
|
||||
$Component = new Component();
|
||||
$Component =& new Component();
|
||||
$Component->init($Controller);
|
||||
|
||||
$this->assertTrue(is_a($Controller->RequestHandler, 'RequestHandlerComponent'));
|
||||
|
@ -64,19 +84,19 @@ class ComponentTest extends CakeTestCase {
|
|||
$this->assertTrue(is_a($Controller->TestPluginComponent->TestPluginOtherComponent, 'TestPluginOtherComponentComponent'));
|
||||
$this->assertFalse(isset($Controller->TestPluginOtherComponent));
|
||||
|
||||
$Controller = new ComponentTestController();
|
||||
$Controller =& new ComponentTestController();
|
||||
$Controller->components = array('Security');
|
||||
|
||||
$Component = new Component();
|
||||
$Component =& new Component();
|
||||
$Component->init($Controller);
|
||||
|
||||
$this->assertTrue(is_a($Controller->Security, 'SecurityComponent'));
|
||||
$this->assertTrue(is_a($Controller->Security->Session, 'SessionComponent'));
|
||||
|
||||
$Controller = new ComponentTestController();
|
||||
$Controller =& new ComponentTestController();
|
||||
$Controller->components = array('Security', 'Cookie', 'RequestHandler');
|
||||
|
||||
$Component = new Component();
|
||||
$Component =& new Component();
|
||||
$Component->init($Controller);
|
||||
|
||||
$this->assertTrue(is_a($Controller->Security, 'SecurityComponent'));
|
||||
|
@ -84,5 +104,30 @@ class ComponentTest extends CakeTestCase {
|
|||
$this->assertTrue(is_a($Controller->RequestHandler, 'RequestHandlerComponent'));
|
||||
$this->assertTrue(is_a($Controller->Cookie, 'CookieComponent'));
|
||||
}
|
||||
|
||||
function testNestedComponentLoading() {
|
||||
$Controller =& new ComponentTestController();
|
||||
$Controller->components = array('Apple');
|
||||
$Controller->constructClasses();
|
||||
|
||||
$this->assertTrue(is_a($Controller->Apple, 'AppleComponent'));
|
||||
$this->assertTrue(is_a($Controller->Apple->Orange, 'OrangeComponent'));
|
||||
$this->assertTrue(is_a($Controller->Apple->Orange->Banana, 'BananaComponent'));
|
||||
}
|
||||
|
||||
function testComponentStartup() {
|
||||
$Controller =& new ComponentTestController();
|
||||
$Controller->components = array('Apple');
|
||||
$Controller->constructClasses();
|
||||
|
||||
$this->assertTrue(is_a($Controller->Apple, 'AppleComponent'));
|
||||
$this->assertEqual($Controller->Apple->name, null);
|
||||
|
||||
$Controller->Component->startup($Controller);
|
||||
|
||||
$this->assertEqual($Controller->Apple->name, 'ComponentTest');
|
||||
}
|
||||
}
|
||||
echo round(floatval(memory_get_usage()) / 1024 / 1024, 4) . "mb";
|
||||
|
||||
?>
|
|
@ -170,7 +170,7 @@ class AuthTest extends CakeTestCase {
|
|||
|
||||
$this->Controller =& new AuthTestController();
|
||||
|
||||
$this->Controller->_initComponents();
|
||||
$this->Controller->Component->init($this->Controller);
|
||||
ClassRegistry::addObject('view', new View($this->Controller));
|
||||
$this->Controller->Session->del('Auth');
|
||||
$this->Controller->Session->del('Message.auth');
|
||||
|
@ -362,7 +362,7 @@ class AuthTest extends CakeTestCase {
|
|||
$expected = Router::normalize($this->Controller->Auth->loginRedirect);
|
||||
$this->assertEqual($expected, $this->Controller->Auth->redirect());
|
||||
|
||||
$this->Controller->Session->del('Auth');
|
||||
$this->Controller->Session->del('Auth');
|
||||
|
||||
$this->Controller->params['url']['url'] = 'admin/';
|
||||
$this->Controller->Auth->initialize($this->Controller);
|
||||
|
@ -389,8 +389,8 @@ class AuthTest extends CakeTestCase {
|
|||
$this->Controller->Auth->loginRedirect = false;
|
||||
$this->Controller->Auth->startup($this->Controller);
|
||||
$expected = Router::normalize('admin');
|
||||
$this->assertEqual($expected, $this->Controller->Auth->redirect());
|
||||
|
||||
$this->assertEqual($expected, $this->Controller->Auth->redirect());
|
||||
|
||||
//Ticket #4750
|
||||
//named params
|
||||
$this->Controller->Session->del('Auth');
|
||||
|
@ -412,9 +412,9 @@ class AuthTest extends CakeTestCase {
|
|||
$this->Controller->Auth->userModel = 'AuthUser';
|
||||
$this->Controller->Auth->startup($this->Controller);
|
||||
$expected = Router::normalize('posts/view/1');
|
||||
$this->assertEqual($expected, $this->Controller->Session->read('Auth.redirect'));
|
||||
$this->assertEqual($expected, $this->Controller->Session->read('Auth.redirect'));
|
||||
|
||||
|
||||
|
||||
$_SERVER['HTTP_REFERER'] = $backup;
|
||||
$this->Controller->Session->del('Auth');
|
||||
}
|
||||
|
|
|
@ -41,7 +41,7 @@ class EmailTest extends CakeTestCase {
|
|||
$this->Controller =& new EmailTestController();
|
||||
|
||||
restore_error_handler();
|
||||
@$this->Controller->_initComponents();
|
||||
@$this->Controller->Component->init($this->Controller);
|
||||
set_error_handler('simpleTestErrorHandler');
|
||||
|
||||
$this->Controller->Email->startup($this->Controller);
|
||||
|
@ -63,10 +63,10 @@ class EmailTest extends CakeTestCase {
|
|||
$this->Controller->Email->subject = 'Cake SMTP test';
|
||||
$this->Controller->Email->replyTo = 'noreply@example.com';
|
||||
$this->Controller->Email->template = null;
|
||||
|
||||
|
||||
$this->Controller->Email->delivery = 'smtp';
|
||||
$this->assertTrue($this->Controller->Email->send('This is the body of the message'));
|
||||
|
||||
|
||||
$this->Controller->Email->_debug = true;
|
||||
if (stristr(PHP_OS, 'win') === false) {
|
||||
$this->Controller->Email->_newLine = "\n";
|
||||
|
@ -95,7 +95,7 @@ This is the body of the message
|
|||
|
||||
</pre>
|
||||
TEMPDOC;
|
||||
|
||||
|
||||
$this->assertTrue($this->Controller->Email->send('This is the body of the message'));
|
||||
$this->assertEqual($this->Controller->Session->read('Message.email.message'), $expect);
|
||||
}
|
||||
|
@ -111,7 +111,7 @@ TEMPDOC;
|
|||
$this->Controller->Email->template = null;
|
||||
$this->Controller->Email->smtpOptions['username'] = 'test';
|
||||
$this->Controller->Email->smtpOptions['password'] = 'testing';
|
||||
|
||||
|
||||
$this->Controller->Email->delivery = 'smtp';
|
||||
$result = $this->Controller->Email->send('This is the body of the message');
|
||||
if (!$result) {
|
||||
|
@ -143,7 +143,7 @@ TEMPDOC;
|
|||
if (stristr(PHP_OS, 'win') === false) {
|
||||
$this->Controller->Email->_newLine = "\n";
|
||||
}
|
||||
|
||||
|
||||
$this->Controller->Email->sendAs = 'text';
|
||||
$expect = <<<TEMPDOC
|
||||
<pre>To: postmaster@localhost
|
||||
|
@ -167,13 +167,13 @@ TEMPDOC;
|
|||
$this->assertEqual($this->Controller->Session->read('Message.email.message'), $expect);
|
||||
|
||||
$this->Controller->Email->sendAs = 'html';
|
||||
$expect = str_replace('Content-Type: text/plain; charset=UTF-8', 'Content-Type: text/html; charset=UTF-8', $expect);
|
||||
$expect = str_replace('Content-Type: text/plain; charset=UTF-8', 'Content-Type: text/html; charset=UTF-8', $expect);
|
||||
$this->assertTrue($this->Controller->Email->send('This is the body of the message'));
|
||||
$this->assertEqual($this->Controller->Session->read('Message.email.message'), $expect);
|
||||
|
||||
|
||||
// TODO: better test for format of message sent?
|
||||
$this->Controller->Email->sendAs = 'both';
|
||||
$expect = str_replace('Content-Type: text/html; charset=UTF-8', 'Content-Type: multipart/alternative; boundary="alt-"' . "\n", $expect);
|
||||
$expect = str_replace('Content-Type: text/html; charset=UTF-8', 'Content-Type: multipart/alternative; boundary="alt-"' . "\n", $expect);
|
||||
$this->assertTrue($this->Controller->Email->send('This is the body of the message'));
|
||||
$this->assertEqual($this->Controller->Session->read('Message.email.message'), $expect);
|
||||
}
|
||||
|
|
|
@ -59,7 +59,7 @@ class SecurityComponentTest extends CakeTestCase {
|
|||
|
||||
function setUp() {
|
||||
$this->Controller =& new SecurityTestController();
|
||||
$this->Controller->_initComponents();
|
||||
$this->Controller->Component->init($this->Controller);
|
||||
$this->Controller->Security->blackHoleCallback = 'fail';
|
||||
}
|
||||
|
||||
|
@ -451,7 +451,7 @@ DIGEST;
|
|||
}
|
||||
|
||||
function testLoginValidation() {
|
||||
|
||||
|
||||
}
|
||||
|
||||
function testValidateHasManyModel() {
|
||||
|
@ -539,7 +539,7 @@ DIGEST;
|
|||
$this->Controller->Security->startup($this->Controller);
|
||||
$_SERVER['PHP_AUTH_USER'] = $user = 'Willy Test';
|
||||
$_SERVER['PHP_AUTH_PW'] = $pw = 'some password for the nice test';
|
||||
|
||||
|
||||
$result = $this->Controller->Security->loginCredentials('basic');
|
||||
$expected = array('username' => $user, 'password' => $pw);
|
||||
$this->assertIdentical($result, $expected);
|
||||
|
|
|
@ -35,11 +35,11 @@ class ControllerPost extends CakeTestModel {
|
|||
var $useTable = 'posts';
|
||||
var $invalidFields = array('name' => 'error_msg');
|
||||
var $lastQuery = null;
|
||||
|
||||
|
||||
function beforeFind($query) {
|
||||
$this->lastQuery = $query;
|
||||
}
|
||||
|
||||
|
||||
function find($type, $options = array()) {
|
||||
if ($type == 'popular') {
|
||||
$conditions = array($this->name . '.' . $this->primaryKey => '> 1');
|
||||
|
@ -67,7 +67,7 @@ class TestController extends AppController {
|
|||
var $helpers = array('Xml');
|
||||
var $components = array('Security');
|
||||
var $uses = array('ControllerComment');
|
||||
|
||||
|
||||
function index($testId, $test2Id) {
|
||||
$this->data['testId'] = $testId;
|
||||
$this->data['test2Id'] = $test2Id;
|
||||
|
@ -144,7 +144,7 @@ class ControllerTest extends CakeTestCase {
|
|||
$this->assertEqual($Controller->params['paging']['ControllerPost']['page'], 1);
|
||||
$this->assertEqual($results, array(1, 2, 3));
|
||||
}
|
||||
|
||||
|
||||
function testPaginateExtraParams() {
|
||||
$Controller =& new Controller();
|
||||
$Controller->uses = array('ControllerPost', 'ControllerComment');
|
||||
|
@ -157,14 +157,14 @@ class ControllerTest extends CakeTestCase {
|
|||
$this->assertEqual($Controller->params['paging']['ControllerPost']['page'], 1);
|
||||
$this->assertEqual(Set::extract($result, '{n}.ControllerPost.id'), array(1, 2, 3));
|
||||
$this->assertTrue(!isset($Controller->ControllerPost->lastQuery['contain']));
|
||||
|
||||
|
||||
$Controller->passedArgs = array('page' => '-1');
|
||||
$Controller->paginate = array('ControllerPost' => array('contain' => array('ControllerComment')));
|
||||
$result = $Controller->paginate('ControllerPost');
|
||||
$this->assertEqual($Controller->params['paging']['ControllerPost']['page'], 1);
|
||||
$this->assertEqual(Set::extract($result, '{n}.ControllerPost.id'), array(1, 2, 3));
|
||||
$this->assertFalse(!isset($Controller->ControllerPost->lastQuery['contain']));
|
||||
|
||||
|
||||
$Controller->paginate = array('ControllerPost' => array('popular', 'fields' => array('id', 'title')));
|
||||
$result = $Controller->paginate('ControllerPost');
|
||||
$this->assertEqual(Set::extract($result, '{n}.ControllerPost.id'), array(2, 3));
|
||||
|
@ -256,7 +256,7 @@ class ControllerTest extends CakeTestCase {
|
|||
$this->assertTrue($Controller->_afterScaffoldSaveError(''));
|
||||
$this->assertFalse($Controller->_scaffoldError(''));
|
||||
}
|
||||
|
||||
|
||||
function test__postConditionMatch() {
|
||||
$Controller =& new Controller();
|
||||
$value = 'val';
|
||||
|
@ -354,7 +354,8 @@ class ControllerTest extends CakeTestCase {
|
|||
foreach ($codes as $code => $msg) {
|
||||
$MockController =& new MockController();
|
||||
$MockController->components = array('Test');
|
||||
$MockController->_initComponents();
|
||||
$MockController->Component =& new Component();
|
||||
$MockController->Component->init($MockController);
|
||||
$MockController->expectCallCount('header', 2);
|
||||
$MockController->redirect($url, (int) $code, false);
|
||||
}
|
||||
|
@ -385,7 +386,7 @@ class ControllerTest extends CakeTestCase {
|
|||
$this->assertEqual(count(array_diff($TestController->uses, $uses)), 0);
|
||||
$this->assertEqual(count(array_diff($TestController->components, $components)), 0);
|
||||
}
|
||||
|
||||
|
||||
function testReferer() {
|
||||
$Controller =& new Controller();
|
||||
$_SERVER['HTTP_REFERER'] = 'http://cakephp.org';
|
||||
|
@ -438,7 +439,7 @@ class ControllerTest extends CakeTestCase {
|
|||
$TestController->constructClasses();
|
||||
$this->assertFalse($TestController->validateErrors());
|
||||
$this->assertEqual($TestController->validate(), 0);
|
||||
|
||||
|
||||
$TestController->ControllerComment->invalidate('some_field', 'error_message');
|
||||
$TestController->ControllerComment->invalidate('some_field2', 'error_message2');
|
||||
$comment = new ControllerComment;
|
||||
|
|
Loading…
Reference in a new issue