Permalink
Browse files

Cleaning up the code and adding tests for new features

  • Loading branch information...
1 parent dcd8811 commit 0c79ad88a320b3904ee43d703421b1e7f2e1c31c @lorenzo lorenzo committed Jul 14, 2011
View
40 lib/Cake/Test/Case/View/Helper/FormHelperTest.php
@@ -660,6 +660,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
*
* @access public
@@ -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();
+ }
}
View
30 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)) {
View
9 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;

0 comments on commit 0c79ad8

Please sign in to comment.