silverstripe-framework/tests/php/Forms/TimeFieldTest.php
Damian Mooyman 029a8b9586
API Substitute Zend_Currency with NumberFormatter based solution
API Substitute Zend_Locale with Locale / NumberFormatter
API Substitute Zend_Date with IntlDateFormatter
API Added DBTIme::Nice12, FormatFromSettings
API Added Short() method to DBDate / DBTime / DBDatetime
API Add Date::getTimestamp()
API Added setSubmittedValue api for FormField
API Add second arg to base FormField::setValue()
API Major refactor of i18n into component data parts
API Implement Resettable interface to reset objects between tests
ENHANCEMENT Changed DBField::create_field return type to `static` to support better type hinting
ENHANCEMENT i18nTextCollector supports __CLASS__
2017-02-09 15:28:59 +13:00

127 lines
3.6 KiB
PHP

<?php
namespace SilverStripe\Forms\Tests;
use SilverStripe\Core\Config\Config;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\Forms\TimeField;
use SilverStripe\Forms\RequiredFields;
use SilverStripe\i18n\i18n;
class TimeFieldTest extends SapphireTest
{
public function setUp()
{
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()));
}
public function testSetLocale()
{
// should get en_NZ by default through setUp()
$f = new TimeField('Time', 'Time');
$f->setLocale('de_DE');
// 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',
'Setting value to "11pm" parses with strtotime enabled'
);
$this->assertTrue($f->validate(new RequiredFields()));
$f = new TimeField('Time', 'Time');
$f->setValue('11:59pm');
$this->assertEquals('23:59:00', $f->dataValue());
$f = new TimeField('Time', 'Time');
$f->setValue('11:59 pm');
$this->assertEquals('23:59:00', $f->dataValue());
$f = new TimeField('Time', 'Time');
$f->setValue('23:59');
$this->assertEquals('23:59:00', $f->dataValue());
$f = new TimeField('Time', 'Time');
$f->setValue('23:59:38');
$this->assertEquals('23:59:38', $f->dataValue());
$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');
$field->setValue('11:00pm');
$field->setValue('');
$this->assertEquals($field->dataValue(), '');
}
/**
* Test that AM/PM is preserved correctly in various situations
*/
public function testPreserveAMPM()
{
// Test with timeformat that includes hour
// Check pm
$f = new TimeField('Time', 'Time');
$f->setTimeFormat('h:mm:ss a');
$f->setValue('3:59 pm');
$this->assertEquals($f->dataValue(), '15:59:00');
// Check am
$f = new TimeField('Time', 'Time');
$f->setTimeFormat('h:mm:ss a');
$f->setValue('3:59 am');
$this->assertEquals($f->dataValue(), '03:59:00');
// Check with ISO date/time
$f = new TimeField('Time', 'Time');
$f->setTimeFormat('h:mm:ss a');
$f->setValue('15:59:00');
$this->assertEquals($f->dataValue(), '15:59:00');
// ISO am
$f = new TimeField('Time', 'Time');
$f->setTimeFormat('h:mm:ss a');
$f->setValue('03:59:00');
$this->assertEquals($f->dataValue(), '03:59:00');
}
}