refactoring Helper::setEntity, adding test cases

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@5970 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
gwoo 2007-11-09 00:17:44 +00:00
parent 431f2c2a88
commit d303a64f65
3 changed files with 117 additions and 83 deletions

View file

@ -325,102 +325,72 @@ class Helper extends Overloadable {
return; return;
} }
$hasField = false; $sameScope = $hasField = false;
$tmpModel = $view->model;
$parts = preg_split('/\/|\./', $entity); $parts = preg_split('/\/|\./', $entity);
$isModel = (ClassRegistry::isKeySet($parts[0]) || $parts[0] == '_Token');
$model = ife($isModel, $parts[0], $view->model);
$sameScope = false;
if (ClassRegistry::isKeySet($tmpModel)) { if($parts[0] !== $view->model) {
$tmpModelObject =& ClassRegistry::getObject($tmpModel); if (ClassRegistry::isKeySet($view->model)) {
if ($tmpModelObject->hasField($parts[0]) || array_key_exists($parts[0], $tmpModelObject->validate)) { $modelObj =& ClassRegistry::getObject($view->model);
$hasField = 0; for ($i = 0; $i < count($parts); $i++) {
$sameScope = true; if ($modelObj->hasField($parts[$i]) || array_key_exists($parts[$i], $modelObj->validate)) {
} $sameScope = true;
if (in_array($model, array_keys($tmpModelObject->hasAndBelongsToMany))) { $hasField = $i;
$hasField = 1; break;
}
}
} }
} }
if ($parts[0] == '_Token' && isset($parts[1])) { if (ClassRegistry::isKeySet($parts[0])) {
$hasField = 1; $newModelObj =& ClassRegistry::getObject($parts[0]);
} elseif (!empty($model) && ClassRegistry::isKeySet($model)) {
$model =& ClassRegistry::getObject($model);
for ($i = 1; $i < count($parts); $i++) { for ($i = 1; $i < count($parts); $i++) {
if ($model->hasField($parts[$i]) || array_key_exists($parts[$i], $model->validate)) { if ($newModelObj->hasField($parts[$i]) || array_key_exists($parts[$i], $newModelObj->validate)) {
$sameScope = false;
$hasField = $i; $hasField = $i;
break; break;
} }
} }
} }
$view->field = null;
$view->fieldSuffix = null;
$view->association = null;
if ($sameScope) { $view->field = $view->modelId = $view->fieldSuffix = $view->association = null;
$view->field = $parts[0];
if (isset($parts[1])) { switch (count($parts)) {
$view->fieldSuffix = $parts[1]; case 1:
} if($view->modelScope === false) {
} elseif ($isModel) { $view->model = $parts[0];
switch (count($parts)) { } else {
case 1:
$view->modelId = null;
if ($view->modelScope) {
$view->association = $parts[0];
$view->field = $parts[0];
} else {
$view->model = $parts[0];
}
break;
case 2:
case 3:
if ($hasField) {
$view->field = $parts[$hasField];
if ($view->modelScope) {
$view->association = $parts[0];
} else {
$view->model = $parts[0];
}
} else {
list($view->model, $view->modelId) = $parts;
}
break;
}
} else {
switch (count($parts)) {
case 1:
$view->field = $parts[0]; $view->field = $parts[0];
$view->association = null; if($sameScope === false) {
break; $view->association = $parts[0];
case 2:
case 3:
if ($hasField || $hasField === 0) {
$view->field = $parts[$hasField];
if ($hasField == 1) {
$view->modelId = $parts[0];
}
} elseif (!$hasField && count($parts) == 2) {
$view->field = $parts[1];
if ($view->modelScope) {
$view->association = $parts[0];
} else {
$view->model = $parts[0];
}
} }
break; }
} break;
} case 2:
if ($view->modelScope === false) {
if ($hasField && isset($parts[$hasField + 1])) { list($view->model, $view->field) = $parts;
$view->fieldSuffix = $parts[$hasField + 1]; } elseif ($sameScope === true && $hasField === 0) {
list($view->field, $view->suffix) = $parts;
} elseif ($sameScope === true && $hasField === 1) {
list($view->modelId, $view->field) = $parts;
} else {
list($view->association, $view->field) = $parts;
}
break;
case 3:
if ($sameScope === true && $hasField === 1) {
list($view->modelId, $view->field, $view->fieldSuffix) = $parts;
} elseif ($hasField === 2) {
list($view->association, $view->modelId, $view->field) = $parts;
} else {
list($view->association, $view->field, $view->fieldSuffix) = $parts;
}
break;
} }
if (!isset($view->model) || empty($view->model)) { if (!isset($view->model) || empty($view->model)) {
$view->model = $view->association; $view->model = $view->association;
$view->association = null; $view->association = null;
} elseif ($view->model == $view->association) { } elseif ($view->model === $view->association) {
$view->association = null; $view->association = null;
} }

View file

@ -43,6 +43,8 @@ class HelperTestPost extends Model {
); );
return $this->_schema; return $this->_schema;
} }
var $hasAndBelongsToMany = array('HelperTestTag'=> array());
} }
@ -63,6 +65,33 @@ class HelperTestComment extends Model {
} }
} }
class HelperTestTag extends Model {
var $useTable = false;
function schema() {
$this->_schema = array(
'id' => array('type' => 'integer', 'null' => false, 'default' => '', 'length' => '8'),
'name' => array('type' => 'string', 'null' => false, 'default' => '', 'length' => '255'),
'created' => array('type' => 'date', 'null' => true, 'default' => '', 'length' => ''),
'modified' => array('type' => 'datetime', 'null' => true, 'default' => '', 'length' => null)
);
return $this->_schema;
}
}
class HelperTestPostsTag extends Model {
var $useTable = false;
function schema() {
$this->_schema = array(
'helper_test_post_id' => array('type' => 'integer', 'null' => false, 'default' => '', 'length' => '8'),
'helper_test_tag_id' => array('type' => 'integer', 'null' => false, 'default' => '', 'length' => '8'),
);
return $this->_schema;
}
}
/** /**
* Short description for class. * Short description for class.
* *
@ -78,6 +107,7 @@ class HelperTest extends UnitTestCase {
$this->Helper = new Helper(); $this->Helper = new Helper();
ClassRegistry::addObject('HelperTestPost', new HelperTestPost()); ClassRegistry::addObject('HelperTestPost', new HelperTestPost());
ClassRegistry::addObject('HelperTestComment', new HelperTestComment()); ClassRegistry::addObject('HelperTestComment', new HelperTestComment());
ClassRegistry::addObject('HelperTestTag', new HelperTestTag());
} }
function testFormFieldNameParsing() { function testFormFieldNameParsing() {
@ -104,6 +134,15 @@ class HelperTest extends UnitTestCase {
$this->assertEqual($this->View->association, null); $this->assertEqual($this->View->association, null);
$this->assertEqual($this->View->fieldSuffix, null); $this->assertEqual($this->View->fieldSuffix, null);
$this->Helper->setEntity('_Token.fields');
$this->assertTrue($this->View->modelScope);
$this->assertEqual($this->View->model, 'HelperTestPost');
$this->assertEqual($this->View->field, 'fields');
$this->assertEqual($this->View->modelId, null);
$this->assertEqual($this->View->association, '_Token');
$this->assertEqual($this->View->fieldSuffix, null);
$this->Helper->setEntity('id'); $this->Helper->setEntity('id');
$this->assertTrue($this->View->modelScope); $this->assertTrue($this->View->modelScope);
$this->assertEqual($this->View->model, 'HelperTestPost'); $this->assertEqual($this->View->model, 'HelperTestPost');
@ -128,6 +167,14 @@ class HelperTest extends UnitTestCase {
$this->assertEqual($this->View->association, null); $this->assertEqual($this->View->association, null);
$this->assertEqual($this->View->fieldSuffix, null); $this->assertEqual($this->View->fieldSuffix, null);
$this->Helper->setEntity('Something.else');
$this->assertTrue($this->View->modelScope);
$this->assertEqual($this->View->model, 'HelperTestPost');
$this->assertEqual($this->View->field, 'else');
$this->assertEqual($this->View->modelId, false);
$this->assertEqual($this->View->association, 'Something');
$this->assertEqual($this->View->fieldSuffix, '');
$this->Helper->setEntity('5.id'); $this->Helper->setEntity('5.id');
$this->assertTrue($this->View->modelScope); $this->assertTrue($this->View->modelScope);
$this->assertEqual($this->View->model, 'HelperTestPost'); $this->assertEqual($this->View->model, 'HelperTestPost');
@ -136,22 +183,38 @@ class HelperTest extends UnitTestCase {
$this->assertEqual($this->View->association, null); $this->assertEqual($this->View->association, null);
$this->assertEqual($this->View->fieldSuffix, null); $this->assertEqual($this->View->fieldSuffix, null);
$this->Helper->setEntity('5.id.time'); $this->Helper->setEntity('5.created.month');
$this->assertTrue($this->View->modelScope); $this->assertTrue($this->View->modelScope);
$this->assertEqual($this->View->model, 'HelperTestPost'); $this->assertEqual($this->View->model, 'HelperTestPost');
$this->assertEqual($this->View->field, 'id'); $this->assertEqual($this->View->field, 'created');
$this->assertEqual($this->View->modelId, '5'); $this->assertEqual($this->View->modelId, '5');
$this->assertEqual($this->View->association, null); $this->assertEqual($this->View->association, null);
$this->assertEqual($this->View->fieldSuffix, 'time'); $this->assertEqual($this->View->fieldSuffix, 'month');
$this->Helper->setEntity('HelperTestComment.id.time'); $this->Helper->setEntity('HelperTestComment.5.id');
$this->assertTrue($this->View->modelScope); $this->assertTrue($this->View->modelScope);
$this->assertEqual($this->View->model, 'HelperTestPost'); $this->assertEqual($this->View->model, 'HelperTestPost');
$this->assertEqual($this->View->field, 'id'); $this->assertEqual($this->View->field, 'id');
$this->assertEqual($this->View->modelId, '5'); $this->assertEqual($this->View->modelId, '5');
$this->assertEqual($this->View->association, 'HelperTestComment'); $this->assertEqual($this->View->association, 'HelperTestComment');
$this->assertEqual($this->View->fieldSuffix, null);
$this->Helper->setEntity('HelperTestComment.id.time');
$this->assertTrue($this->View->modelScope);
$this->assertEqual($this->View->model, 'HelperTestPost');
$this->assertEqual($this->View->field, 'id');
$this->assertEqual($this->View->modelId, null);
$this->assertEqual($this->View->association, 'HelperTestComment');
$this->assertEqual($this->View->fieldSuffix, 'time'); $this->assertEqual($this->View->fieldSuffix, 'time');
$this->Helper->setEntity('HelperTestTag');
$this->assertTrue($this->View->modelScope);
$this->assertEqual($this->View->model, 'HelperTestPost');
$this->assertEqual($this->View->field, 'HelperTestTag');
$this->assertEqual($this->View->modelId, '');
$this->assertEqual($this->View->association, 'HelperTestTag');
$this->assertEqual($this->View->fieldSuffix, '');
$this->Helper->setEntity(null); $this->Helper->setEntity(null);
$this->Helper->setEntity('ModelThatDoesntExist.field_that_doesnt_exist'); $this->Helper->setEntity('ModelThatDoesntExist.field_that_doesnt_exist');
$this->assertFalse($this->View->modelScope); $this->assertFalse($this->View->modelScope);

View file

@ -480,9 +480,10 @@ class FormHelperTest extends CakeTestCase {
$result = $this->Form->inputs('The Legend'); $result = $this->Form->inputs('The Legend');
$this->assertPattern('/<legend>The Legend<\/legend>/', $result); $this->assertPattern('/<legend>The Legend<\/legend>/', $result);
ClassRegistry::getObject('view')->testing = true; $View = ClassRegistry::getObject('view');
$View->testing = true;
$this->Form->testing = true; $this->Form->testing = true;
unset(ClassRegistry::getObject('view')->testing, $this->Form->testing); unset($View->testing, $this->Form->testing);
$this->Form->params['prefix'] = 'admin'; $this->Form->params['prefix'] = 'admin';
$this->Form->action = 'admin_edit'; $this->Form->action = 'admin_edit';