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 { 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 * setUp method
* *
@ -1574,7 +1588,7 @@ class FormHelperTest extends CakeTestCase {
* @return void * @return void
*/ */
public function testMultipleInputValidation() { 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( $Address->validationErrors[0] = array(
'title' => array('This field cannot be empty'), 'title' => array('This field cannot be empty'),
'first_name' => 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->assertTags($result, $expected);
$this->Form->request['controller'] = 'pages'; $this->Form->request['controller'] = 'pages';
$this->Form->request['models'] = array('User', 'Post');
$result = $this->Form->create('User', array('action' => 'signup')); $result = $this->Form->create('User', array('action' => 'signup'));
$expected = array( $expected = array(
'form' => array( 'form' => array(
@ -6038,7 +6051,7 @@ class FormHelperTest extends CakeTestCase {
$this->Form->request->data = array(); $this->Form->request->data = array();
$this->Form->request['controller'] = 'contacts'; $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'))); $result = $this->Form->create(array('url' => array('action' => 'index', 'param')));
$expected = array( $expected = array(
'form' => array( 'form' => array(
@ -7284,4 +7297,25 @@ class FormHelperTest extends CakeTestCase {
$this->Form->email(); $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,7 +139,8 @@ class ClassRegistry {
${$class} = new $class($settings); ${$class} = new $class($settings);
${$class} = (${$class} instanceof Model) ? ${$class} : null; ${$class} = (${$class} instanceof Model) ? ${$class} : null;
} }
if (!isset(${$class}) && $strict) { if (!isset(${$class})) {
if ($strict) {
return false; return false;
} elseif ($plugin && class_exists($plugin . 'AppModel')) { } elseif ($plugin && class_exists($plugin . 'AppModel')) {
$appModel = $plugin . 'AppModel'; $appModel = $plugin . 'AppModel';
@ -155,6 +156,7 @@ class ClassRegistry {
trigger_error(__d('cake_dev', '(ClassRegistry::init() could not create instance of %1$s class %2$s ', $class, $type), E_USER_WARNING); trigger_error(__d('cake_dev', '(ClassRegistry::init() could not create instance of %1$s class %2$s ', $class, $type), E_USER_WARNING);
return $false; return $false;
} }
}
$_this->map($alias, $class); $_this->map($alias, $class);
} elseif (is_numeric($settings)) { } elseif (is_numeric($settings)) {
trigger_error(__d('cake_dev', '(ClassRegistry::init() Attempted to create instance of a class with a numeric name'), E_USER_WARNING); trigger_error(__d('cake_dev', '(ClassRegistry::init() Attempted to create instance of a class with a numeric name'), E_USER_WARNING);

View file

@ -120,7 +120,7 @@ class FormHelper extends AppHelper {
return $object; return $object;
} }
if (!empty($this->_models[$model])) { if (array_key_exists($model, $this->_models)) {
return $this->_models[$model]; return $this->_models[$model];
} }
@ -137,11 +137,11 @@ class FormHelper extends AppHelper {
$object = ClassRegistry::init($model, true); $object = ClassRegistry::init($model, true);
} }
if (!$object) { $this->_models[$model] = $object;
if (!$object) {;
return null; return null;
} }
$this->_models[$model] = $object;
$this->fieldset[$model] = array('fields' => null, 'key' => $object->primaryKey, 'validates' => null); $this->fieldset[$model] = array('fields' => null, 'key' => $object->primaryKey, 'validates' => null);
return $object; return $object;
} }
@ -426,9 +426,6 @@ class FormHelper extends AppHelper {
* @link http://book.cakephp.org/view/1389/Closing-the-Form * @link http://book.cakephp.org/view/1389/Closing-the-Form
*/ */
public function end($options = null) { public function end($options = null) {
if (!empty($this->request['models'])) {
$models = $this->request['models'][0];
}
$out = null; $out = null;
$submit = null; $submit = null;