Replaces all Jetstream model trait functions and relations

This commit is contained in:
Constantin Graf
2026-05-29 12:40:06 +02:00
committed by Constantin Graf
parent 5eb1a6a006
commit 734aab6353
26 changed files with 194 additions and 88 deletions

View File

@@ -34,10 +34,10 @@ class CreateOrganizationTest extends TestCase
// Assert
$response->assertStatus(302);
/** @var Organization|null $newOrganization */
$ownedTeams = $user->fresh()->ownedTeams;
$this->assertCount(2, $ownedTeams);
$this->assertTrue($ownedTeams->contains('name', 'Test Organization'));
$newOrganization = $ownedTeams->firstWhere('name', 'Test Organization');
$ownedOrganizations = $user->fresh()->ownedOrganizations;
$this->assertCount(2, $ownedOrganizations);
$this->assertTrue($ownedOrganizations->contains('name', 'Test Organization'));
$newOrganization = $ownedOrganizations->firstWhere('name', 'Test Organization');
/** @var Member $member */
$member = Member::query()->whereBelongsTo($user, 'user')->whereBelongsTo($newOrganization, 'organization')->firstOrFail();
$this->assertSame(Role::Owner->value, $member->role);

View File

@@ -36,15 +36,15 @@ class DeleteOrganizationTest extends TestCase
// Assert
$this->assertNull($organization->fresh());
$this->assertCount(1, $otherUser->fresh()->teams);
$this->assertFalse($otherUser->fresh()->teams->first()->is($organization));
$this->assertCount(1, $otherUser->fresh()->organizations);
$this->assertFalse($otherUser->fresh()->organizations->first()->is($organization));
}
public function test_personal_organizations_can_be_deleted_but_user_gets_an_new_one_if_this_is_the_only_one_left(): void
{
// Arrange
$user = User::factory()->withPersonalOrganization()->create();
$organization = $user->currentTeam;
$organization = $user->currentOrganization;
$this->actingAs($user);
// Act
@@ -55,7 +55,7 @@ class DeleteOrganizationTest extends TestCase
$this->assertDatabaseMissing(Organization::class, [
'id' => $organization->getKey(),
]);
$this->assertTrue($user->currentTeam->isNot($organization));
$this->assertTrue($user->currentOrganization->isNot($organization));
}
public function test_organization_can_not_be_deleted_if_user_is_not_owner(): void

View File

@@ -24,7 +24,7 @@ class InviteTeamMemberTest extends TestCase
$this->actingAs($user = User::factory()->withPersonalOrganization()->create());
// Act
$response = $this->post('/teams/'.$user->currentTeam->id.'/members', [
$response = $this->post('/teams/'.$user->currentOrganization->id.'/members', [
'email' => 'test@example.com',
'role' => 'admin',
]);
@@ -42,7 +42,7 @@ class InviteTeamMemberTest extends TestCase
$this->actingAs($user = User::factory()->withPersonalOrganization()->create());
$invitation = $user->currentTeam->teamInvitations()->create([
$invitation = $user->currentOrganization->organizationInvitations()->create([
'email' => 'test@example.com',
'role' => 'admin',
]);
@@ -52,7 +52,7 @@ class InviteTeamMemberTest extends TestCase
// Assert
$response->assertStatus(403);
$this->assertCount(1, $user->currentTeam->fresh()->teamInvitations);
$this->assertCount(1, $user->currentOrganization->fresh()->organizationInvitations);
}
public function test_team_member_invitations_can_be_accepted(): void
@@ -61,7 +61,7 @@ class InviteTeamMemberTest extends TestCase
Mail::fake();
$owner = User::factory()->withPersonalOrganization()->create();
$user = User::factory()->withPersonalOrganization()->create();
$invitation = $owner->currentTeam->teamInvitations()->create([
$invitation = $owner->currentOrganization->organizationInvitations()->create([
'email' => $user->email,
'role' => Role::Employee->value,
]);
@@ -76,10 +76,10 @@ class InviteTeamMemberTest extends TestCase
$response = $this->get($acceptUrl);
// Assert
$this->assertCount(0, $owner->currentTeam->fresh()->teamInvitations);
$this->assertCount(0, $owner->currentOrganization->fresh()->organizationInvitations);
$user->refresh();
$this->assertCount(2, $user->organizations);
$this->assertContains($owner->currentTeam->getKey(), $user->organizations->pluck('id'));
$this->assertContains($owner->currentOrganization->getKey(), $user->organizations->pluck('id'));
}
public function test_team_member_invitations_of_placeholder_can_be_accepted_and_migrates_date_to_real_user(): void
@@ -88,15 +88,15 @@ class InviteTeamMemberTest extends TestCase
Mail::fake();
$placeholder = User::factory()->placeholder()->create();
$owner = User::factory()->withPersonalOrganization()->create();
$placeholderMember = Member::factory()->role(Role::Placeholder)->forOrganization($owner->currentTeam)->forUser($placeholder)->create();
$placeholderMember = Member::factory()->role(Role::Placeholder)->forOrganization($owner->currentOrganization)->forUser($placeholder)->create();
$timeEntries = TimeEntry::factory()->forOrganization($owner->currentTeam)->forMember($placeholderMember)->createMany(5);
$timeEntries = TimeEntry::factory()->forOrganization($owner->currentOrganization)->forMember($placeholderMember)->createMany(5);
$user = User::factory()->withPersonalOrganization()->create([
'email' => $placeholder->email,
]);
$invitation = $owner->currentTeam->teamInvitations()->create([
$invitation = $owner->currentOrganization->organizationInvitations()->create([
'email' => $user->email,
'role' => Role::Employee->value,
]);
@@ -114,9 +114,9 @@ class InviteTeamMemberTest extends TestCase
$response->assertRedirect();
$user->refresh();
$this->assertDatabaseMissing(User::class, ['id' => $placeholder->id]);
$this->assertCount(0, $owner->currentTeam->fresh()->teamInvitations);
$this->assertCount(0, $owner->currentOrganization->fresh()->organizationInvitations);
$this->assertCount(2, $user->organizations);
$this->assertContains($owner->currentTeam->getKey(), $user->organizations->pluck('id'));
$this->assertContains($owner->currentOrganization->getKey(), $user->organizations->pluck('id'));
$this->assertCount(5, $user->timeEntries);
}
@@ -126,7 +126,7 @@ class InviteTeamMemberTest extends TestCase
Mail::fake();
$owner = User::factory()->withPersonalOrganization()->create();
$user = User::factory()->withPersonalOrganization()->create();
$invitation = $owner->currentTeam->teamInvitations()->create([
$invitation = $owner->currentOrganization->organizationInvitations()->create([
'email' => 'firstname.lastname@mail.test',
'role' => Role::Employee->value,
]);
@@ -141,7 +141,7 @@ class InviteTeamMemberTest extends TestCase
$response = $this->get($acceptUrl);
// Assert
$this->assertCount(1, $owner->currentTeam->fresh()->teamInvitations);
$this->assertCount(1, $owner->currentOrganization->fresh()->organizationInvitations);
$user->refresh();
$this->assertCount(1, $user->organizations);
}

View File

@@ -17,17 +17,17 @@ class LeaveTeamTest extends TestCase
// Arrange
$user = User::factory()->withPersonalOrganization()->create();
$user->currentTeam->users()->attach(
$user->currentOrganization->users()->attach(
$otherUser = User::factory()->create(), ['role' => 'admin']
);
$this->actingAs($otherUser);
// Act
$response = $this->delete('/teams/'.$user->currentTeam->id.'/members/'.$otherUser->id);
$response = $this->delete('/teams/'.$user->currentOrganization->id.'/members/'.$otherUser->id);
// Assert
$response->assertStatus(403);
$this->assertCount(2, $user->currentTeam->fresh()->users);
$this->assertCount(2, $user->currentOrganization->fresh()->users);
}
}

View File

@@ -17,12 +17,12 @@ class RemoveTeamMemberTest extends TestCase
// Arrange
$this->actingAs($user = User::factory()->withPersonalOrganization()->create());
$user->currentTeam->users()->attach(
$user->currentOrganization->users()->attach(
$otherUser = User::factory()->create(), ['role' => 'admin']
);
// Act
$response = $this->delete('/teams/'.$user->currentTeam->id.'/members/'.$otherUser->id);
$response = $this->delete('/teams/'.$user->currentOrganization->id.'/members/'.$otherUser->id);
// Assert
$response->assertStatus(403);

View File

@@ -19,12 +19,12 @@ class UpdateTeamMemberRoleTest extends TestCase
$user = User::factory()->withPersonalOrganization()->create();
$this->actingAs($user);
$user->currentTeam->users()->attach(
$user->currentOrganization->users()->attach(
$otherUser = User::factory()->create(), ['role' => 'admin']
);
// Act
$response = $this->put('/teams/'.$user->currentTeam->id.'/members/'.$otherUser->id, [
$response = $this->put('/teams/'.$user->currentOrganization->id.'/members/'.$otherUser->id, [
'role' => Role::Employee->value,
]);

View File

@@ -32,15 +32,15 @@ class UpdateTeamTest extends TestCase
$this->actingAs($user);
// Act
$response = $this->put('/teams/'.$user->currentTeam->id, [
$response = $this->put('/teams/'.$user->currentOrganization->id, [
'name' => 'Test Organization',
'currency' => 'USD',
]);
// Assert
$response->assertValid(errorBag: 'updateTeamName');
$this->assertCount(1, $user->fresh()->ownedTeams);
$organization = $user->currentTeam->fresh();
$this->assertCount(1, $user->fresh()->ownedOrganizations);
$organization = $user->currentOrganization->fresh();
$this->assertEquals('Test Organization', $organization->name);
$this->assertEquals('USD', $organization->currency);
}

View File

@@ -4,9 +4,11 @@ declare(strict_types=1);
namespace Tests\Unit\Filament\Resources;
use App\Enums\Role;
use App\Exceptions\Api\CanNotDeleteUserWhoIsOwnerOfOrganizationWithMultipleMembers;
use App\Filament\Resources\TimeEntryResource;
use App\Filament\Resources\UserResource;
use App\Models\Member;
use App\Models\Organization;
use App\Models\User;
use App\Service\DeletionService;
@@ -103,7 +105,7 @@ class UserResourceTest extends FilamentTestCase
$this->assertSame($userFake->email, $user->email);
$this->assertSame($userFake->timezone, $user->timezone);
$this->assertSame($userFake->week_start->value, $user->week_start->value);
$organization = $user->ownedTeams()->first();
$organization = $user->ownedOrganizations()->first();
$this->assertNotNull($organization);
$this->assertSame('EUR', $organization->currency);
$this->assertTrue(Hash::check('password', $user->password));
@@ -152,7 +154,9 @@ class UserResourceTest extends FilamentTestCase
// Arrange
$user = User::factory()->create();
$ownedOrganization = Organization::factory()->withOwner($user)->create();
Member::factory()->forOrganization($ownedOrganization)->forUser($user)->role(Role::Owner)->create();
$organization = Organization::factory()->create();
Member::factory()->forOrganization($organization)->forUser($user)->role(Role::Employee)->create();
// Act
$response = Livewire::test(UserResource\RelationManagers\OrganizationsRelationManager::class, [
@@ -163,7 +167,7 @@ class UserResourceTest extends FilamentTestCase
// Assert
$response->assertSuccessful();
$response->assertCanSeeTableRecords($user->organizations()->get());
$response->assertCanNotSeeTableRecords($user->ownedTeams()->get());
$response->assertCanSeeTableRecords($user->ownedOrganizations()->get());
}
public function test_can_list_related_owned_organizations(): void
@@ -171,7 +175,9 @@ class UserResourceTest extends FilamentTestCase
// Arrange
$user = User::factory()->create();
$ownedOrganization = Organization::factory()->withOwner($user)->create();
Member::factory()->forOrganization($ownedOrganization)->forUser($user)->role(Role::Owner)->create();
$organization = Organization::factory()->create();
Member::factory()->forOrganization($organization)->forUser($user)->role(Role::Employee)->create();
// Act
$response = Livewire::test(UserResource\RelationManagers\OwnedOrganizationsRelationManager::class, [
@@ -181,7 +187,7 @@ class UserResourceTest extends FilamentTestCase
// Assert
$response->assertSuccessful();
$response->assertCanSeeTableRecords($user->ownedTeams()->get());
$response->assertCanNotSeeTableRecords($user->organizations()->get());
$response->assertCanSeeTableRecords($user->ownedOrganizations()->get());
$response->assertCanNotSeeTableRecords([$organization]);
}
}

View File

@@ -62,6 +62,9 @@ class UserModelTest extends ModelTestAbstract
$user->organizations()->attach($organization, [
'role' => Role::Employee->value,
]);
$owner->organizations()->attach($organization, [
'role' => Role::Owner->value,
]);
$otherOrganization = Organization::factory()->create();
$otherUser = User::factory()->create();
$otherUser->organizations()->attach($otherOrganization, [
@@ -80,6 +83,70 @@ class UserModelTest extends ModelTestAbstract
$this->assertContains($owner->getKey(), $userIds);
}
public function test_is_member_of_organization_returns_true_for_user_attached_to_organization(): void
{
// Arrange
$organization = Organization::factory()->create();
$user = User::factory()->create();
$user->organizations()->attach($organization, [
'role' => Role::Employee->value,
]);
// Act
$isMemberOfOrganization = $user->isMemberOfOrganization($organization);
// Assert
$this->assertTrue($isMemberOfOrganization);
}
public function test_is_member_of_organization_returns_false_for_user_not_attached_to_organization(): void
{
// Arrange
$organization = Organization::factory()->create();
$user = User::factory()->create();
// Act
$isMemberOfOrganization = $user->isMemberOfOrganization($organization);
// Assert
$this->assertFalse($isMemberOfOrganization);
}
public function test_is_member_of_organization_uses_loaded_organizations_relation(): void
{
// Arrange
$organization = Organization::factory()->create();
$user = User::factory()
->attachToOrganization($organization, [
'role' => Role::Employee->value,
])
->create();
$user->load('organizations');
// Act
$isMemberOfOrganization = $user->isMemberOfOrganization($organization);
// Assert
$this->assertTrue($isMemberOfOrganization);
}
public function test_is_member_of_organization_does_not_query_when_organizations_relation_is_loaded(): void
{
// Arrange
$organization = Organization::factory()->create();
$user = User::factory()->create();
$user->load('organizations');
$user->organizations()->attach($organization, [
'role' => Role::Employee->value,
]);
// Act
$isMemberOfOrganization = $user->isMemberOfOrganization($organization);
// Assert
$this->assertFalse($isMemberOfOrganization);
}
public function test_it_has_many_time_entries(): void
{
// Arrange