1+ <?php
2+
3+ use Eclipse \Core \Models \User ;
4+ use Illuminate \Foundation \Testing \RefreshDatabase ;
5+ use Illuminate \Support \Facades \Auth ;
6+ use Illuminate \Support \Facades \Gate ;
7+ use Illuminate \Auth \Access \AuthorizationException ;
8+
9+ uses (RefreshDatabase::class);
10+
11+ beforeEach (function () {
12+ $ this ->set_up_super_admin_and_tenant ();
13+ });
14+
15+ test ('authorized user with permission can trash another user ' , function () {
16+ $ user = User::factory ()->create ();
17+ Auth::login ($ this ->superAdmin );
18+ $ this ->assertTrue ($ this ->superAdmin ->hasPermissionTo ('delete_user ' ));
19+ $ this ->assertTrue ($ this ->superAdmin ->can ('delete ' , $ user ));
20+ $ user ->delete ();
21+ $ this ->assertTrue ($ user ->fresh ()->trashed ());
22+ });
23+
24+ test ('non-authorized user cannot trash another user ' , function () {
25+ $ user = User::factory ()->create ();
26+ $ targetUser = User::factory ()->create ();
27+ Auth::login ($ user );
28+ $ this ->assertFalse ($ user ->hasPermissionTo ('delete_user ' ));
29+ $ this ->assertFalse ($ user ->can ('delete ' , $ targetUser ));
30+ $ this ->expectException (AuthorizationException::class);
31+ Gate::authorize ('delete ' , $ targetUser );
32+ });
33+
34+ test ('user cannot trash himself ' , function () {
35+ Auth::login ($ this ->superAdmin );
36+ $ this ->assertFalse ($ this ->superAdmin ->can ('delete ' , $ this ->superAdmin ));
37+ try {
38+ Gate::authorize ('delete ' , $ this ->superAdmin );
39+ $ this ->fail ('User was able to authorize self-deletion, which should not be allowed ' );
40+ } catch (AuthorizationException $ e ) {
41+ $ this ->assertTrue (true );
42+ }
43+ $ this ->assertFalse ($ this ->superAdmin ->fresh ()->trashed ());
44+ });
45+
46+ test ('authorized user with restore permission can restore a trashed user ' , function () {
47+ $ user = User::factory ()->create ();
48+ $ user ->delete ();
49+ Auth::login ($ this ->superAdmin );
50+ $ this ->assertTrue ($ this ->superAdmin ->hasPermissionTo ('restore_user ' ));
51+ $ this ->assertTrue ($ this ->superAdmin ->can ('restore ' , $ user ));
52+ $ user ->restore ();
53+ $ this ->assertFalse ($ user ->fresh ()->trashed ());
54+ });
55+
56+ test ('authorized user with restore_any permission can restore any trashed user ' , function () {
57+ $ userToTrash = User::factory ()->create ();
58+ $ userToTrash ->delete ();
59+ $ limitedAdmin = User::factory ()->create ();
60+ $ limitedAdmin ->givePermissionTo ('restore_any_user ' );
61+ Auth::login ($ limitedAdmin );
62+ $ this ->assertTrue ($ limitedAdmin ->hasPermissionTo ('restore_any_user ' ));
63+ $ this ->assertTrue ($ limitedAdmin ->can ('restoreAny ' , User::class));
64+ $ userToTrash ->restore ();
65+ $ this ->assertFalse ($ userToTrash ->fresh ()->trashed ());
66+ });
67+
68+ test ('non-authorized user cannot restore another user ' , function () {
69+ $ userToTrash = User::factory ()->create ();
70+ $ userToTrash ->delete ();
71+ $ nonAuthorizedUser = User::factory ()->create ();
72+ Auth::login ($ nonAuthorizedUser );
73+ $ this ->assertFalse ($ nonAuthorizedUser ->hasPermissionTo ('restore_user ' ));
74+ $ this ->assertFalse ($ nonAuthorizedUser ->can ('restore ' , $ userToTrash ));
75+ $ this ->expectException (AuthorizationException::class);
76+ Gate::authorize ('restore ' , $ userToTrash );
77+ });
78+
79+ test ('trashed user cannot login ' , function () {
80+ $ userToTrash = User::factory ()->create ([
81+ 'email ' => 'trashed@example.com ' ,
82+ 'password ' => bcrypt ('password ' )
83+ ]);
84+ $ userToTrash ->delete ();
85+ Auth::logout ();
86+ $ attempt = Auth::attempt ([
87+ 'email ' => 'trashed@example.com ' ,
88+ 'password ' => 'password '
89+ ]);
90+ $ this ->assertFalse ($ attempt );
91+ });
92+
93+ test ('authorized user with permission can force delete a trashed user ' , function () {
94+ $ user = User::factory ()->create ();
95+ $ user ->delete ();
96+ Auth::login ($ this ->superAdmin );
97+ $ this ->assertTrue ($ this ->superAdmin ->hasPermissionTo ('force_delete_user ' ));
98+ $ this ->assertTrue ($ this ->superAdmin ->can ('forceDelete ' , $ user ));
99+ $ user ->forceDelete ();
100+ $ this ->assertNull (User::withTrashed ()->find ($ user ->id ));
101+ });
102+
103+ test ('non-authorized user cannot force delete a trashed user ' , function () {
104+ $ userToTrash = User::factory ()->create ();
105+ $ userToTrash ->delete ();
106+ $ nonAuthorizedUser = User::factory ()->create ();
107+ Auth::login ($ nonAuthorizedUser );
108+ $ this ->assertFalse ($ nonAuthorizedUser ->hasPermissionTo ('force_delete_user ' ));
109+ $ this ->assertFalse ($ nonAuthorizedUser ->can ('forceDelete ' , $ userToTrash ));
110+ $ this ->expectException (AuthorizationException::class);
111+ Gate::authorize ('forceDelete ' , $ userToTrash );
112+ });
113+
114+ test ('can view trashed users when user has permissions ' , function () {
115+ $ trashedUser = User::factory ()->create ();
116+ $ trashedUser ->delete ();
117+ Auth::login ($ this ->superAdmin );
118+ $ this ->assertTrue ($ this ->superAdmin ->hasPermissionTo ('view_any_user ' ));
119+ $ this ->assertTrue ($ this ->superAdmin ->hasPermissionTo ('view_user ' ));
120+ $ this ->assertTrue ($ this ->superAdmin ->can ('viewAny ' , User::class));
121+ $ this ->assertTrue ($ this ->superAdmin ->can ('view ' , $ trashedUser ));
122+ });
123+
124+ test ('filament resource can handle trashed users ' , function () {
125+ $ userToTrash = User::factory ()->create ([
126+ 'name ' => 'Trashed User ' ,
127+ 'email ' => 'trashed@example.com '
128+ ]);
129+ $ userToTrash ->delete ();
130+ Auth::login ($ this ->superAdmin );
131+ $ this ->assertTrue ($ this ->superAdmin ->can ('viewAny ' , User::class));
132+ $ this ->assertNotNull (User::withTrashed ()->where ('email ' , 'trashed@example.com ' )->first ());
133+ $ this ->assertTrue (User::withTrashed ()->where ('email ' , 'trashed@example.com ' )->first ()->trashed ());
134+ });
0 commit comments