From c706c3f12107e9942cd217fac45266a9027145b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20K=C3=BChne?= Date: Wed, 6 Dec 2023 08:26:33 +0100 Subject: [PATCH] Dont set the locale implicitly, just use the value provided by \Locale::getDefault() if none is set Add tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Matthias Kühne --- src/View/Helper/CurrencyFormat.php | 2 +- src/View/Helper/DateFormat.php | 2 +- src/View/Helper/NumberFormat.php | 2 +- test/View/Helper/CurrencyFormatTest.php | 12 ++++++++++++ test/View/Helper/DateFormatTest.php | 12 ++++++++++++ test/View/Helper/NumberFormatTest.php | 12 ++++++++++++ 6 files changed, 39 insertions(+), 3 deletions(-) diff --git a/src/View/Helper/CurrencyFormat.php b/src/View/Helper/CurrencyFormat.php index a8edbd0c..2e829691 100644 --- a/src/View/Helper/CurrencyFormat.php +++ b/src/View/Helper/CurrencyFormat.php @@ -212,7 +212,7 @@ public function setLocale($locale) public function getLocale() { if ($this->locale === null) { - $this->locale = Locale::getDefault(); + return Locale::getDefault(); } return $this->locale; diff --git a/src/View/Helper/DateFormat.php b/src/View/Helper/DateFormat.php index 5714b9f4..e6707249 100644 --- a/src/View/Helper/DateFormat.php +++ b/src/View/Helper/DateFormat.php @@ -98,7 +98,7 @@ public function setLocale($locale) public function getLocale() { if ($this->locale === null) { - $this->locale = Locale::getDefault(); + return Locale::getDefault(); } return $this->locale; diff --git a/src/View/Helper/NumberFormat.php b/src/View/Helper/NumberFormat.php index 4b4c0648..6cb9537b 100644 --- a/src/View/Helper/NumberFormat.php +++ b/src/View/Helper/NumberFormat.php @@ -214,7 +214,7 @@ public function setLocale($locale) public function getLocale() { if ($this->locale === null) { - $this->locale = Locale::getDefault(); + return Locale::getDefault(); } return $this->locale; diff --git a/test/View/Helper/CurrencyFormatTest.php b/test/View/Helper/CurrencyFormatTest.php index b7206a10..ff2e94b6 100644 --- a/test/View/Helper/CurrencyFormatTest.php +++ b/test/View/Helper/CurrencyFormatTest.php @@ -108,4 +108,16 @@ public static function assertMbStringEquals(string $expected, string $test, stri $test = str_replace(["\xC2\xA0", ' '], '', $test); self::assertEquals($expected, $test, $message); } + + public function testNoImplicitLocale(): void + { + Locale::setDefault('de'); + self::assertEquals(Locale::getDefault(), $this->helper->getLocale()); + + Locale::setDefault('en'); + self::assertEquals(Locale::getDefault(), $this->helper->getLocale()); + + $this->helper->setLocale('es'); + self::assertEquals('es', $this->helper->getLocale()); + } } diff --git a/test/View/Helper/DateFormatTest.php b/test/View/Helper/DateFormatTest.php index 0630cc1b..1cd13d21 100644 --- a/test/View/Helper/DateFormatTest.php +++ b/test/View/Helper/DateFormatTest.php @@ -292,4 +292,16 @@ public function testIntlCalendarIsHandledAsWell(): void $helper->__invoke($calendar, IntlDateFormatter::FULL, IntlDateFormatter::FULL, 'it_IT', 'dd-MM-Y') ); } + + public function testNoImplicitLocale(): void + { + Locale::setDefault('de'); + self::assertEquals(Locale::getDefault(), $this->helper->getLocale()); + + Locale::setDefault('en'); + self::assertEquals(Locale::getDefault(), $this->helper->getLocale()); + + $this->helper->setLocale('es'); + self::assertEquals('es', $this->helper->getLocale()); + } } diff --git a/test/View/Helper/NumberFormatTest.php b/test/View/Helper/NumberFormatTest.php index 8286d1e5..6b7f2765 100644 --- a/test/View/Helper/NumberFormatTest.php +++ b/test/View/Helper/NumberFormatTest.php @@ -196,4 +196,16 @@ public static function assertMbStringEquals(string $expected, string $test, stri $test = str_replace(["\xC2\xA0", ' '], '', $test); self::assertEquals($expected, $test, $message); } + + public function testNoImplicitLocale(): void + { + Locale::setDefault('de'); + self::assertEquals(Locale::getDefault(), $this->helper->getLocale()); + + Locale::setDefault('en'); + self::assertEquals(Locale::getDefault(), $this->helper->getLocale()); + + $this->helper->setLocale('es'); + self::assertEquals('es', $this->helper->getLocale()); + } }