Browse files

Remove a number of unused methods from Helper.

These methods were only ever used by FormHelper in the core. They are
complex and not useful not that FormHelper does not persist state.
I'm not aware of many userland Helpers that used this functionality
either, which makes their removal more straight-forward.
  • Loading branch information...
1 parent 4ce49fc commit 71c26e913f62627d7380ac08acad2356a5ebe451 @markstory markstory committed Mar 5, 2014
Showing with 1 addition and 718 deletions.
  1. +0 −287 src/View/Helper.php
  2. +1 −431 tests/TestCase/View/HelperTest.php
View
287 src/View/Helper.php
@@ -113,41 +113,6 @@ class Helper extends Object implements EventListener {
protected $_View;
/**
- * A list of strings that should be treated as suffixes, or
- * sub inputs for a parent input. This is used for date/time
- * inputs primarily.
- *
- * @var array
- */
- protected $_fieldSuffixes = array(
- 'year', 'month', 'day', 'hour', 'min', 'second', 'meridian'
- );
-
-/**
- * The name of the current model entities are in scope of.
- *
- * @see Helper::setEntity()
- * @var string
- */
- protected $_modelScope;
-
-/**
- * The name of the current model association entities are in scope of.
- *
- * @see Helper::setEntity()
- * @var string
- */
- protected $_association;
-
-/**
- * The dot separated list of elements the current field entity is for.
- *
- * @see Helper::setEntity()
- * @var string
- */
- protected $_entityPath;
-
-/**
* Minimized attributes
*
* @var array
@@ -471,229 +436,6 @@ protected function _confirm($message, $okCode, $cancelCode = '', $options = arra
}
/**
- * Sets this helper's model and field properties to the dot-separated value-pair in $entity.
- *
- * @param string $entity A field name, like "ModelName.fieldName" or "ModelName.ID.fieldName"
- * @param boolean $setScope Sets the view scope to the model specified in $tagValue
- * @return void
- */
- public function setEntity($entity, $setScope = false) {
- if ($entity === null) {
- $this->_modelScope = false;
- }
- if ($setScope === true) {
- $this->_modelScope = $entity;
- }
- $parts = array_values(Hash::filter(explode('.', $entity)));
- 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 && $this->_modelScope && !$setScope) ||
- (
- $count === 2 &&
- in_array($lastPart, $this->_fieldSuffixes) &&
- $this->_modelScope &&
- $parts[0] !== $this->_modelScope
- )
- ) {
- $entity = $this->_modelScope . '.' . $entity;
- }
-
- // 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
- ) {
- $entity = $this->_modelScope . '.' . $entity;
- }
-
- $this->_association = null;
-
- $isHabtm = (
- isset($this->fieldset[$this->_modelScope]['fields'][$parts[0]]['type']) &&
- $this->fieldset[$this->_modelScope]['fields'][$parts[0]]['type'] === 'multiple'
- );
-
- // habtm models are special
- if ($count === 1 && $isHabtm) {
- $this->_association = $parts[0];
- $entity = $parts[0] . '.' . $parts[0];
- } else {
- // check for associated model.
- $reversed = array_reverse($parts);
- foreach ($reversed as $i => $part) {
- if ($i > 0 && preg_match('/^[A-Z]/', $part)) {
- $this->_association = $part;
- break;
- }
- }
- }
- $this->_entityPath = $entity;
- }
-
-/**
- * Returns the entity reference of the current context as an array of identity parts
- *
- * @return array An array containing the identity elements of an entity
- */
- public function entity() {
- return explode('.', $this->_entityPath);
- }
-
-/**
- * Gets the currently-used model of the rendering context.
- *
- * @return string
- */
- public function model() {
- if ($this->_association) {
- return $this->_association;
- }
- return $this->_modelScope;
- }
-
-/**
- * Gets the currently-used model field of the rendering context.
- * Strips off field suffixes 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 ($count > 2 && in_array($last, $this->_fieldSuffixes)) {
- $last = isset($entity[$count - 2]) ? $entity[$count - 2] : null;
- }
- return $last;
- }
-
-/**
- * Gets the input field name for the current tag. Creates input name attributes
- * using CakePHP's `Model[field]` formatting.
- *
- * @param array|string $options If an array, should be an array of attributes that $key needs to be added to.
- * If a string or null, will be used as the View entity.
- * @param string $field
- * @param string $key The name of the attribute to be set, defaults to 'name'
- * @return mixed If an array was given for $options, an array with $key set will be returned.
- * If a string was supplied a string will be returned.
- */
- protected function _name($options = array(), $field = null, $key = 'name') {
- if ($options === null) {
- $options = array();
- } elseif (is_string($options)) {
- $field = $options;
- $options = 0;
- }
-
- if (!empty($field)) {
- $this->setEntity($field);
- }
-
- if (is_array($options) && array_key_exists($key, $options)) {
- return $options;
- }
-
- switch ($field) {
- case '_method':
- $name = $field;
- break;
- default:
- $entity = $this->entity();
- $first = array_shift($entity);
- $name = $first . ($entity ? '[' . implode('][', $entity) . ']' : '');
- break;
- }
-
- if (is_array($options)) {
- $options[$key] = $name;
- return $options;
- }
- return $name;
- }
-
-/**
- * Gets the data for the current tag
- *
- * @param array|string $options If an array, should be an array of attributes that $key needs to be added to.
- * If a string or null, will be used as the View entity.
- * @param string $field
- * @param string $key The name of the attribute to be set, defaults to 'value'
- * @return mixed If an array was given for $options, an array with $key set will be returned.
- * If a string was supplied a string will be returned.
- */
- public function value($options = array(), $field = null, $key = 'value') {
- if ($options === null) {
- $options = array();
- } elseif (is_string($options)) {
- $field = $options;
- $options = 0;
- }
-
- if (is_array($options) && isset($options[$key])) {
- return $options;
- }
-
- if (!empty($field)) {
- $this->setEntity($field);
- }
- $result = null;
- $data = $this->request->data;
-
- $entity = $this->entity();
- if (!empty($data) && is_array($data) && !empty($entity)) {
- $result = Hash::get($data, implode('.', $entity));
- }
-
- $habtmKey = $this->field();
- if (empty($result) && isset($data[$habtmKey][$habtmKey]) && is_array($data[$habtmKey])) {
- $result = $data[$habtmKey][$habtmKey];
- }
-
- if (is_array($options)) {
- if ($result === null && isset($options['default'])) {
- $result = $options['default'];
- }
- unset($options['default']);
- }
-
- if (is_array($options)) {
- $options[$key] = $result;
- return $options;
- }
- return $result;
- }
-
-/**
- * Sets the defaults for an input tag. Will set the
- * name, value, and id attributes for an array of html attributes.
- *
- * @param string $field The field name to initialize.
- * @param array $options Array of options to use while initializing an input field.
- * @return array Array options for the form input.
- */
- protected function _initInputField($field, $options = array()) {
- if ($field !== null) {
- $this->setEntity($field);
- }
- $options = (array)$options;
- $options = $this->_name($options);
- $options = $this->value($options);
- $options = $this->domId($options);
- return $options;
- }
-
-/**
* Adds the given class to the element options
*
* @param array $options Array options/attributes to add a class to
@@ -739,33 +481,4 @@ public function implementedEvents() {
return $events;
}
-/**
- * Transforms a recordset from a hasAndBelongsToMany association to a list of selected
- * options for a multiple select element
- *
- * @param string|array $data
- * @param string $key
- * @return array
- */
- protected function _selectedArray($data, $key = 'id') {
- if (!is_array($data)) {
- $model = $data;
- if (!empty($this->request->data[$model][$model])) {
- return $this->request->data[$model][$model];
- }
- if (!empty($this->request->data[$model])) {
- $data = $this->request->data[$model];
- }
- }
- $array = array();
- if (!empty($data)) {
- foreach ($data as $row) {
- if (isset($row[$key])) {
- $array[$row[$key]] = $row[$key];
- }
- }
- }
- return empty($array) ? null : $array;
- }
-
}
View
432 tests/TestCase/View/HelperTest.php
@@ -197,46 +197,6 @@ public function tearDown() {
}
/**
- * Provider for setEntity test.
- *
- * @return array
- */
- public static function entityProvider() {
- return array(
- array(
- 'HelperTestPost.id',
- array('HelperTestPost', 'id'),
- 'HelperTestPost',
- 'id'
- ),
- array(
- 'HelperTestComment.body',
- array('HelperTestComment', 'body'),
- 'HelperTestComment',
- 'body'
- ),
- array(
- 'HelperTest.1.Comment.body',
- array('HelperTest', '1', 'Comment', 'body'),
- 'Comment',
- 'body'
- ),
- array(
- 'HelperTestComment.BigField',
- array('HelperTestComment', 'BigField'),
- 'HelperTestComment',
- 'BigField'
- ),
- array(
- 'HelperTestComment.min',
- array('HelperTestComment', 'min'),
- 'HelperTestComment',
- 'min'
- )
- );
- }
-
-/**
* Test settings merging
*
* @return void
@@ -255,259 +215,6 @@ public function testSettingsMerging() {
}
/**
- * Test setting an entity and retrieving the entity, model and field.
- *
- * @dataProvider entityProvider
- * @return void
- */
- 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());
- }
-
-/**
- * test setEntity with setting a scope.
- *
- * @return void
- */
- public function testSetEntityScoped() {
- $this->Helper->setEntity('HelperTestPost', true);
- $this->assertEquals(array('HelperTestPost'), $this->Helper->entity());
-
- $this->Helper->setEntity('id');
- $expected = array('HelperTestPost', 'id');
- $this->assertEquals($expected, $this->Helper->entity());
-
- $this->Helper->setEntity('HelperTestComment.body');
- $expected = array('HelperTestComment', 'body');
- $this->assertEquals($expected, $this->Helper->entity());
-
- $this->Helper->setEntity('body');
- $expected = array('HelperTestPost', 'body');
- $this->assertEquals($expected, $this->Helper->entity());
-
- $this->Helper->setEntity('2.body');
- $expected = array('HelperTestPost', '2', 'body');
- $this->assertEquals($expected, $this->Helper->entity());
-
- $this->Helper->setEntity('Something.else');
- $expected = array('Something', 'else');
- $this->assertEquals($expected, $this->Helper->entity());
-
- $this->Helper->setEntity('HelperTestComment.5.id');
- $expected = array('HelperTestComment', 5, 'id');
- $this->assertEquals($expected, $this->Helper->entity());
-
- $this->Helper->setEntity('HelperTestComment.id.time');
- $expected = array('HelperTestComment', 'id', 'time');
- $this->assertEquals($expected, $this->Helper->entity());
-
- $this->Helper->setEntity('HelperTestComment.created.year');
- $expected = array('HelperTestComment', 'created', 'year');
- $this->assertEquals($expected, $this->Helper->entity());
-
- $this->Helper->setEntity(null);
- $this->Helper->setEntity('ModelThatDoesntExist.field_that_doesnt_exist');
- $expected = array('ModelThatDoesntExist', 'field_that_doesnt_exist');
- $this->assertEquals($expected, $this->Helper->entity());
- }
-
-/**
- * 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->Helper->entity());
-
- $this->assertEquals('HelperTestComment', $this->Helper->model());
- }
-
-/**
- * Test creating saveMany() compatible entities
- *
- * @return void
- */
- public function testSetEntitySaveMany() {
- $this->Helper->setEntity('HelperTestPost', true);
-
- $this->Helper->setEntity('0.HelperTestPost.id');
- $expected = array('0', 'HelperTestPost', 'id');
- $this->assertEquals($expected, $this->Helper->entity());
- }
-
-/**
- * Test that setEntity doesn't make CamelCase fields that are not associations an
- * associated model.
- *
- * @return void
- */
- public function testSetEntityAssociatedCamelCaseField() {
- $this->Helper->fieldset = array(
- 'HelperTestComment' => array(
- 'fields' => array('BigField' => array('type' => 'integer'))
- )
- );
- $this->Helper->setEntity('HelperTestComment', true);
- $this->Helper->setEntity('HelperTestComment.BigField');
-
- $this->assertEquals('HelperTestComment', $this->Helper->model());
- $this->assertEquals('BigField', $this->Helper->field());
- }
-
-/**
- * Test that multiple fields work when they are camelcase and in fieldset
- *
- * @return void
- */
- public function testSetEntityAssociatedCamelCaseFieldHabtmMultiple() {
- $this->Helper->fieldset = array(
- 'HelperTestComment' => array(
- 'fields' => array('Tag' => array('type' => 'multiple'))
- )
- );
- $this->Helper->setEntity('HelperTestComment', true);
- $this->Helper->setEntity('Tag');
-
- $this->assertEquals('Tag', $this->Helper->model());
- $this->assertEquals('Tag', $this->Helper->field());
- $this->assertEquals(array('Tag', 'Tag'), $this->Helper->entity());
- }
-
-/**
- * Test that habtm associations can have property fields created.
- *
- * @return void
- */
- public function testSetEntityHabtmPropertyFieldNames() {
- $this->Helper->fieldset = array(
- 'HelperTestComment' => array(
- 'fields' => array('Tag' => array('type' => 'multiple'))
- )
- );
- $this->Helper->setEntity('HelperTestComment', true);
-
- $this->Helper->setEntity('Tag.name');
- $this->assertEquals('Tag', $this->Helper->model());
- $this->assertEquals('name', $this->Helper->field());
- $this->assertEquals(array('Tag', 'name'), $this->Helper->entity());
- }
-
-/**
- * test that 'view' doesn't break things.
- *
- * @return void
- */
- public function testSetEntityWithView() {
- $this->assertNull($this->Helper->setEntity('Allow.view.group_id'));
- $this->assertNull($this->Helper->setEntity('Allow.view'));
- $this->assertNull($this->Helper->setEntity('View.view'));
- }
-
-/**
- * test getting values from Helper
- *
- * @return void
- */
- public function testValue() {
- $this->Helper->request->data = array('fullname' => 'This is me');
- $this->Helper->setEntity('fullname');
- $result = $this->Helper->value('fullname');
- $this->assertEquals('This is me', $result);
-
- $this->Helper->request->data = array(
- 'Post' => array('name' => 'First Post')
- );
- $this->Helper->setEntity('Post.name');
- $result = $this->Helper->value('Post.name');
- $this->assertEquals('First Post', $result);
-
- $this->Helper->request->data = array(
- 'Post' => array(2 => array('name' => 'First Post'))
- );
- $this->Helper->setEntity('Post.2.name');
- $result = $this->Helper->value('Post.2.name');
- $this->assertEquals('First Post', $result);
-
- $this->Helper->request->data = array(
- 'Post' => array(
- 2 => array('created' => array('year' => '2008'))
- )
- );
- $this->Helper->setEntity('Post.2.created');
- $result = $this->Helper->value('Post.2.created');
- $this->assertEquals(array('year' => '2008'), $result);
-
- $this->Helper->request->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->assertEquals('2008', $result);
- }
-
-/**
- * Test default values with value()
- *
- * @return void
- */
- public function testValueWithDefault() {
- $this->Helper->request->data = array('zero' => 0);
- $this->Helper->setEntity('zero');
- $result = $this->Helper->value(array('default' => 'something'), 'zero');
- $this->assertEquals(array('value' => 0), $result);
-
- $this->Helper->request->data = array('zero' => '0');
- $result = $this->Helper->value(array('default' => 'something'), 'zero');
- $this->assertEquals(array('value' => '0'), $result);
-
- $this->Helper->setEntity('inexistent');
- $result = $this->Helper->value(array('default' => 'something'), 'inexistent');
- $this->assertEquals(array('value' => 'something'), $result);
- }
-
-/**
- * Test habtm data fetching and ensure no pollution happens.
- *
- * @return void
- */
- public function testValueHabtmKeys() {
- $this->Helper->request->data = array(
- 'HelperTestTag' => array('HelperTestTag' => '')
- );
- $this->Helper->setEntity('HelperTestTag.HelperTestTag');
- $result = $this->Helper->value('HelperTestTag.HelperTestTag');
- $this->assertEquals('', $result);
-
- $this->Helper->request->data = array(
- 'HelperTestTag' => array(
- 'HelperTestTag' => array(2, 3, 4)
- )
- );
- $this->Helper->setEntity('HelperTestTag.HelperTestTag');
- $result = $this->Helper->value('HelperTestTag.HelperTestTag');
- $this->assertEquals(array(2, 3, 4), $result);
-
- $this->Helper->request->data = array(
- 'HelperTestTag' => array(
- 'body' => '',
- 'title' => 'winning'
- ),
- );
- $this->Helper->setEntity('HelperTestTag.body');
- $result = $this->Helper->value('HelperTestTag.body');
- $this->assertEquals('', $result);
- }
-
-/**
* Ensure HTML escaping of URL params. So link addresses are valid and not exploited
*
* @return void
@@ -690,147 +397,10 @@ public function testAssetTimestampPluginsAndThemes() {
}
/**
- * testFieldsWithSameName method
- *
- * @return void
- */
- public function testFieldsWithSameName() {
- $this->Helper->setEntity('HelperTestTag', true);
-
- $this->Helper->setEntity('HelperTestTag.id');
- $expected = array('HelperTestTag', 'id');
- $this->assertEquals($expected, $this->Helper->entity());
-
- $this->Helper->setEntity('My.id');
- $expected = array('My', 'id');
- $this->assertEquals($expected, $this->Helper->entity());
-
- $this->Helper->setEntity('MyOther.id');
- $expected = array('MyOther', 'id');
- $this->assertEquals($expected, $this->Helper->entity());
- }
-
-/**
- * testFieldSameAsModel method
- *
- * @return void
- */
- public function testFieldSameAsModel() {
- $this->Helper->setEntity('HelperTestTag', true);
-
- $this->Helper->setEntity('helper_test_post');
- $expected = array('HelperTestTag', 'helper_test_post');
- $this->assertEquals($expected, $this->Helper->entity());
-
- $this->Helper->setEntity('HelperTestTag');
- $expected = array('HelperTestTag', 'HelperTestTag');
- $this->assertEquals($expected, $this->Helper->entity());
- }
-
-/**
- * testFieldSuffixForDate method
- *
- * @return void
- */
- public function testFieldSuffixForDate() {
- $this->Helper->setEntity('HelperTestPost', true);
- $expected = array('HelperTestPost');
- $this->assertEquals($expected, $this->Helper->entity());
-
- foreach (array('year', 'month', 'day', 'hour', 'min', 'meridian') as $d) {
- $this->Helper->setEntity('date.' . $d);
- $expected = array('HelperTestPost', 'date', $d);
- $this->assertEquals($expected, $this->Helper->entity());
- }
- }
-
-/**
- * testMulitDimensionValue method
- *
- * @return void
- */
- public function testMultiDimensionValue() {
- $this->Helper->data = array();
- for ($i = 0; $i < 2; $i++) {
- $this->Helper->request->data['Model'][$i] = 'what';
- $result[] = $this->Helper->value("Model.{$i}");
- $this->Helper->request->data['Model'][$i] = array();
- for ($j = 0; $j < 2; $j++) {
- $this->Helper->request->data['Model'][$i][$j] = 'how';
- $result[] = $this->Helper->value("Model.{$i}.{$j}");
- }
- }
- $expected = array('what', 'how', 'how', 'what', 'how', 'how');
- $this->assertEquals($expected, $result);
-
- $this->Helper->request->data['HelperTestComment']['5']['id'] = 'ok';
- $result = $this->Helper->value('HelperTestComment.5.id');
- $this->assertEquals('ok', $result);
-
- $this->Helper->setEntity('HelperTestPost', true);
- $this->Helper->request->data['HelperTestPost']['5']['created']['month'] = '10';
- $result = $this->Helper->value('5.created.month');
- $this->assertEquals(10, $result);
-
- $this->Helper->request->data['HelperTestPost']['0']['id'] = 100;
- $result = $this->Helper->value('HelperTestPost.0.id');
- $this->assertEquals(100, $result);
- }
-
-/**
- * testMultiDimensionalField method
+ * Test generating paths with webroot().
*
* @return void
*/
- public function testMultiDimensionalField() {
- $this->Helper->setEntity('HelperTestPost', true);
-
- $entity = 'HelperTestPost.2.HelperTestComment.1.title';
- $this->Helper->setEntity($entity);
- $expected = array(
- 'HelperTestPost', '2', 'HelperTestComment', '1', 'title'
- );
- $this->assertEquals($expected, $this->Helper->entity());
-
- $entity = 'HelperTestPost.1.HelperTestComment.1.HelperTestTag.1.created';
- $this->Helper->setEntity($entity);
- $expected = array(
- 'HelperTestPost', '1', 'HelperTestComment', '1',
- 'HelperTestTag', '1', 'created'
- );
- $this->assertEquals($expected, $this->Helper->entity());
-
- $entity = 'HelperTestPost.0.HelperTestComment.1.HelperTestTag.1.fake';
- $expected = array(
- 'HelperTestPost', '0', 'HelperTestComment', '1',
- 'HelperTestTag', '1', 'fake'
- );
- $this->Helper->setEntity($entity);
-
- $entity = '1.HelperTestComment.1.HelperTestTag.created.year';
- $this->Helper->setEntity($entity);
-
- $this->Helper->request->data['HelperTestPost'][2]['HelperTestComment'][1]['title'] = 'My Title';
- $result = $this->Helper->value('HelperTestPost.2.HelperTestComment.1.title');
- $this->assertEquals('My Title', $result);
-
- $this->Helper->request->data['HelperTestPost'][2]['HelperTestComment'][1]['created']['year'] = 2008;
- $result = $this->Helper->value('HelperTestPost.2.HelperTestComment.1.created.year');
- $this->assertEquals(2008, $result);
-
- $this->Helper->request->data[2]['HelperTestComment'][1]['created']['year'] = 2008;
- $result = $this->Helper->value('HelperTestPost.2.HelperTestComment.1.created.year');
- $this->assertEquals(2008, $result);
-
- $this->Helper->request->data['HelperTestPost']['title'] = 'My Title';
- $result = $this->Helper->value('title');
- $this->assertEquals('My Title', $result);
-
- $this->Helper->request->data['My']['title'] = 'My Title';
- $result = $this->Helper->value('My.title');
- $this->assertEquals('My Title', $result);
- }
-
public function testWebrootPaths() {
$this->Helper->request->webroot = '/';
$result = $this->Helper->webroot('/img/cake.power.gif');

0 comments on commit 71c26e9

Please sign in to comment.