From f506ea4439a99d1bc2b15a6cc1d839893338927d Mon Sep 17 00:00:00 2001 From: Mohammad Alavi Date: Fri, 1 Jul 2022 19:36:25 +0430 Subject: [PATCH] fix: middleware and tests --- Middlewares/LocalizationMiddleware.php | 4 ++++ Providers/MainServiceProvider.php | 22 +++++++---------- .../CheckLocalizationMiddlewareTest.php | 24 +++++++++++++++---- 3 files changed, 31 insertions(+), 19 deletions(-) diff --git a/Middlewares/LocalizationMiddleware.php b/Middlewares/LocalizationMiddleware.php index a9a2e69..6e2e252 100644 --- a/Middlewares/LocalizationMiddleware.php +++ b/Middlewares/LocalizationMiddleware.php @@ -14,6 +14,10 @@ class LocalizationMiddleware extends Middleware { public function handle(Request $request, Closure $next) { + if (!config('vendor-localization.localization_enabled')) { + return $next($request); + } + // find and validate the lang on that request $lang = $this->validateLanguage($this->findLanguage($request)); diff --git a/Providers/MainServiceProvider.php b/Providers/MainServiceProvider.php index e9ea248..afae239 100644 --- a/Providers/MainServiceProvider.php +++ b/Providers/MainServiceProvider.php @@ -11,24 +11,18 @@ */ class MainServiceProvider extends ParentMainServiceProvider { + /** + * Container Service Providers. + */ + public array $serviceProviders = [ + LocalizationServiceProvider::class, + MiddlewareServiceProvider::class, + ]; + /** * Container Aliases */ public array $aliases = [ ]; - - public function __construct( - $app, - /** - * Container Service Providers. - */ - public array $serviceProviders = [ - LocalizationServiceProvider::class, - ]) { - if (config('vendor-localization.localization_enabled')) { - array_push($serviceProviders, MiddlewareServiceProvider::class); - } - parent::__construct($app); - } } diff --git a/UI/API/Tests/Functional/CheckLocalizationMiddlewareTest.php b/UI/API/Tests/Functional/CheckLocalizationMiddlewareTest.php index 7a3645e..5b187c7 100644 --- a/UI/API/Tests/Functional/CheckLocalizationMiddlewareTest.php +++ b/UI/API/Tests/Functional/CheckLocalizationMiddlewareTest.php @@ -27,8 +27,13 @@ public function testIfMiddlewareSetsDefaultAppLanguage(): void $response = $this->makeCall($data, $requestHeaders); - $response->assertStatus(200) - ->assertHeader('content-language', $defaultLanguage); + if (config('vendor-localization.localization_enabled')) { + $response->assertStatus(200) + ->assertHeader('content-language', $defaultLanguage); + } else { + $response->assertStatus(200) + ->assertHeaderMissing('content-language', $defaultLanguage); + } } public function testIfMiddlewareSetsCustomLanguage(): void @@ -41,8 +46,13 @@ public function testIfMiddlewareSetsCustomLanguage(): void $response = $this->makeCall($data, $requestHeaders); - $response->assertStatus(200) - ->assertHeader('content-language', $language); + if (config('vendor-localization.localization_enabled')) { + $response->assertStatus(200) + ->assertHeader('content-language', $language); + } else { + $response->assertStatus(200) + ->assertHeaderMissing('content-language', $language); + } } public function testIfMiddlewareThrowsErrorOnWrongLanguage(): void @@ -55,6 +65,10 @@ public function testIfMiddlewareThrowsErrorOnWrongLanguage(): void $response = $this->makeCall($data, $requestHeaders); - $response->assertStatus(412); + if (config('vendor-localization.localization_enabled')) { + $response->assertStatus(412); + } else { + $response->assertStatus(200); + } } }