Cleaning up the code and adding tests for new features

This commit is contained in:
Jose Lorenzo Rodriguez 2011-07-14 17:26:55 -04:30
parent dcd8811771
commit 0c79ad88a3
3 changed files with 56 additions and 23 deletions

View file

@ -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 @@ class FormHelperTest extends CakeTestCase {
* @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 @@ class FormHelperTest extends CakeTestCase {
$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 @@ class FormHelperTest extends CakeTestCase {
$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 @@ class FormHelperTest extends CakeTestCase {
$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();
}
}

View file

@ -139,21 +139,23 @@ class ClassRegistry {
${$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)) {

View file

@ -120,7 +120,7 @@ class FormHelper extends AppHelper {
return $object;
}
if (!empty($this->_models[$model])) {
if (array_key_exists($model, $this->_models)) {
return $this->_models[$model];
}
@ -137,11 +137,11 @@ class FormHelper extends AppHelper {
$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 @@ class FormHelper extends AppHelper {
* @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;