Skip to content

Commit 4f91290

Browse files
committed
Redirect to homepage when global filter is invalid
1 parent db3aa7d commit 4f91290

File tree

4 files changed

+12
-10
lines changed

4 files changed

+12
-10
lines changed

src/Filters/GlobalFilters/GlobalFilters.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,6 @@ public function toArray(): array
5454

5555
public function findFilter(string $key): ?Filter
5656
{
57-
return collect($this->getFilters())
58-
->firstWhere(fn (GlobalRequiredFilter $filter) => class_exists($key)
59-
? $filter instanceof $key
60-
: $filter->getKey() === $key
61-
);
57+
return $this->filterContainer()->findFilterHandler($key);
6258
}
6359
}

src/Http/Middleware/HandleGlobalFilters.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ public function handle(Request $request, Closure $next)
2929
->each(fn (GlobalRequiredFilter $globalFilter, int $index) => $globalFilter
3030
->setCurrentValue($filterKeys[$index])
3131
);
32+
33+
if (sharp()->context()->globalFilterUrlSegmentValue() !== $filterKey) {
34+
return redirect()->route('code16.sharp.home', [
35+
'filterKey' => sharp()->context()->globalFilterUrlSegmentValue(),
36+
]);
37+
}
3238
}
3339
}
3440

tests/Http/GlobalFilterControllerTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function defaultValue(): mixed
3737

3838
$this
3939
->followingRedirects()
40-
->post(route('code16.sharp.filters.update', ['test']), ['filterValues' => ['test' => '1']])
40+
->post(route('code16.sharp.filters.update', ['2']), ['filterValues' => ['test' => '1']])
4141
->assertOk();
4242

4343
$this->assertEquals('1', sharp()->context()->globalFilterValue('test'));
@@ -46,7 +46,7 @@ public function defaultValue(): mixed
4646
it('sets to global filter to the default value if missing', function () {
4747
$this
4848
->followingRedirects()
49-
->post(route('code16.sharp.filters.update', ['test']), ['filterValues' => []])
49+
->post(route('code16.sharp.filters.update', ['2']), ['filterValues' => []])
5050
->assertOk();
5151

5252
$this->assertEquals(2, sharp()->context()->globalFilterValue('test'));
@@ -55,7 +55,7 @@ public function defaultValue(): mixed
5555
it('does not allow to set a global filter to an unexpected value', function () {
5656
$this
5757
->followingRedirects()
58-
->post(route('code16.sharp.filters.update', ['test']), ['filterValues' => ['test' => 4]])
58+
->post(route('code16.sharp.filters.update', ['2']), ['filterValues' => ['test' => 4]])
5959
->assertOk();
6060

6161
$this->assertEquals(2, sharp()->context()->globalFilterValue('test'));

tests/Http/GlobalFilterRoutesTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,11 @@
5050
expect(sharp()->context()->globalFilterValue('test'))->toEqual('one');
5151
});
5252

53-
it('wont set an invalid value of the current filterKey according to the URL', function () {
53+
it('redirects to the homepage if an invalid filterKey is set in the URL', function () {
5454
fakeGlobalFilter();
5555

5656
$this->get('/sharp/five/s-list/person/s-show/person/1')
57-
->assertOk();
57+
->assertRedirect(route('code16.sharp.home', ['filterKey' => 'two']));
5858

5959
expect(sharp()->context()->globalFilterValue('test'))->toEqual('two');
6060
});

0 commit comments

Comments
 (0)