Skip to content

Commit e15cce0

Browse files
authored
Explicitly setting currentTeam relation on switchTeam to avoid refresh. (#331)
1 parent b57b50f commit e15cce0

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

src/HasTeams.php

+2
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ public function switchTeam($team)
4545
'current_team_id' => $team->id,
4646
])->save();
4747

48+
$this->setRelation('currentTeam', $team);
49+
4850
return true;
4951
}
5052

tests/TeamBehaviorTest.php

+25
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,31 @@ public function test_has_team_permission_checks_token_permissions()
129129
$this->assertTrue($john->hasTeamPermission($team, 'foo'));
130130
}
131131

132+
public function test_user_does_not_need_to_refresh_after_switching_teams()
133+
{
134+
$this->migrate();
135+
136+
$action = new CreateTeam;
137+
138+
$user = User::forceCreate([
139+
'name' => 'Taylor Otwell',
140+
'email' => '[email protected]',
141+
'password' => 'secret',
142+
]);
143+
144+
$personalTeam = $action->create($user, ['name' => 'Personal Team']);
145+
146+
$personalTeam->forceFill(['personal_team' => true])->save();
147+
148+
$anotherTeam = $action->create($user, ['name' => 'Test Team']);
149+
150+
$this->assertTrue($user->isCurrentTeam($personalTeam));
151+
152+
$user->switchTeam($anotherTeam);
153+
154+
$this->assertTrue($user->isCurrentTeam($anotherTeam));
155+
}
156+
132157
protected function migrate()
133158
{
134159
$this->artisan('migrate', ['--database' => 'testbench'])->run();

0 commit comments

Comments
 (0)