Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: lang() causes Error on CLI #6209

Merged
merged 3 commits into from
Jul 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion system/Config/Services.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
use Config\Toolbar as ToolbarConfig;
use Config\Validation as ValidationConfig;
use Config\View as ViewConfig;
use Locale;

/**
* Services Configuration file.
Expand Down Expand Up @@ -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);
}
Expand Down
17 changes: 12 additions & 5 deletions tests/system/CommonFunctionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ final class CommonFunctionsTest extends CIUnitTestCase
protected function setUp(): void
{
unset($_ENV['foo'], $_SERVER['foo']);
Services::reset();
$this->resetServices();

parent::setUp();
}
Expand Down Expand Up @@ -593,8 +593,6 @@ public function testTraceWithCSP()

public function testCspStyleNonce()
{
$this->resetServices();

$config = config('App');
$config->CSPEnabled = true;

Expand All @@ -603,11 +601,20 @@ public function testCspStyleNonce()

public function testCspScriptNonce()
{
$this->resetServices();

$config = config('App');
$config->CSPEnabled = true;

$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();
}
}