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
This commit is contained in:
gwoo 2007-08-22 01:31:34 +00:00
parent 4a79765b26
commit 780fe16c09
2 changed files with 108 additions and 33 deletions

View file

@ -941,40 +941,62 @@ class Controller extends Object {
if ($modelClass == null) { if ($modelClass == null) {
$modelClass = $this->modelClass; $modelClass = $this->modelClass;
} }
foreach ($this->{$modelClass}->_tableInfo->value as $field) { $fields = $this->{$modelClass}->schema();
$useNewDate = false; foreach ($fields->value as $field => $value) {
$dateFields = array('Y'=>'_year', 'm'=>'_month', 'd'=>'_day', 'H'=>'_hour', 'i'=>'_min', 's'=>'_sec'); if (in_array($value['type'], array('datetime', 'timestamp', 'date', 'time'))) {
foreach ($dateFields as $default => $var) { $useNewDate = false;
if (isset($this->data[$modelClass][$field['name'] . $var])) { $date = array();
${$var} = $this->data[$modelClass][$field['name'] . $var]; $dates = array('Y'=>'_year', 'm'=>'_month', 'd'=>'_day');
unset($this->data[$modelClass][$field['name'] . $var]); foreach ($dates as $default => $var) {
$useNewDate = true; if (isset($this->data[$modelClass][$field . $var])) {
} else { if (!empty($this->data[$modelClass][$field . $var])) {
if ($var == 'year') { $date[$var] = $this->data[$modelClass][$field . $var];
${$var} = '0000'; }
} else { $useNewDate = true;
${$var} = '00'; unset($this->data[$modelClass][$field . $var]);
} }
} }
} if (count($date) == 3 && in_array($value['type'], array('datetime', 'timestamp', 'date'))) {
if ($_hour != 12 && (isset($this->data[$modelClass][$field['name'] . '_meridian']) && 'pm' == $this->data[$modelClass][$field['name'] . '_meridian'])) { $date = join('-', array_values($date));
$_hour = $_hour + 12; } else {
} $date = null;
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']);
$newDate = null; if ($value['type'] != 'date') {
if (in_array($field['type'], array('datetime', 'timestamp')) && $useNewDate) { $time = array();
$newDate = "{$_year}-{$_month}-{$_day} {$_hour}:{$_min}:{$_sec}"; $times = array('H'=>'_hour', 'i'=>'_min', 's'=>'_sec');
} elseif ('date' == $field['type'] && $useNewDate) { foreach($times as $default => $var) {
$newDate = "{$_year}-{$_month}-{$_day}"; if (isset($this->data[$modelClass][$field . $var])) {
} elseif ('time' == $field['type'] && $useNewDate) { if (!empty($this->data[$modelClass][$field . $var])) {
$newDate = "{$_hour}:{$_min}:{$_sec}"; $time[$var] = $this->data[$modelClass][$field . $var];
} }
if ($newDate && !in_array($field['name'], array('created', 'updated', 'modified'))) { $useNewDate = true;
$this->data[$modelClass][$field['name']] = $newDate; 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;
}
} }
} }
} }

View file

@ -27,6 +27,11 @@
* @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
*/ */
uses('controller' . DS . 'controller'); uses('controller' . DS . 'controller');
class ControllerPost extends CakeTestModel {
var $name = 'ControllerPost';
var $useTable = 'posts';
}
/** /**
* Short description for class. * Short description for class.
* *
@ -35,8 +40,56 @@ uses('controller' . DS . 'controller');
*/ */
class ControllerTest extends CakeTestCase { class ControllerTest extends CakeTestCase {
function skip() { var $fixtures = array('core.post');
$this->skipif (true, 'ControllerTest not implemented');
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);
} }
} }
?> ?>