From 7fdea8bb28941d58b6c0893618ecff2eecc5c86b Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Mon, 17 Jun 2024 11:51:17 +0300 Subject: [PATCH] Fixed route redirection --- .../LocalizationByParameterPrefix.php | 24 +++++++++++++++++++ .../LocalizationByParameterWithRedirect.php | 4 ++-- .../ParameterPrefixForFacadeTest.php | 18 +++++++++++++- .../ParameterPrefixForMacroTest.php | 18 +++++++++++++- 4 files changed, 60 insertions(+), 4 deletions(-) diff --git a/src/Middlewares/LocalizationByParameterPrefix.php b/src/Middlewares/LocalizationByParameterPrefix.php index 24514c9..4a5ff83 100644 --- a/src/Middlewares/LocalizationByParameterPrefix.php +++ b/src/Middlewares/LocalizationByParameterPrefix.php @@ -7,6 +7,8 @@ use Illuminate\Http\Request; use Illuminate\Support\Arr; use Illuminate\Support\Str; +use LaravelLang\Config\Facades\Config; +use LaravelLang\Locales\Facades\Locales; use LaravelLang\Routes\Helpers\Route; class LocalizationByParameterPrefix extends LocalizationByParameterWithRedirect @@ -27,8 +29,30 @@ protected function routeName(Request $request): ?string return null; } + protected function invalidParameter(Request $request): ?bool + { + return ! $this->hasParameter($request) + || ! $this->trim($this->detect($request)) + || ! $this->isInstalled($this->detect($request)) + || $this->isDefault($this->detect($request)); + } + + protected function isDefault(bool|float|int|string|null $locale): bool + { + if ($this->allowRedirect()) { + return Locales::info($locale)->code === Locales::getDefault()->code; + } + + return false; + } + protected function routePrefix(): string { return Route::prefix(); } + + protected function allowRedirect(): bool + { + return Config::shared()->routes->redirect; + } } diff --git a/src/Middlewares/LocalizationByParameterWithRedirect.php b/src/Middlewares/LocalizationByParameterWithRedirect.php index 3dff9cf..fa0979a 100644 --- a/src/Middlewares/LocalizationByParameterWithRedirect.php +++ b/src/Middlewares/LocalizationByParameterWithRedirect.php @@ -71,8 +71,8 @@ protected function defaultLocale(): string return Locales::getDefault()->code; } - protected function isInstalled(bool|float|int|string|null $parameter): bool + protected function isInstalled(bool|float|int|string|null $locale): bool { - return Locales::isInstalled((string) $parameter); + return Locales::isInstalled((string) $locale); } } diff --git a/tests/Feature/Middlewares/ParameterPrefixForFacadeTest.php b/tests/Feature/Middlewares/ParameterPrefixForFacadeTest.php index 908c5e0..1952ff1 100644 --- a/tests/Feature/Middlewares/ParameterPrefixForFacadeTest.php +++ b/tests/Feature/Middlewares/ParameterPrefixForFacadeTest.php @@ -2,6 +2,7 @@ declare(strict_types=1); +use LaravelLang\Config\Enums\Name; use LaravelLang\Routes\Helpers\Route as RouteName; use Tests\Constants\LocaleValue; @@ -15,7 +16,22 @@ ->assertJsonPath($foo, LocaleValue::TranslationFrench); }); -test('main locale', function (string $locale) { +test('main locale with enabled redirect', function (string $locale) { + config()->set(Name::Shared() . '.routes.redirect_default', true); + + $foo = 'test'; + + getJson(route(RouteName::prefix() . 'via.group.facade', compact('foo', 'locale'))) + ->assertRedirectToRoute('via.group.facade', [ + 'foo' => $foo, + ]); + + assertEventNotDispatched(); +})->with('main-locales'); + +test('main locale with disabled redirect', function (string $locale) { + config()->set(Name::Shared() . '.routes.redirect_default', false); + $foo = 'test'; getJson(route(RouteName::prefix() . 'via.group.facade', compact('foo', 'locale'))) diff --git a/tests/Feature/Middlewares/ParameterPrefixForMacroTest.php b/tests/Feature/Middlewares/ParameterPrefixForMacroTest.php index 93890e4..4945453 100644 --- a/tests/Feature/Middlewares/ParameterPrefixForMacroTest.php +++ b/tests/Feature/Middlewares/ParameterPrefixForMacroTest.php @@ -2,6 +2,7 @@ declare(strict_types=1); +use LaravelLang\Config\Enums\Name; use LaravelLang\Routes\Helpers\Route as RouteName; use Tests\Constants\LocaleValue; @@ -15,7 +16,22 @@ ->assertJsonPath($foo, LocaleValue::TranslationFrench); }); -test('main locale', function (string $locale) { +test('main locale with enabled redirect', function (string $locale) { + config()->set(Name::Shared() . '.routes.redirect_default', true); + + $foo = 'test'; + + getJson(route(RouteName::prefix() . 'via.group.macro', compact('foo', 'locale'))) + ->assertRedirectToRoute('via.group.macro', [ + 'foo' => $foo, + ]); + + assertEventNotDispatched(); +})->with('main-locales'); + +test('main locale with disabled redirect', function (string $locale) { + config()->set(Name::Shared() . '.routes.redirect_default', false); + $foo = 'test'; getJson(route(RouteName::prefix() . 'via.group.macro', compact('foo', 'locale')))