Skip to content

Commit

Permalink
Cleaning up the code and adding tests for new features
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Jul 14, 2011
1 parent dcd8811 commit 0c79ad8
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 23 deletions.
40 changes: 37 additions & 3 deletions lib/Cake/Test/Case/View/Helper/FormHelperTest.php
Expand Up @@ -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
*
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand Down Expand Up @@ -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();
}
}
30 changes: 16 additions & 14 deletions lib/Cake/Utility/ClassRegistry.php
Expand Up @@ -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)) {
Expand Down
9 changes: 3 additions & 6 deletions lib/Cake/View/Helper/FormHelper.php
Expand Up @@ -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];
}

Expand All @@ -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;
}
Expand Down Expand Up @@ -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;

Expand Down

0 comments on commit 0c79ad8

Please sign in to comment.