Skip to content

Commit

Permalink
Merge branch '2.0-form-helper' into 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Jul 16, 2011
2 parents c6c1bf1 + d5ce09c commit 108505a
Show file tree
Hide file tree
Showing 10 changed files with 512 additions and 333 deletions.
55 changes: 22 additions & 33 deletions lib/Cake/Controller/Controller.php
Expand Up @@ -127,12 +127,6 @@ class Controller extends Object {
*/
public $viewVars = array();

/**
* An array containing the class names of the models this controller uses.
*
* @var array Array of model objects.
*/
public $modelNames = array();

/**
* The name of the view file to render. The name specified
Expand Down Expand Up @@ -596,9 +590,13 @@ public function loadModel($modelClass = null, $id = null) {
if ($modelClass === null) {
$modelClass = $this->modelClass;
}
list($plugin, $modelClass) = pluginSplit($modelClass, true);

$this->modelNames[] = $modelClass;
$this->uses = ($this->uses) ? $this->uses : array();
if (!in_array($modelClass, $this->uses)) {
$this->uses[] = $modelClass;
}

list($plugin, $modelClass) = pluginSplit($modelClass, true);

$this->{$modelClass} = ClassRegistry::init(array(
'class' => $plugin . $modelClass, 'alias' => $modelClass, 'id' => $id
Expand Down Expand Up @@ -806,34 +804,25 @@ public function render($view = null, $layout = null) {
App::uses($viewClass, $plugin . 'View');
}

$this->request->params['models'] = $this->modelNames;

$View = new $viewClass($this);

if (!empty($this->modelNames)) {
$models = array();
foreach ($this->modelNames as $currentModel) {
if (isset($this->$currentModel) && is_a($this->$currentModel, 'Model')) {
$models[] = Inflector::underscore($currentModel);
}
$isValidModel = (
isset($this->$currentModel) && is_a($this->$currentModel, 'Model') &&
!empty($this->$currentModel->validationErrors)
);
if ($isValidModel) {
$View->validationErrors[Inflector::camelize($currentModel)] =&
$this->$currentModel->validationErrors;
}
if (!empty($this->uses)) {
foreach ($this->uses as $model) {
list($plugin, $className) = pluginSplit($model);
$this->request->params['models'][$model] = compact('plugin', 'className');
}
$models = array_diff(ClassRegistry::keys(), $models);
foreach ($models as $currentModel) {
if (ClassRegistry::isKeySet($currentModel)) {
$currentObject = ClassRegistry::getObject($currentModel);
if (is_a($currentObject, 'Model') && !empty($currentObject->validationErrors)) {
$View->validationErrors[Inflector::camelize($currentModel)] =&
$currentObject->validationErrors;
}
}
} if ($this->uses === false || $this->uses === array()) {
$this->request->params['models'][$this->modelClass] = array('plugin' => $this->plugin, 'className' => $this->modelClass);
}

$models = ClassRegistry::keys();
foreach ($models as $currentModel) {
$currentObject = ClassRegistry::getObject($currentModel);
if (is_a($currentObject, 'Model')) {
$className = get_class($currentObject);
list($plugin, $package) = pluginSplit(App::location($className));
$this->request->params['models'][$currentObject->alias] = compact('plugin', 'className');
$View->validationErrors[$currentObject->alias] =& $currentObject->validationErrors;
}
}

Expand Down
13 changes: 13 additions & 0 deletions lib/Cake/Core/App.php
Expand Up @@ -587,6 +587,19 @@ public static function load($className) {
return false;
}

/**
* Returns the package name where a class was defined to be located at
*
* @param string $className name of the class to obtain the package name from
* @return string package name or null if not declared
*/
public static function location($className) {
if (!empty(self::$__classMap[$className])) {
return self::$__classMap[$className];
}
return null;
}

/**
* Finds classes based on $name or specific file(s) to search. Calling App::import() will
* not construct any classes contained in the files. It will only find and require() the file.
Expand Down
7 changes: 5 additions & 2 deletions lib/Cake/Model/Model.php
Expand Up @@ -702,10 +702,13 @@ private function __constructLinkedModel($assoc, $className = null, $plugin = nul
}

if (!isset($this->{$assoc}) || $this->{$assoc}->name !== $className) {
$model = array('class' => $plugin . '.' . $className, 'alias' => $assoc);
if ($plugin) {
$plugin .= '.';
}
$model = array('class' => $plugin . $className, 'alias' => $assoc);
$this->{$assoc} = ClassRegistry::init($model);
if ($plugin) {
ClassRegistry::addObject($plugin . '.' . $className, $this->{$assoc});
ClassRegistry::addObject($plugin . $className, $this->{$assoc});
}
if ($assoc) {
$this->tableToModel[$this->{$assoc}->table] = $assoc;
Expand Down
15 changes: 12 additions & 3 deletions lib/Cake/Test/Case/Controller/ControllerTest.php
Expand Up @@ -425,7 +425,7 @@ public function testLoadModel() {
$result = $Controller->loadModel('ControllerPost');
$this->assertTrue($result);
$this->assertTrue(is_a($Controller->ControllerPost, 'ControllerPost'));
$this->assertTrue(in_array('ControllerPost', $Controller->modelNames));
$this->assertTrue(in_array('ControllerPost', $Controller->uses));

ClassRegistry::flush();
unset($Controller);
Expand Down Expand Up @@ -456,7 +456,7 @@ public function testLoadModelInPlugins() {
$result = $Controller->loadModel('Comment');
$this->assertTrue($result);
$this->assertInstanceOf('Comment', $Controller->Comment);
$this->assertTrue(in_array('Comment', $Controller->modelNames));
$this->assertTrue(in_array('Comment', $Controller->uses));

ClassRegistry::flush();
unset($Controller);
Expand Down Expand Up @@ -613,6 +613,7 @@ public function testRender() {
App::build(array(
'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View'. DS)
), true);
ClassRegistry::flush();
$request = new CakeRequest('controller_posts/index');
$request->params['action'] = 'index';

Expand All @@ -636,13 +637,20 @@ public function testRender() {
$Controller->ControllerComment->validationErrors = array('title' => 'tooShort');
$expected = $Controller->ControllerComment->validationErrors;

ClassRegistry::flush();
$Controller->viewPath = 'Posts';
$result = $Controller->render('index');
$View = $Controller->View;
$this->assertTrue(isset($View->validationErrors['ControllerComment']));
$this->assertEqual($expected, $View->validationErrors['ControllerComment']);

$expectedModels = array(
'ControllerAlias' => array('plugin' => null, 'className' => 'ControllerAlias'),
'ControllerComment' => array('plugin' => null, 'className' => 'ControllerComment'),
'ControllerPost' => array('plugin' => null, 'className' => 'ControllerPost')
);
$this->assertEqual($expectedModels, $Controller->request->params['models']);


$Controller->ControllerComment->validationErrors = array();
ClassRegistry::flush();

Expand Down Expand Up @@ -998,6 +1006,7 @@ public function testSetAction() {
* @return void
*/
public function testValidateErrors() {
ClassRegistry::flush();
$request = new CakeRequest('controller_posts/index');

$TestController = new TestController($request);
Expand Down
10 changes: 10 additions & 0 deletions lib/Cake/Test/Case/Core/AppTest.php
Expand Up @@ -736,4 +736,14 @@ public function testLoadClassInLibs() {
App::uses('TestUtilityClass', 'Utility');
$this->assertTrue(class_exists('CustomLibClass'));
}

/**
* Tests that App::location() returns the defined path for a class
*
* @return void
*/
public function testClassLocation() {
App::uses('MyCustomClass', 'MyPackage/Name');
$this->assertEquals('MyPackage/Name', App::location('MyCustomClass'));
}
}
8 changes: 8 additions & 0 deletions lib/Cake/Test/Case/Utility/ClassRegistryTest.php
Expand Up @@ -280,4 +280,12 @@ public function testPluginAppModel() {
$this->assertSame($PluginUser, $PluginUserCopy);
CakePlugin::unload();
}

/**
* Tests that passing the string parameter to init() will return false if the model does not exists
*
*/
public function testInitStrict() {
$this->assertFalse(ClassRegistry::init('NonExistent', true));
}
}

0 comments on commit 108505a

Please sign in to comment.