Fixing empty time value handling in Model::deconstruct(). Both null and 00:00:00 are valid values now. Test cases added and refactored. Fixes #6488, #6018, Refs #5659

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@8225 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
mark_story 2009-07-08 03:25:30 +00:00
parent 9918583e7e
commit fb6b16c467
2 changed files with 123 additions and 64 deletions

View file

@ -827,8 +827,11 @@ class Model extends Overloadable {
$type = $this->getColumnType($field);
if (in_array($type, array('datetime', 'timestamp', 'date', 'time'))) {
$useNewDate = (isset($data['year']) || isset($data['month']) || isset($data['day']) || isset($data['hour']) || isset($data['minute']));
$useNewDate = (isset($data['year']) || isset($data['month']) ||
isset($data['day']) || isset($data['hour']) || isset($data['minute']));
$dateFields = array('Y' => 'year', 'm' => 'month', 'd' => 'day', 'H' => 'hour', 'i' => 'min', 's' => 'sec');
$timeFields = array('H' => 'hour', 'i' => 'min', 's' => 'sec');
$db =& ConnectionManager::getDataSource($this->useDbConfig);
$format = $db->columns[$type]['format'];
@ -840,27 +843,42 @@ class Model extends Overloadable {
if (isset($data['hour']) && isset($data['meridian']) && $data['hour'] == 12 && 'am' == $data['meridian']) {
$data['hour'] = '00';
}
foreach ($dateFields as $key => $val) {
if (in_array($val, array('hour', 'min', 'sec'))) {
if (!isset($data[$val]) || $data[$val] === '0' || empty($data[$val])) {
if ($type == 'time') {
foreach ($timeFields as $key => $val) {
if (!isset($data[$val]) || $data[$val] === '0' || $data[$val] === '00') {
$data[$val] = '00';
} elseif ($data[$val] === '') {
$data[$val] = '';
} else {
$data[$val] = sprintf('%02d', $data[$val]);
}
if (!empty($data[$val])) {
$date[$key] = $data[$val];
} else {
return null;
}
}
if (in_array($type, array('datetime', 'timestamp', 'date')) && !isset($data[$val]) || isset($data[$val]) && (empty($data[$val]) || $data[$val][0] === '-')) {
return null;
} elseif (isset($data[$val]) && !empty($data[$val])) {
$date[$key] = $data[$val];
}
if ($type == 'datetime' || $type == 'timestamp' || $type == 'date') {
foreach ($dateFields as $key => $val) {
if ($val == 'hour' || $val == 'min' || $val == 'sec') {
if (!isset($data[$val]) || $data[$val] === '0' || $data[$val] === '00') {
$data[$val] = '00';
} else {
$data[$val] = sprintf('%02d', $data[$val]);
}
}
if (!isset($data[$val]) || isset($data[$val]) && (empty($data[$val]) || $data[$val][0] === '-')) {
return null;
}
if (isset($data[$val]) && !empty($data[$val])) {
$date[$key] = $data[$val];
}
}
}
$date = str_replace(array_keys($date), array_values($date), $format);
if ($type == 'time' && $date == '00:00:00') {
return null;
}
if ($useNewDate && (!empty($date))) {
if ($useNewDate && !empty($date)) {
return $date;
}
}

View file

@ -26,6 +26,9 @@
*/
App::import('Core', array('AppModel', 'Model'));
require_once dirname(__FILE__) . DS . 'models.php';
SimpleTest::ignore('BaseModelTest');
/**
* ModelBaseTest
*
@ -603,12 +606,97 @@ class ModelTest extends BaseModelTest {
$this->assertEqual($Post->getColumnTypes(), array_combine($columns, $types));
}
/**
* testDeconstructFields method
* test deconstruct() with time fields.
*
* @return void
**/
function testDeconstructFieldsTime() {
$this->loadFixtures('Apple');
$TestModel =& new Apple();
$data = array();
$data['Apple']['mytime']['hour'] = '';
$data['Apple']['mytime']['min'] = '';
$data['Apple']['mytime']['sec'] = '';
$TestModel->data = null;
$TestModel->set($data);
$expected = array('Apple'=> array('mytime'=> ''));
$this->assertEqual($TestModel->data, $expected);
$data = array();
$data['Apple']['mytime']['hour'] = '';
$data['Apple']['mytime']['min'] = '';
$data['Apple']['mytime']['meridan'] = '';
$TestModel->data = null;
$TestModel->set($data);
$expected = array('Apple'=> array('mytime'=> ''));
$this->assertEqual($TestModel->data, $expected, 'Empty values are not returning properly. %s');
$data = array();
$data['Apple']['mytime']['hour'] = '12';
$data['Apple']['mytime']['min'] = '0';
$data['Apple']['mytime']['meridian'] = 'am';
$TestModel->data = null;
$TestModel->set($data);
$expected = array('Apple'=> array('mytime'=> '00:00:00'));
$this->assertEqual($TestModel->data, $expected, 'Midnight is not returning proper values. %s');
$data = array();
$data['Apple']['mytime']['hour'] = '00';
$data['Apple']['mytime']['min'] = '00';
$TestModel->data = null;
$TestModel->set($data);
$expected = array('Apple'=> array('mytime'=> '00:00:00'));
$this->assertEqual($TestModel->data, $expected, 'Midnight is not returning proper values. %s');
$data = array();
$data['Apple']['mytime']['hour'] = '03';
$data['Apple']['mytime']['min'] = '04';
$data['Apple']['mytime']['sec'] = '04';
$TestModel->data = null;
$TestModel->set($data);
$expected = array('Apple'=> array('mytime'=> '03:04:04'));
$this->assertEqual($TestModel->data, $expected);
$data = array();
$data['Apple']['mytime']['hour'] = '3';
$data['Apple']['mytime']['min'] = '4';
$data['Apple']['mytime']['sec'] = '4';
$TestModel->data = null;
$TestModel->set($data);
$expected = array('Apple' => array('mytime'=> '03:04:04'));
$this->assertEqual($TestModel->data, $expected);
$data = array();
$data['Apple']['mytime']['hour'] = '03';
$data['Apple']['mytime']['min'] = '4';
$data['Apple']['mytime']['sec'] = '4';
$TestModel->data = null;
$TestModel->set($data);
$expected = array('Apple'=> array('mytime'=> '03:04:04'));
$this->assertEqual($TestModel->data, $expected);
$db = ConnectionManager::getDataSource('test_suite');
$data = array();
$data['Apple']['mytime'] = $db->expression('NOW()');
$TestModel->data = null;
$TestModel->set($data);
$this->assertEqual($TestModel->data, $data);
}
/**
* testDeconstructFields with datetime, timestamp, and date fields
*
* @access public
* @return void
*/
function testDeconstructFields() {
function testDeconstructFieldsDateTime() {
$this->loadFixtures('Apple');
$TestModel =& new Apple();
@ -635,17 +723,6 @@ class ModelTest extends BaseModelTest {
$expected = array('Apple'=> array('date'=> ''));
$this->assertEqual($TestModel->data, $expected);
$data = array();
$data['Apple']['mytime']['hour'] = '';
$data['Apple']['mytime']['min'] = '';
$data['Apple']['mytime']['sec'] = '';
$TestModel->data = null;
$TestModel->set($data);
$expected = array('Apple'=> array('mytime'=> ''));
$this->assertEqual($TestModel->data, $expected);
//test other data variations
$data = array();
$data['Apple']['created']['year'] = '2007';
$data['Apple']['created']['month'] = '08';
@ -781,49 +858,13 @@ class ModelTest extends BaseModelTest {
$TestModel->set($data);
$expected = array('Apple'=> array('date'=> '2006-12-25'));
$this->assertEqual($TestModel->data, $expected);
$data = array();
$data['Apple']['mytime']['hour'] = '03';
$data['Apple']['mytime']['min'] = '04';
$data['Apple']['mytime']['sec'] = '04';
$TestModel->data = null;
$TestModel->set($data);
$expected = array('Apple'=> array('mytime'=> '03:04:04'));
$this->assertEqual($TestModel->data, $expected);
$data = array();
$data['Apple']['mytime']['hour'] = '3';
$data['Apple']['mytime']['min'] = '4';
$data['Apple']['mytime']['sec'] = '4';
$TestModel->data = null;
$TestModel->set($data);
$expected = array('Apple' => array('mytime'=> '03:04:04'));
$this->assertEqual($TestModel->data, $expected);
$data = array();
$data['Apple']['mytime']['hour'] = '03';
$data['Apple']['mytime']['min'] = '4';
$data['Apple']['mytime']['sec'] = '4';
$TestModel->data = null;
$TestModel->set($data);
$expected = array('Apple'=> array('mytime'=> '03:04:04'));
$this->assertEqual($TestModel->data, $expected);
$db = ConnectionManager::getDataSource('test_suite');
$data = array();
$data['Apple']['modified'] = $db->expression('NOW()');
$TestModel->data = null;
$TestModel->set($data);
$this->assertEqual($TestModel->data, $data);
$data = array();
$data['Apple']['mytime'] = $db->expression('NOW()');
$TestModel->data = null;
$TestModel->set($data);
$this->assertEqual($TestModel->data, $data);
}
/**
* testTablePrefixSwitching method