From 35f152b333d0d729fae55f6495fcb333d2406c63 Mon Sep 17 00:00:00 2001 From: jalbertocr Date: Wed, 19 Feb 2014 20:50:30 -0300 Subject: [PATCH 1/5] Make sure a datetime instance is valid to avoid fatal errors. --- .../Test/Case/View/Helper/FormHelperTest.php | 85 +++++++++++++++++++ lib/Cake/View/Helper/FormHelper.php | 44 +++++++--- 2 files changed, 119 insertions(+), 10 deletions(-) diff --git a/lib/Cake/Test/Case/View/Helper/FormHelperTest.php b/lib/Cake/Test/Case/View/Helper/FormHelperTest.php index 1051fa66b..cb96bfeee 100644 --- a/lib/Cake/Test/Case/View/Helper/FormHelperTest.php +++ b/lib/Cake/Test/Case/View/Helper/FormHelperTest.php @@ -6552,6 +6552,22 @@ class FormHelperTest extends CakeTestCase { '*/select', ); $this->assertTags($result, $expected); + + $this->Form->request->data['Model']['field'] = '12a'; + $result = $this->Form->month('Model.field'); + $expected = array( + array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')), + array('option' => array('value' => '')), + '/option', + array('option' => array('value' => '01')), + date('F', strtotime('2008-01-01 00:00:00')), + '/option', + array('option' => array('value' => '02')), + date('F', strtotime('2008-02-01 00:00:00')), + '/option', + '*/select', + ); + $this->assertTags($result, $expected); } /** @@ -6662,6 +6678,23 @@ class FormHelperTest extends CakeTestCase { '/select', ); $this->assertTags($result, $expected); + + $this->Form->request->data['Model']['field'] = '12e'; + $result = $this->Form->day('Model.field'); + $expected = array( + array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')), + array('option' => array('value' => '')), + '/option', + array('option' => array('value' => '01')), + '1', + '/option', + array('option' => array('value' => '02')), + '2', + '/option', + $daysRegex, + '/select', + ); + $this->assertTags($result, $expected); } /** @@ -6754,6 +6787,25 @@ class FormHelperTest extends CakeTestCase { '/select', ); $this->assertTags($result, $expected); + + $result = $this->Form->minute('Model.field', array('value' => '#invalid#')); + $expected = array( + array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')), + array('option' => array('value' => '')), + '/option', + array('option' => array('value' => '00')), + '00', + '/option', + array('option' => array('value' => '01')), + '01', + '/option', + array('option' => array('value' => '02')), + '02', + '/option', + $minutesRegex, + '/select', + ); + $this->assertTags($result, $expected); } /** @@ -6852,6 +6904,23 @@ class FormHelperTest extends CakeTestCase { '/select', ); $this->assertTags($result, $expected); + + $this->Form->request->data['Model']['field'] = '18a'; + $result = $this->Form->hour('Model.field', false); + $expected = array( + array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')), + array('option' => array('value' => '')), + '/option', + array('option' => array('value' => '01')), + '1', + '/option', + array('option' => array('value' => '02')), + '2', + '/option', + $hoursRegex, + '/select', + ); + $this->assertTags($result, $expected); } /** @@ -7034,6 +7103,22 @@ class FormHelperTest extends CakeTestCase { $result = $this->Form->year('published', array(), array(), array('empty' => false)); $this->assertContains('data[Contact][published][year]', $result); + + $this->Form->request->data['Contact']['published'] = '2014ee'; + $result = $this->Form->year('Contact.published', 2010, 2011); + $expected = array( + array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')), + array('option' => array('value' => '')), + '/option', + array('option' => array('value' => '2011')), + '2011', + '/option', + array('option' => array('value' => '2010')), + '2010', + '/option', + '/select', + ); + $this->assertTags($result, $expected); } /** diff --git a/lib/Cake/View/Helper/FormHelper.php b/lib/Cake/View/Helper/FormHelper.php index ef9b5767f..8f6ede2f3 100644 --- a/lib/Cake/View/Helper/FormHelper.php +++ b/lib/Cake/View/Helper/FormHelper.php @@ -2116,7 +2116,11 @@ class FormHelper extends AppHelper { $attributes = $this->_dateTimeSelected('day', $fieldName, $attributes); if (strlen($attributes['value']) > 2) { - $attributes['value'] = date_create($attributes['value'])->format('d'); + if (($Date = date_create($attributes['value'])) !== false) { + $attributes['value'] = $Date->format('d'); + } else { + $attributes['value'] = null; + } } elseif ($attributes['value'] === false) { $attributes['value'] = null; } @@ -2163,7 +2167,11 @@ class FormHelper extends AppHelper { } if (strlen($attributes['value']) > 4 || $attributes['value'] === 'now') { - $attributes['value'] = date_create($attributes['value'])->format('Y'); + if (($Date = date_create($attributes['value'])) !== false) { + $attributes['value'] = $Date->format('Y'); + } else { + $attributes['value'] = null; + } } elseif ($attributes['value'] === false) { $attributes['value'] = null; } @@ -2199,7 +2207,11 @@ class FormHelper extends AppHelper { $attributes = $this->_dateTimeSelected('month', $fieldName, $attributes); if (strlen($attributes['value']) > 2) { - $attributes['value'] = date_create($attributes['value'])->format('m'); + if (($Date = date_create($attributes['value'])) !== false) { + $attributes['value'] = $Date->format('m'); + } else { + $attributes['value'] = null; + } } elseif ($attributes['value'] === false) { $attributes['value'] = null; } @@ -2235,11 +2247,15 @@ class FormHelper extends AppHelper { $attributes = $this->_dateTimeSelected('hour', $fieldName, $attributes); if (strlen($attributes['value']) > 2) { - $Date = new DateTime($attributes['value']); - if ($format24Hours) { - $attributes['value'] = $Date->format('H'); - } else { - $attributes['value'] = $Date->format('g'); + try { + $Date = new DateTime($attributes['value']); + if ($format24Hours) { + $attributes['value'] = $Date->format('H'); + } else { + $attributes['value'] = $Date->format('g'); + } + } catch (Exception $e) { + $attributes['value'] = null; } } elseif ($attributes['value'] === false) { $attributes['value'] = null; @@ -2278,7 +2294,11 @@ class FormHelper extends AppHelper { $attributes = $this->_dateTimeSelected('min', $fieldName, $attributes); if (strlen($attributes['value']) > 2) { - $attributes['value'] = date_create($attributes['value'])->format('i'); + if (($Date = date_create($attributes['value'])) !== false) { + $attributes['value'] = $Date->format('i'); + } else { + $attributes['value'] = null; + } } elseif ($attributes['value'] === false) { $attributes['value'] = null; } @@ -2346,7 +2366,11 @@ class FormHelper extends AppHelper { $attributes['value'] = date('a'); } } else { - $attributes['value'] = date_create($attributes['value'])->format('a'); + if (($Date = date_create($attributes['value'])) !== false) { + $attributes['value'] = $Date->format('a'); + } else { + $attributes['value'] = null; + } } } } From cdeb51167b03d685d97ee15b0614f3b07ddab1dc Mon Sep 17 00:00:00 2001 From: jalbertocr Date: Wed, 19 Feb 2014 20:54:00 -0300 Subject: [PATCH 2/5] Fix types in docblocks. --- lib/Cake/View/Helper/FormHelper.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/Cake/View/Helper/FormHelper.php b/lib/Cake/View/Helper/FormHelper.php index 8f6ede2f3..a6af6fbdf 100644 --- a/lib/Cake/View/Helper/FormHelper.php +++ b/lib/Cake/View/Helper/FormHelper.php @@ -2349,7 +2349,7 @@ class FormHelper extends AppHelper { * - `value` The selected value of the input. * * @param string $fieldName Prefix name for the SELECT element - * @param array|string $attributes Array of Attributes + * @param array $attributes Array of Attributes * @return string Completed meridian select input * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#FormHelper::meridian */ @@ -2404,7 +2404,7 @@ class FormHelper extends AppHelper { * @param string $fieldName Prefix name for the SELECT element * @param string $dateFormat DMY, MDY, YMD, or null to not generate date inputs. * @param string $timeFormat 12, 24, or null to not generate time inputs. - * @param array|string $attributes array of Attributes + * @param array $attributes Array of Attributes * @return string Generated set of select boxes for the date and time formats chosen. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#FormHelper::dateTime */ From 8b2e1ab9e58702379ab612ba7774885aac135a31 Mon Sep 17 00:00:00 2001 From: jalbertocr Date: Wed, 19 Feb 2014 22:41:33 -0300 Subject: [PATCH 3/5] Don't do assignments inside conditionals. --- lib/Cake/View/Helper/FormHelper.php | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/lib/Cake/View/Helper/FormHelper.php b/lib/Cake/View/Helper/FormHelper.php index a6af6fbdf..9b22a923a 100644 --- a/lib/Cake/View/Helper/FormHelper.php +++ b/lib/Cake/View/Helper/FormHelper.php @@ -2116,7 +2116,8 @@ class FormHelper extends AppHelper { $attributes = $this->_dateTimeSelected('day', $fieldName, $attributes); if (strlen($attributes['value']) > 2) { - if (($Date = date_create($attributes['value'])) !== false) { + $Date = date_create($attributes['value']); + if ($Date) { $attributes['value'] = $Date->format('d'); } else { $attributes['value'] = null; @@ -2167,7 +2168,8 @@ class FormHelper extends AppHelper { } if (strlen($attributes['value']) > 4 || $attributes['value'] === 'now') { - if (($Date = date_create($attributes['value'])) !== false) { + $Date = date_create($attributes['value']); + if ($Date) { $attributes['value'] = $Date->format('Y'); } else { $attributes['value'] = null; @@ -2207,7 +2209,8 @@ class FormHelper extends AppHelper { $attributes = $this->_dateTimeSelected('month', $fieldName, $attributes); if (strlen($attributes['value']) > 2) { - if (($Date = date_create($attributes['value'])) !== false) { + $Date = date_create($attributes['value']); + if ($Date) { $attributes['value'] = $Date->format('m'); } else { $attributes['value'] = null; @@ -2294,7 +2297,8 @@ class FormHelper extends AppHelper { $attributes = $this->_dateTimeSelected('min', $fieldName, $attributes); if (strlen($attributes['value']) > 2) { - if (($Date = date_create($attributes['value'])) !== false) { + $Date = date_create($attributes['value']); + if ($Date) { $attributes['value'] = $Date->format('i'); } else { $attributes['value'] = null; @@ -2366,7 +2370,8 @@ class FormHelper extends AppHelper { $attributes['value'] = date('a'); } } else { - if (($Date = date_create($attributes['value'])) !== false) { + $Date = date_create($attributes['value']); + if ($Date) { $attributes['value'] = $Date->format('a'); } else { $attributes['value'] = null; From a80cbc205a8164ec7733882cf9a9d942f61e25d2 Mon Sep 17 00:00:00 2001 From: mark_story Date: Wed, 19 Feb 2014 21:46:53 -0500 Subject: [PATCH 4/5] Simplify conditionals. Set default values to remove else cases. --- lib/Cake/View/Helper/FormHelper.php | 51 +++++++++++++---------------- 1 file changed, 23 insertions(+), 28 deletions(-) diff --git a/lib/Cake/View/Helper/FormHelper.php b/lib/Cake/View/Helper/FormHelper.php index 9b22a923a..3abbebc79 100644 --- a/lib/Cake/View/Helper/FormHelper.php +++ b/lib/Cake/View/Helper/FormHelper.php @@ -2116,11 +2116,10 @@ class FormHelper extends AppHelper { $attributes = $this->_dateTimeSelected('day', $fieldName, $attributes); if (strlen($attributes['value']) > 2) { - $Date = date_create($attributes['value']); - if ($Date) { - $attributes['value'] = $Date->format('d'); - } else { - $attributes['value'] = null; + $date = date_create($attributes['value']); + $attributes['value'] = null; + if ($date) { + $attributes['value'] = $date->format('d'); } } elseif ($attributes['value'] === false) { $attributes['value'] = null; @@ -2168,11 +2167,10 @@ class FormHelper extends AppHelper { } if (strlen($attributes['value']) > 4 || $attributes['value'] === 'now') { - $Date = date_create($attributes['value']); - if ($Date) { - $attributes['value'] = $Date->format('Y'); - } else { - $attributes['value'] = null; + $date = date_create($attributes['value']); + $attributes['value'] = null; + if ($date) { + $attributes['value'] = $date->format('Y'); } } elseif ($attributes['value'] === false) { $attributes['value'] = null; @@ -2209,11 +2207,10 @@ class FormHelper extends AppHelper { $attributes = $this->_dateTimeSelected('month', $fieldName, $attributes); if (strlen($attributes['value']) > 2) { - $Date = date_create($attributes['value']); - if ($Date) { - $attributes['value'] = $Date->format('m'); - } else { - $attributes['value'] = null; + $date = date_create($attributes['value']); + $attributes['value'] = null; + if ($date) { + $attributes['value'] = $date->format('m'); } } elseif ($attributes['value'] === false) { $attributes['value'] = null; @@ -2251,11 +2248,11 @@ class FormHelper extends AppHelper { if (strlen($attributes['value']) > 2) { try { - $Date = new DateTime($attributes['value']); + $date = new DateTime($attributes['value']); if ($format24Hours) { - $attributes['value'] = $Date->format('H'); + $attributes['value'] = $date->format('H'); } else { - $attributes['value'] = $Date->format('g'); + $attributes['value'] = $date->format('g'); } } catch (Exception $e) { $attributes['value'] = null; @@ -2297,11 +2294,10 @@ class FormHelper extends AppHelper { $attributes = $this->_dateTimeSelected('min', $fieldName, $attributes); if (strlen($attributes['value']) > 2) { - $Date = date_create($attributes['value']); - if ($Date) { - $attributes['value'] = $Date->format('i'); - } else { - $attributes['value'] = null; + $date = date_create($attributes['value']); + $attributes['value'] = null; + if ($date) { + $attributes['value'] = $date->format('i'); } } elseif ($attributes['value'] === false) { $attributes['value'] = null; @@ -2370,11 +2366,10 @@ class FormHelper extends AppHelper { $attributes['value'] = date('a'); } } else { - $Date = date_create($attributes['value']); - if ($Date) { - $attributes['value'] = $Date->format('a'); - } else { - $attributes['value'] = null; + $date = date_create($attributes['value']); + $attributes['value'] = null; + if ($date) { + $attributes['value'] = $date->format('a'); } } } From 62cb73398830aba684839060e60cd8fb732df767 Mon Sep 17 00:00:00 2001 From: Sam Date: Fri, 21 Feb 2014 00:54:10 +1100 Subject: [PATCH 5/5] One liner. Dont call function parseAccept() twice in RequestHandlerComponent. --- lib/Cake/Controller/Component/RequestHandlerComponent.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Cake/Controller/Component/RequestHandlerComponent.php b/lib/Cake/Controller/Component/RequestHandlerComponent.php index da8c2243f..723606284 100644 --- a/lib/Cake/Controller/Component/RequestHandlerComponent.php +++ b/lib/Cake/Controller/Component/RequestHandlerComponent.php @@ -159,7 +159,7 @@ class RequestHandlerComponent extends Component { return; } - $accepts = $this->response->mapType($this->request->parseAccept()); + $accepts = $this->response->mapType($accept); $preferedTypes = current($accepts); if (array_intersect($preferedTypes, array('html', 'xhtml'))) { return null;