Skip to content

Commit

Permalink
Fixed Dusk authorization tests to support Laravel 10 and 11
Browse files Browse the repository at this point in the history
* Unified redirect behavior when user is authorized

* Allow laravel 10

* Fixed code style

---------

Co-authored-by: tabuna <5102591+tabuna@users.noreply.github.com>
  • Loading branch information
tabuna and tabuna authored Feb 4, 2025
1 parent 1d4f015 commit c58d0d1
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
15 changes: 14 additions & 1 deletion src/Platform/Http/Controllers/LoginController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

namespace Orchid\Platform\Http\Controllers;

use Composer\InstalledVersions;
use Composer\Semver\VersionParser;
use Illuminate\Auth\EloquentUserProvider;
use Illuminate\Auth\Middleware\RedirectIfAuthenticated;
use Illuminate\Contracts\Auth\Factory as Auth;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Contracts\View\Factory;
Expand Down Expand Up @@ -41,7 +44,17 @@ public function __construct(Auth $auth)
{
$this->guard = $auth->guard(config('platform.guard'));

$this->middleware('guest', [
/**
* @deprecated logic for older Laravel versions
*/
$middleware = 'guest';

if (InstalledVersions::satisfies(new VersionParser, 'laravel/framework', '>11.17.0')) {
$middleware = RedirectIfAuthenticated::class;
RedirectIfAuthenticated::redirectUsing(static fn () => route(config('platform.index')));
}

$this->middleware($middleware, [
'except' => [
'logout',
'switchLogout',
Expand Down
4 changes: 2 additions & 2 deletions tests/Browser/LoginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ public function testLogout(): void
// Redirect to home
$browser
->visitRoute('platform.login')
->waitForLocation('/');
->waitForRoute(config('platform.index'));

// Logout
$browser
->visitRoute('platform.profile')
->clickLink($user->name)
->waitForText('Sign out')
->press('Sign out')
->waitForText('404');
->waitForRoute('platform.login');

// Redirect to login
$browser
Expand Down
4 changes: 3 additions & 1 deletion tests/Feature/Platform/AuthTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ public function testRouteDashboardLoginAuth(): void
$this->assertTrue(
// Home for Laravel 10.x and earlier
// '/' for Laravel 11.x and later
$response->isRedirect(url('/home')) || $response->isRedirect(url('/'))
$response->isRedirect(url('/home'))
|| $response->isRedirect(url('/'))
|| $response->isRedirect(route(config('platform.index')))
);
}

Expand Down

0 comments on commit c58d0d1

Please sign in to comment.