From 9a9ac6f3a7fd112e4d685f3cf507d528ed9cf102 Mon Sep 17 00:00:00 2001 From: euromark Date: Tue, 23 Oct 2012 13:43:48 +0200 Subject: [PATCH 01/15] allow input type=number to also be magic --- lib/Cake/View/Helper/FormHelper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Cake/View/Helper/FormHelper.php b/lib/Cake/View/Helper/FormHelper.php index 301acaaec..1fb8d7a81 100644 --- a/lib/Cake/View/Helper/FormHelper.php +++ b/lib/Cake/View/Helper/FormHelper.php @@ -994,7 +994,7 @@ class FormHelper extends AppHelper { if ( (!isset($options['options']) && in_array($options['type'], $types)) || - (isset($magicType) && $options['type'] == 'text') + (isset($magicType) && in_array($options['type'], array('text', 'number'))) ) { $varName = Inflector::variable( Inflector::pluralize(preg_replace('/_id$/', '', $fieldKey)) From 5064601c6f3b02c4a30c2e989f3fd67d8962f0bf Mon Sep 17 00:00:00 2001 From: euromark Date: Tue, 23 Oct 2012 14:44:09 +0200 Subject: [PATCH 02/15] adding test case --- .../Test/Case/View/Helper/FormHelperTest.php | 23 +++++++++++++++++++ lib/Cake/View/Helper/FormHelper.php | 7 +++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/lib/Cake/Test/Case/View/Helper/FormHelperTest.php b/lib/Cake/Test/Case/View/Helper/FormHelperTest.php index 9da345458..340247be0 100644 --- a/lib/Cake/Test/Case/View/Helper/FormHelperTest.php +++ b/lib/Cake/Test/Case/View/Helper/FormHelperTest.php @@ -2650,6 +2650,29 @@ class FormHelperTest extends CakeTestCase { '/div' ); $this->assertTags($result, $expected); + + $this->View->viewVars['balances'] = array(0 => 'nothing', 1 => 'some', 100 => 'a lot'); + $this->Form->request->data = array('ValidateUser' => array('balance' => 1)); + $result = $this->Form->input('ValidateUser.balance'); + $expected = array( + 'div' => array('class' => 'input select'), + 'label' => array('for' => 'ValidateUserBalance'), + 'Balance', + '/label', + 'select' => array('name' => 'data[ValidateUser][balance]', 'id' => 'ValidateUserBalance'), + array('option' => array('value' => '0')), + 'nothing', + '/option', + array('option' => array('value' => '1', 'selected' => 'selected')), + 'some', + '/option', + array('option' => array('value' => '100')), + 'a lot', + '/option', + '/select', + '/div' + ); + $this->assertTags($result, $expected); } /** diff --git a/lib/Cake/View/Helper/FormHelper.php b/lib/Cake/View/Helper/FormHelper.php index 1fb8d7a81..6d82b2865 100644 --- a/lib/Cake/View/Helper/FormHelper.php +++ b/lib/Cake/View/Helper/FormHelper.php @@ -1006,12 +1006,17 @@ class FormHelper extends AppHelper { } $options['options'] = $varOptions; } + + if ($options['type'] === 'select' && array_key_exists('step', $options)) { + unset($options['step']); + } } $autoLength = ( !array_key_exists('maxlength', $options) && isset($fieldDef['length']) && - is_scalar($fieldDef['length']) + is_scalar($fieldDef['length']) && + $options['type'] !== 'select' ); if ($autoLength && $options['type'] == 'text') { $options['maxlength'] = $fieldDef['length']; From 044cf60b69563ce60e73c60cac7f1361a5b346e4 Mon Sep 17 00:00:00 2001 From: euromark Date: Tue, 23 Oct 2012 16:07:43 +0200 Subject: [PATCH 03/15] move the test case to its own method --- .../Test/Case/View/Helper/FormHelperTest.php | 53 +++++++++++-------- 1 file changed, 30 insertions(+), 23 deletions(-) diff --git a/lib/Cake/Test/Case/View/Helper/FormHelperTest.php b/lib/Cake/Test/Case/View/Helper/FormHelperTest.php index 340247be0..60dfdde24 100644 --- a/lib/Cake/Test/Case/View/Helper/FormHelperTest.php +++ b/lib/Cake/Test/Case/View/Helper/FormHelperTest.php @@ -2614,6 +2614,36 @@ class FormHelperTest extends CakeTestCase { $this->assertTags($result, $expected); } +/** + * Test that magic input() selects are created for type=number + * + * @return void + */ + public function testInputMagicSelectForTypeNumber() { + $this->View->viewVars['balances'] = array(0 => 'nothing', 1 => 'some', 100 => 'a lot'); + $this->Form->request->data = array('ValidateUser' => array('balance' => 1)); + $result = $this->Form->input('ValidateUser.balance'); + $expected = array( + 'div' => array('class' => 'input select'), + 'label' => array('for' => 'ValidateUserBalance'), + 'Balance', + '/label', + 'select' => array('name' => 'data[ValidateUser][balance]', 'id' => 'ValidateUserBalance'), + array('option' => array('value' => '0')), + 'nothing', + '/option', + array('option' => array('value' => '1', 'selected' => 'selected')), + 'some', + '/option', + array('option' => array('value' => '100')), + 'a lot', + '/option', + '/select', + '/div' + ); + $this->assertTags($result, $expected); + } + /** * Test that magic input() selects can easily be converted into radio types without error. * @@ -2650,29 +2680,6 @@ class FormHelperTest extends CakeTestCase { '/div' ); $this->assertTags($result, $expected); - - $this->View->viewVars['balances'] = array(0 => 'nothing', 1 => 'some', 100 => 'a lot'); - $this->Form->request->data = array('ValidateUser' => array('balance' => 1)); - $result = $this->Form->input('ValidateUser.balance'); - $expected = array( - 'div' => array('class' => 'input select'), - 'label' => array('for' => 'ValidateUserBalance'), - 'Balance', - '/label', - 'select' => array('name' => 'data[ValidateUser][balance]', 'id' => 'ValidateUserBalance'), - array('option' => array('value' => '0')), - 'nothing', - '/option', - array('option' => array('value' => '1', 'selected' => 'selected')), - 'some', - '/option', - array('option' => array('value' => '100')), - 'a lot', - '/option', - '/select', - '/div' - ); - $this->assertTags($result, $expected); } /** From 2ac08bf024055dcf0a87ab81c5aee09080e52bf8 Mon Sep 17 00:00:00 2001 From: euromark Date: Tue, 23 Oct 2012 19:04:52 +0200 Subject: [PATCH 04/15] fix whitespace --- lib/Cake/View/Helper/FormHelper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Cake/View/Helper/FormHelper.php b/lib/Cake/View/Helper/FormHelper.php index 6d82b2865..4faff6a92 100644 --- a/lib/Cake/View/Helper/FormHelper.php +++ b/lib/Cake/View/Helper/FormHelper.php @@ -1016,7 +1016,7 @@ class FormHelper extends AppHelper { !array_key_exists('maxlength', $options) && isset($fieldDef['length']) && is_scalar($fieldDef['length']) && - $options['type'] !== 'select' + $options['type'] !== 'select' ); if ($autoLength && $options['type'] == 'text') { $options['maxlength'] = $fieldDef['length']; From 528671f468c9d5d74c11f04fe02b54201ddab156 Mon Sep 17 00:00:00 2001 From: mark_story Date: Thu, 25 Oct 2012 10:07:39 -0400 Subject: [PATCH 05/15] Replace Set with Hash. --- lib/Cake/Network/CakeRequest.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/Cake/Network/CakeRequest.php b/lib/Cake/Network/CakeRequest.php index bee097565..081558597 100644 --- a/lib/Cake/Network/CakeRequest.php +++ b/lib/Cake/Network/CakeRequest.php @@ -15,8 +15,7 @@ * @since CakePHP(tm) v 2.0 * @license MIT License (http://www.opensource.org/licenses/mit-license.php) */ - -App::uses('Set', 'Utility'); +App::uses('Hash', 'Utility'); /** * A class that helps wrap Request information and particulars about a single request. @@ -353,7 +352,7 @@ class CakeRequest implements ArrayAccess { $this->_processFileData($newPath, $fields, $field); } else { $newPath .= '.' . $field; - $this->data = Set::insert($this->data, $newPath, $fields); + $this->data = Hash::insert($this->data, $newPath, $fields); } } } From d269588e78ce0b20e986136a598e548bdfabe3aa Mon Sep 17 00:00:00 2001 From: Ber Clausen Date: Thu, 25 Oct 2012 12:19:53 -0300 Subject: [PATCH 06/15] Avoid duplicating RequestHandler component. --- lib/Cake/Controller/CakeErrorController.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/Cake/Controller/CakeErrorController.php b/lib/Cake/Controller/CakeErrorController.php index 577864881..3bcbd72ce 100644 --- a/lib/Cake/Controller/CakeErrorController.php +++ b/lib/Cake/Controller/CakeErrorController.php @@ -50,7 +50,11 @@ class CakeErrorController extends AppController { */ public function __construct($request = null, $response = null) { parent::__construct($request, $response); - if (count(Router::extensions())) { + if ( + count(Router::extensions()) && + !array_key_exists('RequestHandler', $this->components) && + !in_array('RequestHandler', $this->components, true) + ) { $this->components[] = 'RequestHandler'; } $this->constructClasses(); From c741f60367be5bba21fb96f65ac220106148ceaa Mon Sep 17 00:00:00 2001 From: Ber Clausen Date: Thu, 25 Oct 2012 17:32:32 -0300 Subject: [PATCH 07/15] Make Model::find('first') always return an array. --- lib/Cake/Model/Model.php | 15 +++++++++------ .../Case/Model/Behavior/TranslateBehaviorTest.php | 2 +- lib/Cake/Test/Case/Model/ModelDeleteTest.php | 14 +++++++------- 3 files changed, 17 insertions(+), 14 deletions(-) diff --git a/lib/Cake/Model/Model.php b/lib/Cake/Model/Model.php index a66574b91..9e7cec275 100644 --- a/lib/Cake/Model/Model.php +++ b/lib/Cake/Model/Model.php @@ -2613,9 +2613,12 @@ class Model extends Object implements CakeEventListener { * * Note: find(list) + database views have issues with MySQL 5.0. Try upgrading to MySQL 5.1 if you * have issues with database views. + * + * Note: find(count) has its own return values. + * * @param string $type Type of find operation (all / first / count / neighbors / list / threaded) * @param array $query Option fields (conditions / fields / joins / limit / offset / order / page / group / callbacks) - * @return array Array of records + * @return array Array of records, or Null on failure. * @link http://book.cakephp.org/2.0/en/models/deleting-data.html#deleteall */ public function find($type = 'first', $query = array()) { @@ -2638,10 +2641,10 @@ class Model extends Object implements CakeEventListener { if ($type === 'all') { return $results; - } else { - if ($this->findMethods[$type] === true) { - return $this->{'_find' . ucfirst($type)}('after', $query, $results); - } + } + + if ($this->findMethods[$type] === true) { + return $this->{'_find' . ucfirst($type)}('after', $query, $results); } } @@ -2707,7 +2710,7 @@ class Model extends Object implements CakeEventListener { return $query; } elseif ($state === 'after') { if (empty($results[0])) { - return false; + return array(); } return $results[0]; } diff --git a/lib/Cake/Test/Case/Model/Behavior/TranslateBehaviorTest.php b/lib/Cake/Test/Case/Model/Behavior/TranslateBehaviorTest.php index 9a7ab4342..73d460b92 100644 --- a/lib/Cake/Test/Case/Model/Behavior/TranslateBehaviorTest.php +++ b/lib/Cake/Test/Case/Model/Behavior/TranslateBehaviorTest.php @@ -422,7 +422,7 @@ class TranslateBehaviorTest extends CakeTestCase { $TestModel = new TranslatedItem(); $TestModel->locale = 'rus'; $result = $TestModel->read(null, 1); - $this->assertFalse($result); + $this->assertEquals(array(), $result); $TestModel->locale = array('rus'); $result = $TestModel->read(null, 1); diff --git a/lib/Cake/Test/Case/Model/ModelDeleteTest.php b/lib/Cake/Test/Case/Model/ModelDeleteTest.php index 329eef387..80eef2210 100644 --- a/lib/Cake/Test/Case/Model/ModelDeleteTest.php +++ b/lib/Cake/Test/Case/Model/ModelDeleteTest.php @@ -107,7 +107,7 @@ class ModelDeleteTest extends BaseModelTest { $result = $Portfolio->find('first', array( 'conditions' => array('Portfolio.id' => 1) )); - $this->assertFalse($result); + $this->assertEquals(array(), $result); $result = $Portfolio->ItemsPortfolio->find('all', array( 'conditions' => array('ItemsPortfolio.portfolio_id' => 1) @@ -195,7 +195,7 @@ class ModelDeleteTest extends BaseModelTest { $this->assertTrue($result); $result = $TestModel->read(null, 2); - $this->assertFalse($result); + $this->assertEquals(array(), $result); $TestModel->recursive = -1; $result = $TestModel->find('all', array( @@ -216,7 +216,7 @@ class ModelDeleteTest extends BaseModelTest { $this->assertTrue($result); $result = $TestModel->read(null, 3); - $this->assertFalse($result); + $this->assertEquals(array(), $result); $TestModel->recursive = -1; $result = $TestModel->find('all', array( @@ -448,16 +448,16 @@ class ModelDeleteTest extends BaseModelTest { $TestModel->recursive = 2; $result = $TestModel->read(null, 2); - $this->assertFalse($result); + $this->assertEquals(array(), $result); $result = $TestModel->Comment->read(null, 5); - $this->assertFalse($result); + $this->assertEquals(array(), $result); $result = $TestModel->Comment->read(null, 6); - $this->assertFalse($result); + $this->assertEquals(array(), $result); $result = $TestModel->Comment->Attachment->read(null, 1); - $this->assertFalse($result); + $this->assertEquals(array(), $result); $result = $TestModel->find('count'); $this->assertEquals(2, $result); From c359e4b6890a3cfa979b413f4e1b63c80ccd96cc Mon Sep 17 00:00:00 2001 From: mark_story Date: Thu, 25 Oct 2012 20:46:54 -0400 Subject: [PATCH 08/15] Fix issue with array based values and interval. Fixes #3299 --- lib/Cake/Test/Case/View/Helper/FormHelperTest.php | 9 +++++++++ lib/Cake/View/Helper/FormHelper.php | 3 +++ 2 files changed, 12 insertions(+) diff --git a/lib/Cake/Test/Case/View/Helper/FormHelperTest.php b/lib/Cake/Test/Case/View/Helper/FormHelperTest.php index 59d5e8c46..855c97c60 100644 --- a/lib/Cake/Test/Case/View/Helper/FormHelperTest.php +++ b/lib/Cake/Test/Case/View/Helper/FormHelperTest.php @@ -2235,6 +2235,15 @@ class FormHelperTest extends CakeTestCase { * @return void */ public function testTimeSelectedWithInterval() { + $result = $this->Form->input('Model.start_time', array( + 'type' => 'time', + 'interval' => 15, + 'selected' => array('hour' => '3', 'min' => '57', 'meridian' => 'pm') + )); + $this->assertContains('', $result); + $this->assertContains('', $result); + $this->assertContains('', $result); + $result = $this->Form->input('Model.start_time', array( 'type' => 'time', 'interval' => 15, diff --git a/lib/Cake/View/Helper/FormHelper.php b/lib/Cake/View/Helper/FormHelper.php index 4cf59532d..b5ce4c367 100644 --- a/lib/Cake/View/Helper/FormHelper.php +++ b/lib/Cake/View/Helper/FormHelper.php @@ -2197,6 +2197,9 @@ class FormHelper extends AppHelper { if (!empty($attributes['value'])) { if (is_array($attributes['value'])) { extract($attributes['value']); + if ($meridian === 'pm') { + $hour += 12; + } } else { if (is_numeric($attributes['value'])) { $attributes['value'] = strftime('%Y-%m-%d %H:%M:%S', $attributes['value']); From 50a4b792be7622abc2dfb57d5938925f19ec6e9e Mon Sep 17 00:00:00 2001 From: mark_story Date: Thu, 25 Oct 2012 21:28:01 -0400 Subject: [PATCH 09/15] Clean up code in FormHelper::dateTime() Remove $$ variables and split a huge method into smaller pieces. --- lib/Cake/View/Helper/FormHelper.php | 197 +++++++++++++++------------- 1 file changed, 105 insertions(+), 92 deletions(-) diff --git a/lib/Cake/View/Helper/FormHelper.php b/lib/Cake/View/Helper/FormHelper.php index b5ce4c367..6926991b3 100644 --- a/lib/Cake/View/Helper/FormHelper.php +++ b/lib/Cake/View/Helper/FormHelper.php @@ -2195,50 +2195,12 @@ class FormHelper extends AppHelper { } if (!empty($attributes['value'])) { - if (is_array($attributes['value'])) { - extract($attributes['value']); - if ($meridian === 'pm') { - $hour += 12; - } - } else { - if (is_numeric($attributes['value'])) { - $attributes['value'] = strftime('%Y-%m-%d %H:%M:%S', $attributes['value']); - } - $meridian = 'am'; - $pos = strpos($attributes['value'], '-'); - if ($pos !== false) { - $date = explode('-', $attributes['value']); - $days = explode(' ', $date[2]); - $day = $days[0]; - $month = $date[1]; - $year = $date[0]; - } else { - $days[1] = $attributes['value']; - } - - if (!empty($timeFormat)) { - $time = explode(':', $days[1]); - - if ($time[0] >= '12' && $timeFormat == '12') { - $meridian = 'pm'; - } elseif ($time[0] == '00' && $timeFormat == '12') { - $time[0] = 12; - } elseif ($time[0] >= 12) { - $meridian = 'pm'; - } - if ($time[0] == 0 && $timeFormat == '12') { - $time[0] = 12; - } - $hour = $min = null; - if (isset($time[1])) { - $hour = $time[0]; - $min = $time[1]; - } - } - } + list($year, $month, $day, $hour, $min, $meridian) = $this->_getDateTimeValue( + $attributes['value'], + $timeFormat + ); } - $elements = array('Day', 'Month', 'Year', 'Hour', 'Minute', 'Meridian'); $defaults = array( 'minYear' => null, 'maxYear' => null, 'separator' => '-', 'interval' => 1, 'monthNames' => true @@ -2269,42 +2231,42 @@ class FormHelper extends AppHelper { list($year, $month, $day, $hour, $min, $meridian) = $newTime; } - if (isset($attributes['id'])) { - if (is_string($attributes['id'])) { - // build out an array version - foreach ($elements as $element) { - $selectAttrName = 'select' . $element . 'Attr'; - ${$selectAttrName} = $attributes; - ${$selectAttrName}['id'] = $attributes['id'] . $element; - } - } elseif (is_array($attributes['id'])) { - // check for missing ones and build selectAttr for each element - $attributes['id'] += array( - 'month' => '', 'year' => '', 'day' => '', - 'hour' => '', 'minute' => '', 'meridian' => '' - ); - foreach ($elements as $element) { - $selectAttrName = 'select' . $element . 'Attr'; - ${$selectAttrName} = $attributes; - ${$selectAttrName}['id'] = $attributes['id'][strtolower($element)]; - } + $keys = array('Day', 'Month', 'Year', 'Hour', 'Minute', 'Meridian'); + $attrs = array_fill_keys($keys, $attributes); + + $hasId = isset($attributes['id']); + if ($hasId && is_array($attributes['id'])) { + // check for missing ones and build selectAttr for each element + $attributes['id'] += array( + 'month' => '', + 'year' => '', + 'day' => '', + 'hour' => '', + 'minute' => '', + 'meridian' => '' + ); + foreach ($keys as $key) { + $attrs[$key]['id'] = $attributes['id'][strtolower($key)]; } - } else { - // build the selectAttrName with empty id's to pass - foreach ($elements as $element) { - $selectAttrName = 'select' . $element . 'Attr'; - ${$selectAttrName} = $attributes; + } + if ($hasId && is_string($attributes['id'])) { + // build out an array version + foreach ($keys as $key) { + $attrs[$key]['id'] = $attributes['id'] . $key; } } if (is_array($attributes['empty'])) { $attributes['empty'] += array( - 'month' => true, 'year' => true, 'day' => true, - 'hour' => true, 'minute' => true, 'meridian' => true + 'month' => true, + 'year' => true, + 'day' => true, + 'hour' => true, + 'minute' => true, + 'meridian' => true ); - foreach ($elements as $element) { - $selectAttrName = 'select' . $element . 'Attr'; - ${$selectAttrName}['empty'] = $attributes['empty'][strtolower($element)]; + foreach ($keys as $key) { + $attrs[$key]['empty'] = $attributes['empty'][strtolower($key)]; } } @@ -2312,47 +2274,98 @@ class FormHelper extends AppHelper { foreach (preg_split('//', $dateFormat, -1, PREG_SPLIT_NO_EMPTY) as $char) { switch ($char) { case 'Y': - $selectYearAttr['value'] = $year; + $attrs['Year']['value'] = $year; $selects[] = $this->year( - $fieldName, $minYear, $maxYear, $selectYearAttr + $fieldName, $minYear, $maxYear, $attrs['Year'] ); break; case 'M': - $selectMonthAttr['value'] = $month; - $selectMonthAttr['monthNames'] = $monthNames; - $selects[] = $this->month($fieldName, $selectMonthAttr); + $attrs['Month']['value'] = $month; + $attrs['Month']['monthNames'] = $monthNames; + $selects[] = $this->month($fieldName, $attrs['Month']); break; case 'D': - $selectDayAttr['value'] = $day; - $selects[] = $this->day($fieldName, $selectDayAttr); + $attrs['Day']['value'] = $day; + $selects[] = $this->day($fieldName, $attrs['Day']); break; } } $opt = implode($separator, $selects); - $selectMinuteAttr['interval'] = $interval; + $attrs['Minute']['interval'] = $interval; switch ($timeFormat) { case '24': - $selectHourAttr['value'] = $hour; - $selectMinuteAttr['value'] = $min; - $opt .= $this->hour($fieldName, true, $selectHourAttr) . ':' . - $this->minute($fieldName, $selectMinuteAttr); + $attrs['Hour']['value'] = $hour; + $attrs['Minute']['value'] = $min; + $opt .= $this->hour($fieldName, true, $attrs['Hour']) . ':' . + $this->minute($fieldName, $attrs['Minute']); break; case '12': - $selectHourAttr['value'] = $hour; - $selectMinuteAttr['value'] = $min; - $selectMeridianAttr['value'] = $meridian; - $opt .= $this->hour($fieldName, false, $selectHourAttr) . ':' . - $this->minute($fieldName, $selectMinuteAttr) . ' ' . - $this->meridian($fieldName, $selectMeridianAttr); - break; - default: - $opt .= ''; + $attrs['Hour']['value'] = $hour; + $attrs['Minute']['value'] = $min; + $attrs['Meridian']['value'] = $meridian; + $opt .= $this->hour($fieldName, false, $attrs['Hour']) . ':' . + $this->minute($fieldName, $attrs['Minute']) . ' ' . + $this->meridian($fieldName, $attrs['Meridian']); break; } return $opt; } +/** + * Parse the value for a datetime selected value + * + * @param string|array $value The selected value. + * @param integer $timeFormat The time format + * @return array Array of selected value. + */ + protected function _getDateTimeValue($value, $timeFormat) { + $year = $month = $day = $hour = $min = $meridian = null; + if (is_array($value)) { + extract($value); + if ($meridian === 'pm') { + $hour += 12; + } + return array($year, $month, $day, $hour, $min, $meridian); + } + + if (is_numeric($value)) { + $value = strftime('%Y-%m-%d %H:%M:%S', $value); + } + $meridian = 'am'; + $pos = strpos($value, '-'); + if ($pos !== false) { + $date = explode('-', $value); + $days = explode(' ', $date[2]); + $day = $days[0]; + $month = $date[1]; + $year = $date[0]; + } else { + $days[1] = $value; + } + + if (!empty($timeFormat)) { + $time = explode(':', $days[1]); + + if ($time[0] >= '12' && $timeFormat == '12') { + $meridian = 'pm'; + } elseif ($time[0] == '00' && $timeFormat == '12') { + $time[0] = 12; + } elseif ($time[0] >= 12) { + $meridian = 'pm'; + } + if ($time[0] == 0 && $timeFormat == '12') { + $time[0] = 12; + } + $hour = $min = null; + if (isset($time[1])) { + $hour = $time[0]; + $min = $time[1]; + } + } + return array($year, $month, $day, $hour, $min, $meridian); + } + /** * Gets the input field name for the current tag * From 559130b87b508ef3575492c4b4168a4669f80d7e Mon Sep 17 00:00:00 2001 From: mark_story Date: Thu, 25 Oct 2012 21:56:00 -0400 Subject: [PATCH 10/15] Try and make test less likely to fail. --- lib/Cake/Test/Case/Cache/Engine/MemcacheEngineTest.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/Cake/Test/Case/Cache/Engine/MemcacheEngineTest.php b/lib/Cake/Test/Case/Cache/Engine/MemcacheEngineTest.php index 88a7b6143..54c225c72 100644 --- a/lib/Cake/Test/Case/Cache/Engine/MemcacheEngineTest.php +++ b/lib/Cake/Test/Case/Cache/Engine/MemcacheEngineTest.php @@ -229,12 +229,11 @@ class MemcacheEngineTest extends CakeTestCase { $result = Cache::write('other_test', $data, 'memcache'); $this->assertTrue($result); - sleep(2); + sleep(3); $result = Cache::read('other_test', 'memcache'); $this->assertFalse($result); Cache::config('memcache', array('duration' => '+1 second')); - sleep(2); $result = Cache::read('other_test', 'memcache'); $this->assertFalse($result); From 0ddd130833f0b95a979234eea32bd4a711ac44d3 Mon Sep 17 00:00:00 2001 From: ADmad Date: Fri, 26 Oct 2012 12:58:24 +0530 Subject: [PATCH 11/15] Improved "required" field detection. Closes #3305. --- .../Test/Case/View/Helper/FormHelperTest.php | 19 +++++++++++++++++++ lib/Cake/View/Helper/FormHelper.php | 6 +++--- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/lib/Cake/Test/Case/View/Helper/FormHelperTest.php b/lib/Cake/Test/Case/View/Helper/FormHelperTest.php index 855c97c60..89b37c724 100644 --- a/lib/Cake/Test/Case/View/Helper/FormHelperTest.php +++ b/lib/Cake/Test/Case/View/Helper/FormHelperTest.php @@ -119,6 +119,11 @@ class Contact extends CakeTestModel { 'between' => array('rule' => array('between', 5, 30), 'allowEmpty' => true), ), 'imnotrequiredeither' => array('required' => true, 'rule' => array('between', 5, 30), 'allowEmpty' => true), + 'iamrequiredalways' => array( + 'email' => array('rule' => 'email'), + 'rule_on_create' => array('rule' => array('maxLength', 50), 'on' => 'create'), + 'rule_on_update' => array('rule' => array('between', 1, 50), 'on' => 'update'), + ), ); /** @@ -7068,6 +7073,20 @@ class FormHelperTest extends CakeTestCase { '/div' ); $this->assertTags($result, $expected); + + $result = $this->Form->input('Contact.iamrequiredalways'); + $expected = array( + 'div' => array('class' => 'input text required'), + 'label' => array('for' => 'ContactIamrequiredalways'), + 'Iamrequiredalways', + '/label', + 'input' => array( + 'type' => 'text', 'name' => 'data[Contact][iamrequiredalways]', + 'id' => 'ContactIamrequiredalways' + ), + '/div' + ); + $this->assertTags($result, $expected); } /** diff --git a/lib/Cake/View/Helper/FormHelper.php b/lib/Cake/View/Helper/FormHelper.php index 6926991b3..24f586801 100644 --- a/lib/Cake/View/Helper/FormHelper.php +++ b/lib/Cake/View/Helper/FormHelper.php @@ -249,11 +249,11 @@ class FormHelper extends AppHelper { } foreach ($validationRules as $rule) { $rule->isUpdate($this->requestType === 'put'); - if ($rule->isEmptyAllowed()) { - return false; + if (!$rule->isEmptyAllowed()) { + return true; } } - return true; + return false; } /** From 39dcb80b451dec37e44750482a0d207109dcac11 Mon Sep 17 00:00:00 2001 From: Ceeram Date: Fri, 26 Oct 2012 16:32:15 +0200 Subject: [PATCH 12/15] Fix failing test, find('first') now returns empty array when no record was found --- lib/Cake/Test/Case/Console/Command/AclShellTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Cake/Test/Case/Console/Command/AclShellTest.php b/lib/Cake/Test/Case/Console/Command/AclShellTest.php index 7dfbbc061..eaafdcf10 100644 --- a/lib/Cake/Test/Case/Console/Command/AclShellTest.php +++ b/lib/Cake/Test/Case/Console/Command/AclShellTest.php @@ -177,7 +177,7 @@ class AclShellTest extends CakeTestCase { $Aro = ClassRegistry::init('Aro'); $result = $Aro->findById(3); - $this->assertFalse($result); + $this->assertEmpty($result); } /** From 870d77c89e3462ac01d765b73b62bc8c52830646 Mon Sep 17 00:00:00 2001 From: Ceeram Date: Fri, 26 Oct 2012 20:49:04 +0200 Subject: [PATCH 13/15] Make the test more accurate on type of result --- lib/Cake/Test/Case/Console/Command/AclShellTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Cake/Test/Case/Console/Command/AclShellTest.php b/lib/Cake/Test/Case/Console/Command/AclShellTest.php index eaafdcf10..cf98cabf5 100644 --- a/lib/Cake/Test/Case/Console/Command/AclShellTest.php +++ b/lib/Cake/Test/Case/Console/Command/AclShellTest.php @@ -177,7 +177,7 @@ class AclShellTest extends CakeTestCase { $Aro = ClassRegistry::init('Aro'); $result = $Aro->findById(3); - $this->assertEmpty($result); + $this->assertSame(array(), $result); } /** From a7d9422c099694031503c2772c5e2cf3c4d39385 Mon Sep 17 00:00:00 2001 From: Ber Clausen Date: Fri, 26 Oct 2012 19:18:05 -0300 Subject: [PATCH 14/15] Test all empty array with assertSame() because assertEquals() does not check the type. --- lib/Cake/Test/Case/BasicsTest.php | 2 +- .../Console/Command/Task/ControllerTaskTest.php | 4 ++-- lib/Cake/Test/Case/Core/AppTest.php | 6 +++--- lib/Cake/Test/Case/Log/CakeLogTest.php | 2 +- .../Model/Behavior/TranslateBehaviorTest.php | 6 +++--- .../Model/Datasource/Database/SqlserverTest.php | 2 +- lib/Cake/Test/Case/Model/ModelDeleteTest.php | 16 ++++++++-------- lib/Cake/Test/Case/Model/ModelReadTest.php | 2 +- lib/Cake/Test/Case/Model/ModelWriteTest.php | 4 ++-- lib/Cake/Test/Case/Routing/RouterTest.php | 6 +++--- lib/Cake/Test/Case/Utility/SetTest.php | 2 +- lib/Cake/Test/Case/View/Helper/JsHelperTest.php | 2 +- 12 files changed, 27 insertions(+), 27 deletions(-) diff --git a/lib/Cake/Test/Case/BasicsTest.php b/lib/Cake/Test/Case/BasicsTest.php index 0c12fed6f..0998dfa77 100644 --- a/lib/Cake/Test/Case/BasicsTest.php +++ b/lib/Cake/Test/Case/BasicsTest.php @@ -67,7 +67,7 @@ class BasicsTest extends CakeTestCase { $one = array('minYear' => null, 'maxYear' => null, 'separator' => '-', 'interval' => 1, 'monthNames' => true); $two = array('minYear' => null, 'maxYear' => null, 'separator' => '-', 'interval' => 1, 'monthNames' => true); $result = array_diff_key($one, $two); - $this->assertEquals(array(), $result); + $this->assertSame(array(), $result); } /** diff --git a/lib/Cake/Test/Case/Console/Command/Task/ControllerTaskTest.php b/lib/Cake/Test/Case/Console/Command/Task/ControllerTaskTest.php index a17d2bb9d..4d11cbe60 100644 --- a/lib/Cake/Test/Case/Console/Command/Task/ControllerTaskTest.php +++ b/lib/Cake/Test/Case/Console/Command/Task/ControllerTaskTest.php @@ -181,7 +181,7 @@ class ControllerTaskTest extends CakeTestCase { public function testDoHelpersNo() { $this->Task->expects($this->any())->method('in')->will($this->returnValue('n')); $result = $this->Task->doHelpers(); - $this->assertEquals(array(), $result); + $this->assertSame(array(), $result); } /** @@ -218,7 +218,7 @@ class ControllerTaskTest extends CakeTestCase { public function testDoComponentsNo() { $this->Task->expects($this->any())->method('in')->will($this->returnValue('n')); $result = $this->Task->doComponents(); - $this->assertEquals(array(), $result); + $this->assertSame(array(), $result); } /** diff --git a/lib/Cake/Test/Case/Core/AppTest.php b/lib/Cake/Test/Case/Core/AppTest.php index 3b6cef55e..0c66766cd 100644 --- a/lib/Cake/Test/Case/Core/AppTest.php +++ b/lib/Cake/Test/Case/Core/AppTest.php @@ -347,7 +347,7 @@ class AppTest extends CakeTestCase { $this->assertEquals($expected, $result); $result = App::objects('NonExistingType'); - $this->assertEquals(array(), $result); + $this->assertSame(array(), $result); App::build(array( 'plugins' => array( @@ -414,9 +414,9 @@ class AppTest extends CakeTestCase { $this->assertTrue(in_array('OtherComponent', $result)); $result = App::objects('TestPluginTwo.behavior'); - $this->assertEquals(array(), $result); + $this->assertSame(array(), $result); $result = App::objects('TestPluginTwo.Model/Behavior'); - $this->assertEquals(array(), $result); + $this->assertSame(array(), $result); $result = App::objects('model', null, false); $this->assertTrue(in_array('Comment', $result)); diff --git a/lib/Cake/Test/Case/Log/CakeLogTest.php b/lib/Cake/Test/Case/Log/CakeLogTest.php index e54f2c71a..2caaac0c6 100644 --- a/lib/Cake/Test/Case/Log/CakeLogTest.php +++ b/lib/Cake/Test/Case/Log/CakeLogTest.php @@ -175,7 +175,7 @@ class CakeLogTest extends CakeTestCase { CakeLog::drop('file'); $result = CakeLog::configured(); - $this->assertEquals(array(), $result); + $this->assertSame(array(), $result); } /** diff --git a/lib/Cake/Test/Case/Model/Behavior/TranslateBehaviorTest.php b/lib/Cake/Test/Case/Model/Behavior/TranslateBehaviorTest.php index 73d460b92..a73ccd17c 100644 --- a/lib/Cake/Test/Case/Model/Behavior/TranslateBehaviorTest.php +++ b/lib/Cake/Test/Case/Model/Behavior/TranslateBehaviorTest.php @@ -422,7 +422,7 @@ class TranslateBehaviorTest extends CakeTestCase { $TestModel = new TranslatedItem(); $TestModel->locale = 'rus'; $result = $TestModel->read(null, 1); - $this->assertEquals(array(), $result); + $this->assertSame(array(), $result); $TestModel->locale = array('rus'); $result = $TestModel->read(null, 1); @@ -460,10 +460,10 @@ class TranslateBehaviorTest extends CakeTestCase { Configure::write('debug', 0); $result = $TestModel->find('list', array('recursive' => 1, 'callbacks' => false)); - $this->assertEquals(array(), $result); + $this->assertSame(array(), $result); $result = $TestModel->find('list', array('recursive' => 1, 'callbacks' => 'after')); - $this->assertEquals(array(), $result); + $this->assertSame(array(), $result); Configure::write('debug', $debug); } diff --git a/lib/Cake/Test/Case/Model/Datasource/Database/SqlserverTest.php b/lib/Cake/Test/Case/Model/Datasource/Database/SqlserverTest.php index e31595bf3..ee7695a5f 100644 --- a/lib/Cake/Test/Case/Model/Datasource/Database/SqlserverTest.php +++ b/lib/Cake/Test/Case/Model/Datasource/Database/SqlserverTest.php @@ -574,7 +574,7 @@ class SqlserverTest extends CakeTestCase { $indexes = array('client_id' => array('column' => 'client_id')); $result = $this->db->buildIndex($indexes, 'items'); - $this->assertEquals(array(), $result); + $this->assertSame(array(), $result); $indexes = array('client_id' => array('column' => array('client_id', 'period_id'), 'unique' => 1)); $result = $this->db->buildIndex($indexes, 'items'); diff --git a/lib/Cake/Test/Case/Model/ModelDeleteTest.php b/lib/Cake/Test/Case/Model/ModelDeleteTest.php index 80eef2210..7a69585c6 100644 --- a/lib/Cake/Test/Case/Model/ModelDeleteTest.php +++ b/lib/Cake/Test/Case/Model/ModelDeleteTest.php @@ -107,12 +107,12 @@ class ModelDeleteTest extends BaseModelTest { $result = $Portfolio->find('first', array( 'conditions' => array('Portfolio.id' => 1) )); - $this->assertEquals(array(), $result); + $this->assertSame(array(), $result); $result = $Portfolio->ItemsPortfolio->find('all', array( 'conditions' => array('ItemsPortfolio.portfolio_id' => 1) )); - $this->assertEquals(array(), $result); + $this->assertSame(array(), $result); } /** @@ -195,7 +195,7 @@ class ModelDeleteTest extends BaseModelTest { $this->assertTrue($result); $result = $TestModel->read(null, 2); - $this->assertEquals(array(), $result); + $this->assertSame(array(), $result); $TestModel->recursive = -1; $result = $TestModel->find('all', array( @@ -216,7 +216,7 @@ class ModelDeleteTest extends BaseModelTest { $this->assertTrue($result); $result = $TestModel->read(null, 3); - $this->assertEquals(array(), $result); + $this->assertSame(array(), $result); $TestModel->recursive = -1; $result = $TestModel->find('all', array( @@ -448,16 +448,16 @@ class ModelDeleteTest extends BaseModelTest { $TestModel->recursive = 2; $result = $TestModel->read(null, 2); - $this->assertEquals(array(), $result); + $this->assertSame(array(), $result); $result = $TestModel->Comment->read(null, 5); - $this->assertEquals(array(), $result); + $this->assertSame(array(), $result); $result = $TestModel->Comment->read(null, 6); - $this->assertEquals(array(), $result); + $this->assertSame(array(), $result); $result = $TestModel->Comment->Attachment->read(null, 1); - $this->assertEquals(array(), $result); + $this->assertSame(array(), $result); $result = $TestModel->find('count'); $this->assertEquals(2, $result); diff --git a/lib/Cake/Test/Case/Model/ModelReadTest.php b/lib/Cake/Test/Case/Model/ModelReadTest.php index 6f14e47a2..c416bba3f 100644 --- a/lib/Cake/Test/Case/Model/ModelReadTest.php +++ b/lib/Cake/Test/Case/Model/ModelReadTest.php @@ -4290,7 +4290,7 @@ class ModelReadTest extends BaseModelTest { $TestModel->resetAssociations(); $result = $TestModel->hasMany; - $this->assertEquals(array(), $result); + $this->assertSame(array(), $result); $result = $TestModel->bindModel(array('hasMany' => array('Comment')), false); $this->assertTrue($result); diff --git a/lib/Cake/Test/Case/Model/ModelWriteTest.php b/lib/Cake/Test/Case/Model/ModelWriteTest.php index 7b4e5a07c..1a015ef42 100644 --- a/lib/Cake/Test/Case/Model/ModelWriteTest.php +++ b/lib/Cake/Test/Case/Model/ModelWriteTest.php @@ -4536,7 +4536,7 @@ class ModelWriteTest extends BaseModelTest { $this->assertFalse($result); $result = $model->find('all'); - $this->assertEquals(array(), $result); + $this->assertSame(array(), $result); $expected = array('Comment' => array( 1 => array('comment' => array('This field cannot be left blank')) )); @@ -5940,7 +5940,7 @@ class ModelWriteTest extends BaseModelTest { $this->assertFalse($result); $result = $model->find('all'); - $this->assertEquals(array(), $result); + $this->assertSame(array(), $result); $expected = array('Comment' => array( 1 => array('comment' => array('This field cannot be left blank')) )); diff --git a/lib/Cake/Test/Case/Routing/RouterTest.php b/lib/Cake/Test/Case/Routing/RouterTest.php index b4c956de0..65e1ed7a9 100644 --- a/lib/Cake/Test/Case/Routing/RouterTest.php +++ b/lib/Cake/Test/Case/Routing/RouterTest.php @@ -111,7 +111,7 @@ class RouterTest extends CakeTestCase { $_SERVER['REQUEST_METHOD'] = 'GET'; $result = Router::parse('/posts/add'); - $this->assertEquals(array(), $result); + $this->assertSame(array(), $result); Router::reload(); $resources = Router::mapResources('Posts', array('id' => '[a-z0-9_]+')); @@ -1907,7 +1907,7 @@ class RouterTest extends CakeTestCase { $this->assertEquals($expected, $result); $result = Router::parse('/blog/foobar'); - $this->assertEquals(array(), $result); + $this->assertSame(array(), $result); $result = Router::url(array('controller' => 'blog_posts', 'action' => 'foo')); $this->assertEquals('/blog_posts/foo', $result); @@ -2117,7 +2117,7 @@ class RouterTest extends CakeTestCase { $this->assertEquals($expected, $result); $result = Router::parse('/badness/test/test_action'); - $this->assertEquals(array(), $result); + $this->assertSame(array(), $result); Router::reload(); Router::connect('/:locale/:controller/:action/*', array(), array('locale' => 'dan|eng')); diff --git a/lib/Cake/Test/Case/Utility/SetTest.php b/lib/Cake/Test/Case/Utility/SetTest.php index e1c18a8a5..00d363dba 100644 --- a/lib/Cake/Test/Case/Utility/SetTest.php +++ b/lib/Cake/Test/Case/Utility/SetTest.php @@ -1967,7 +1967,7 @@ class SetTest extends CakeTestCase { $this->assertEquals($expected, $result); $result = Set::combine($a, 'fail', 'fail'); - $this->assertEquals(array(), $result); + $this->assertSame(array(), $result); } /** diff --git a/lib/Cake/Test/Case/View/Helper/JsHelperTest.php b/lib/Cake/Test/Case/View/Helper/JsHelperTest.php index 950e5c221..47833e085 100644 --- a/lib/Cake/Test/Case/View/Helper/JsHelperTest.php +++ b/lib/Cake/Test/Case/View/Helper/JsHelperTest.php @@ -904,7 +904,7 @@ class JsBaseEngineTest extends CakeTestCase { public function testOptionMapping() { $JsEngine = new OptionEngineHelper($this->View); $result = $JsEngine->testMap(); - $this->assertEquals(array(), $result); + $this->assertSame(array(), $result); $result = $JsEngine->testMap(array('foo' => 'bar', 'baz' => 'sho')); $this->assertEquals(array('foo' => 'bar', 'baz' => 'sho'), $result); From e8f727fe688855369101a4d5fffcdd7458e0473a Mon Sep 17 00:00:00 2001 From: mark_story Date: Sat, 27 Oct 2012 21:12:34 -0400 Subject: [PATCH 15/15] Using fixed points in time. This avoids lulz when the United Kingdom changes change to/from DST at a different time than your local timezone. --- lib/Cake/Test/Case/Utility/CakeTimeTest.php | 23 +++++++++++---------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/lib/Cake/Test/Case/Utility/CakeTimeTest.php b/lib/Cake/Test/Case/Utility/CakeTimeTest.php index 7185ef00f..629446414 100644 --- a/lib/Cake/Test/Case/Utility/CakeTimeTest.php +++ b/lib/Cake/Test/Case/Utility/CakeTimeTest.php @@ -474,7 +474,7 @@ class CakeTimeTest extends CakeTestCase { date_default_timezone_set('UTC'); - $serverTime = new DateTime('now'); + $serverTime = new DateTime('2012-12-11 14:15:20'); $timezones = array('Europe/London', 'Europe/Brussels', 'UTC', 'America/Denver', 'America/Caracas', 'Asia/Kathmandu'); foreach ($timezones as $timezone) { @@ -509,17 +509,18 @@ class CakeTimeTest extends CakeTestCase { * @return void */ public function testToRss() { - $this->assertEquals(date('r'), $this->Time->toRss(time())); + $date = '2012-08-12 12:12:45'; + $time = strtotime($date); + $this->assertEquals(date('r', $time), $this->Time->toRss($time)); - if (!$this->skipIf(!class_exists('DateTimeZone'), '%s DateTimeZone class not available.')) { - $timezones = array('Europe/London', 'Europe/Brussels', 'UTC', 'America/Denver', 'America/Caracas', 'Asia/Kathmandu'); - foreach ($timezones as $timezone) { - $yourTimezone = new DateTimeZone($timezone); - $yourTime = new DateTime('now', $yourTimezone); - $userOffset = $yourTimezone->getOffset($yourTime) / HOUR; - $this->assertEquals($yourTime->format('r'), $this->Time->toRss(time(), $userOffset)); - $this->assertEquals($yourTime->format('r'), $this->Time->toRss(time(), $timezone)); - } + $timezones = array('Europe/London', 'Europe/Brussels', 'UTC', 'America/Denver', 'America/Caracas', 'Asia/Kathmandu'); + foreach ($timezones as $timezone) { + $yourTimezone = new DateTimeZone($timezone); + $yourTime = new DateTime($date, $yourTimezone); + $userOffset = $yourTimezone->getOffset($yourTime) / HOUR; + $time = $yourTime->getTimestamp(); + $this->assertEquals($yourTime->format('r'), $this->Time->toRss($time, $userOffset), "Failed on $timezone"); + $this->assertEquals($yourTime->format('r'), $this->Time->toRss($time, $timezone), "Failed on $timezone"); } }