2010-10-15 05:23:02 +02:00
|
|
|
<?php
|
2016-08-19 00:51:35 +02:00
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
namespace SilverStripe\Forms\Tests;
|
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Core\Config\Config;
|
|
|
|
use SilverStripe\Dev\CSSContentParser;
|
|
|
|
use SilverStripe\Dev\SapphireTest;
|
|
|
|
use SilverStripe\Control\Controller;
|
|
|
|
use SilverStripe\Forms\MemberDatetimeOptionsetField;
|
|
|
|
use SilverStripe\Forms\FieldList;
|
|
|
|
use SilverStripe\Forms\Form;
|
|
|
|
use SilverStripe\Forms\RequiredFields;
|
2016-10-14 03:30:05 +02:00
|
|
|
use SilverStripe\i18n\i18n;
|
|
|
|
use SilverStripe\Security\Member;
|
|
|
|
use Zend_Locale_Format;
|
|
|
|
use Zend_Date;
|
2016-08-19 00:51:35 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
class MemberDatetimeOptionsetFieldTest extends SapphireTest
|
|
|
|
{
|
2010-10-15 05:23:02 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
protected static $fixture_file = 'MemberDatetimeOptionsetFieldTest.yml';
|
2010-10-15 05:23:02 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
protected function createDateFormatFieldForMember($member)
|
|
|
|
{
|
|
|
|
include_once 'Zend/Date.php';
|
|
|
|
$defaultDateFormat = Zend_Locale_Format::getDateFormat($member->Locale);
|
|
|
|
$dateFormatMap = array(
|
|
|
|
'yyyy-MM-dd' => Zend_Date::now()->toString('yyyy-MM-dd'),
|
|
|
|
'yyyy/MM/dd' => Zend_Date::now()->toString('yyyy/MM/dd'),
|
|
|
|
'MM/dd/yyyy' => Zend_Date::now()->toString('MM/dd/yyyy'),
|
|
|
|
'dd/MM/yyyy' => Zend_Date::now()->toString('dd/MM/yyyy'),
|
|
|
|
);
|
|
|
|
$dateFormatMap[$defaultDateFormat] = Zend_Date::now()->toString($defaultDateFormat) . ' (default)';
|
|
|
|
$field = new MemberDatetimeOptionsetField(
|
|
|
|
'DateFormat',
|
|
|
|
'Date format',
|
|
|
|
$dateFormatMap
|
|
|
|
);
|
|
|
|
$field->setValue($member->DateFormat);
|
|
|
|
return $field;
|
|
|
|
}
|
2010-10-15 05:23:02 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
protected function createTimeFormatFieldForMember($member)
|
|
|
|
{
|
|
|
|
include_once 'Zend/Date.php';
|
|
|
|
$defaultTimeFormat = Zend_Locale_Format::getTimeFormat($member->Locale);
|
|
|
|
$timeFormatMap = array(
|
|
|
|
'h:mm a' => Zend_Date::now()->toString('h:mm a'),
|
|
|
|
'H:mm' => Zend_Date::now()->toString('H:mm'),
|
|
|
|
);
|
|
|
|
$timeFormatMap[$defaultTimeFormat] = Zend_Date::now()->toString($defaultTimeFormat) . ' (default)';
|
|
|
|
$field = new MemberDatetimeOptionsetField(
|
|
|
|
'TimeFormat',
|
|
|
|
'Time format',
|
|
|
|
$timeFormatMap
|
|
|
|
);
|
|
|
|
$field->setValue($member->TimeFormat);
|
|
|
|
return $field;
|
|
|
|
}
|
2010-10-15 05:23:02 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
public function testDateFormatDefaultCheckedInFormField()
|
|
|
|
{
|
|
|
|
Config::inst()->update(i18n::class, 'date_format', 'yyyy-MM-dd');
|
|
|
|
$field = $this->createDateFormatFieldForMember($this->objFromFixture(Member::class, 'noformatmember'));
|
|
|
|
/**
|
|
|
|
* @skipUpgrade
|
|
|
|
*/
|
|
|
|
$field->setForm(
|
|
|
|
new Form(
|
|
|
|
new Controller(),
|
|
|
|
'Form',
|
|
|
|
new FieldList(),
|
|
|
|
new FieldList()
|
|
|
|
)
|
|
|
|
); // fake form
|
|
|
|
$parser = new CSSContentParser($field->Field());
|
|
|
|
$xmlArr = $parser->getBySelector('#Form_Form_DateFormat_yyyy-MM-dd');
|
|
|
|
$this->assertEquals('checked', (string) $xmlArr[0]['checked']);
|
|
|
|
}
|
2010-10-15 05:23:02 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
public function testTimeFormatDefaultCheckedInFormField()
|
|
|
|
{
|
|
|
|
Config::inst()->update(i18n::class, 'time_format', 'h:mm:ss a');
|
|
|
|
$field = $this->createTimeFormatFieldForMember($this->objFromFixture(Member::class, 'noformatmember'));
|
|
|
|
/**
|
|
|
|
* @skipUpgrade
|
|
|
|
*/
|
|
|
|
$field->setForm(
|
|
|
|
new Form(
|
|
|
|
new Controller(),
|
|
|
|
'Form',
|
|
|
|
new FieldList(),
|
|
|
|
new FieldList()
|
|
|
|
)
|
|
|
|
); // fake form
|
|
|
|
$parser = new CSSContentParser($field->Field());
|
|
|
|
$xmlArr = $parser->getBySelector('#Form_Form_TimeFormat_h:mm:ss_a');
|
|
|
|
$this->assertEquals('checked', (string) $xmlArr[0]['checked']);
|
|
|
|
}
|
2010-10-15 05:23:02 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
public function testDateFormatChosenIsCheckedInFormField()
|
|
|
|
{
|
|
|
|
$member = $this->objFromFixture(Member::class, 'noformatmember');
|
|
|
|
$member->setField('DateFormat', 'MM/dd/yyyy');
|
|
|
|
$field = $this->createDateFormatFieldForMember($member);
|
|
|
|
/**
|
|
|
|
* @skipUpgrade
|
|
|
|
*/
|
|
|
|
$field->setForm(
|
|
|
|
new Form(
|
|
|
|
new Controller(),
|
|
|
|
'Form',
|
|
|
|
new FieldList(),
|
|
|
|
new FieldList()
|
|
|
|
)
|
|
|
|
); // fake form
|
|
|
|
$parser = new CSSContentParser($field->Field());
|
|
|
|
$xmlArr = $parser->getBySelector('#Form_Form_DateFormat_MM_dd_yyyy');
|
|
|
|
$this->assertEquals('checked', (string) $xmlArr[0]['checked']);
|
|
|
|
}
|
2010-10-15 05:23:02 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
public function testDateFormatCustomFormatAppearsInCustomInputInField()
|
|
|
|
{
|
|
|
|
$member = $this->objFromFixture(Member::class, 'noformatmember');
|
|
|
|
$member->setField('DateFormat', 'dd MM yy');
|
|
|
|
$field = $this->createDateFormatFieldForMember($member);
|
|
|
|
/**
|
|
|
|
* @skipUpgrade
|
|
|
|
*/
|
|
|
|
$field->setForm(
|
|
|
|
new Form(
|
|
|
|
new Controller(),
|
|
|
|
'Form',
|
|
|
|
new FieldList(),
|
|
|
|
new FieldList()
|
|
|
|
)
|
|
|
|
); // fake form
|
|
|
|
$parser = new CSSContentParser($field->Field());
|
|
|
|
$xmlInputArr = $parser->getBySelector('.valcustom input');
|
|
|
|
$this->assertEquals('checked', (string) $xmlInputArr[0]['checked']);
|
|
|
|
$this->assertEquals('dd MM yy', (string) $xmlInputArr[1]['value']);
|
|
|
|
}
|
2010-10-15 05:23:02 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
public function testDateFormValid()
|
|
|
|
{
|
|
|
|
$field = new MemberDatetimeOptionsetField('DateFormat', 'DateFormat');
|
|
|
|
$validator = new RequiredFields();
|
|
|
|
$this->assertTrue($field->validate($validator));
|
|
|
|
$field->setValue(
|
|
|
|
array(
|
|
|
|
'Options' => '__custom__',
|
|
|
|
'Custom' => 'dd MM yyyy'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$this->assertTrue($field->validate($validator));
|
|
|
|
$field->setValue(
|
|
|
|
array(
|
|
|
|
'Options' => '__custom__',
|
|
|
|
'Custom' => 'sdfdsfdfd1244'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$this->assertFalse($field->validate($validator));
|
|
|
|
}
|
2010-10-15 05:23:02 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
public function testDescriptionTemplate()
|
|
|
|
{
|
|
|
|
$field = new MemberDatetimeOptionsetField('DateFormat', 'DateFormat');
|
2016-07-06 10:16:27 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEmpty($field->getDescription());
|
2016-07-06 10:16:27 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
$field->setDescription('Test description');
|
|
|
|
$this->assertEquals('Test description', $field->getDescription());
|
2016-07-06 10:16:27 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
$field->setDescriptionTemplate(get_class($field).'_description_time');
|
|
|
|
$this->assertNotEmpty($field->getDescription());
|
|
|
|
$this->assertNotEquals('Test description', $field->getDescription());
|
|
|
|
}
|
2012-04-12 02:20:18 +02:00
|
|
|
}
|