From 310697094d6bc2adb5d1cd15f55e3ec30dc83a31 Mon Sep 17 00:00:00 2001 From: kenjis Date: Wed, 29 Jun 2022 11:43:41 +0900 Subject: [PATCH 1/3] test: refactor tests --- tests/system/CommonFunctionsTest.php | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/tests/system/CommonFunctionsTest.php b/tests/system/CommonFunctionsTest.php index c41952fd7554..572a2c658362 100644 --- a/tests/system/CommonFunctionsTest.php +++ b/tests/system/CommonFunctionsTest.php @@ -50,7 +50,7 @@ final class CommonFunctionsTest extends CIUnitTestCase protected function setUp(): void { unset($_ENV['foo'], $_SERVER['foo']); - Services::reset(); + $this->resetServices(); parent::setUp(); } @@ -593,8 +593,6 @@ public function testTraceWithCSP() public function testCspStyleNonce() { - $this->resetServices(); - $config = config('App'); $config->CSPEnabled = true; @@ -603,8 +601,6 @@ public function testCspStyleNonce() public function testCspScriptNonce() { - $this->resetServices(); - $config = config('App'); $config->CSPEnabled = true; From 765c6cd1cbef5df518129396713a09c2d347a51f Mon Sep 17 00:00:00 2001 From: kenjis Date: Thu, 30 Jun 2022 17:39:33 +0900 Subject: [PATCH 2/3] test: add test for lang() error on CLI --- tests/system/CommonFunctionsTest.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/tests/system/CommonFunctionsTest.php b/tests/system/CommonFunctionsTest.php index 572a2c658362..5c2b6efa7f83 100644 --- a/tests/system/CommonFunctionsTest.php +++ b/tests/system/CommonFunctionsTest.php @@ -606,4 +606,15 @@ public function testCspScriptNonce() $this->assertStringStartsWith('nonce="', csp_script_nonce()); } + + public function testLangOnCLI() + { + Services::createRequest(new App(), true); + + $message = lang('CLI.generator.fileCreate', ['TestController.php']); + + $this->assertSame('File created: TestController.php', $message); + + $this->resetServices(); + } } From a2a23a6823b0f27e20a054bb4cc5bf0a989587b4 Mon Sep 17 00:00:00 2001 From: kenjis Date: Thu, 30 Jun 2022 17:41:26 +0900 Subject: [PATCH 3/3] fix: lang() causes error on CLI Call to undefined method CodeIgniter\HTTP\CLIRequest::getLocale() --- system/Config/Services.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/system/Config/Services.php b/system/Config/Services.php index 69b666ff04d4..169795c77a44 100644 --- a/system/Config/Services.php +++ b/system/Config/Services.php @@ -75,6 +75,7 @@ use Config\Toolbar as ToolbarConfig; use Config\Validation as ValidationConfig; use Config\View as ViewConfig; +use Locale; /** * Services Configuration file. @@ -363,8 +364,14 @@ public static function language(?string $locale = null, bool $getShared = true) return static::getSharedInstance('language', $locale)->setLocale($locale); } + if (AppServices::request() instanceof IncomingRequest) { + $requestLocale = AppServices::request()->getLocale(); + } else { + $requestLocale = Locale::getDefault(); + } + // Use '?:' for empty string check - $locale = $locale ?: AppServices::request()->getLocale(); + $locale = $locale ?: $requestLocale; return new Language($locale); }