From c5d676fa4e1d37fa4d940b97adb7f2ad80acdbb8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sam=20Minn=C3=A9e?= Date: Fri, 9 Oct 2020 10:33:51 +1300 Subject: [PATCH] FIX Avoid test failure on use of narrow-NBSP (#9725) 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. Co-authored-by: Robbie Averill --- tests/php/Forms/FormTest.php | 27 +++++++++++++++++++++++---- tests/php/Forms/NumericFieldTest.php | 13 ++++++++++--- 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/tests/php/Forms/FormTest.php b/tests/php/Forms/FormTest.php index b4bbee79f..b31a4a254 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,23 @@ 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) + ); } /**