From e989625316f920d37db6f6f8018ed0bb3b53cfa6 Mon Sep 17 00:00:00 2001 From: gwoo Date: Sat, 12 Jan 2008 17:35:12 +0000 Subject: [PATCH] update loading of components, fixes #3899, tests added git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@6368 3807eeeb-6ff5-0310-8944-8be069107fe0 --- cake/libs/controller/component.php | 21 ++++++----- .../cases/libs/controller/component.test.php | 36 +++++++++++++++++-- .../components/test_plugin_component.php | 33 +++++++++++++++++ .../test_plugin_other_component.php | 32 +++++++++++++++++ 4 files changed, 110 insertions(+), 12 deletions(-) create mode 100644 cake/tests/test_app/plugins/test_plugin/controllers/components/test_plugin_component.php create mode 100644 cake/tests/test_app/plugins/test_plugin/controllers/components/test_plugin_other_component.php diff --git a/cake/libs/controller/component.php b/cake/libs/controller/component.php index 4da9fc034..bd6879638 100644 --- a/cake/libs/controller/component.php +++ b/cake/libs/controller/component.php @@ -74,11 +74,6 @@ class Component extends Object { foreach (array_keys($loaded) as $component) { $tempComponent =& $loaded[$component]; - if (isset($tempComponent->components) && is_array($tempComponent->components)) { - foreach ($tempComponent->components as $subComponent) { - $this->controller->{$component}->{$subComponent} =& $loaded[$subComponent]; - } - } if (is_callable(array($tempComponent, 'initialize'))) { $tempComponent->initialize($controller); } @@ -93,7 +88,7 @@ class Component extends Object { * @return array Components loaded * @access protected */ - function &_loadComponents(&$loaded, $components) { + function &_loadComponents(&$loaded, $components, $parent = null) { foreach ($components as $component) { $parts = preg_split('/\/|\./', $component); @@ -131,17 +126,25 @@ class Component extends Object { $base = null; if ($componentCn == 'SessionComponent') { $this->controller->{$component} =& new $componentCn($this->controller->base); - } else { + } elseif ($parent === null) { $this->controller->{$component} =& new $componentCn(); + $loaded[$component] =& $this->controller->{$component}; + } elseif ($parent !== null) { + $this->controller->{$parent}->{$component} =& new $componentCn(); + $loaded[$component] =& $this->controller->{$parent}->{$component}; } - $loaded[$component] =& $this->controller->{$component}; if (isset($this->controller->{$component}->components) && is_array($this->controller->{$component}->components)) { - $loaded =& $this->_loadComponents($loaded, $this->controller->{$component}->components); + $loaded =& $this->_loadComponents($loaded, $this->controller->{$component}->components, $component); } } + + if (isset($loaded[$parent])) { + $loaded[$parent]->{$component} =& $loaded[$component]; + } } + return $loaded; } } diff --git a/cake/tests/cases/libs/controller/component.test.php b/cake/tests/cases/libs/controller/component.test.php index cf90df900..2b46c74f0 100644 --- a/cake/tests/cases/libs/controller/component.test.php +++ b/cake/tests/cases/libs/controller/component.test.php @@ -26,17 +26,47 @@ * @lastmodified $Date$ * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License */ -uses('controller' . DS . 'component'); +uses('controller' . DS . 'component', 'controller' . DS . 'app_controller'); /** * Short description for class. * * @package cake.tests * @subpackage cake.tests.cases.libs.controller */ +class ComponentTestController extends AppController { + var $name = 'ComponentTestController'; + var $uses = array(); +} class ComponentTest extends CakeTestCase { - function skip() { - $this->skipif (true, 'ComponentTest not implemented'); + function setUp() { + Configure::write('pluginPaths', array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS)); + $this->Controller = new ComponentTestController(); + } + + function tearDown() { + unset($this->Controller); + } + + function testLoadComponents() { + $this->Controller->components = array('RequestHandler'); + $Component = new Component($this->Controller); + + $loaded = array(); + $result = $Component->init($this->Controller); + $this->assertTrue(is_object($this->Controller->RequestHandler)); + + $this->Controller->plugin = 'test_plugin'; + $this->Controller->components = array('RequestHandler', 'TestPluginComponent'); + + $result = $Component->init($this->Controller); + + $this->assertTrue(is_object($this->Controller->RequestHandler)); + $this->assertTrue(is_object($this->Controller->TestPluginComponent)); + $this->assertTrue(is_object($this->Controller->TestPluginComponent->TestPluginOtherComponent)); + $this->assertFalse(isset($this->Controller->TestPluginOtherComponent)); + + } } ?> \ No newline at end of file diff --git a/cake/tests/test_app/plugins/test_plugin/controllers/components/test_plugin_component.php b/cake/tests/test_app/plugins/test_plugin/controllers/components/test_plugin_component.php new file mode 100644 index 000000000..0d308d85e --- /dev/null +++ b/cake/tests/test_app/plugins/test_plugin/controllers/components/test_plugin_component.php @@ -0,0 +1,33 @@ + + * Copyright 2005-2008, Cake Software Foundation, Inc. + * 1785 E. Sahara Avenue, Suite 490-204 + * Las Vegas, Nevada 89104 + * + * Licensed under The Open Group Test Suite License + * Redistributions of files must retain the above copyright notice. + * + * @filesource + * @copyright Copyright 2005-2008, Cake Software Foundation, Inc. + * @link https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests + * @package cake.tests + * @subpackage cake.tests.test_app.plugins.test_plugin.views.helpers + * @since CakePHP(tm) v 1.2.0.4206 + * @version $Revision$ + * @modifiedby $LastChangedBy$ + * @lastmodified $Date$ + * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License + */ +class TestPluginComponentComponent extends Object { + + var $components = array('TestPlugin.TestPluginOtherComponent'); +} +?> \ No newline at end of file diff --git a/cake/tests/test_app/plugins/test_plugin/controllers/components/test_plugin_other_component.php b/cake/tests/test_app/plugins/test_plugin/controllers/components/test_plugin_other_component.php new file mode 100644 index 000000000..87ef41296 --- /dev/null +++ b/cake/tests/test_app/plugins/test_plugin/controllers/components/test_plugin_other_component.php @@ -0,0 +1,32 @@ + + * Copyright 2005-2008, Cake Software Foundation, Inc. + * 1785 E. Sahara Avenue, Suite 490-204 + * Las Vegas, Nevada 89104 + * + * Licensed under The Open Group Test Suite License + * Redistributions of files must retain the above copyright notice. + * + * @filesource + * @copyright Copyright 2005-2008, Cake Software Foundation, Inc. + * @link https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests + * @package cake.tests + * @subpackage cake.tests.test_app.plugins.test_plugin.views.helpers + * @since CakePHP(tm) v 1.2.0.4206 + * @version $Revision$ + * @modifiedby $LastChangedBy$ + * @lastmodified $Date$ + * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License + */ +class TestPluginOtherComponentComponent extends Object { + +} +?> \ No newline at end of file