|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace CodeZero\LocalizedRoutes\Tests\Unit; |
| 4 | + |
| 5 | +use CodeZero\LocalizedRoutes\Tests\TestCase; |
| 6 | +use Illuminate\Support\Facades\Route; |
| 7 | + |
| 8 | +class RedirectToLocalizedTest extends TestCase |
| 9 | +{ |
| 10 | + /** @test */ |
| 11 | + public function it_redirects_to_the_localized_url() |
| 12 | + { |
| 13 | + $this->withoutExceptionHandling(); |
| 14 | + $this->setSupportedLocales(['en', 'nl']); |
| 15 | + $this->setUseLocaleMiddleware(false); |
| 16 | + $this->setRedirectToLocalizedUrls(true); |
| 17 | + |
| 18 | + Route::localized(function () { |
| 19 | + Route::get('/', function () {}); |
| 20 | + Route::get('about', function () {}); |
| 21 | + }); |
| 22 | + |
| 23 | + Route::fallback(\CodeZero\LocalizedRoutes\Controller\FallbackController::class); |
| 24 | + |
| 25 | + $this->setAppLocale('en'); |
| 26 | + $this->get('/')->assertRedirect('en'); |
| 27 | + $this->get('en')->assertOk(); |
| 28 | + $this->get('about')->assertRedirect('en/about'); |
| 29 | + $this->get('en/about')->assertOk(); |
| 30 | + |
| 31 | + $this->setAppLocale('nl'); |
| 32 | + $this->get('/')->assertRedirect('nl'); |
| 33 | + $this->get('nl')->assertOk(); |
| 34 | + $this->get('about')->assertRedirect('nl/about'); |
| 35 | + $this->get('nl/about')->assertOk(); |
| 36 | + } |
| 37 | + |
| 38 | + /** @test */ |
| 39 | + public function it_redirects_when_default_locale_slug_is_omitted() |
| 40 | + { |
| 41 | + $this->withoutExceptionHandling(); |
| 42 | + $this->setSupportedLocales(['en', 'nl']); |
| 43 | + $this->setUseLocaleMiddleware(false); |
| 44 | + $this->setOmitUrlPrefixForLocale('en'); |
| 45 | + $this->setRedirectToLocalizedUrls(true); |
| 46 | + |
| 47 | + Route::localized(function () { |
| 48 | + Route::get('/', function () {}); |
| 49 | + Route::get('about', function () {}); |
| 50 | + }); |
| 51 | + |
| 52 | + Route::fallback(\CodeZero\LocalizedRoutes\Controller\FallbackController::class); |
| 53 | + |
| 54 | + $this->setAppLocale('en'); |
| 55 | + $this->get('en')->assertRedirect('/'); |
| 56 | + $this->get('/')->assertOk(); |
| 57 | + $this->get('en/about')->assertRedirect('about'); |
| 58 | + $this->get('about')->assertOk(); |
| 59 | + |
| 60 | + $this->setAppLocale('nl'); |
| 61 | + $this->get('nl')->assertOk(); |
| 62 | + $this->get('nl/about')->assertOk(); |
| 63 | + } |
| 64 | + |
| 65 | + /** @test */ |
| 66 | + public function it_throws_404_and_does_not_redirect_if_no_localized_route_is_registered() |
| 67 | + { |
| 68 | + $this->setSupportedLocales(['en', 'nl']); |
| 69 | + $this->setUseLocaleMiddleware(false); |
| 70 | + $this->setRedirectToLocalizedUrls(true); |
| 71 | + |
| 72 | + Route::fallback(\CodeZero\LocalizedRoutes\Controller\FallbackController::class); |
| 73 | + |
| 74 | + $this->setAppLocale('en'); |
| 75 | + $this->get('missing')->assertNotFound(); |
| 76 | + } |
| 77 | +} |
0 commit comments