From 38e509dd96816bcba75021da4f05efdae36a3f55 Mon Sep 17 00:00:00 2001 From: Sam Minnee Date: Mon, 28 Sep 2020 15:24:15 +1300 Subject: [PATCH] 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. --- tests/php/Forms/FormTest.php | 26 ++++++++++++++++++++++---- tests/php/Forms/NumericFieldTest.php | 13 ++++++++++--- 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/tests/php/Forms/FormTest.php b/tests/php/Forms/FormTest.php index b4bbee79f..bc2a12966 100644 --- a/tests/php/Forms/FormTest.php +++ b/tests/php/Forms/FormTest.php @@ -1052,8 +1052,8 @@ class FormTest extends FunctionalTest ); $this->assertContains( - 'clean($body), 'Our reloaded form should contain a SomeFrenchNumericField with the value "9 876,5432"' ); @@ -1064,8 +1064,8 @@ class FormTest extends FunctionalTest ); $this->assertContains( - 'clean($body), 'Our reloaded form should contain a SomeFrenchMoneyField[Amount] with the value "9 876,54"' ); @@ -1108,4 +1108,22 @@ class FormTest extends FunctionalTest 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(' ', null, 'UTF-8'), + html_entity_decode(' ', null, 'UTF-8'), // narrow non-breaking space + ], + ' ', + trim($input) + ); + } } diff --git a/tests/php/Forms/NumericFieldTest.php b/tests/php/Forms/NumericFieldTest.php index 622c3783b..f8b146e08 100644 --- a/tests/php/Forms/NumericFieldTest.php +++ b/tests/php/Forms/NumericFieldTest.php @@ -13,16 +13,23 @@ class NumericFieldTest extends SapphireTest /** * 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 * * @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) { - $nbsp = html_entity_decode(' ', null, 'UTF-8'); - return str_replace(' ', $nbsp, trim($input)); + return str_replace( + [ + html_entity_decode(' ', null, 'UTF-8'), + html_entity_decode(' ', null, 'UTF-8'), // narrow non-breaking space + ], + ' ', + trim($input) + ); } /**