Skip to content

Commit e55dae7

Browse files
committed
add case-insensitivity for database driver
1 parent d35f2d4 commit e55dae7

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

src/Models/Redirection.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public static function getStatuses(): array
6161
*/
6262
public function scopeWhereOldUrl(Builder $query, string $url): Builder
6363
{
64-
return $query->where('old_url', $url);
64+
return $query->where('old_url', config('redirection.case-sensitive') ? $url : strtolower($url));
6565
}
6666

6767
/**
@@ -74,7 +74,7 @@ public function scopeWhereOldUrl(Builder $query, string $url): Builder
7474
*/
7575
public function scopeWhereNewUrl(Builder $query, string $url): Builder
7676
{
77-
return $query->where('new_url', $url);
77+
return $query->where('new_url', config('redirection.case-sensitive') ? $url : strtolower($url));
7878
}
7979

8080
/**
@@ -85,7 +85,8 @@ public function scopeWhereNewUrl(Builder $query, string $url): Builder
8585
*/
8686
public function setOldUrlAttribute(string $value): void
8787
{
88-
$this->attributes['old_url'] = trim(parse_url($value)['path'], '/');
88+
$value = trim(parse_url($value)['path'], '/');
89+
$this->attributes['old_url'] = config('redirection.case-sensitive') ? $value : strtolower($value);
8990
}
9091

9192
/**
@@ -96,7 +97,8 @@ public function setOldUrlAttribute(string $value): void
9697
*/
9798
public function setNewUrlAttribute(string $value): void
9899
{
99-
$this->attributes['new_url'] = trim(parse_url($value)['path'], '/');
100+
$value = trim(parse_url($value)['path'], '/');
101+
$this->attributes['new_url'] = config('redirection.case-sensitive') ? $value : strtolower($value);
100102
}
101103

102104
/**
@@ -127,7 +129,9 @@ public function syncOldRedirects(RedirectionModelContract $model, string $finalU
127129
*/
128130
public static function findValidOrNull(string $path): ?Redirection
129131
{
130-
return static::where('old_url', $path === '/' ? $path : trim($path, '/'))
132+
$path = ($path === '/' ? $path : trim($path, '/'));
133+
134+
return static::where('old_url', config('redirection.case-sensitive') ? $path : strtolower($path))
131135
->whereNotNull('new_url')
132136
->whereIn('status_code', array_keys(self::getStatuses()))
133137
->latest()

tests/Drivers/DatabaseRedirectionTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,36 @@ public function it_redirects_a_request(): void
2222
->assertRedirect('new/url');
2323
}
2424

25+
/** @test */
26+
public function it_doesnt_redirect_case_sensitive_requests(): void
27+
{
28+
$this->app['config']->set('redirection.driver', 'database');
29+
$this->app['config']->set('redirection.case-sensitive', true);
30+
31+
Redirection::create([
32+
'old_url' => 'old-url',
33+
'new_url' => 'new/url',
34+
]);
35+
36+
$this->get('old-URL')
37+
->assertNotFound();
38+
}
39+
40+
/** @test */
41+
public function it_does_redirect_case_insensitive_requests(): void
42+
{
43+
$this->app['config']->set('redirection.driver', 'database');
44+
$this->app['config']->set('redirection.case-sensitive', false);
45+
46+
Redirection::create([
47+
'old_url' => 'old-url',
48+
'new_url' => 'new/url',
49+
]);
50+
51+
$this->get('OLD-URL')
52+
->assertRedirect('new/url');
53+
}
54+
2555
/** @test */
2656
public function it_redirects_nested_requests(): void
2757
{

0 commit comments

Comments
 (0)