Skip to content
Open
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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"laravel/framework": "^10.0|^11.0|^12.0"
},
"require-dev": {
"orchestra/testbench-browser-kit": "^8.5|^9.0|^10.0",
"orchestra/testbench": "^8.5|^9.0|^10.0",
"phpunit/phpunit": "^10.1|^11.0"
},
"suggest": {
Expand Down
147 changes: 63 additions & 84 deletions tests/LaravelLocalizationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@

use PHPUnit\Framework\Attributes\DataProvider;
use Illuminate\Support\Facades\Request;
use Illuminate\Support\Facades\Route;
use Mcamara\LaravelLocalization\Facades\LaravelLocalization as LaravelLocalizationFacade;
use Mcamara\LaravelLocalization\LanguageNegotiator;
use Mcamara\LaravelLocalization\LaravelLocalization;
use Mcamara\LaravelLocalization\Middleware\LaravelLocalizationRedirectFilter;
use Mcamara\LaravelLocalization\Middleware\LaravelLocalizationRoutes;
use Mcamara\LaravelLocalization\Middleware\LocaleCookieRedirect;

final class LaravelLocalizationTest extends TestCase
{
Expand All @@ -25,41 +31,41 @@ protected function setRoutes($locale = false)
app('laravellocalization')->setLocale($locale);
}

app('router')->group([
'prefix' => app('laravellocalization')->setLocale(),
Route::group([
'prefix' => LaravelLocalizationFacade::setLocale(),
'middleware' => [
'Mcamara\LaravelLocalization\Middleware\LaravelLocalizationRoutes',
'Mcamara\LaravelLocalization\Middleware\LaravelLocalizationRedirectFilter',
LaravelLocalizationRoutes::class,
LaravelLocalizationRedirectFilter::class,
],
], function () {
app('router')->get('/', ['as'=> 'index', function () {
Route::get('/', function () {
return app('translator')->get('LaravelLocalization::routes.hello');
}, ]);
})->name('index');

app('router')->get('test', ['as'=> 'test', function () {
Route::get('test', function () {
return app('translator')->get('LaravelLocalization::routes.test_text');
}, ]);
})->name('test');

app('router')->get(app('laravellocalization')->transRoute('LaravelLocalization::routes.about'), ['as'=> 'about', function () {
Route::get(app('laravellocalization')->transRoute('LaravelLocalization::routes.about'), function () {
return app('laravellocalization')->getLocalizedURL('es') ?: 'Not url available';
}, ]);
})->name('about');

app('router')->get(app('laravellocalization')->transRoute('LaravelLocalization::routes.view'), ['as'=> 'view', function () {
Route::get(app('laravellocalization')->transRoute('LaravelLocalization::routes.view'), function () {
return app('laravellocalization')->getLocalizedURL('es') ?: 'Not url available';
}, ]);
})->name('view');

app('router')->get(app('laravellocalization')->transRoute('LaravelLocalization::routes.view_project'), ['as'=> 'view_project', function () {
Route::get(app('laravellocalization')->transRoute('LaravelLocalization::routes.view_project'), function () {
return app('laravellocalization')->getLocalizedURL('es') ?: 'Not url available';
}, ]);
})->name('view_project');

app('router')->get(app('laravellocalization')->transRoute('LaravelLocalization::routes.manage'), ['as'=> 'manage', function () {
Route::get(app('laravellocalization')->transRoute('LaravelLocalization::routes.manage'), function () {
return app('laravellocalization')->getLocalizedURL('es') ?: 'Not url available';
}, ]);
})->name('manage');
});

app('router')->get('/skipped', ['as'=> 'skipped', function () {
Route::get('/skipped', function () {
return Request::url();
}, ]);
})->name('skipped');
}

/**
Expand Down Expand Up @@ -138,18 +144,18 @@ protected function getEnvironmentSetUp($app)

public function testSetLocale(): void
{
$this->assertEquals(route('about'), 'http://localhost/about');
$this->assertEquals('http://localhost/about', route('about'));

$this->refreshApplication('es');
$this->assertEquals('es', app('laravellocalization')->setLocale('es'));
$this->assertEquals('es', app('laravellocalization')->getCurrentLocale());
$this->assertEquals(route('about'), 'http://localhost/acerca');
$this->assertEquals('http://localhost/acerca', route('about'));

$this->refreshApplication();

$this->assertEquals('en', app('laravellocalization')->setLocale('en'));

$this->assertEquals(route('about'), 'http://localhost/about');
$this->assertEquals('http://localhost/about', route('about'));

$this->assertNull(app('laravellocalization')->setLocale('de'));
$this->assertEquals('en', app('laravellocalization')->getCurrentLocale());
Expand Down Expand Up @@ -250,19 +256,15 @@ public function testGetLocalizedURL(): void

app('laravellocalization')->setLocale('en');

$crawler = $this->call(
'GET',
self::TEST_URL.'about',
[],
[],
[],
['HTTP_ACCEPT_LANGUAGE' => 'en,es']
$response = $this->get(
uri: self::TEST_URL.'about',
headers: ['HTTP_ACCEPT_LANGUAGE' => 'en,es']
);

$this->assertResponseOk();
$response->assertStatus(200);
$this->assertEquals(
self::TEST_URL.'es/acerca',
$crawler->getContent()
$response->getContent()
);

$this->refreshApplication();
Expand All @@ -279,19 +281,15 @@ public function testGetLocalizedURL(): void
app('laravellocalization')->getLocalizedURL('en', self::TEST_URL.'test?a=1')
);

$crawler = $this->call(
'GET',
app('laravellocalization')->getLocalizedURL('en', self::TEST_URL.'test'),
[],
[],
[],
['HTTP_ACCEPT_LANGUAGE' => 'en,es']
$response = $this->get(
uri: app('laravellocalization')->getLocalizedURL('en', self::TEST_URL.'test'),
headers: ['HTTP_ACCEPT_LANGUAGE' => 'en,es']
);

$this->assertResponseOk();
$response->assertStatus(200);
$this->assertEquals(
'Test text',
$crawler->getContent()
$response->getContent()
);

$this->refreshApplication('es');
Expand Down Expand Up @@ -368,19 +366,15 @@ public static function getRouteNameFromAPathDataProvider(): array
}

public function testGetLocalizedUrlForIgnoredUrls(): void {
$crawler = $this->call(
'GET',
self::TEST_URL.'skipped',
[],
[],
[],
['HTTP_ACCEPT_LANGUAGE' => 'en,es']
$response = $this->get(
uri: self::TEST_URL.'skipped',
headers: ['HTTP_ACCEPT_LANGUAGE' => 'en,es']
);

$this->assertResponseOk();
$response->assertStatus(200);
$this->assertEquals(
self::TEST_URL.'skipped',
$crawler->getContent()
$response->getContent()
);
}

Expand Down Expand Up @@ -774,13 +768,11 @@ public function testLanguageNegotiation($accept_string, $must_resolve_to, $asd =
$request = $this->createMock(\Illuminate\Http\Request::class);
$request->expects($this->any())->method('header')->with('Accept-Language')->willReturn($accept_string);

$negotiator = app(\Mcamara\LaravelLocalization\LanguageNegotiator::class,
[
'defaultLocale' => 'wrong',
'supportedLanguages' => $full_config['supportedLocales'],
'request' => $request
]
);
$negotiator = app(LanguageNegotiator::class, [
'defaultLocale' => 'wrong',
'supportedLanguages' => $full_config['supportedLocales'],
'request' => $request
]);

$language = $negotiator->negotiateLanguage();

Expand Down Expand Up @@ -829,13 +821,11 @@ public function testLanguageNegotiationWithMapping(): void {
$request = $this->createMock(\Illuminate\Http\Request::class);
$request->expects($this->any())->method('header')->with('Accept-Language')->willReturn($accept_string);

$negotiator = app(\Mcamara\LaravelLocalization\LanguageNegotiator::class,
[
'defaultLocale' => 'wrong',
'supportedLanguages' => $full_config['supportedLocales'],
'request' => $request
]
);
$negotiator = app(LanguageNegotiator::class, [
'defaultLocale' => 'wrong',
'supportedLanguages' => $full_config['supportedLocales'],
'request' => $request
]);

$language = $negotiator->negotiateLanguage();

Expand All @@ -859,39 +849,28 @@ public function testSetLocaleWithMapping(): void
$this->assertEquals('http://localhost/custom', app('laravellocalization')->localizeURL('http://localhost/custom', 'en'));
}



public function testRedirectWithHiddenDefaultLocaleInUrlAndSavedLocale()
{
app('router')->group([
'prefix' => app('laravellocalization')->setLocale(),
Route::group([
'prefix' => LaravelLocalizationFacade::setLocale(),
'middleware' => [
'Mcamara\LaravelLocalization\Middleware\LaravelLocalizationRedirectFilter',
'Mcamara\LaravelLocalization\Middleware\LocaleCookieRedirect',
LaravelLocalizationRedirectFilter::class,
LocaleCookieRedirect::class,
],
], function (){
app('router')->get('/', ['as'=> 'index', function () {
], function () {
Route::get('/', function () {
return 'Index page';
}, ]);
})->name('index');
});

app('config')->set('laravellocalization.hideDefaultLocaleInURL', true);

$savedLocale = 'es';

$crawler = $this->call(
'GET',
self::TEST_URL,
[],
['locale' => $savedLocale],
[],
[]
);

$this->assertResponseStatus(302);
$this->assertRedirectedTo(self::TEST_URL . $savedLocale);
$response = $this->withUnencryptedCookie('locale', $savedLocale)->get(self::TEST_URL);

$localeCookie = $crawler->headers->getCookies()[0];
$this->assertEquals($savedLocale, $localeCookie->getValue());
$response->assertStatus(302);
$response->assertRedirect(self::TEST_URL . $savedLocale);
$response->assertPlainCookie('locale', $savedLocale);
}
}
2 changes: 1 addition & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
use Mcamara\LaravelLocalization\LaravelLocalizationServiceProvider;
use Orchestra\Testbench\BrowserKit\TestCase as BaseTestCase;
use Orchestra\Testbench\TestCase as BaseTestCase;

abstract class TestCase extends BaseTestCase
{
Expand Down
Loading