From 780fe16c098d52ca94528d4abfb839bec84ec2c8 Mon Sep 17 00:00:00 2001 From: gwoo Date: Wed, 22 Aug 2007 01:31:34 +0000 Subject: [PATCH] refactoring cleanUpFields with tests for #3101 git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@5568 3807eeeb-6ff5-0310-8944-8be069107fe0 --- cake/libs/controller/controller.php | 84 ++++++++++++------- .../cases/libs/controller/controller.test.php | 57 ++++++++++++- 2 files changed, 108 insertions(+), 33 deletions(-) diff --git a/cake/libs/controller/controller.php b/cake/libs/controller/controller.php index 7078c65a7..9f49eca87 100644 --- a/cake/libs/controller/controller.php +++ b/cake/libs/controller/controller.php @@ -941,40 +941,62 @@ class Controller extends Object { if ($modelClass == null) { $modelClass = $this->modelClass; } - foreach ($this->{$modelClass}->_tableInfo->value as $field) { - $useNewDate = false; - $dateFields = array('Y'=>'_year', 'm'=>'_month', 'd'=>'_day', 'H'=>'_hour', 'i'=>'_min', 's'=>'_sec'); - foreach ($dateFields as $default => $var) { - if (isset($this->data[$modelClass][$field['name'] . $var])) { - ${$var} = $this->data[$modelClass][$field['name'] . $var]; - unset($this->data[$modelClass][$field['name'] . $var]); - $useNewDate = true; - } else { - if ($var == 'year') { - ${$var} = '0000'; - } else { - ${$var} = '00'; + $fields = $this->{$modelClass}->schema(); + foreach ($fields->value as $field => $value) { + if (in_array($value['type'], array('datetime', 'timestamp', 'date', 'time'))) { + $useNewDate = false; + $date = array(); + $dates = array('Y'=>'_year', 'm'=>'_month', 'd'=>'_day'); + foreach ($dates as $default => $var) { + if (isset($this->data[$modelClass][$field . $var])) { + if (!empty($this->data[$modelClass][$field . $var])) { + $date[$var] = $this->data[$modelClass][$field . $var]; + } + $useNewDate = true; + unset($this->data[$modelClass][$field . $var]); } } - } - if ($_hour != 12 && (isset($this->data[$modelClass][$field['name'] . '_meridian']) && 'pm' == $this->data[$modelClass][$field['name'] . '_meridian'])) { - $_hour = $_hour + 12; - } - if ($_hour == 12 && (isset($this->data[$modelClass][$field['name'] . '_meridian']) && 'am' == $this->data[$modelClass][$field['name'] . '_meridian'])) { - $_hour = '00'; - } - unset($this->data[$modelClass][$field['name'] . '_meridian']); + if (count($date) == 3 && in_array($value['type'], array('datetime', 'timestamp', 'date'))) { + $date = join('-', array_values($date)); + } else { + $date = null; + } - $newDate = null; - if (in_array($field['type'], array('datetime', 'timestamp')) && $useNewDate) { - $newDate = "{$_year}-{$_month}-{$_day} {$_hour}:{$_min}:{$_sec}"; - } elseif ('date' == $field['type'] && $useNewDate) { - $newDate = "{$_year}-{$_month}-{$_day}"; - } elseif ('time' == $field['type'] && $useNewDate) { - $newDate = "{$_hour}:{$_min}:{$_sec}"; - } - if ($newDate && !in_array($field['name'], array('created', 'updated', 'modified'))) { - $this->data[$modelClass][$field['name']] = $newDate; + if ($value['type'] != 'date') { + $time = array(); + $times = array('H'=>'_hour', 'i'=>'_min', 's'=>'_sec'); + foreach($times as $default => $var) { + if (isset($this->data[$modelClass][$field . $var])) { + if (!empty($this->data[$modelClass][$field . $var])) { + $time[$var] = $this->data[$modelClass][$field . $var]; + } + $useNewDate = true; + unset($this->data[$modelClass][$field . $var]); + } + } + + $meridian = false; + if (isset($this->data[$modelClass][$field . '_meridian'])) { + $meridian = $this->data[$modelClass][$field . '_meridian']; + unset($this->data[$modelClass][$field . '_meridian']); + } + + if (isset($time['_hour']) && $time['_hour'] != 12 && 'pm' == $meridian) { + $time['_hour'] = $_hour + 12; + } + if (isset($time['_hour']) && $time['_hour'] == 12 && 'am' == $meridian) { + $time['_hour'] = '00'; + } + + $time = join(':', array_values($time)); + if($date && $time) { + $date = $date . ' ' . $time; + } + } + + if($useNewDate) { + $this->data[$modelClass][$field] = $date; + } } } } diff --git a/cake/tests/cases/libs/controller/controller.test.php b/cake/tests/cases/libs/controller/controller.test.php index 018cd3467..7ca35d79b 100644 --- a/cake/tests/cases/libs/controller/controller.test.php +++ b/cake/tests/cases/libs/controller/controller.test.php @@ -27,6 +27,11 @@ * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License */ uses('controller' . DS . 'controller'); + +class ControllerPost extends CakeTestModel { + var $name = 'ControllerPost'; + var $useTable = 'posts'; +} /** * Short description for class. * @@ -35,8 +40,56 @@ uses('controller' . DS . 'controller'); */ class ControllerTest extends CakeTestCase { - function skip() { - $this->skipif (true, 'ControllerTest not implemented'); + var $fixtures = array('core.post'); + + function testCleanUpFields() { + $Controller =& new Controller(); + $Controller->modelClass = 'ControllerPost'; + $Controller->ControllerPost =& new ControllerPost(); + + $Controller->data['ControllerPost']['created_year'] = ''; + $Controller->data['ControllerPost']['created_month'] = ''; + $Controller->data['ControllerPost']['created_day'] = ''; + $Controller->data['ControllerPost']['created_hour'] = ''; + $Controller->data['ControllerPost']['created_min'] = ''; + $Controller->data['ControllerPost']['created_sec'] = ''; + + $Controller->cleanUpFields(); + $expected = array('ControllerPost'=> array('created'=> '')); + $this->assertEqual($Controller->data, $expected); + + $Controller->data['ControllerPost']['created_year'] = '2007'; + $Controller->data['ControllerPost']['created_month'] = '08'; + $Controller->data['ControllerPost']['created_day'] = '20'; + $Controller->data['ControllerPost']['created_hour'] = ''; + $Controller->data['ControllerPost']['created_min'] = ''; + $Controller->data['ControllerPost']['created_sec'] = ''; + + $Controller->cleanUpFields(); + $expected = array('ControllerPost'=> array('created'=> '2007-08-20')); + $this->assertEqual($Controller->data, $expected); + + $Controller->data['ControllerPost']['created_year'] = '2007'; + $Controller->data['ControllerPost']['created_month'] = '08'; + $Controller->data['ControllerPost']['created_day'] = '20'; + $Controller->data['ControllerPost']['created_hour'] = '10'; + $Controller->data['ControllerPost']['created_min'] = '12'; + $Controller->data['ControllerPost']['created_sec'] = ''; + + $Controller->cleanUpFields(); + $expected = array('ControllerPost'=> array('created'=> '2007-08-20 10:12')); + $this->assertEqual($Controller->data, $expected); + + $Controller->data['ControllerPost']['created_year'] = '2007'; + $Controller->data['ControllerPost']['created_month'] = ''; + $Controller->data['ControllerPost']['created_day'] = '12'; + $Controller->data['ControllerPost']['created_hour'] = '20'; + $Controller->data['ControllerPost']['created_min'] = ''; + $Controller->data['ControllerPost']['created_sec'] = ''; + + $Controller->cleanUpFields(); + $expected = array('ControllerPost'=> array('created'=> '')); + $this->assertEqual($Controller->data, $expected); } } ?> \ No newline at end of file