silverstripe-framework/tests/forms/NumericFieldTest.php

201 lines
4.9 KiB
PHP
Raw Normal View History

<?php
/**
* @package framework
* @subpackage tests
*/
class NumericFieldTest extends SapphireTest {
2014-08-15 08:53:05 +02:00
protected $usesDatabase = false;
/**
* In some cases and locales, validation expects non-breaking spaces.
*
* Duplicates non-public NumericField::clean method
*
* @param string $input
* @return string The input value, with all spaces replaced with non-breaking spaces
*/
protected function clean($input) {
$nbsp = html_entity_decode('&nbsp;', null, 'UTF-8');
return str_replace(' ', $nbsp, trim($input));
}
protected function checkInputValidation($locale, $tests) {
i18n::set_locale($locale);
$field = new NumericField('Number');
$validator = new RequiredFields('Number');
foreach($tests as $input => $output) {
// Both decimal and thousands B
$field->setValue($input);
if($output === false) {
$this->assertFalse(
$field->validate($validator),
"Expect validation to fail for input $input in locale $locale"
);
$this->assertEquals(
0,
$field->dataValue(),
"Expect invalid value to be rewritten to 0 in locale $locale"
);
// Even invalid values shouldn't be rewritten
$this->assertEquals(
$this->clean($input),
$field->Value(),
"Expected input $input to be saved in the field in locale $locale"
);
} else {
$this->assertTrue(
$field->validate($validator),
"Expect validation to succeed for $input in locale $locale"
);
$this->assertEquals(
$output,
$field->dataValue(),
"Expect value $input to be mapped to $output in locale $locale"
);
}
}
}
/**
* Test that data loaded in via Form::loadDataFrom(DataObject) will populate the field correctly,
* and can format the database value appropriately for the frontend
*
* @param string $locale
* @param array $tests
*/
public function checkDataFormatting($locale, $tests) {
i18n::set_locale($locale);
$field = new NumericField('Number');
$form = new Form(new Controller(), 'Form', new FieldList($field), new FieldList());
$dataObject = new NumericFieldTest_Object();
foreach($tests as $input => $output) {
// Given a dataobject as a context, the field should assume the field value is not localised
$dataObject->Number = (string)$input;
$form->loadDataFrom($dataObject, Form::MERGE_CLEAR_MISSING);
// Test value
$this->assertEquals(
$input,
$field->dataValue(),
"Expected $input loaded via dataobject to be left intact in locale $locale"
);
// Test expected formatted value (Substitute nbsp for spaces)
$this->assertEquals(
$this->clean($output),
$field->Value(),
"Expected $input to be formatted as $output in locale $locale"
);
}
}
/**
* German locale values (same as dutch)
*/
public function testGermanLocales() {
$this->checkDataFormatting('de_DE', $formatting = array(
'13000' => "13.000",
'15' => '15',
'12.0' => '12,0',
'12.1' => '12,1',
'14000.5' => "14.000,5",
));
$this->checkDataFormatting('nl_NL', $formatting);
$this->checkInputValidation('de_DE', $validation = array(
'13000' => 13000,
'12,00' => 12.00,
'12.00' => false,
'11 000' => false,
'11.000' => 11000,
'11,000' => 11.0,
'15 000,5' => false,
'15 000.5' => false,
'15.000,5' => 15000.5,
'15,000.5' => false,
));
$this->checkInputValidation('nl_NL', $validation);
}
/**
* French locale values
*/
public function testFrenchLocales() {
$this->checkDataFormatting('fr_FR', array(
'13000' => "13 000",
'15' => '15',
'12.0' => '12,0',
'12.1' => '12,1',
'14000.5' => "14 000,5",
));
$this->checkInputValidation('fr_FR', array(
'13000' => 13000,
'12,00' => 12.00,
'12.00' => false,
'11 000' => 11000,
'11.000' => false,
'11,000' => 11.000,
'15 000,5' => 15000.5,
'15 000.5' => false,
'15.000,5' => false,
'15,000.5' => false,
));
}
/**
* US locale values
*/
public function testUSLocales() {
$this->checkDataFormatting('en_US', array(
'13000' => "13,000",
'15' => '15',
'12.0' => '12.0',
'12.1' => '12.1',
'14000.5' => "14,000.5",
));
$this->checkInputValidation('en_US', array(
'13000' => 13000,
'12,00' => false,
'12.00' => 12.00,
'11 000' => false,
'11.000' => 11.0,
'11,000' => 11000,
'15 000,5' => false,
'15 000.5' => false,
'15.000,5' => false,
'15,000.5' => 15000.5,
));
}
public function testReadonly() {
i18n::set_locale('en_US');
$field = new NumericField('Number');
$this->assertRegExp("#<span[^>]+>\s*0\s*<\/span>#", "".$field->performReadonlyTransformation()->Field()."");
}
public function testNumberTypeOnInputHtml() {
$field = new NumericField('Number');
$html = $field->Field();
$this->assertContains('type="number"', $html, 'number type set');
$this->assertContains('step="any"', $html, 'step value set to any');
}
}
class NumericFieldTest_Object extends DataObject implements TestOnly {
private static $db = array(
'Number' => 'Float'
);
Merge remote-tracking branch 'origin/3.1' Conflicts: .travis.yml README.md admin/code/LeftAndMain.php admin/css/screen.css admin/scss/screen.scss api/RestfulService.php conf/ConfigureFromEnv.php control/injector/ServiceConfigurationLocator.php control/injector/SilverStripeServiceConfigurationLocator.php core/ClassInfo.php core/Object.php css/AssetUploadField.css css/ComplexTableField_popup.css dev/CSSContentParser.php dev/DevelopmentAdmin.php docs/en/changelogs/index.md docs/en/misc/contributing/code.md docs/en/reference/execution-pipeline.md filesystem/GD.php filesystem/ImagickBackend.php filesystem/Upload.php forms/Form.php forms/FormField.php forms/HtmlEditorConfig.php forms/gridfield/GridFieldDetailForm.php forms/gridfield/GridFieldSortableHeader.php lang/en.yml model/Aggregate.php model/DataList.php model/DataObject.php model/DataQuery.php model/Image.php model/MySQLDatabase.php model/SQLQuery.php model/fieldtypes/HTMLText.php model/fieldtypes/Text.php scss/AssetUploadField.scss search/filters/SearchFilter.php security/Authenticator.php security/LoginForm.php security/Member.php security/MemberAuthenticator.php security/MemberLoginForm.php security/Security.php tests/behat/features/bootstrap/SilverStripe/Framework/Test/Behaviour/CmsFormsContext.php tests/control/HTTPTest.php tests/control/RequestHandlingTest.php tests/filesystem/UploadTest.php tests/forms/FormTest.php tests/forms/NumericFieldTest.php tests/model/DataListTest.php tests/model/DataObjectTest.php tests/model/TextTest.php tests/security/MemberAuthenticatorTest.php tests/security/SecurityDefaultAdminTest.php tests/view/SSViewerCacheBlockTest.php tests/view/SSViewerTest.php
2014-11-18 00:45:54 +01:00
}