Browse Source

Add invalid value tests to DateTimeType::marshall()

Mark Story 12 years ago
parent
commit
edd99b34b6

+ 5 - 2
src/Database/Type/DateTimeType.php

@@ -81,14 +81,17 @@ class DateTimeType extends \Cake\Database\Type {
 
 		$date = new DateTime();
 		$date->setTime(0, 0, 0);
-		if (isset($value['year'], $value['month'], $value['day'])) {
+		if (
+			isset($value['year'], $value['month'], $value['day']) &&
+			(is_numeric($value['year']) & is_numeric($value['month']) && is_numeric($value['day']))
+		) {
 			$date->setDate($value['year'], $value['month'], $value['day']);
 		}
 		if (isset($value['hour'], $value['minute'])) {
 			if (isset($value['meridian'])) {
 				$value['hour'] = strtolower($value['meridian']) === 'am' ? $value['hour'] : $value['hour'] + 12;
 			}
-			$date->setTime($value['hour'], $value['minute'], $value['second']);
+			$date->setTime((int)$value['hour'], (int)$value['minute'], (int)$value['second']);
 		}
 		return $date;
 	}

+ 23 - 0
tests/TestCase/Database/Type/DateTimeTypeTest.php

@@ -111,6 +111,29 @@ class DateTimeTypeTest extends TestCase {
 				],
 				new \DateTime('2014-02-14 13:14:15')
 			],
+			[
+				[
+					'year' => 2014, 'month' => 2, 'day' => 14,
+				],
+				new \DateTime('2014-02-14 00:00:00')
+			],
+
+			// Invalid array types
+			[
+				['year' => 'farts', 'month' => 'derp'],
+				new \DateTime(date('Y-m-d 00:00:00'))
+			],
+			[
+				['year' => 'farts', 'month' => 'derp', 'day' => 'farts'],
+				new \DateTime(date('Y-m-d 00:00:00'))
+			],
+			[
+				[
+					'year' => '2014', 'month' => '02', 'day' => '14',
+					'hour' => 'farts', 'minute' => 'farts'
+				],
+				new \DateTime('2014-02-14 00:00:00')
+			],
 		];
 	}