2010-10-15 05:23:02 +02:00
|
|
|
<?php
|
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;
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-10-15 05:23:02 +02:00
|
|
|
/**
|
2014-08-15 08:53:05 +02:00
|
|
|
* @package framework
|
2010-10-15 05:23:02 +02:00
|
|
|
* @subpackage forms
|
|
|
|
*/
|
|
|
|
class MemberDatetimeOptionsetFieldTest extends SapphireTest {
|
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
protected static $fixture_file = 'MemberDatetimeOptionsetFieldTest.yml';
|
2010-10-15 05:23:02 +02:00
|
|
|
|
|
|
|
protected function createDateFormatFieldForMember($member) {
|
|
|
|
require_once 'Zend/Date.php';
|
|
|
|
$defaultDateFormat = Zend_Locale_Format::getDateFormat($member->Locale);
|
|
|
|
$dateFormatMap = array(
|
2014-02-11 12:12:37 +01:00
|
|
|
'yyyy-MM-dd' => Zend_Date::now()->toString('yyyy-MM-dd'),
|
2010-10-15 05:23:02 +02:00
|
|
|
'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)';
|
2011-12-23 10:30:49 +01:00
|
|
|
$field = new MemberDatetimeOptionsetField(
|
2010-10-15 05:23:02 +02:00
|
|
|
'DateFormat',
|
|
|
|
'Date format',
|
|
|
|
$dateFormatMap
|
|
|
|
);
|
|
|
|
$field->setValue($member->DateFormat);
|
|
|
|
return $field;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function createTimeFormatFieldForMember($member) {
|
|
|
|
require_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)';
|
2011-12-23 10:30:49 +01:00
|
|
|
$field = new MemberDatetimeOptionsetField(
|
2010-10-15 05:23:02 +02:00
|
|
|
'TimeFormat',
|
|
|
|
'Time format',
|
|
|
|
$timeFormatMap
|
|
|
|
);
|
|
|
|
$field->setValue($member->TimeFormat);
|
|
|
|
return $field;
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testDateFormatDefaultCheckedInFormField() {
|
2016-08-19 00:51:35 +02:00
|
|
|
Config::inst()->update('SilverStripe\\i18n\\i18n', 'date_format', 'yyyy-MM-dd');
|
2016-06-23 01:37:22 +02:00
|
|
|
$field = $this->createDateFormatFieldForMember($this->objFromFixture('SilverStripe\\Security\\Member', 'noformatmember'));
|
2016-08-19 00:51:35 +02:00
|
|
|
/** @skipUpgrade */
|
2012-09-26 23:34:00 +02:00
|
|
|
$field->setForm(new Form(new MemberDatetimeOptionsetFieldTest_Controller(), 'Form', new FieldList(),
|
|
|
|
new FieldList())); // fake form
|
2010-10-15 05:23:02 +02:00
|
|
|
$parser = new CSSContentParser($field->Field());
|
2014-02-11 12:12:37 +01:00
|
|
|
$xmlArr = $parser->getBySelector('#Form_Form_DateFormat_yyyy-MM-dd');
|
2010-10-15 05:23:02 +02:00
|
|
|
$this->assertEquals('checked', (string) $xmlArr[0]['checked']);
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testTimeFormatDefaultCheckedInFormField() {
|
2016-08-19 00:51:35 +02:00
|
|
|
Config::inst()->update('SilverStripe\\i18n\\i18n', 'time_format', 'h:mm:ss a');
|
2016-06-23 01:37:22 +02:00
|
|
|
$field = $this->createTimeFormatFieldForMember($this->objFromFixture('SilverStripe\\Security\\Member', 'noformatmember'));
|
2016-08-19 00:51:35 +02:00
|
|
|
/** @skipUpgrade */
|
2012-09-26 23:34:00 +02:00
|
|
|
$field->setForm(new Form(new MemberDatetimeOptionsetFieldTest_Controller(), 'Form', new FieldList(),
|
|
|
|
new FieldList())); // fake form
|
2010-10-15 05:23:02 +02:00
|
|
|
$parser = new CSSContentParser($field->Field());
|
2015-02-13 05:35:39 +01:00
|
|
|
$xmlArr = $parser->getBySelector('#Form_Form_TimeFormat_h:mm:ss_a');
|
2010-10-15 05:23:02 +02:00
|
|
|
$this->assertEquals('checked', (string) $xmlArr[0]['checked']);
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testDateFormatChosenIsCheckedInFormField() {
|
2016-06-23 01:37:22 +02:00
|
|
|
$member = $this->objFromFixture('SilverStripe\\Security\\Member', 'noformatmember');
|
2010-10-15 05:23:02 +02:00
|
|
|
$member->setField('DateFormat', 'MM/dd/yyyy');
|
|
|
|
$field = $this->createDateFormatFieldForMember($member);
|
2016-08-19 00:51:35 +02:00
|
|
|
/** @skipUpgrade */
|
2012-09-26 23:34:00 +02:00
|
|
|
$field->setForm(new Form(new MemberDatetimeOptionsetFieldTest_Controller(), 'Form', new FieldList(),
|
|
|
|
new FieldList())); // fake form
|
2010-10-15 05:23:02 +02:00
|
|
|
$parser = new CSSContentParser($field->Field());
|
2010-10-19 00:34:09 +02:00
|
|
|
$xmlArr = $parser->getBySelector('#Form_Form_DateFormat_MM_dd_yyyy');
|
2010-10-15 05:23:02 +02:00
|
|
|
$this->assertEquals('checked', (string) $xmlArr[0]['checked']);
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testDateFormatCustomFormatAppearsInCustomInputInField() {
|
2016-06-23 01:37:22 +02:00
|
|
|
$member = $this->objFromFixture('SilverStripe\\Security\\Member', 'noformatmember');
|
2010-10-15 05:23:02 +02:00
|
|
|
$member->setField('DateFormat', 'dd MM yy');
|
|
|
|
$field = $this->createDateFormatFieldForMember($member);
|
2016-08-19 00:51:35 +02:00
|
|
|
/** @skipUpgrade */
|
2012-09-26 23:34:00 +02:00
|
|
|
$field->setForm(new Form(new MemberDatetimeOptionsetFieldTest_Controller(), 'Form', new FieldList(),
|
|
|
|
new FieldList())); // fake form
|
2010-10-15 05:23:02 +02:00
|
|
|
$parser = new CSSContentParser($field->Field());
|
2015-02-13 05:35:39 +01:00
|
|
|
$xmlInputArr = $parser->getBySelector('.valcustom input');
|
2010-10-15 05:23:02 +02:00
|
|
|
$this->assertEquals('checked', (string) $xmlInputArr[0]['checked']);
|
|
|
|
$this->assertEquals('dd MM yy', (string) $xmlInputArr[1]['value']);
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testDateFormValid() {
|
2011-12-23 10:30:49 +01:00
|
|
|
$field = new MemberDatetimeOptionsetField('DateFormat', 'DateFormat');
|
2014-11-12 03:19:12 +01:00
|
|
|
$validator = new RequiredFields();
|
|
|
|
$this->assertTrue($field->validate($validator));
|
2015-02-13 05:35:39 +01:00
|
|
|
$field->setValue(array(
|
|
|
|
'Options' => '__custom__',
|
|
|
|
'Custom' => 'dd MM yyyy'
|
|
|
|
));
|
2014-11-12 03:19:12 +01:00
|
|
|
$this->assertTrue($field->validate($validator));
|
2015-02-13 05:35:39 +01:00
|
|
|
$field->setValue(array(
|
|
|
|
'Options' => '__custom__',
|
|
|
|
'Custom' => 'sdfdsfdfd1244'
|
|
|
|
));
|
2014-11-12 03:19:12 +01:00
|
|
|
$this->assertFalse($field->validate($validator));
|
2010-10-15 05:23:02 +02:00
|
|
|
}
|
|
|
|
|
2016-07-06 10:16:27 +02:00
|
|
|
public function testDescriptionTemplate() {
|
|
|
|
$field = new MemberDatetimeOptionsetField('DateFormat', 'DateFormat');
|
|
|
|
|
|
|
|
$this->assertEmpty($field->getDescription());
|
|
|
|
|
|
|
|
$field->setDescription('Test description');
|
|
|
|
$this->assertEquals('Test description', $field->getDescription());
|
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
$field->setDescriptionTemplate(get_class($field).'_description_time');
|
2016-07-06 10:16:27 +02:00
|
|
|
$this->assertNotEmpty($field->getDescription());
|
|
|
|
$this->assertNotEquals('Test description', $field->getDescription());
|
|
|
|
}
|
|
|
|
|
2010-10-15 05:23:02 +02:00
|
|
|
}
|
|
|
|
class MemberDatetimeOptionsetFieldTest_Controller extends Controller {
|
|
|
|
|
2016-07-29 01:22:03 +02:00
|
|
|
public function Link($action = null) {
|
|
|
|
return Controller::join_links('test', $action, '/');
|
2010-10-15 05:23:02 +02:00
|
|
|
}
|
|
|
|
|
2012-04-12 02:20:18 +02:00
|
|
|
}
|