4
4
5
5
use Carbon \Carbon ;
6
6
use App \Models \User ;
7
+ use function Pest \Laravel \get ;
7
8
use App \Events \UserBannedEvent ;
8
9
use App \Events \UserUnbannedEvent ;
9
10
use Spatie \Permission \Models \Role ;
10
11
use Illuminate \Support \Facades \Event ;
11
- use Illuminate \Support \Facades \Queue ;
12
12
use App \Filament \Resources \UserResource ;
13
- use Illuminate \Support \Facades \Notification ;
14
- use App \Filament \Resources \UserResource \Pages \ListUsers ;
15
-
16
13
17
14
beforeEach (function (): void {
18
15
Event::fake ();
19
- Notification::fake ();
20
- Queue::fake ();
21
- $ this ->user = $ this ->login ();
16
+ $ this ->user = User::factory (['email ' => 'user@laravel.cm ' ])->create ();
17
+ Role::create (['name ' => 'admin ' ]);
18
+ $ this ->user ->assignRole (['admin ' ]);
19
+ $ this ->actingAs ($ this ->user , 'web ' );
22
20
});
23
21
24
22
describe (UserResource::class, function () {
25
- it ('only admin can ban a user and send a ban notification ' , function () {
23
+ it ('can render admin page ' , function (): void {
24
+ get (UserResource::getUrl ())->assertSuccessful ();
25
+ });
26
26
27
- Role:: create ([ ' name ' => ' user ' ]);
28
- $ admin = $ this ->user -> assignRole ( ' user ' );
27
+ it ( ' only admin can ban a user and send a ban notification ' , function () {
28
+ $ this ->get ( ' /cp ' )-> assertSuccessful ( );
29
29
30
30
$ user = User::factory ()->create ();
31
31
32
- // $this->actingAs($admin);
33
-
34
32
UserResource::BanUserAction ($ user , 'Violation des règles de la communauté ' );
35
33
36
34
$ user ->refresh ();
42
40
});
43
41
44
42
it ('can unban a user and send a unban notification ' , function () {
45
- Role::create (['name ' => 'admin ' ]);
46
- $ admin = $ this ->user ->assignRole ('admin ' );
47
-
43
+ $ this ->get ('/cp ' )->assertSuccessful ();
44
+
48
45
$ user = User::factory ()->create ([
49
46
'banned_at ' => now (),
50
47
'banned_reason ' => 'Violation des règles de la communauté '
51
48
]);
52
49
53
- $ this ->actingAs ($ admin );
54
-
55
50
UserResource::UnbanUserAction ($ user );
56
51
57
52
$ user ->refresh ();
62
57
Event::assertDispatched (UserUnbannedEvent::class);
63
58
});
64
59
60
+ it ('does not ban an already banned user ' , function () {
61
+ $ this ->get ('/cp ' )->assertSuccessful ();
62
+
63
+ $ user = User::factory ()->create (['banned_at ' => now ()]);
64
+
65
+ UserResource::BanUserAction ($ user , 'Violation des règles ' );
66
+
67
+ expect ($ user ->banned_reason )->not ->toBe ('Violation des règles ' )
68
+ ->and ($ user ->banned_at )->not ->toBeNull ();
69
+ });
70
+
65
71
it ('prevents a banned user from logging in ' , function () {
66
72
$ user = User::factory ()->create ([
67
73
'banned_at ' => now (),
72
78
->assertRedirect (route ('login ' ))
73
79
->assertSessionHasErrors (['email ' ]);
74
80
});
75
- });
81
+ })-> group ( ' users ' ) ;
0 commit comments