Repository: spatie/laravel-permission (php)
Issue: Needed: Add tests for UnauthorizedException config switches
The `config` file has settings for `permission.display_permission_in_exception` and `permission.display_role_in_exception`, but no tests to ensure they function correctly.

A PR containing tests for these would be welcome.
Pull request description: Add tests for display roles/permissions on UnauthorizedException
- Add tests for display roles/permissions on UnauthorizedException
- Test the right message according middleware
- Remove message text duplication

Closes #2226

Patch:
diff --git a/src/Exceptions/UnauthorizedException.php b/src/Exceptions/UnauthorizedException.php
index da6ab32..2a270fa 100644
--- a/src/Exceptions/UnauthorizedException.php
+++ b/src/Exceptions/UnauthorizedException.php
@@ -15,8 +15,7 @@ class UnauthorizedException extends HttpException
         $message = 'User does not have the right roles.';
 
         if (config('permission.display_role_in_exception')) {
-            $permStr = implode(', ', $roles);
-            $message = 'User does not have the right roles. Necessary roles are '.$permStr;
+            $message .= ' Necessary roles are '.implode(', ', $roles);
         }
 
         $exception = new static(403, $message, null, []);
@@ -30,8 +29,7 @@ class UnauthorizedException extends HttpException
         $message = 'User does not have the right permissions.';
 
         if (config('permission.display_permission_in_exception')) {
-            $permStr = implode(', ', $permissions);
-            $message = 'User does not have the right permissions. Necessary permissions are '.$permStr;
+            $message .= ' Necessary permissions are '.implode(', ', $permissions);
         }
 
         $exception = new static(403, $message, null, []);
@@ -45,8 +43,7 @@ class UnauthorizedException extends HttpException
         $message = 'User does not have any of the necessary access rights.';
 
         if (config('permission.display_permission_in_exception') && config('permission.display_role_in_exception')) {
-            $permStr = implode(', ', $rolesOrPermissions);
-            $message = 'User does not have the right permissions. Necessary permissions are '.$permStr;
+            $message .= ' Necessary roles or permissions are '.implode(', ', $rolesOrPermissions);
         }
 
         $exception = new static(403, $message, null, []);


Test patch:
diff --git a/tests/PermissionMiddlewareTest.php b/tests/PermissionMiddlewareTest.php
index fb49073..8d294fd 100644
--- a/tests/PermissionMiddlewareTest.php
+++ b/tests/PermissionMiddlewareTest.php
@@ -5,6 +5,7 @@ namespace Spatie\Permission\Test;
 use Illuminate\Http\Request;
 use Illuminate\Http\Response;
 use Illuminate\Support\Facades\Auth;
+use Illuminate\Support\Facades\Config;
 use InvalidArgumentException;
 use Spatie\Permission\Contracts\Permission;
 use Spatie\Permission\Exceptions\UnauthorizedException;
@@ -147,6 +148,7 @@ class PermissionMiddlewareTest extends TestCase
     {
         Auth::login($this->testUser);
 
+        $message = null;
         $requiredPermissions = [];
 
         try {
@@ -154,12 +156,33 @@ class PermissionMiddlewareTest extends TestCase
                 return (new Response())->setContent('<html></html>');
             }, 'some-permission');
         } catch (UnauthorizedException $e) {
+            $message = $e->getMessage();
             $requiredPermissions = $e->getRequiredPermissions();
         }
 
+        $this->assertEquals('User does not have the right permissions.', $message);
         $this->assertEquals(['some-permission'], $requiredPermissions);
     }
 
+    /** @test */
+    public function the_required_permissions_can_be_displayed_in_the_exception()
+    {
+        Auth::login($this->testUser);
+        Config::set(['permission.display_permission_in_exception' => true]);
+
+        $message = null;
+
+        try {
+            $this->permissionMiddleware->handle(new Request(), function () {
+                return (new Response())->setContent('<html></html>');
+            }, 'some-permission');
+        } catch (UnauthorizedException $e) {
+            $message = $e->getMessage();
+        }
+
+        $this->assertStringEndsWith('Necessary permissions are some-permission', $message);
+    }
+
     /** @test */
     public function use_not_existing_custom_guard_in_permission()
     {
diff --git a/tests/RoleMiddlewareTest.php b/tests/RoleMiddlewareTest.php
index 70fd27e..e100af3 100644
--- a/tests/RoleMiddlewareTest.php
+++ b/tests/RoleMiddlewareTest.php
@@ -5,6 +5,7 @@ namespace Spatie\Permission\Test;
 use Illuminate\Http\Request;
 use Illuminate\Http\Response;
 use Illuminate\Support\Facades\Auth;
+use Illuminate\Support\Facades\Config;
 use InvalidArgumentException;
 use Spatie\Permission\Exceptions\UnauthorizedException;
 use Spatie\Permission\Middlewares\RoleMiddleware;
@@ -113,6 +114,7 @@ class RoleMiddlewareTest extends TestCase
     {
         Auth::login($this->testUser);
 
+        $message = null;
         $requiredRoles = [];
 
         try {
@@ -120,12 +122,33 @@ class RoleMiddlewareTest extends TestCase
                 return (new Response())->setContent('<html></html>');
             }, 'some-role');
         } catch (UnauthorizedException $e) {
+            $message = $e->getMessage();
             $requiredRoles = $e->getRequiredRoles();
         }
 
+        $this->assertEquals('User does not have the right roles.', $message);
         $this->assertEquals(['some-role'], $requiredRoles);
     }
 
+    /** @test */
+    public function the_required_roles_can_be_displayed_in_the_exception()
+    {
+        Auth::login($this->testUser);
+        Config::set(['permission.display_role_in_exception' => true]);
+
+        $message = null;
+
+        try {
+            $this->roleMiddleware->handle(new Request(), function () {
+                return (new Response())->setContent('<html></html>');
+            }, 'some-role');
+        } catch (UnauthorizedException $e) {
+            $message = $e->getMessage();
+        }
+
+        $this->assertStringEndsWith('Necessary roles are some-role', $message);
+    }
+
     /** @test */
     public function use_not_existing_custom_guard_in_role()
     {
diff --git a/tests/RoleOrPermissionMiddlewareTest.php b/tests/RoleOrPermissionMiddlewareTest.php
index 00e81b2..345f020 100644
--- a/tests/RoleOrPermissionMiddlewareTest.php
+++ b/tests/RoleOrPermissionMiddlewareTest.php
@@ -5,6 +5,7 @@ namespace Spatie\Permission\Test;
 use Illuminate\Http\Request;
 use Illuminate\Http\Response;
 use Illuminate\Support\Facades\Auth;
+use Illuminate\Support\Facades\Config;
 use InvalidArgumentException;
 use Spatie\Permission\Exceptions\UnauthorizedException;
 use Spatie\Permission\Middlewares\RoleOrPermissionMiddleware;
@@ -123,6 +124,47 @@ class RoleOrPermissionMiddlewareTest extends TestCase
         );
     }
 
+    /** @test */
+    public function the_required_permissions_or_roles_can_be_fetched_from_the_exception()
+    {
+        Auth::login($this->testUser);
+
+        $message = null;
+        $requiredRolesOrPermissions = [];
+
+        try {
+            $this->roleOrPermissionMiddleware->handle(new Request(), function () {
+                return (new Response())->setContent('<html></html>');
+            }, 'some-permission|some-role');
+        } catch (UnauthorizedException $e) {
+            $message = $e->getMessage();
+            $requiredRolesOrPermissions = $e->getRequiredPermissions();
+        }
+
+        $this->assertEquals('User does not have any of the necessary access rights.', $message);
+        $this->assertEquals(['some-permission', 'some-role'], $requiredRolesOrPermissions);
+    }
+
+    /** @test */
+    public function the_required_permissions_or_roles_can_be_displayed_in_the_exception()
+    {
+        Auth::login($this->testUser);
+        Config::set(['permission.display_permission_in_exception' => true]);
+        Config::set(['permission.display_role_in_exception' => true]);
+
+        $message = null;
+
+        try {
+            $this->roleOrPermissionMiddleware->handle(new Request(), function () {
+                return (new Response())->setContent('<html></html>');
+            }, 'some-permission|some-role');
+        } catch (UnauthorizedException $e) {
+            $message = $e->getMessage();
+        }
+
+        $this->assertStringEndsWith('Necessary roles or permissions are some-permission, some-role', $message);
+    }
+
     protected function runMiddleware($middleware, $name, $guard = null)
     {
         try {