Permalink
Browse files

Fix Helper::field() for Model.min

This field would be treated as a field suffix even though
it is not. Expand existing tests, and reformat some code.

Fixes #2240
  • Loading branch information...
1 parent 0b0d180 commit 7d756fda81423cdbc317755a46ce231ede448ab5 @markstory markstory committed Nov 11, 2011
Showing with 31 additions and 9 deletions.
  1. +23 −7 lib/Cake/Test/Case/View/HelperTest.php
  2. +8 −2 lib/Cake/View/Helper.php
@@ -220,32 +220,48 @@ public static function entityProvider() {
return array(
array(
'HelperTestPost.id',
- array('HelperTestPost', 'id')
+ array('HelperTestPost', 'id'),
+ 'HelperTestPost',
+ 'id'
),
array(
'HelperTestComment.body',
- array('HelperTestComment', 'body')
+ array('HelperTestComment', 'body'),
+ 'HelperTestComment',
+ 'body'
),
array(
'HelperTest.1.Comment.body',
- array('HelperTest', '1', 'Comment', 'body')
+ array('HelperTest', '1', 'Comment', 'body'),
+ 'Comment',
+ 'body'
),
array(
'HelperTestComment.BigField',
- array('HelperTestComment', 'BigField')
+ array('HelperTestComment', 'BigField'),
+ 'HelperTestComment',
+ 'BigField'
+ ),
+ array(
+ 'HelperTestComment.min',
+ array('HelperTestComment', 'min'),
+ 'HelperTestComment',
+ 'min'
)
);
}
/**
- * testFormFieldNameParsing method
+ * Test setting an entity and retriving the entity, model and field.
*
* @dataProvider entityProvider
* @return void
*/
- public function testSetEntity($entity, $expected) {
+ public function testSetEntity($entity, $expected, $modelKey, $fieldKey) {
$this->Helper->setEntity($entity);
$this->assertEquals($expected, $this->Helper->entity());
+ $this->assertEquals($modelKey, $this->Helper->model());
+ $this->assertEquals($fieldKey, $this->Helper->field());
}
/**
@@ -255,7 +271,7 @@ public function testSetEntity($entity, $expected) {
*/
public function testSetEntityScoped() {
$this->Helper->setEntity('HelperTestPost', true);
- $this->assertEquals(array('HelperTestPost'), $this->Helper->entity());
+ $this->assertEquals(array('HelperTestPost'), $this->Helper->entity());
$this->Helper->setEntity('id');
$expected = array('HelperTestPost', 'id');
@@ -446,7 +446,11 @@ public function setEntity($entity, $setScope = false) {
// 0.name, 0.created.month style inputs. Excludes inputs with the modelScope in them.
if (
- $count >= 2 && is_numeric($parts[0]) && !is_numeric($parts[1]) && $this->_modelScope && strpos($entity, $this->_modelScope) === false
+ $count >= 2 &&
+ is_numeric($parts[0]) &&
+ !is_numeric($parts[1]) &&
+ $this->_modelScope &&
+ strpos($entity, $this->_modelScope) === false
) {
$entity = $this->_modelScope . '.' . $entity;
}
@@ -500,14 +504,16 @@ public function model() {
/**
* Gets the currently-used model field of the rendering context.
+ * Strips off fieldsuffixes such as year, month, day, hour, min, meridian
+ * when the current entity is longer than 2 elements.
*
* @return string
*/
public function field() {
$entity = $this->entity();
$count = count($entity);
$last = $entity[$count - 1];
- if (in_array($last, $this->_fieldSuffixes)) {
+ if ($count > 2 && in_array($last, $this->_fieldSuffixes)) {
$last = isset($entity[$count - 2]) ? $entity[$count - 2] : null;
}
return $last;

0 comments on commit 7d756fd

Please sign in to comment.