Permalink
Browse files

Fixing Helper::value() when models exist in ClassRegistry

and field values are empty.
Fixes #1817
  • Loading branch information...
1 parent 81a2563 commit 1d0d3f80fb65149615af6b6e16024b9bf30d483f @markstory markstory committed Jul 7, 2011
Showing with 37 additions and 13 deletions.
  1. +36 −12 lib/Cake/Test/Case/View/HelperTest.php
  2. +1 −1 lib/Cake/View/Helper.php
@@ -366,7 +366,34 @@ public function testValue() {
$this->Helper->setEntity('Post.2.created.year');
$result = $this->Helper->value('Post.2.created.year');
$this->assertEqual($result, '2008');
+ }
+
+/**
+ * Test default values with value()
+ *
+ * @return void
+ */
+ function testValueWithDefault() {
+ $this->Helper->request->data = array('zero' => 0);
+ $this->Helper->setEntity('zero');
+ $result = $this->Helper->value(array('default' => 'something'), 'zero');
+ $this->assertEqual($result, array('value' => 0));
+ $this->Helper->request->data = array('zero' => '0');
+ $result = $this->Helper->value(array('default' => 'something'), 'zero');
+ $this->assertEqual($result, array('value' => '0'));
+
+ $this->Helper->setEntity('inexistent');
+ $result = $this->Helper->value(array('default' => 'something'), 'inexistent');
+ $this->assertEqual($result, array('value' => 'something'));
+ }
+
+/**
+ * Test habtm data fetching and ensure no pollution happens.
+ *
+ * @return void
+ */
+ function testValueHabtmKeys() {
$this->Helper->request->data = array(
'HelperTestTag' => array('HelperTestTag' => '')
);
@@ -393,18 +420,15 @@ public function testValue() {
$result = $this->Helper->value('HelperTestTag.HelperTestTag');
$this->assertEqual($result, array(3 => 3, 5 => 5));
- $this->Helper->request->data = array('zero' => 0);
- $this->Helper->setEntity('zero');
- $result = $this->Helper->value(array('default' => 'something'), 'zero');
- $this->assertEqual($result, array('value' => 0));
-
- $this->Helper->request->data = array('zero' => '0');
- $result = $this->Helper->value(array('default' => 'something'), 'zero');
- $this->assertEqual($result, array('value' => '0'));
-
- $this->Helper->setEntity('inexistent');
- $result = $this->Helper->value(array('default' => 'something'), 'inexistent');
- $this->assertEqual($result, array('value' => 'something'));
+ $this->Helper->request->data = array(
+ 'HelperTestTag' => array(
+ 'body' => '',
+ 'title' => 'winning'
+ ),
+ );
+ $this->Helper->setEntity('HelperTestTag.body');
+ $result = $this->Helper->value('HelperTestTag.body');
+ $this->assertEqual($result, '');
}
/**
View
@@ -638,7 +638,7 @@ public function value($options = array(), $field = null, $key = 'value') {
$result = Set::extract($data, implode('.', $entity));
}
- $habtmKey = $entity[0];
+ $habtmKey = $this->field();
if (empty($result) && isset($data[$habtmKey][$habtmKey]) && is_array($data[$habtmKey])) {
$result = $data[$habtmKey][$habtmKey];
} elseif (empty($result) && isset($data[$habtmKey]) && is_array($data[$habtmKey])) {

0 comments on commit 1d0d3f8

Please sign in to comment.