Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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
  • Loading branch information
markstory committed Jul 11, 2017
1 parent a6bf3f0 commit b12452c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/Database/Type/DateTimeType.php
Expand Up @@ -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);
Expand Down
38 changes: 35 additions & 3 deletions tests/TestCase/Database/Type/TimeTypeTest.php
Expand Up @@ -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;

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

/**
Expand Down Expand Up @@ -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
*/
Expand All @@ -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.
*
Expand Down

0 comments on commit b12452c

Please sign in to comment.