FIX: Avoid test failure on use of narrow-NBSP

For whatever reason (different locale version) my local dev env uses
narrow-NBSPs (Unicode 8239) rather than regular NBSP in its localised
strings. This patch makes the tests robust against this difference.

Note that this occurred running the tests in Lando.
This commit is contained in:
Sam Minnee 2020-09-28 15:24:15 +13:00
parent bb7cf17e3c
commit 38e509dd96
2 changed files with 32 additions and 7 deletions

View File

@ -1052,8 +1052,8 @@ class FormTest extends FunctionalTest
); );
$this->assertContains( $this->assertContains(
'<input type="text" name="SomeFrenchNumericField" value="9 876,5432" ', '<input type="text" name="SomeFrenchNumericField" value="9 876,5432" ',
$body, $this->clean($body),
'Our reloaded form should contain a SomeFrenchNumericField with the value "9 876,5432"' 'Our reloaded form should contain a SomeFrenchNumericField with the value "9 876,5432"'
); );
@ -1064,8 +1064,8 @@ class FormTest extends FunctionalTest
); );
$this->assertContains( $this->assertContains(
'<input type="text" name="SomeFrenchMoneyField[Amount]" value="9 876,54" ', '<input type="text" name="SomeFrenchMoneyField[Amount]" value="9 876,54" ',
$body, $this->clean($body),
'Our reloaded form should contain a SomeFrenchMoneyField[Amount] with the value "9 876,54"' 'Our reloaded form should contain a SomeFrenchMoneyField[Amount] with the value "9 876,54"'
); );
@ -1108,4 +1108,22 @@ class FormTest extends FunctionalTest
new FieldList() new FieldList()
); );
} }
/**
* In some cases and locales, validation expects non-breaking spaces.
* This homogenises narrow and regular NBSPs to a regular space character
*
* @param string $input
* @return string The input value, with all non-breaking spaces replaced with spaces
*/
protected function clean($input)
{
return str_replace(
[
html_entity_decode('&nbsp;', null, 'UTF-8'),
html_entity_decode('&#8239;', null, 'UTF-8'), // narrow non-breaking space
],
' ',
trim($input)
);
}
} }

View File

@ -13,16 +13,23 @@ class NumericFieldTest extends SapphireTest
/** /**
* In some cases and locales, validation expects non-breaking spaces. * In some cases and locales, validation expects non-breaking spaces.
* This homogenises narrow and regular NBSPs to a regular space character
* *
* Duplicates non-public NumericField::clean method * Duplicates non-public NumericField::clean method
* *
* @param string $input * @param string $input
* @return string The input value, with all spaces replaced with non-breaking spaces * @return string The input value, with all non-breaking spaces replaced with spaces
*/ */
protected function clean($input) protected function clean($input)
{ {
$nbsp = html_entity_decode('&nbsp;', null, 'UTF-8'); return str_replace(
return str_replace(' ', $nbsp, trim($input)); [
html_entity_decode('&nbsp;', null, 'UTF-8'),
html_entity_decode('&#8239;', null, 'UTF-8'), // narrow non-breaking space
],
' ',
trim($input)
);
} }
/** /**