Fixing Helper::value() when only a fieldname is used. Refactoring Helper::value() to reduce method calls. Added tests to Helper. Fixes #6033

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@8012 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
mark_story 2009-01-26 19:45:55 +00:00
parent 27065d7aab
commit 2da0c208d5
2 changed files with 50 additions and 8 deletions

View file

@ -567,15 +567,26 @@ class Helper extends Overloadable {
$result = null;
if (isset($this->data[$this->model()][$this->field()])) {
$result = $this->data[$this->model()][$this->field()];
} elseif (isset($this->data[$this->field()]) && is_array($this->data[$this->field()])) {
if (ClassRegistry::isKeySet($this->field())) {
$model =& ClassRegistry::getObject($this->field());
$result = $this->__selectedArray($this->data[$this->field()], $model->primaryKey);
$modelName = $this->model();
$fieldName = $this->field();
$modelID = $this->modelID();
if (is_null($fieldName)) {
$fieldName = $modelName;
$modelName = null;
}
if (isset($this->data[$fieldName]) && $modelName === null) {
$result = $this->data[$fieldName];
} elseif (isset($this->data[$modelName][$fieldName])) {
$result = $this->data[$modelName][$fieldName];
} elseif (isset($this->data[$fieldName]) && is_array($this->data[$fieldName])) {
if (ClassRegistry::isKeySet($fieldName)) {
$model =& ClassRegistry::getObject($fieldName);
$result = $this->__selectedArray($this->data[$fieldName], $model->primaryKey);
}
} elseif (isset($this->data[$this->model()][$this->modelID()][$this->field()])) {
$result = $this->data[$this->model()][$this->modelID()][$this->field()];
} elseif (isset($this->data[$modelName][$modelID][$fieldName])) {
$result = $this->data[$modelName][$modelID][$fieldName];
}
if (is_array($result)) {

View file

@ -316,6 +316,37 @@ class HelperTest extends CakeTestCase {
$this->assertEqual($this->View->association, null);
$this->assertEqual($this->View->fieldSuffix, null);
}
/**
* test getting values from Helper
*
* @return void
**/
function testValue() {
$this->Helper->data = array('fullname' => 'This is me');
$this->Helper->setEntity('fullname');
$result = $this->Helper->value('fullname');
$this->assertEqual($result, 'This is me');
$this->Helper->data = array('Post' => array('name' => 'First Post'));
$this->Helper->setEntity('Post.name');
$result = $this->Helper->value('Post.name');
$this->assertEqual($result, 'First Post');
$this->Helper->data = array('Post' => array(2 => array('name' => 'First Post')));
$this->Helper->setEntity('Post.2.name');
$result = $this->Helper->value('Post.2.name');
$this->assertEqual($result, 'First Post');
$this->Helper->data = array('Post' => array(2 => array('created' => array('year' => '2008'))));
$this->Helper->setEntity('Post.2.created');
$result = $this->Helper->value('Post.2.created');
$this->assertEqual($result, array('year' => '2008'));
$this->Helper->data = array('Post' => array(2 => array('created' => array('year' => '2008'))));
$this->Helper->setEntity('Post.2.created.year');
$result = $this->Helper->value('Post.2.created.year');
$this->assertEqual($result, '2008');
}
/**
* testFieldsWithSameName method
*