Skip to content

Commit 0c79ad8

Browse files
committed
Cleaning up the code and adding tests for new features
1 parent dcd8811 commit 0c79ad8

File tree

3 files changed

+56
-23
lines changed

3 files changed

+56
-23
lines changed

lib/Cake/Test/Case/View/Helper/FormHelperTest.php

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,20 @@ class TestMail extends CakeTestModel {
659659
*/
660660
class FormHelperTest extends CakeTestCase {
661661

662+
/**
663+
* Fixtures to be used
664+
*
665+
* @var array
666+
*/
667+
public $fixtures = array('core.post');
668+
669+
/**
670+
* Do not load the fixtures by default
671+
*
672+
* @var boolean
673+
*/
674+
public $autoFixtures = false;
675+
662676
/**
663677
* setUp method
664678
*
@@ -1574,7 +1588,7 @@ public function testFormValidationMultiRecord() {
15741588
* @return void
15751589
*/
15761590
public function testMultipleInputValidation() {
1577-
$Address = ClassRegistry::init(array('class' => 'Address', 'table' => false));
1591+
$Address = ClassRegistry::init(array('class' => 'Address', 'table' => false, 'ds' => 'test'));
15781592
$Address->validationErrors[0] = array(
15791593
'title' => array('This field cannot be empty'),
15801594
'first_name' => array('This field cannot be empty')
@@ -6023,7 +6037,6 @@ public function testCreate() {
60236037
$this->assertTags($result, $expected);
60246038

60256039
$this->Form->request['controller'] = 'pages';
6026-
$this->Form->request['models'] = array('User', 'Post');
60276040
$result = $this->Form->create('User', array('action' => 'signup'));
60286041
$expected = array(
60296042
'form' => array(
@@ -6038,7 +6051,7 @@ public function testCreate() {
60386051

60396052
$this->Form->request->data = array();
60406053
$this->Form->request['controller'] = 'contacts';
6041-
$this->Form->request['models'] = array('Contact' => 'Contact');
6054+
$this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
60426055
$result = $this->Form->create(array('url' => array('action' => 'index', 'param')));
60436056
$expected = array(
60446057
'form' => array(
@@ -7284,4 +7297,25 @@ public function testHtml5InputException() {
72847297
$this->Form->email();
72857298
}
72867299

7300+
/**
7301+
* Tests that a model can be loaded from the model names passed in the request object
7302+
*
7303+
* @return void
7304+
*/
7305+
public function testIntrospectModelFromRequest() {
7306+
$this->loadFixtures('Post');
7307+
App::build(array(
7308+
'plugins' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
7309+
));
7310+
CakePlugin::load('TestPlugin');
7311+
$this->Form->request['models'] = array('TestPluginPost' => array('plugin' => 'TestPlugin', 'className' => 'TestPluginPost'));
7312+
7313+
$this->assertFalse(ClassRegistry::isKeySet('TestPluginPost'));
7314+
$this->Form->create('TestPluginPost');
7315+
$this->assertTrue(ClassRegistry::isKeySet('TestPluginPost'));
7316+
$this->assertType('TestPluginPost', ClassRegistry::getObject('TestPluginPost'));
7317+
7318+
CakePlugin::unload();
7319+
App::build();
7320+
}
72877321
}

lib/Cake/Utility/ClassRegistry.php

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -139,21 +139,23 @@ public static function init($class, $strict = false) {
139139
${$class} = new $class($settings);
140140
${$class} = (${$class} instanceof Model) ? ${$class} : null;
141141
}
142-
if (!isset(${$class}) && $strict) {
143-
return false;
144-
} elseif ($plugin && class_exists($plugin . 'AppModel')) {
145-
$appModel = $plugin . 'AppModel';
146-
} else {
147-
$appModel = 'AppModel';
148-
}
149-
if (!empty($appModel)) {
150-
$settings['name'] = $class;
151-
${$class} = new $appModel($settings);
152-
}
153-
154142
if (!isset(${$class})) {
155-
trigger_error(__d('cake_dev', '(ClassRegistry::init() could not create instance of %1$s class %2$s ', $class, $type), E_USER_WARNING);
156-
return $false;
143+
if ($strict) {
144+
return false;
145+
} elseif ($plugin && class_exists($plugin . 'AppModel')) {
146+
$appModel = $plugin . 'AppModel';
147+
} else {
148+
$appModel = 'AppModel';
149+
}
150+
if (!empty($appModel)) {
151+
$settings['name'] = $class;
152+
${$class} = new $appModel($settings);
153+
}
154+
155+
if (!isset(${$class})) {
156+
trigger_error(__d('cake_dev', '(ClassRegistry::init() could not create instance of %1$s class %2$s ', $class, $type), E_USER_WARNING);
157+
return $false;
158+
}
157159
}
158160
$_this->map($alias, $class);
159161
} elseif (is_numeric($settings)) {

lib/Cake/View/Helper/FormHelper.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ protected function _getModel($model) {
120120
return $object;
121121
}
122122

123-
if (!empty($this->_models[$model])) {
123+
if (array_key_exists($model, $this->_models)) {
124124
return $this->_models[$model];
125125
}
126126

@@ -137,11 +137,11 @@ protected function _getModel($model) {
137137
$object = ClassRegistry::init($model, true);
138138
}
139139

140-
if (!$object) {
140+
$this->_models[$model] = $object;
141+
if (!$object) {;
141142
return null;
142143
}
143144

144-
$this->_models[$model] = $object;
145145
$this->fieldset[$model] = array('fields' => null, 'key' => $object->primaryKey, 'validates' => null);
146146
return $object;
147147
}
@@ -426,9 +426,6 @@ public function create($model = null, $options = array()) {
426426
* @link http://book.cakephp.org/view/1389/Closing-the-Form
427427
*/
428428
public function end($options = null) {
429-
if (!empty($this->request['models'])) {
430-
$models = $this->request['models'][0];
431-
}
432429
$out = null;
433430
$submit = null;
434431

0 commit comments

Comments
 (0)