Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixing more failing tests.

Fixing use of properties that are going away.
  • Loading branch information...
commit 4073584eef04942733308760b8054c5ea7eb880f 1 parent 87fe663
@markstory markstory authored
View
18 lib/Cake/Test/Case/View/HelperTest.php
@@ -276,7 +276,6 @@ public function testSetEntityScoped() {
$expected = array('HelperTestPost', 'body');
$this->assertEquals($expected, $this->View->entity());
-
$this->Helper->setEntity('2.body');
$expected = array('HelperTestPost', '2', 'body');
$this->assertEquals($expected, $this->View->entity());
@@ -304,6 +303,21 @@ public function testSetEntityScoped() {
}
/**
+ * Test that setEntity() and model()/field() work with associated models.
+ *
+ * @return void
+ */
+ public function testSetEntityAssociated() {
+ $this->Helper->setEntity('HelperTestPost', true);
+
+ $this->Helper->setEntity('HelperTestPost.1.HelperTestComment.1.title');
+ $expected = array('HelperTestPost', '1', 'HelperTestComment', '1', 'title');
+ $this->assertEquals($expected, $this->View->entity());
+
+ $this->assertEquals('HelperTestComment', $this->Helper->model());
+ }
+
+/**
* test that 'view' doesn't break things.
*
* @return void
@@ -553,7 +567,7 @@ public function testFieldSuffixForDate() {
$expected = array('HelperTestPost');
$this->assertEquals($expected, $this->View->entity());
- foreach (array('year', 'month', 'day', 'hour', 'minute', 'meridian') as $d) {
+ foreach (array('year', 'month', 'day', 'hour', 'min', 'meridian') as $d) {
$this->Helper->setEntity('date.' . $d);
$expected = array('HelperTestPost', 'date', $d);
$this->assertEquals($expected, $this->View->entity());
View
41 lib/Cake/View/Helper.php
@@ -111,7 +111,7 @@ class Helper extends Object {
* @var array
*/
protected $_fieldSuffixes = array(
- 'year', 'month', 'day', 'hour', 'minute', 'second', 'meridian'
+ 'year', 'month', 'day', 'hour', 'min', 'second', 'meridian'
);
/**
@@ -395,7 +395,6 @@ public function setEntity($entity, $setScope = false) {
if ($entity === null) {
$view->modelScope = false;
}
-
if ($setScope === true) {
$view->modelScope = $entity;
}
@@ -403,9 +402,10 @@ public function setEntity($entity, $setScope = false) {
if (empty($parts)) {
return;
}
-
$count = count($parts);
$lastPart = isset($parts[$count - 1]) ? $parts[$count - 1] : null;
+
+ // Either 'body' or 'date.month' type inputs.
if (
($count === 1 &&
$view->modelScope &&
@@ -416,6 +416,8 @@ public function setEntity($entity, $setScope = false) {
) {
$entity = $view->modelScope . '.' . $entity;
}
+
+ // 0.name style inputs.
if (
$count === 2 &&
is_numeric($parts[0]) &&
@@ -423,6 +425,26 @@ public function setEntity($entity, $setScope = false) {
) {
$entity = $view->modelScope . '.' . $entity;
}
+
+ $view->association = null;
+
+ // check for associated model.
+ $reversed = array_reverse($parts);
+ foreach ($reversed as $part) {
+ if (preg_match('/^[A-Z]/', $part)) {
+ $view->association = $part;
+ break;
+ }
+ }
+
+ // habtm models are special
+ if (
+ isset($this->fieldset[$view->modelScope]['fields'][$parts[0]]['type']) &&
+ $this->fieldset[$view->modelScope]['fields'][$parts[0]]['type'] === 'multiple'
+ ) {
+ $entity = $parts[0] . '.' . $parts[0];
+ }
+
$view->entityPath = $entity;
return;
@@ -575,7 +597,10 @@ public function setEntity($entity, $setScope = false) {
*/
public function model() {
$entity = $this->_View->entity();
- return isset($entity[0]) ? $entity[0] : null;
+ if ($this->_View->association) {
+ return $this->_View->association;
+ }
+ return $this->_View->modelScope;
}
/**
@@ -881,11 +906,13 @@ private function __selectedArray($data, $key = 'id') {
}
$array = array();
if (!empty($data)) {
- foreach ($data as $var) {
- $array[$var[$key]] = $var[$key];
+ foreach ($data as $row) {
+ if (isset($row[$key])) {
+ $array[$row[$key]] = $row[$key];
+ }
}
}
- return $array;
+ return empty($array) ? null : $array;
}
/**
View
9 lib/Cake/View/Helper/FormHelper.php
@@ -2122,9 +2122,12 @@ protected function _name($options = array(), $field = null, $key = 'name') {
return $options;
}
- $name = !empty($this->_View->field) ? $this->_View->field : $this->_View->model;
- if (!empty($this->_View->fieldSuffix)) {
- $name .= '[' . $this->_View->fieldSuffix . ']';
+ $entity = $this->_View->entity();
+ $model = $this->model();
+ $name = $model === $entity[0] && isset($entity[1]) ? $entity[1] : $entity[0];
+ $last = $entity[count($entity) - 1];
+ if (in_array($last, $this->_fieldSuffixes)) {
+ $name .= '[' . $last . ']';
}
if (is_array($options)) {
Please sign in to comment.
Something went wrong with that request. Please try again.