From b56f3a867146c01b59cfcddc887ed399c0af8901 Mon Sep 17 00:00:00 2001 From: stanjg Date: Fri, 1 Jun 2018 16:22:06 +0200 Subject: [PATCH] Expanded the middleware test --- app/Http/Middleware/LanguageMiddleware.php | 2 +- .../Middleware/LanguageMiddlewareTest.php | 19 +++++++++++++++++-- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/app/Http/Middleware/LanguageMiddleware.php b/app/Http/Middleware/LanguageMiddleware.php index f0c341276..04e5f3a98 100644 --- a/app/Http/Middleware/LanguageMiddleware.php +++ b/app/Http/Middleware/LanguageMiddleware.php @@ -48,7 +48,7 @@ class LanguageMiddleware */ public function handle(Request $request, Closure $next) { - $this->app->setLocale($request->user()->language ?? $this->config->get('app.locale')); + $this->app->setLocale($request->user()->language ?? $this->config->get('app.locale', 'en')); return $next($request); } diff --git a/tests/Unit/Http/Middleware/LanguageMiddlewareTest.php b/tests/Unit/Http/Middleware/LanguageMiddlewareTest.php index c156665aa..3e1662a19 100644 --- a/tests/Unit/Http/Middleware/LanguageMiddlewareTest.php +++ b/tests/Unit/Http/Middleware/LanguageMiddlewareTest.php @@ -6,6 +6,7 @@ use Mockery as m; use Illuminate\Foundation\Application; use Illuminate\Contracts\Config\Repository; use Pterodactyl\Http\Middleware\LanguageMiddleware; +use Pterodactyl\Models\User; class LanguageMiddlewareTest extends MiddlewareTestCase { @@ -31,16 +32,30 @@ class LanguageMiddlewareTest extends MiddlewareTestCase } /** - * Test that a language is defined via the middleware. + * Test that a language is defined via the middleware for guests. */ - public function testLanguageIsSet() + public function testLanguageIsSetForGuest() { + $this->request->shouldReceive('user')->withNoArgs()->andReturnNull(); + $this->config->shouldReceive('get')->with('app.locale', 'en')->once()->andReturn('en'); $this->appMock->shouldReceive('setLocale')->with('en')->once()->andReturnNull(); $this->getMiddleware()->handle($this->request, $this->getClosureAssertions()); } + /** + * Test that a language is defined via the middleware for a user. + */ + public function testLanguageIsSetWithAuthenticatedUser() { + $user = factory(User::class)->make(['language' => 'de']); + + $this->request->shouldReceive('user')->withNoArgs()->andReturn($user); + $this->appMock->shouldReceive('setLocale')->with('de')->once()->andReturnNull(); + + $this->getMiddleware()->handle($this->request, $this->getClosureAssertions()); + } + /** * Return an instance of the middleware using mocked dependencies. *