Fixed problem with merge into when project members already exist in destination member

This commit is contained in:
Constantin Graf
2025-03-10 13:46:57 +01:00
committed by Constantin Graf
parent 02a716897d
commit 73ce5f793d
7 changed files with 190 additions and 43 deletions

View File

@@ -350,6 +350,58 @@ class MemberEndpointTest extends ApiEndpointTestAbstract
$memberDestination->refresh();
$this->assertCount(3, $memberDestination->timeEntries);
$this->assertCount(1, $memberDestination->projectMembers);
$this->assertDatabaseHas(ProjectMember::class, [
'project_id' => $project->getKey(),
'member_id' => $memberDestination->getKey(),
'user_id' => $userDestination->getKey(),
]);
}
public function test_merge_into_assigns_resources_of_source_member_to_destination_member_and_deletes_member_with_existing_destination_resources(): void
{
// Arrange
$data = $this->createUserWithPermission([
'members:merge-into',
]);
$userSource = User::factory()->placeholder()->create();
$memberSource = Member::factory()->forUser($userSource)->forOrganization($data->organization)->role(Role::Placeholder)->create();
TimeEntry::factory()->forMember($memberSource)->createMany(3);
$project = Project::factory()->forOrganization($data->organization)->create();
ProjectMember::factory()->forMember($memberSource)->forProject($project)->create([
'billable_rate' => 32100,
]);
$userDestination = User::factory()->create();
$memberDestination = Member::factory()->forUser($userDestination)->forOrganization($data->organization)->role(Role::Admin)->create();
ProjectMember::factory()->forMember($memberDestination)->forProject($project)->create([
'billable_rate' => 12300,
]);
TimeEntry::factory()->forMember($memberDestination)->createMany(3);
Passport::actingAs($data->user);
// Act
$response = $this->withoutExceptionHandling()->postJson(route('api.v1.members.merge-into', [$data->organization->getKey(), $memberSource->getKey()]), [
'member_id' => $memberDestination->getKey(),
]);
// Assert
$response->assertStatus(204);
$this->assertSame('', $response->getContent());
$this->assertDatabaseMissing(Member::class, [
'id' => $memberSource->getKey(),
]);
$this->assertDatabaseMissing(User::class, [
'id' => $userSource->getKey(),
]);
$memberDestination->refresh();
$this->assertCount(6, $memberDestination->timeEntries);
$this->assertCount(1, $memberDestination->projectMembers);
$this->assertDatabaseHas(ProjectMember::class, [
'project_id' => $project->getKey(),
'billable_rate' => 12300,
'member_id' => $memberDestination->getKey(),
'user_id' => $userDestination->getKey(),
]);
}
public function test_update_member_fails_if_user_tries_to_change_role_of_the_current_owner(): void

View File

@@ -113,4 +113,94 @@ class MemberServiceTest extends TestCaseWithDatabase
$this->assertSame($otherMember->getKey(), $otherTimeEntry->member_id);
$this->assertSame(1, $otherUser->organizations()->count());
}
public function test_assign_organization_entities_to_different_member_without_any_entries(): void
{
// Arrange
$organization = Organization::factory()->create();
$project = Project::factory()->forOrganization($organization)->create();
$otherUser = User::factory()->create();
$fromUser = User::factory()->create();
$toUser = User::factory()->create();
$otherUserMember = Member::factory()->forOrganization($organization)->forUser($otherUser)->create();
$fromUserMember = Member::factory()->forOrganization($organization)->forUser($fromUser)->create();
$toUserMember = Member::factory()->forOrganization($organization)->forUser($toUser)->create();
TimeEntry::factory()->forOrganization($organization)->forMember($otherUserMember)->createMany(3);
TimeEntry::factory()->forOrganization($organization)->forMember($fromUserMember)->createMany(3);
ProjectMember::factory()->forProject($project)->forMember($otherUserMember)->create();
ProjectMember::factory()->forProject($project)->forMember($fromUserMember)->create();
// Act
$this->memberService->assignOrganizationEntitiesToDifferentMember($organization, $fromUserMember, $toUserMember);
// Assert
$this->assertSame(3, TimeEntry::query()->whereBelongsTo($toUser, 'user')->count());
$this->assertSame(3, TimeEntry::query()->whereBelongsTo($otherUser, 'user')->count());
$this->assertSame(0, TimeEntry::query()->whereBelongsTo($fromUser, 'user')->count());
$this->assertSame(1, ProjectMember::query()->whereBelongsTo($toUser, 'user')->count());
$this->assertSame(1, ProjectMember::query()->whereBelongsTo($otherUser, 'user')->count());
$this->assertSame(0, ProjectMember::query()->whereBelongsTo($fromUser, 'user')->count());
$this->assertSame(3, TimeEntry::query()->whereBelongsTo($toUserMember, 'member')->count());
$this->assertSame(3, TimeEntry::query()->whereBelongsTo($otherUserMember, 'member')->count());
$this->assertSame(0, TimeEntry::query()->whereBelongsTo($fromUserMember, 'member')->count());
$this->assertSame(1, ProjectMember::query()->whereBelongsTo($toUserMember, 'member')->count());
$this->assertSame(1, ProjectMember::query()->whereBelongsTo($otherUserMember, 'member')->count());
$this->assertSame(0, ProjectMember::query()->whereBelongsTo($fromUserMember, 'member')->count());
}
public function test_assign_organization_entities_to_different_member_with_entries(): void
{
// Arrange
$organization = Organization::factory()->create();
$project = Project::factory()->forOrganization($organization)->create();
$otherUser = User::factory()->create();
$fromUser = User::factory()->create();
$toUser = User::factory()->create();
$otherUserMember = Member::factory()->forOrganization($organization)->forUser($otherUser)->create();
$fromUserMember = Member::factory()->forOrganization($organization)->forUser($fromUser)->create();
$toUserMember = Member::factory()->forOrganization($organization)->forUser($toUser)->create();
TimeEntry::factory()->forOrganization($organization)->forMember($otherUserMember)->createMany(3);
TimeEntry::factory()->forOrganization($organization)->forMember($fromUserMember)->createMany(3);
TimeEntry::factory()->forOrganization($organization)->forMember($toUserMember)->createMany(3);
ProjectMember::factory()->forProject($project)->forMember($otherUserMember)->create([
'billable_rate' => 1,
]);
ProjectMember::factory()->forProject($project)->forMember($fromUserMember)->create([
'billable_rate' => 2,
]);
ProjectMember::factory()->forProject($project)->forMember($toUserMember)->create([
'billable_rate' => 3,
]);
// Act
$this->memberService->assignOrganizationEntitiesToDifferentMember($organization, $fromUserMember, $toUserMember);
// Assert
$this->assertSame(6, TimeEntry::query()->whereBelongsTo($toUser, 'user')->count());
$this->assertSame(3, TimeEntry::query()->whereBelongsTo($otherUser, 'user')->count());
$this->assertSame(0, TimeEntry::query()->whereBelongsTo($fromUser, 'user')->count());
$this->assertSame(1, ProjectMember::query()->whereBelongsTo($toUser, 'user')->count());
$this->assertSame(1, ProjectMember::query()->whereBelongsTo($otherUser, 'user')->count());
$this->assertSame(0, ProjectMember::query()->whereBelongsTo($fromUser, 'user')->count());
$this->assertSame(6, TimeEntry::query()->whereBelongsTo($toUserMember, 'member')->count());
$this->assertSame(3, TimeEntry::query()->whereBelongsTo($otherUserMember, 'member')->count());
$this->assertSame(0, TimeEntry::query()->whereBelongsTo($fromUserMember, 'member')->count());
$this->assertSame(1, ProjectMember::query()->whereBelongsTo($toUserMember, 'member')->count());
$this->assertSame(1, ProjectMember::query()->whereBelongsTo($otherUserMember, 'member')->count());
$this->assertSame(0, ProjectMember::query()->whereBelongsTo($fromUserMember, 'member')->count());
$this->assertDatabaseCount(ProjectMember::class, 2);
$this->assertDatabaseHas(ProjectMember::class, [
'project_id' => $project->id,
'member_id' => $toUserMember->id,
'billable_rate' => 3,
]);
$this->assertDatabaseHas(ProjectMember::class, [
'project_id' => $project->id,
'member_id' => $otherUserMember->id,
'billable_rate' => 1,
]);
}
}

View File

@@ -59,22 +59,6 @@ class UserServiceTest extends TestCase
$this->assertSame(0, ProjectMember::query()->whereBelongsTo($fromUser, 'user')->count());
}
public function test_assign_organization_entities_to_different_user_fails_if_new_user_is_not_member_of_organization(): void
{
// Arrange
$organization = Organization::factory()->create();
$fromUser = User::factory()->create();
$toUser = User::factory()->create();
$fromUserMember = Member::factory()->forOrganization($organization)->forUser($fromUser)->create();
// Act
try {
$this->userService->assignOrganizationEntitiesToDifferentUser($organization, $fromUser, $toUser);
} catch (\InvalidArgumentException $e) {
$this->assertSame('User is not a member of the organization', $e->getMessage());
}
}
public function test_change_ownership_changes_ownership_of_organization_to_new_user(): void
{
// Arrange