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;
|
2017-01-26 05:20:08 +01:00
|
|
|
use SilverStripe\ORM\FieldType\DBDatetime;
|
2016-10-14 03:30:05 +02:00
|
|
|
use SilverStripe\Security\Member;
|
2016-08-19 00:51:35 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
class MemberDatetimeOptionsetFieldTest extends SapphireTest
|
|
|
|
{
|
|
|
|
protected static $fixture_file = 'MemberDatetimeOptionsetFieldTest.yml';
|
2010-10-15 05:23:02 +02:00
|
|
|
|
2017-01-26 05:20:08 +01:00
|
|
|
/**
|
|
|
|
* @param Member $member
|
|
|
|
* @return MemberDatetimeOptionsetField
|
|
|
|
*/
|
2016-12-16 05:34:21 +01:00
|
|
|
protected function createDateFormatFieldForMember($member)
|
|
|
|
{
|
2017-01-26 05:20:08 +01:00
|
|
|
$defaultDateFormat = $member->getDefaultDateFormat();
|
2016-12-16 05:34:21 +01:00
|
|
|
$dateFormatMap = array(
|
2017-01-26 05:20:08 +01:00
|
|
|
'yyyy-MM-dd' => DBDatetime::now()->Format('yyyy-MM-dd'),
|
|
|
|
'yyyy/MM/dd' => DBDatetime::now()->Format('yyyy/MM/dd'),
|
|
|
|
'MM/dd/yyyy' => DBDatetime::now()->Format('MM/dd/yyyy'),
|
|
|
|
'dd/MM/yyyy' => DBDatetime::now()->Format('dd/MM/yyyy'),
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
2017-01-26 05:20:08 +01:00
|
|
|
$dateFormatMap[$defaultDateFormat] = DBDatetime::now()->Format($defaultDateFormat) . ' (default)';
|
2016-12-16 05:34:21 +01:00
|
|
|
$field = new MemberDatetimeOptionsetField(
|
|
|
|
'DateFormat',
|
|
|
|
'Date format',
|
|
|
|
$dateFormatMap
|
|
|
|
);
|
2017-01-26 05:20:08 +01:00
|
|
|
$field->setValue($member->getDateFormat());
|
2016-12-16 05:34:21 +01:00
|
|
|
return $field;
|
|
|
|
}
|
2010-10-15 05:23:02 +02:00
|
|
|
|
2017-01-26 05:20:08 +01:00
|
|
|
/**
|
|
|
|
* @param Member $member
|
|
|
|
* @return MemberDatetimeOptionsetField
|
|
|
|
*/
|
2016-12-16 05:34:21 +01:00
|
|
|
protected function createTimeFormatFieldForMember($member)
|
|
|
|
{
|
2017-01-26 05:20:08 +01:00
|
|
|
$defaultTimeFormat = $member->getDefaultTimeFormat();
|
2016-12-16 05:34:21 +01:00
|
|
|
$timeFormatMap = array(
|
2017-01-26 05:20:08 +01:00
|
|
|
'h:mm a' => DBDatetime::now()->Format('h:mm a'),
|
|
|
|
'H:mm' => DBDatetime::now()->Format('H:mm'),
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
2017-01-26 05:20:08 +01:00
|
|
|
$timeFormatMap[$defaultTimeFormat] = DBDatetime::now()->Format($defaultTimeFormat) . ' (default)';
|
2016-12-16 05:34:21 +01:00
|
|
|
$field = new MemberDatetimeOptionsetField(
|
|
|
|
'TimeFormat',
|
|
|
|
'Time format',
|
|
|
|
$timeFormatMap
|
|
|
|
);
|
2017-01-26 05:20:08 +01:00
|
|
|
$field->setValue($member->getTimeFormat());
|
2016-12-16 05:34:21 +01:00
|
|
|
return $field;
|
|
|
|
}
|
2010-10-15 05:23:02 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
public function testDateFormatDefaultCheckedInFormField()
|
|
|
|
{
|
2017-01-26 05:20:08 +01:00
|
|
|
/** @var Member $member */
|
|
|
|
$member = $this->objFromFixture(Member::class, 'noformatmember');
|
|
|
|
$field = $this->createDateFormatFieldForMember($member);
|
|
|
|
/** @skipUpgrade */
|
2016-12-16 05:34:21 +01:00
|
|
|
$field->setForm(
|
|
|
|
new Form(
|
|
|
|
new Controller(),
|
|
|
|
'Form',
|
|
|
|
new FieldList(),
|
|
|
|
new FieldList()
|
|
|
|
)
|
|
|
|
); // fake form
|
2017-01-26 05:20:08 +01:00
|
|
|
// `MMM d, y` is default format for default locale (en_US)
|
2016-12-16 05:34:21 +01:00
|
|
|
$parser = new CSSContentParser($field->Field());
|
2017-01-26 05:20:08 +01:00
|
|
|
$xmlArr = $parser->getBySelector('#Form_Form_DateFormat_MMM_d_y');
|
2016-12-16 05:34:21 +01:00
|
|
|
$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()
|
|
|
|
{
|
2017-01-26 05:20:08 +01:00
|
|
|
/** @var Member $member */
|
|
|
|
$member = $this->objFromFixture(Member::class, 'noformatmember');
|
|
|
|
$field = $this->createTimeFormatFieldForMember($member);
|
|
|
|
/** @skipUpgrade */
|
2016-12-16 05:34:21 +01:00
|
|
|
$field->setForm(
|
|
|
|
new Form(
|
|
|
|
new Controller(),
|
|
|
|
'Form',
|
|
|
|
new FieldList(),
|
|
|
|
new FieldList()
|
|
|
|
)
|
|
|
|
); // fake form
|
2017-01-26 05:20:08 +01:00
|
|
|
// `h:mm:ss a` is the default for en_US locale
|
2016-12-16 05:34:21 +01:00
|
|
|
$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()
|
|
|
|
{
|
2017-01-26 05:20:08 +01:00
|
|
|
/** @var Member $member */
|
2016-12-16 05:34:21 +01:00
|
|
|
$member = $this->objFromFixture(Member::class, 'noformatmember');
|
|
|
|
$member->setField('DateFormat', 'MM/dd/yyyy');
|
|
|
|
$field = $this->createDateFormatFieldForMember($member);
|
2017-01-26 05:20:08 +01:00
|
|
|
/** @skipUpgrade */
|
2016-12-16 05:34:21 +01:00
|
|
|
$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()
|
|
|
|
{
|
2017-01-26 05:20:08 +01:00
|
|
|
/** @var Member $member */
|
2016-12-16 05:34:21 +01:00
|
|
|
$member = $this->objFromFixture(Member::class, 'noformatmember');
|
|
|
|
$member->setField('DateFormat', 'dd MM yy');
|
|
|
|
$field = $this->createDateFormatFieldForMember($member);
|
2017-01-26 05:20:08 +01:00
|
|
|
/** @skipUpgrade */
|
2016-12-16 05:34:21 +01:00
|
|
|
$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));
|
2017-01-26 05:20:08 +01:00
|
|
|
$field->setSubmittedValue([
|
2016-12-16 05:34:21 +01:00
|
|
|
'Options' => '__custom__',
|
|
|
|
'Custom' => 'dd MM yyyy'
|
2017-01-26 05:20:08 +01:00
|
|
|
]);
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertTrue($field->validate($validator));
|
2017-01-26 05:20:08 +01:00
|
|
|
$field->setSubmittedValue([
|
2016-12-16 05:34:21 +01:00
|
|
|
'Options' => '__custom__',
|
|
|
|
'Custom' => 'sdfdsfdfd1244'
|
2017-01-26 05:20:08 +01:00
|
|
|
]);
|
|
|
|
// @todo - Be less forgiving of invalid CLDR date format strings
|
|
|
|
$this->assertTrue($field->validate($validator));
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
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
|
|
|
}
|