From 2da0c208d5c10f6db9a029ab39502ee5c579dfef Mon Sep 17 00:00:00 2001 From: mark_story Date: Mon, 26 Jan 2009 19:45:55 +0000 Subject: [PATCH] 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 --- cake/libs/view/helper.php | 27 +++++++++++++------ cake/tests/cases/libs/view/helper.test.php | 31 ++++++++++++++++++++++ 2 files changed, 50 insertions(+), 8 deletions(-) diff --git a/cake/libs/view/helper.php b/cake/libs/view/helper.php index 1c4786646..b365d7d74 100644 --- a/cake/libs/view/helper.php +++ b/cake/libs/view/helper.php @@ -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)) { diff --git a/cake/tests/cases/libs/view/helper.test.php b/cake/tests/cases/libs/view/helper.test.php index 90aa28811..922719c99 100644 --- a/cake/tests/cases/libs/view/helper.test.php +++ b/cake/tests/cases/libs/view/helper.test.php @@ -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 *