From b12452c891f271d8bbeaad3b8dc74eca1e17d308 Mon Sep 17 00:00:00 2001 From: Mark Story Date: Mon, 10 Jul 2017 21:44:20 -0400 Subject: [PATCH] Make timestamp checks less permissive Some locale based formats look like numeric values. If locale parsing is enabled we need to enter that case instead of the numeric one. By restricting the valid format of timestamps we can do that. Refs #10873 --- src/Database/Type/DateTimeType.php | 2 +- tests/TestCase/Database/Type/TimeTypeTest.php | 38 +++++++++++++++++-- 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/src/Database/Type/DateTimeType.php b/src/Database/Type/DateTimeType.php index e358a9511ab..49d261a9bee 100644 --- a/src/Database/Type/DateTimeType.php +++ b/src/Database/Type/DateTimeType.php @@ -157,7 +157,7 @@ public function marshal($value) if ($value === '' || $value === null || $value === false || $value === true) { return null; } - if (is_numeric($value)) { + if (ctype_digit($value)) { $date = new $class('@' . $value); } elseif (is_string($value) && $this->_useLocaleParser) { return $this->_parseValue($value); diff --git a/tests/TestCase/Database/Type/TimeTypeTest.php b/tests/TestCase/Database/Type/TimeTypeTest.php index 098ff6b2397..fe5976be6d3 100644 --- a/tests/TestCase/Database/Type/TimeTypeTest.php +++ b/tests/TestCase/Database/Type/TimeTypeTest.php @@ -15,6 +15,7 @@ namespace Cake\Test\TestCase\Database\Type; use Cake\Database\Type\TimeType; +use Cake\I18n\I18n; use Cake\I18n\Time; use Cake\TestSuite\TestCase; @@ -26,12 +27,17 @@ class TimeTypeTest extends TestCase /** * @var \Cake\Database\Type\TimeType */ - public $type; + protected $type; /** * @var \Cake\Database\Driver */ - public $driver; + protected $driver; + + /** + * @var string + */ + protected $locale; /** * Setup @@ -43,6 +49,18 @@ public function setUp() parent::setUp(); $this->type = new TimeType(); $this->driver = $this->getMockBuilder('Cake\Database\Driver')->getMock(); + $this->locale = I18n::locale(); + } + + /** + * Teardown + * + * @return void + */ + public function tearDown() + { + parent::tearDown(); + I18n::locale($this->locale); } /** @@ -181,7 +199,7 @@ public function testMarshal($value, $expected) } /** - * Tests marshalling dates using the locale aware parser + * Tests marshalling times using the locale aware parser * * @return void */ @@ -195,6 +213,20 @@ public function testMarshalWithLocaleParsing() $this->assertNull($this->type->marshal('derp:23')); } + /** + * Tests marshalling times in denmark. + * + * @return void + */ + public function testMarshalWithLocaleParsingDanishLocale() + { + I18n::locale('da_DK'); + $this->type->useLocaleParser(); + $expected = new Time('03:20:00'); + $result = $this->type->marshal('03.20'); + $this->assertEquals($expected->format('H:i'), $result->format('H:i')); + } + /** * Test that toImmutable changes all the methods to create frozen time instances. *