2009-06-16 08:39:57 +02:00
|
|
|
<?php
|
2016-08-19 00:51:35 +02:00
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
namespace SilverStripe\Forms\Tests;
|
|
|
|
|
2017-04-03 02:00:59 +02:00
|
|
|
use IntlDateFormatter;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Core\Config\Config;
|
|
|
|
use SilverStripe\Dev\SapphireTest;
|
|
|
|
use SilverStripe\Forms\TimeField;
|
|
|
|
use SilverStripe\Forms\RequiredFields;
|
|
|
|
use SilverStripe\i18n\i18n;
|
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
class TimeFieldTest extends SapphireTest
|
|
|
|
{
|
2017-03-24 04:00:54 +01:00
|
|
|
protected function setUp()
|
2016-12-16 05:34:21 +01:00
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
i18n::set_locale('en_NZ');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testConstructorWithoutArgs()
|
|
|
|
{
|
|
|
|
$f = new TimeField('Time');
|
|
|
|
$this->assertEquals($f->dataValue(), null);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testConstructorWithString()
|
|
|
|
{
|
|
|
|
$f = new TimeField('Time', 'Time', '23:00:00');
|
|
|
|
$this->assertEquals($f->dataValue(), '23:00:00');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testValidate()
|
|
|
|
{
|
|
|
|
$f = new TimeField('Time', 'Time', '11pm');
|
|
|
|
$this->assertTrue($f->validate(new RequiredFields()));
|
|
|
|
|
|
|
|
$f = new TimeField('Time', 'Time', '23:59');
|
|
|
|
$this->assertTrue($f->validate(new RequiredFields()));
|
|
|
|
|
|
|
|
$f = new TimeField('Time', 'Time', 'wrong');
|
|
|
|
$this->assertFalse($f->validate(new RequiredFields()));
|
|
|
|
}
|
|
|
|
|
2017-03-30 23:37:21 +02:00
|
|
|
public function testValidateLenientWithHtml5()
|
|
|
|
{
|
|
|
|
$f = new TimeField('Time', 'Time', '23:59:59');
|
|
|
|
$f->setHTML5(true);
|
|
|
|
$this->assertTrue($f->validate(new RequiredFields()));
|
|
|
|
|
|
|
|
$f = new TimeField('Time', 'Time', '23:59'); // leave out seconds
|
|
|
|
$f->setHTML5(true);
|
|
|
|
$this->assertTrue($f->validate(new RequiredFields()));
|
|
|
|
}
|
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
public function testSetLocale()
|
|
|
|
{
|
|
|
|
// should get en_NZ by default through setUp()
|
|
|
|
$f = new TimeField('Time', 'Time');
|
2017-04-03 02:00:59 +02:00
|
|
|
$f->setHTML5(false);
|
|
|
|
$f->setLocale('fr_FR');
|
2016-12-16 05:34:21 +01:00
|
|
|
// TODO Find a hour format thats actually different
|
|
|
|
$f->setValue('23:59');
|
|
|
|
$this->assertEquals($f->dataValue(), '23:59:00');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSetValueWithUseStrToTime()
|
|
|
|
{
|
|
|
|
$f = new TimeField('Time', 'Time');
|
|
|
|
$f->setValue('11pm');
|
|
|
|
$this->assertEquals(
|
|
|
|
$f->dataValue(),
|
|
|
|
'23:00:00',
|
2017-01-26 05:20:08 +01:00
|
|
|
'Setting value to "11pm" parses with strtotime enabled'
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
$this->assertTrue($f->validate(new RequiredFields()));
|
|
|
|
|
|
|
|
$f = new TimeField('Time', 'Time');
|
|
|
|
$f->setValue('11:59pm');
|
2017-01-26 05:20:08 +01:00
|
|
|
$this->assertEquals('23:59:00', $f->dataValue());
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
$f = new TimeField('Time', 'Time');
|
|
|
|
$f->setValue('11:59 pm');
|
2017-01-26 05:20:08 +01:00
|
|
|
$this->assertEquals('23:59:00', $f->dataValue());
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
$f = new TimeField('Time', 'Time');
|
|
|
|
$f->setValue('23:59');
|
2017-01-26 05:20:08 +01:00
|
|
|
$this->assertEquals('23:59:00', $f->dataValue());
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
$f = new TimeField('Time', 'Time');
|
|
|
|
$f->setValue('23:59:38');
|
2017-01-26 05:20:08 +01:00
|
|
|
$this->assertEquals('23:59:38', $f->dataValue());
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
$f = new TimeField('Time', 'Time');
|
|
|
|
$f->setValue('12:00 am');
|
|
|
|
$this->assertEquals($f->dataValue(), '00:00:00');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testOverrideWithNull()
|
|
|
|
{
|
|
|
|
$field = new TimeField('Time', 'Time');
|
2017-04-03 02:00:59 +02:00
|
|
|
$field->setValue('11:00:00');
|
2016-12-16 05:34:21 +01:00
|
|
|
$field->setValue('');
|
|
|
|
$this->assertEquals($field->dataValue(), '');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that AM/PM is preserved correctly in various situations
|
|
|
|
*/
|
2017-04-03 02:00:59 +02:00
|
|
|
public function testSetTimeFormat()
|
2016-12-16 05:34:21 +01:00
|
|
|
{
|
|
|
|
|
|
|
|
// Test with timeformat that includes hour
|
|
|
|
|
|
|
|
// Check pm
|
|
|
|
$f = new TimeField('Time', 'Time');
|
2017-04-03 02:00:59 +02:00
|
|
|
$f->setHTML5(false);
|
2017-01-26 05:20:08 +01:00
|
|
|
$f->setTimeFormat('h:mm:ss a');
|
2016-12-16 05:34:21 +01:00
|
|
|
$f->setValue('3:59 pm');
|
|
|
|
$this->assertEquals($f->dataValue(), '15:59:00');
|
|
|
|
|
|
|
|
// Check am
|
|
|
|
$f = new TimeField('Time', 'Time');
|
2017-04-03 02:00:59 +02:00
|
|
|
$f->setHTML5(false);
|
2017-01-26 05:20:08 +01:00
|
|
|
$f->setTimeFormat('h:mm:ss a');
|
2016-12-16 05:34:21 +01:00
|
|
|
$f->setValue('3:59 am');
|
|
|
|
$this->assertEquals($f->dataValue(), '03:59:00');
|
|
|
|
|
|
|
|
// Check with ISO date/time
|
|
|
|
$f = new TimeField('Time', 'Time');
|
2017-04-03 02:00:59 +02:00
|
|
|
$f->setHTML5(false);
|
2017-01-26 05:20:08 +01:00
|
|
|
$f->setTimeFormat('h:mm:ss a');
|
2016-12-16 05:34:21 +01:00
|
|
|
$f->setValue('15:59:00');
|
|
|
|
$this->assertEquals($f->dataValue(), '15:59:00');
|
|
|
|
|
|
|
|
// ISO am
|
|
|
|
$f = new TimeField('Time', 'Time');
|
2017-04-03 02:00:59 +02:00
|
|
|
$f->setHTML5(false);
|
2017-01-26 05:20:08 +01:00
|
|
|
$f->setTimeFormat('h:mm:ss a');
|
2016-12-16 05:34:21 +01:00
|
|
|
$f->setValue('03:59:00');
|
|
|
|
$this->assertEquals($f->dataValue(), '03:59:00');
|
|
|
|
}
|
2017-03-30 23:37:21 +02:00
|
|
|
|
|
|
|
public function testLenientSubmissionParseWithoutSecondsOnHtml5()
|
|
|
|
{
|
|
|
|
$f = new TimeField('Time', 'Time');
|
|
|
|
$f->setSubmittedValue('23:59');
|
|
|
|
$this->assertEquals($f->Value(), '23:59:00');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \LogicException
|
2017-04-03 02:00:59 +02:00
|
|
|
* @expectedExceptionMessageRegExp /Please opt-out .* if using setTimeFormat/
|
2017-03-30 23:37:21 +02:00
|
|
|
*/
|
|
|
|
public function testHtml5WithCustomFormatThrowsException()
|
|
|
|
{
|
|
|
|
$f = new TimeField('Time', 'Time');
|
|
|
|
$f->setValue('15:59:00');
|
|
|
|
$f->setTimeFormat('mm:HH');
|
|
|
|
$f->Value();
|
|
|
|
}
|
2017-04-03 02:00:59 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \LogicException
|
|
|
|
* @expectedExceptionMessageRegExp /Please opt-out .* if using setTimeLength/
|
|
|
|
*/
|
|
|
|
public function testHtml5WithCustomDateLengthThrowsException()
|
|
|
|
{
|
|
|
|
$f = new TimeField('Time', 'Time');
|
|
|
|
$f->setValue('15:59:00');
|
|
|
|
$f->setTimeLength(IntlDateFormatter::MEDIUM);
|
|
|
|
$f->Value();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \LogicException
|
|
|
|
* @expectedExceptionMessageRegExp /Please opt-out .* if using setLocale/
|
|
|
|
*/
|
|
|
|
public function testHtml5WithCustomLocaleThrowsException()
|
|
|
|
{
|
|
|
|
$f = new TimeField('Time', 'Time');
|
|
|
|
$f->setValue('15:59:00');
|
|
|
|
$f->setLocale('de_DE');
|
|
|
|
$f->Value();
|
|
|
|
}
|
2012-03-24 04:04:52 +01:00
|
|
|
}
|