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) {
$modelClass = $this->modelClass;
}
foreach ($this->{$modelClass}->_tableInfo->value as $field) {
$fields = $this->{$modelClass}->schema();
foreach ($fields->value as $field => $value) {
if (in_array($value['type'], array('datetime', 'timestamp', 'date', 'time'))) {
$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]);
$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 (count($date) == 3 && in_array($value['type'], array('datetime', 'timestamp', 'date'))) {
$date = join('-', array_values($date));
} else {
if ($var == 'year') {
${$var} = '0000';
} else {
${$var} = '00';
$date = null;
}
}
}
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']);
$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 ($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;
}
if ($newDate && !in_array($field['name'], array('created', 'updated', 'modified'))) {
$this->data[$modelClass][$field['name']] = $newDate;
}
}
}

View file

@ -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);
}
}
?>