|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace ChinLeung\MultilingualRoutes\Tests; |
| 4 | + |
| 5 | +use ChinLeung\LaravelLocales\LaravelLocalesServiceProvider; |
| 6 | +use ChinLeung\MultilingualRoutes\MultilingualRoutesServiceProvider; |
| 7 | +use Illuminate\Support\Facades\Route; |
| 8 | +use Orchestra\Testbench\TestCase; |
| 9 | + |
| 10 | +class UnprefixedRedirectTest extends TestCase |
| 11 | +{ |
| 12 | + protected function setUp(): void |
| 13 | + { |
| 14 | + parent::setUp(); |
| 15 | + |
| 16 | + config([ |
| 17 | + 'locales.supported' => ['en', 'fr'], |
| 18 | + 'laravel-multilingual-routes.default' => 'en', |
| 19 | + 'laravel-multilingual-routes.prefix_default' => true, |
| 20 | + 'laravel-multilingual-routes.redirect_unprefixed' => true, |
| 21 | + ]); |
| 22 | + } |
| 23 | + |
| 24 | + /** @test */ |
| 25 | + public function an_unprefixed_localized_route_redirects_to_the_default_locale(): void |
| 26 | + { |
| 27 | + Route::multilingual('posts/{post}', static function () { |
| 28 | + // |
| 29 | + })->where('post', '[0-9]+'); |
| 30 | + |
| 31 | + $this->get('/posts/123')->assertRedirect('/en/posts/123'); |
| 32 | + $this->get('/posts/not-a-number')->assertNotFound(); |
| 33 | + } |
| 34 | + |
| 35 | + /** @test */ |
| 36 | + public function get_parameters_are_maintained_on_redirect(): void |
| 37 | + { |
| 38 | + Route::multilingual('search'); |
| 39 | + |
| 40 | + $response = $this->get('/search?query=multilingual%20routes&page=2'); |
| 41 | + $location = $response->headers->get('Location'); |
| 42 | + parse_str(parse_url($location, PHP_URL_QUERY), $query); |
| 43 | + |
| 44 | + $this->assertSame('/en/search', parse_url($location, PHP_URL_PATH)); |
| 45 | + $this->assertSame('multilingual routes', $query['query']); |
| 46 | + $this->assertSame('2', $query['page']); |
| 47 | + } |
| 48 | + |
| 49 | + /** @test */ |
| 50 | + public function a_url_without_an_applicable_default_locale_route_is_not_redirected(): void |
| 51 | + { |
| 52 | + Route::multilingual('french-only')->only('fr'); |
| 53 | + |
| 54 | + $this->get('/french-only')->assertNotFound(); |
| 55 | + $this->get('/not-localized')->assertNotFound(); |
| 56 | + } |
| 57 | + |
| 58 | + /** @test */ |
| 59 | + public function an_unprefixed_default_locale_does_not_create_a_redirect(): void |
| 60 | + { |
| 61 | + config(['laravel-multilingual-routes.prefix_default' => false]); |
| 62 | + |
| 63 | + Route::multilingual('posts', static fn () => 'posts'); |
| 64 | + |
| 65 | + $this->get('/posts')->assertOk()->assertSee('posts'); |
| 66 | + } |
| 67 | + |
| 68 | + /** @test */ |
| 69 | + public function a_non_get_route_does_not_create_an_unprefixed_redirect(): void |
| 70 | + { |
| 71 | + Route::multilingual('posts', static function () { |
| 72 | + // |
| 73 | + })->method('post'); |
| 74 | + |
| 75 | + $this->post('/posts')->assertNotFound(); |
| 76 | + $this->post('/en/posts')->assertOk(); |
| 77 | + } |
| 78 | + |
| 79 | + /** @test */ |
| 80 | + public function the_home_route_redirects_when_the_default_home_is_prefixed(): void |
| 81 | + { |
| 82 | + config(['laravel-multilingual-routes.prefix_default_home' => true]); |
| 83 | + |
| 84 | + Route::multilingual('/'); |
| 85 | + |
| 86 | + $this->get('/')->assertRedirect('/en'); |
| 87 | + } |
| 88 | + |
| 89 | + /** @test */ |
| 90 | + public function an_unprefixed_default_home_route_is_left_untouched(): void |
| 91 | + { |
| 92 | + config(['laravel-multilingual-routes.prefix_default_home' => false]); |
| 93 | + |
| 94 | + Route::multilingual('/', static fn () => 'home'); |
| 95 | + |
| 96 | + $this->get('/')->assertOk()->assertSee('home'); |
| 97 | + } |
| 98 | + |
| 99 | + /** @test */ |
| 100 | + public function an_unprefixed_redirect_respects_route_groups(): void |
| 101 | + { |
| 102 | + Route::prefix('admin')->group(static function () { |
| 103 | + Route::multilingual('posts'); |
| 104 | + }); |
| 105 | + |
| 106 | + $response = $this->get('/admin/posts'); |
| 107 | + |
| 108 | + $response->assertRedirect('/en/admin/posts'); |
| 109 | + $this->assertSame('/en/admin/posts', $response->headers->get('Location')); |
| 110 | + } |
| 111 | + |
| 112 | + protected function getPackageProviders($app) |
| 113 | + { |
| 114 | + return [ |
| 115 | + LaravelLocalesServiceProvider::class, |
| 116 | + MultilingualRoutesServiceProvider::class, |
| 117 | + ]; |
| 118 | + } |
| 119 | +} |
0 commit comments