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