Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions src/Http/Middleware/HandleGlobalFilters.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Code16\Sharp\Filters\GlobalFilters\GlobalFilters;
use Code16\Sharp\Filters\GlobalRequiredFilter;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\URL;

class HandleGlobalFilters
Expand All @@ -20,9 +21,7 @@ public function handle(Request $request, Closure $next)
if ($this->globalFiltersHandler->isEnabled()) {
$globalFilters = $this->globalFiltersHandler->getFilters();
if (count($globalFilterValues) !== count($globalFilters)) {
return redirect()->route('code16.sharp.home', [
'globalFilter' => sharp()->context()->globalFilterUrlSegmentValue(),
]);
return $this->redirectTo($request);
}

collect($globalFilters)
Expand All @@ -34,9 +33,7 @@ public function handle(Request $request, Closure $next)
&& ! $request->wantsJson()
&& $request->isMethod('GET')
) {
return redirect()->route('code16.sharp.home', [
'globalFilter' => sharp()->context()->globalFilterUrlSegmentValue(),
]);
return $this->redirectTo($request);
}
}
}
Expand All @@ -45,4 +42,17 @@ public function handle(Request $request, Closure $next)

return $next($request);
}

protected function redirectTo(Request $request)
{
return $request->route()?->hasParameter('globalFilter')
? redirect()->route($request->route()->getName(), [
'globalFilter' => sharp()->context()->globalFilterUrlSegmentValue(),
...Arr::except($request->route()->parameters(), 'globalFilter'),
...$request->query(),
])
: redirect()->route('code16.sharp.home', [
'globalFilter' => sharp()->context()->globalFilterUrlSegmentValue(),
]);
}
}
14 changes: 11 additions & 3 deletions tests/Http/GlobalFilterRoutesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,23 @@ public function execute(array $data = []): array {}
expect(sharp()->context()->globalFilterValue('test'))->toEqual('one');
});

it('redirects to the homepage if an invalid globalFilter is set in the URL', function () {
it('redirects to the corresponding route with valid globalFilter if an invalid globalFilter is set in the URL', function () {
fakeGlobalFilter();

$this->get('/sharp/five/s-list/person/s-show/person/1')
->assertRedirect(route('code16.sharp.home', ['globalFilter' => 'two']));
$this->get('/sharp/five/s-list/person/s-show/person/1?highlight=1')
->assertRedirect('/sharp/two/s-list/person/s-show/person/1?highlight=1');

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

it('redirects to the corresponding route with valid globalFilter if a different number of filters is sent', function () {
fakeGlobalFilter('test1');
fakeGlobalFilter('test2');

$this->get('/sharp/two/s-list/person/s-show/person/1?highlight=1')
->assertRedirect('/sharp/two~two/s-list/person/s-show/person/1?highlight=1');
});

it('redirects to route with correct globalFilters when missing and multiple global filters are defined', function () {
fakeGlobalFilter('test1');
fakeGlobalFilter('test2');
Expand Down
Loading