forked from laravel/jetstream
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRemoveTeamMemberTest.php
123 lines (87 loc) · 3.08 KB
/
RemoveTeamMemberTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
namespace Laravel\Jetstream\Tests;
use App\Actions\Jetstream\CreateTeam;
use App\Actions\Jetstream\RemoveTeamMember;
use App\Models\Team;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Gate;
use Illuminate\Validation\ValidationException;
use Laravel\Jetstream\Events\RemovingTeamMember;
use Laravel\Jetstream\Events\TeamMemberRemoved;
use Laravel\Jetstream\Jetstream;
use Laravel\Jetstream\Tests\Fixtures\TeamPolicy;
use Laravel\Jetstream\Tests\Fixtures\User;
class RemoveTeamMemberTest extends OrchestraTestCase
{
public function setUp(): void
{
parent::setUp();
Gate::policy(Team::class, TeamPolicy::class);
Jetstream::useUserModel(User::class);
}
public function test_team_members_can_be_removed()
{
Event::fake([TeamMemberRemoved::class]);
$this->migrate();
$team = $this->createTeam();
$otherUser = User::forceCreate([
'name' => 'Adam Wathan',
'email' => 'adam@laravel.com',
'password' => 'secret',
]);
$team->users()->attach($otherUser, ['role' => null]);
$this->assertCount(1, $team->fresh()->users);
Auth::login($team->owner);
$action = new RemoveTeamMember;
$action->remove($team->owner, $team, $otherUser);
$this->assertCount(0, $team->fresh()->users);
Event::assertDispatched(TeamMemberRemoved::class);
}
public function test_a_team_owner_cant_remove_themselves()
{
$this->expectException(ValidationException::class);
Event::fake([RemovingTeamMember::class]);
$this->migrate();
$team = $this->createTeam();
Auth::login($team->owner);
$action = new RemoveTeamMember;
$action->remove($team->owner, $team, $team->owner);
}
public function test_the_user_must_be_authorized_to_remove_team_members()
{
$this->expectException(AuthorizationException::class);
$this->migrate();
$team = $this->createTeam();
$adam = User::forceCreate([
'name' => 'Adam Wathan',
'email' => 'adam@laravel.com',
'password' => 'secret',
]);
$abigail = User::forceCreate([
'name' => 'Abigail Otwell',
'email' => 'abigail@laravel.com',
'password' => 'secret',
]);
$team->users()->attach($adam, ['role' => null]);
$team->users()->attach($abigail, ['role' => null]);
Auth::login($team->owner);
$action = new RemoveTeamMember;
$action->remove($adam, $team, $abigail);
}
protected function createTeam()
{
$action = new CreateTeam;
$user = User::forceCreate([
'name' => 'Taylor Otwell',
'email' => 'taylor@laravel.com',
'password' => 'secret',
]);
return $action->create($user, ['name' => 'Test Team']);
}
protected function migrate()
{
$this->artisan('migrate', ['--database' => 'testbench'])->run();
}
}