From 0c79ad88a320b3904ee43d703421b1e7f2e1c31c Mon Sep 17 00:00:00 2001 From: Jose Lorenzo Rodriguez Date: Thu, 14 Jul 2011 17:26:55 -0430 Subject: [PATCH] Cleaning up the code and adding tests for new features --- .../Test/Case/View/Helper/FormHelperTest.php | 40 +++++++++++++++++-- lib/Cake/Utility/ClassRegistry.php | 30 +++++++------- lib/Cake/View/Helper/FormHelper.php | 9 ++--- 3 files changed, 56 insertions(+), 23 deletions(-) diff --git a/lib/Cake/Test/Case/View/Helper/FormHelperTest.php b/lib/Cake/Test/Case/View/Helper/FormHelperTest.php index 7a148a2a7dc..6cf381231d8 100644 --- a/lib/Cake/Test/Case/View/Helper/FormHelperTest.php +++ b/lib/Cake/Test/Case/View/Helper/FormHelperTest.php @@ -659,6 +659,20 @@ class TestMail extends CakeTestModel { */ class FormHelperTest extends CakeTestCase { +/** + * Fixtures to be used + * + * @var array + */ + public $fixtures = array('core.post'); + +/** + * Do not load the fixtures by default + * + * @var boolean + */ + public $autoFixtures = false; + /** * setUp method * @@ -1574,7 +1588,7 @@ public function testFormValidationMultiRecord() { * @return void */ public function testMultipleInputValidation() { - $Address = ClassRegistry::init(array('class' => 'Address', 'table' => false)); + $Address = ClassRegistry::init(array('class' => 'Address', 'table' => false, 'ds' => 'test')); $Address->validationErrors[0] = array( 'title' => array('This field cannot be empty'), 'first_name' => array('This field cannot be empty') @@ -6023,7 +6037,6 @@ public function testCreate() { $this->assertTags($result, $expected); $this->Form->request['controller'] = 'pages'; - $this->Form->request['models'] = array('User', 'Post'); $result = $this->Form->create('User', array('action' => 'signup')); $expected = array( 'form' => array( @@ -6038,7 +6051,7 @@ public function testCreate() { $this->Form->request->data = array(); $this->Form->request['controller'] = 'contacts'; - $this->Form->request['models'] = array('Contact' => 'Contact'); + $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact')); $result = $this->Form->create(array('url' => array('action' => 'index', 'param'))); $expected = array( 'form' => array( @@ -7284,4 +7297,25 @@ public function testHtml5InputException() { $this->Form->email(); } +/** + * Tests that a model can be loaded from the model names passed in the request object + * + * @return void + */ + public function testIntrospectModelFromRequest() { + $this->loadFixtures('Post'); + App::build(array( + 'plugins' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS) + )); + CakePlugin::load('TestPlugin'); + $this->Form->request['models'] = array('TestPluginPost' => array('plugin' => 'TestPlugin', 'className' => 'TestPluginPost')); + + $this->assertFalse(ClassRegistry::isKeySet('TestPluginPost')); + $this->Form->create('TestPluginPost'); + $this->assertTrue(ClassRegistry::isKeySet('TestPluginPost')); + $this->assertType('TestPluginPost', ClassRegistry::getObject('TestPluginPost')); + + CakePlugin::unload(); + App::build(); + } } diff --git a/lib/Cake/Utility/ClassRegistry.php b/lib/Cake/Utility/ClassRegistry.php index 9dd5d220679..ab1517b6379 100644 --- a/lib/Cake/Utility/ClassRegistry.php +++ b/lib/Cake/Utility/ClassRegistry.php @@ -139,21 +139,23 @@ public static function init($class, $strict = false) { ${$class} = new $class($settings); ${$class} = (${$class} instanceof Model) ? ${$class} : null; } - if (!isset(${$class}) && $strict) { - return false; - } elseif ($plugin && class_exists($plugin . 'AppModel')) { - $appModel = $plugin . 'AppModel'; - } else { - $appModel = 'AppModel'; - } - if (!empty($appModel)) { - $settings['name'] = $class; - ${$class} = new $appModel($settings); - } - if (!isset(${$class})) { - trigger_error(__d('cake_dev', '(ClassRegistry::init() could not create instance of %1$s class %2$s ', $class, $type), E_USER_WARNING); - return $false; + if ($strict) { + return false; + } elseif ($plugin && class_exists($plugin . 'AppModel')) { + $appModel = $plugin . 'AppModel'; + } else { + $appModel = 'AppModel'; + } + if (!empty($appModel)) { + $settings['name'] = $class; + ${$class} = new $appModel($settings); + } + + if (!isset(${$class})) { + trigger_error(__d('cake_dev', '(ClassRegistry::init() could not create instance of %1$s class %2$s ', $class, $type), E_USER_WARNING); + return $false; + } } $_this->map($alias, $class); } elseif (is_numeric($settings)) { diff --git a/lib/Cake/View/Helper/FormHelper.php b/lib/Cake/View/Helper/FormHelper.php index 8f90ea66864..4510cdd849d 100644 --- a/lib/Cake/View/Helper/FormHelper.php +++ b/lib/Cake/View/Helper/FormHelper.php @@ -120,7 +120,7 @@ protected function _getModel($model) { return $object; } - if (!empty($this->_models[$model])) { + if (array_key_exists($model, $this->_models)) { return $this->_models[$model]; } @@ -137,11 +137,11 @@ protected function _getModel($model) { $object = ClassRegistry::init($model, true); } - if (!$object) { + $this->_models[$model] = $object; + if (!$object) {; return null; } - $this->_models[$model] = $object; $this->fieldset[$model] = array('fields' => null, 'key' => $object->primaryKey, 'validates' => null); return $object; } @@ -426,9 +426,6 @@ public function create($model = null, $options = array()) { * @link http://book.cakephp.org/view/1389/Closing-the-Form */ public function end($options = null) { - if (!empty($this->request['models'])) { - $models = $this->request['models'][0]; - } $out = null; $submit = null;