Added billable rates; Added project members; Added visibility to projects

This commit is contained in:
Constantin Graf
2024-03-28 18:50:04 +01:00
parent bb42b0940a
commit ba0212ea01
71 changed files with 3236 additions and 174 deletions

View File

@@ -32,7 +32,7 @@ class ClientEndpointTest extends ApiEndpointTestAbstract
$data = $this->createUserWithPermission([
'clients:view',
]);
$clients = Client::factory()->forOrganization($data->organization)->createMany(4);
$clients = Client::factory()->forOrganization($data->organization)->randomCreatedAt()->createMany(4);
Passport::actingAs($data->user);
// Act
@@ -41,15 +41,16 @@ class ClientEndpointTest extends ApiEndpointTestAbstract
// Assert
$response->assertStatus(200);
$response->assertJsonCount(4, 'data');
$clients = Client::query()->orderBy('created_at', 'desc')->get();
$response->assertJson(fn (AssertableJson $json) => $json
->has('data')
->has('links')
->has('meta')
->count('data', 4)
->where('data.0.id', $clients->sortByDesc('created_at')->get(0)->getKey())
->where('data.1.id', $clients->sortByDesc('created_at')->get(1)->getKey())
->where('data.2.id', $clients->sortByDesc('created_at')->get(2)->getKey())
->where('data.3.id', $clients->sortByDesc('created_at')->get(3)->getKey())
->where('data.0.id', $clients->get(0)->getKey())
->where('data.1.id', $clients->get(1)->getKey())
->where('data.2.id', $clients->get(2)->getKey())
->where('data.3.id', $clients->get(3)->getKey())
);
}

View File

@@ -14,12 +14,12 @@ class MemberEndpointTest extends ApiEndpointTestAbstract
{
// Arrange
$data = $this->createUserWithPermission([
'users:view',
'members:view',
]);
Passport::actingAs($data->user);
// Act
$response = $this->getJson(route('api.v1.users.index', $data->organization->id));
$response = $this->getJson(route('api.v1.members.index', $data->organization->id));
// Assert
$response->assertStatus(200);
@@ -28,7 +28,7 @@ class MemberEndpointTest extends ApiEndpointTestAbstract
public function test_invite_placeholder_succeeds_if_data_is_valid(): void
{
$data = $this->createUserWithPermission([
'users:invite-placeholder',
'members:invite-placeholder',
], true);
$user = User::factory()->create([
'is_placeholder' => true,
@@ -39,7 +39,7 @@ class MemberEndpointTest extends ApiEndpointTestAbstract
Passport::actingAs($data->user);
// Act
$response = $this->postJson(route('api.v1.users.invite-placeholder', [
$response = $this->postJson(route('api.v1.members.invite-placeholder', [
'organization' => $data->organization->id,
'user' => $user->id,
]));
@@ -61,7 +61,7 @@ class MemberEndpointTest extends ApiEndpointTestAbstract
Passport::actingAs($data->user);
// Act
$response = $this->postJson(route('api.v1.users.invite-placeholder', ['organization' => $data->organization->id, 'user' => $user->id]));
$response = $this->postJson(route('api.v1.members.invite-placeholder', ['organization' => $data->organization->id, 'user' => $user->id]));
// Assert
$response->assertForbidden();
@@ -71,7 +71,7 @@ class MemberEndpointTest extends ApiEndpointTestAbstract
{
// Arrange
$data = $this->createUserWithPermission([
'users:invite-placeholder',
'members:invite-placeholder',
]);
$otherOrganization = Organization::factory()->create();
$user = User::factory()->create([
@@ -81,7 +81,7 @@ class MemberEndpointTest extends ApiEndpointTestAbstract
Passport::actingAs($data->user);
// Act
$response = $this->postJson(route('api.v1.users.invite-placeholder', ['organization' => $data->organization->id, 'user' => $user->id]));
$response = $this->postJson(route('api.v1.members.invite-placeholder', ['organization' => $data->organization->id, 'user' => $user->id]));
// Assert
$response->assertForbidden();
@@ -91,12 +91,12 @@ class MemberEndpointTest extends ApiEndpointTestAbstract
{
// Arrange
$data = $this->createUserWithPermission([
'users:invite-placeholder',
'members:invite-placeholder',
]);
Passport::actingAs($data->user);
// Act
$response = $this->postJson(route('api.v1.users.invite-placeholder', ['organization' => $data->organization->id, 'user' => $data->user->id]));
$response = $this->postJson(route('api.v1.members.invite-placeholder', ['organization' => $data->organization->id, 'user' => $data->user->id]));
// Assert
$response->assertStatus(400);

View File

@@ -68,12 +68,37 @@ class OrganizationEndpointTest extends ApiEndpointTestAbstract
// Act
$response = $this->putJson(route('api.v1.organizations.update', [$data->organization->getKey()]), [
'name' => $organizationFake->name,
'billable_rate' => $organizationFake->billable_rate,
]);
// Assert
$response->assertStatus(200);
$this->assertDatabaseHas(Organization::class, [
'name' => $organizationFake->name,
'billable_rate' => $organizationFake->billable_rate,
]);
}
public function test_update_endpoint_can_update_billable_rate_of_organization(): void
{
// Arrange
$data = $this->createUserWithPermission([
'organizations:update',
]);
$organizationFake = Organization::factory()->make();
Passport::actingAs($data->user);
// Act
$response = $this->putJson(route('api.v1.organizations.update', [$data->organization->getKey()]), [
'name' => $organizationFake->name,
'billable_rate' => $organizationFake->billable_rate,
]);
// Assert
$response->assertStatus(200);
$this->assertDatabaseHas(Organization::class, [
'name' => $organizationFake->name,
'billable_rate' => $organizationFake->billable_rate,
]);
}
}

View File

@@ -27,13 +27,33 @@ class ProjectEndpointTest extends ApiEndpointTestAbstract
$response->assertForbidden();
}
public function test_index_endpoint_returns_list_of_all_projects_of_organization(): void
public function test_index_endpoint_returns_list_of_all_projects_of_organization_for_user_with_all_projects_permission(): void
{
// Arrange
$data = $this->createUserWithPermission([
'projects:view',
'projects:view:all',
]);
$projects = Project::factory()->forOrganization($data->organization)->createMany(4);
Passport::actingAs($data->user);
// Act
$response = $this->getJson(route('api.v1.projects.index', [$data->organization->getKey()]));
// Assert
$response->assertStatus(200);
$response->assertJsonCount(4, 'data');
}
public function test_index_endpoint_returns_list_of_projects_of_organization_which_are_public_or_where_user_is_member_for_user_with_restricted_permission(): void
{
// Arrange
$data = $this->createUserWithPermission([
'projects:view',
]);
$projects = Project::factory()->forOrganization($data->organization)->createMany(4);
$privateProjects = Project::factory()->forOrganization($data->organization)->isPrivate()->createMany(2);
$publicProjects = Project::factory()->forOrganization($data->organization)->isPublic()->createMany(2);
$privateProjectsWithMembership = Project::factory()->forOrganization($data->organization)->addMember($data->user)->isPrivate()->createMany(2);
Passport::actingAs($data->user);
// Act
@@ -162,6 +182,32 @@ class ProjectEndpointTest extends ApiEndpointTestAbstract
]);
}
public function test_store_endpoint_creates_new_project_with_billable_rate(): void
{
// Arrange
$data = $this->createUserWithPermission([
'projects:create',
]);
$project = Project::factory()->forOrganization($data->organization)->make();
Passport::actingAs($data->user);
// Act
$response = $this->postJson(route('api.v1.projects.store', [$data->organization->getKey()]), [
'name' => $project->name,
'color' => $project->color,
'billable_rate' => 10001,
]);
// Assert
$response->assertStatus(201);
$this->assertDatabaseHas(Project::class, [
'name' => $project->name,
'color' => $project->color,
'organization_id' => $project->organization_id,
'billable_rate' => 10001,
]);
}
public function test_update_endpoint_fails_if_user_is_not_part_of_project_organization(): void
{
// Arrange
@@ -210,12 +256,14 @@ class ProjectEndpointTest extends ApiEndpointTestAbstract
]);
$project = Project::factory()->forOrganization($data->organization)->create();
$projectFake = Project::factory()->make();
$client = Client::factory()->forOrganization($data->organization)->create();
Passport::actingAs($data->user);
// Act
$response = $this->putJson(route('api.v1.projects.update', [$data->organization->getKey(), $project->getKey()]), [
'name' => $projectFake->name,
'color' => $projectFake->color,
'client_id' => $client->getKey(),
]);
// Assert
@@ -223,6 +271,33 @@ class ProjectEndpointTest extends ApiEndpointTestAbstract
$this->assertDatabaseHas(Project::class, [
'name' => $projectFake->name,
'color' => $projectFake->color,
'client_id' => $client->getKey(),
]);
}
public function test_update_endpoint_can_update_projects_billable_rate(): void
{
// Arrange
$data = $this->createUserWithPermission([
'projects:update',
]);
$project = Project::factory()->forOrganization($data->organization)->create();
$projectFake = Project::factory()->make();
Passport::actingAs($data->user);
// Act
$response = $this->putJson(route('api.v1.projects.update', [$data->organization->getKey(), $project->getKey()]), [
'name' => $projectFake->name,
'color' => $projectFake->color,
'billable_rate' => 10002,
]);
// Assert
$response->assertStatus(200);
$this->assertDatabaseHas(Project::class, [
'name' => $projectFake->name,
'color' => $projectFake->color,
'billable_rate' => 10002,
]);
}

View File

@@ -0,0 +1,300 @@
<?php
declare(strict_types=1);
namespace Tests\Unit\Endpoint\Api\V1;
use App\Models\Project;
use App\Models\ProjectMember;
use App\Models\User;
use Laravel\Passport\Passport;
class ProjectMemberEndpointTest extends ApiEndpointTestAbstract
{
public function test_index_endpoint_fails_if_user_has_no_permission_to_view_project_members(): void
{
// Arrange
$data = $this->createUserWithPermission([
]);
$project = Project::factory()->forOrganization($data->organization)->create();
$projectMembers = ProjectMember::factory()->forProject($project)->createMany(4);
Passport::actingAs($data->user);
// Act
$response = $this->getJson(route('api.v1.project-members.index', [
$data->organization->getKey(),
$project->getKey(),
]));
// Assert
$response->assertForbidden();
}
public function test_index_endpoint_fails_if_the_project_does_not_belong_to_given_organization(): void
{
// Arrange
$data = $this->createUserWithPermission([
'project-members:view',
]);
$otherData = $this->createUserWithPermission([
'project-members:view',
]);
$project = Project::factory()->forOrganization($otherData->organization)->create();
$projectMembers = ProjectMember::factory()->forProject($project)->createMany(4);
Passport::actingAs($data->user);
// Act
$response = $this->getJson(route('api.v1.project-members.index', [
$data->organization->getKey(),
$project->getKey(),
]));
// Assert
$response->assertForbidden();
}
public function test_index_endpoint_returns_list_of_all_project_members_of_a_project(): void
{
// Arrange
$data = $this->createUserWithPermission([
'project-members:view',
]);
$project = Project::factory()->forOrganization($data->organization)->create();
$projectMembers = ProjectMember::factory()->forProject($project)->createMany(4);
Passport::actingAs($data->user);
// Act
$response = $this->getJson(route('api.v1.project-members.index', [
$data->organization->getKey(),
$project->getKey(),
]));
// Assert
$response->assertStatus(200);
$response->assertJsonCount(4, 'data');
}
public function test_store_endpoint_fails_if_user_has_no_permission_to_add_members_to_project(): void
{
// Arrange
$data = $this->createUserWithPermission([
]);
$project = Project::factory()->forOrganization($data->organization)->create();
$projectMemberFake = ProjectMember::factory()->make();
$user = User::factory()->attachToOrganization($data->organization)->create();
Passport::actingAs($data->user);
// Act
$response = $this->postJson(route('api.v1.project-members.store', [$data->organization->getKey(), $project->getKey()]), [
'billable_rate' => $projectMemberFake->billable_rate,
'user_id' => $user->getKey(),
]);
// Assert
$response->assertForbidden();
}
public function test_store_endpoint_fails_if_given_project_does_not_belong_to_organization(): void
{
// Arrange
$data = $this->createUserWithPermission([
'project-members:create',
]);
$otherData = $this->createUserWithPermission([
'project-members:create',
]);
$project = Project::factory()->forOrganization($otherData->organization)->create();
$projectMemberFake = ProjectMember::factory()->make();
$user = User::factory()->attachToOrganization($data->organization)->create();
Passport::actingAs($data->user);
// Act
$response = $this->postJson(route('api.v1.project-members.store', [$data->organization->getKey(), $project->getKey()]), [
'billable_rate' => $projectMemberFake->billable_rate,
'user_id' => $user->getKey(),
]);
// Assert
$response->assertForbidden();
}
public function test_store_endpoint_fails_if_given_user_does_not_belong_to_organization(): void
{
// Arrange
$data = $this->createUserWithPermission([
'project-members:create',
]);
$otherData = $this->createUserWithPermission([
'project-members:create',
]);
$project = Project::factory()->forOrganization($data->organization)->create();
$projectMemberFake = ProjectMember::factory()->make();
$user = User::factory()->attachToOrganization($otherData->organization)->create();
Passport::actingAs($data->user);
// Act
$response = $this->postJson(route('api.v1.project-members.store', [$data->organization->getKey(), $project->getKey()]), [
'billable_rate' => $projectMemberFake->billable_rate,
'user_id' => $user->getKey(),
]);
// Assert
$response->assertInvalid(['user_id']);
}
public function test_store_endpoint_creates_new_project_member(): void
{
// Arrange
$data = $this->createUserWithPermission([
'project-members:create',
]);
$project = Project::factory()->forOrganization($data->organization)->create();
$projectMemberFake = ProjectMember::factory()->make();
$user = User::factory()->attachToOrganization($data->organization)->create();
Passport::actingAs($data->user);
// Act
$response = $this->postJson(route('api.v1.project-members.store', [$data->organization->getKey(), $project->getKey()]), [
'billable_rate' => $projectMemberFake->billable_rate,
'user_id' => $user->getKey(),
]);
// Assert
$response->assertStatus(201);
$this->assertDatabaseHas(ProjectMember::class, [
'billable_rate' => $projectMemberFake->billable_rate,
'user_id' => $user->getKey(),
'project_id' => $project->getKey(),
]);
}
public function test_update_endpoint_fails_if_project_member_is_not_part_of_organization(): void
{
// Arrange
$data = $this->createUserWithPermission([
'project-members:update',
]);
$otherData = $this->createUserWithPermission([
'project-members:update',
]);
$project = Project::factory()->forOrganization($otherData->organization)->create();
$projectMember = ProjectMember::factory()->forProject($project)->create();
$projectMemberFake = ProjectMember::factory()->make();
Passport::actingAs($data->user);
// Act
$response = $this->putJson(route('api.v1.project-members.update', [$data->organization->getKey(), $projectMember->getKey()]), [
'billable_rate' => $projectMemberFake->billable_rate,
]);
// Assert
$response->assertForbidden();
}
public function test_update_endpoint_fails_if_user_has_no_permission_to_update_projects(): void
{
// Arrange
$data = $this->createUserWithPermission([
]);
$project = Project::factory()->forOrganization($data->organization)->create();
$projectMember = ProjectMember::factory()->forProject($project)->create();
$projectMemberFake = ProjectMember::factory()->make();
Passport::actingAs($data->user);
// Act
$response = $this->putJson(route('api.v1.project-members.update', [$data->organization->getKey(), $projectMember->getKey()]), [
'billable_rate' => $projectMemberFake->billable_rate,
]);
// Assert
$response->assertForbidden();
}
public function test_update_endpoint_updates_project_member(): void
{
// Arrange
$data = $this->createUserWithPermission([
'project-members:update',
]);
$project = Project::factory()->forOrganization($data->organization)->create();
$projectMember = ProjectMember::factory()->forProject($project)->create();
$projectMemberFake = ProjectMember::factory()->make();
Passport::actingAs($data->user);
// Act
$response = $this->putJson(route('api.v1.project-members.update', [$data->organization->getKey(), $projectMember->getKey()]), [
'billable_rate' => $projectMemberFake->billable_rate,
]);
// Assert
$response->assertStatus(200);
$this->assertDatabaseHas(ProjectMember::class, [
'id' => $projectMember->getKey(),
'billable_rate' => $projectMemberFake->billable_rate,
'user_id' => $projectMember->user_id,
]);
}
public function test_destroy_endpoint_fails_if_user_is_not_part_of_project_members_organization(): void
{
// Arrange
$data = $this->createUserWithPermission([
'project-members:delete',
]);
$otherData = $this->createUserWithPermission([
'project-members:delete',
]);
$project = Project::factory()->forOrganization($otherData->organization)->create();
$projectMember = ProjectMember::factory()->forProject($project)->create();
Passport::actingAs($data->user);
// Act
$response = $this->deleteJson(route('api.v1.project-members.destroy', [$data->organization->getKey(), $projectMember->getKey()]));
// Assert
$response->assertForbidden();
$this->assertDatabaseHas(ProjectMember::class, [
'id' => $projectMember->getKey(),
]);
}
public function test_destroy_endpoint_fails_if_user_has_no_permission_to_delete_projects(): void
{
// Arrange
$data = $this->createUserWithPermission([
]);
$project = Project::factory()->forOrganization($data->organization)->create();
$projectMember = ProjectMember::factory()->forProject($project)->create();
Passport::actingAs($data->user);
// Act
$response = $this->deleteJson(route('api.v1.project-members.destroy', [$data->organization->getKey(), $projectMember->getKey()]));
// Assert
$response->assertForbidden();
$this->assertDatabaseHas(ProjectMember::class, [
'id' => $projectMember->getKey(),
]);
}
public function test_destroy_endpoint_deletes_project(): void
{
// Arrange
$data = $this->createUserWithPermission([
'project-members:delete',
]);
$project = Project::factory()->forOrganization($data->organization)->create();
$projectMember = ProjectMember::factory()->forProject($project)->create();
Passport::actingAs($data->user);
// Act
$response = $this->deleteJson(route('api.v1.project-members.destroy', [$data->organization->getKey(), $projectMember->getKey()]));
// Assert
$response->assertStatus(204);
$response->assertNoContent();
$this->assertDatabaseMissing(ProjectMember::class, [
'id' => $projectMember->getKey(),
]);
}
}

View File

@@ -32,7 +32,7 @@ class TagEndpointTest extends ApiEndpointTestAbstract
$data = $this->createUserWithPermission([
'tags:view',
]);
$tags = Tag::factory()->forOrganization($data->organization)->createMany(4);
$tags = Tag::factory()->forOrganization($data->organization)->randomCreatedAt()->createMany(4);
Passport::actingAs($data->user);
// Act
@@ -41,15 +41,16 @@ class TagEndpointTest extends ApiEndpointTestAbstract
// Assert
$response->assertStatus(200);
$response->assertJsonCount(4, 'data');
$tags = Tag::query()->orderBy('created_at', 'desc')->get();
$response->assertJson(fn (AssertableJson $json) => $json
->has('data')
->has('links')
->has('meta')
->count('data', 4)
->where('data.0.id', $tags->sortByDesc('created_at')->get(0)->getKey())
->where('data.1.id', $tags->sortByDesc('created_at')->get(1)->getKey())
->where('data.2.id', $tags->sortByDesc('created_at')->get(2)->getKey())
->where('data.3.id', $tags->sortByDesc('created_at')->get(3)->getKey())
->where('data.0.id', $tags->get(0)->getKey())
->where('data.1.id', $tags->get(1)->getKey())
->where('data.2.id', $tags->get(2)->getKey())
->where('data.3.id', $tags->get(3)->getKey())
);
}

View File

@@ -382,6 +382,35 @@ class TimeEntryEndpointTest extends ApiEndpointTestAbstract
]);
}
public function test_store_endpoint_validation_fails_if_project_id_is_missing_but_request_has_task_id(): void
{
// Arrange
$data = $this->createUserWithPermission([
'time-entries:create:own',
]);
$timeEntryFake = TimeEntry::factory()->forOrganization($data->organization)->withTask($data->organization)->make();
$timeEntryFake2 = TimeEntry::factory()->forOrganization($data->organization)->withTask($data->organization)->make();
Passport::actingAs($data->user);
// Act
$response = $this->postJson(route('api.v1.time-entries.store', [$data->organization->getKey()]), [
'description' => $timeEntryFake->description,
'billable' => $timeEntryFake->billable,
'start' => $timeEntryFake->start->toIso8601ZuluString(),
'end' => $timeEntryFake->end->toIso8601ZuluString(),
'tags' => $timeEntryFake->tags,
'user_id' => $data->user->getKey(),
'task_id' => $timeEntryFake2->task_id,
]);
// Assert
$response->assertStatus(422);
$response->assertJsonValidationErrors([
'project_id' => 'The project field is required when task is present.',
'task_id' => 'The task is not part of the given project.',
]);
}
public function test_store_endpoint_creates_new_time_entry_for_current_user(): void
{
// Arrange
@@ -578,6 +607,66 @@ class TimeEntryEndpointTest extends ApiEndpointTestAbstract
$response->assertForbidden();
}
public function test_update_endpoint_validation_fails_if_task_id_does_not_belong_to_project_id(): void
{
// Arrange
$data = $this->createUserWithPermission([
'time-entries:update:own',
]);
$timeEntry = TimeEntry::factory()->forOrganization($data->organization)->forUser($data->user)->create();
$timeEntryFake = TimeEntry::factory()->forOrganization($data->organization)->withTask($data->organization)->make();
$timeEntryFake2 = TimeEntry::factory()->forOrganization($data->organization)->withTask($data->organization)->make();
Passport::actingAs($data->user);
// Act
$response = $this->putJson(route('api.v1.time-entries.update', [$data->organization->getKey(), $timeEntry->getKey()]), [
'description' => $timeEntryFake->description,
'billable' => $timeEntryFake->billable,
'start' => $timeEntryFake->start->toIso8601ZuluString(),
'end' => $timeEntryFake->end->toIso8601ZuluString(),
'tags' => $timeEntryFake->tags,
'user_id' => $data->user->getKey(),
'project_id' => $timeEntryFake->project_id,
'task_id' => $timeEntryFake2->task_id,
]);
// Assert
$response->assertStatus(422);
$response->assertJsonValidationErrors([
'task_id' => 'The task is not part of the given project.',
]);
}
public function test_update_endpoint_validation_fails_if_project_id_is_missing_but_request_has_task_id(): void
{
// Arrange
$data = $this->createUserWithPermission([
'time-entries:update:own',
]);
$timeEntry = TimeEntry::factory()->forOrganization($data->organization)->forUser($data->user)->create();
$timeEntryFake = TimeEntry::factory()->forOrganization($data->organization)->withTask($data->organization)->make();
$timeEntryFake2 = TimeEntry::factory()->forOrganization($data->organization)->withTask($data->organization)->make();
Passport::actingAs($data->user);
// Act
$response = $this->putJson(route('api.v1.time-entries.update', [$data->organization->getKey(), $timeEntry->getKey()]), [
'description' => $timeEntryFake->description,
'billable' => $timeEntryFake->billable,
'start' => $timeEntryFake->start->toIso8601ZuluString(),
'end' => $timeEntryFake->end->toIso8601ZuluString(),
'tags' => $timeEntryFake->tags,
'user_id' => $data->user->getKey(),
'task_id' => $timeEntryFake2->task_id,
]);
// Assert
$response->assertStatus(422);
$response->assertJsonValidationErrors([
'project_id' => 'The project field is required when task is present.',
'task_id' => 'The task is not part of the given project.',
]);
}
public function test_update_endpoint_updates_time_entry_for_current_user(): void
{
// Arrange
@@ -585,7 +674,7 @@ class TimeEntryEndpointTest extends ApiEndpointTestAbstract
'time-entries:update:own',
]);
$timeEntry = TimeEntry::factory()->forOrganization($data->organization)->forUser($data->user)->create();
$timeEntryFake = TimeEntry::factory()->forOrganization($data->organization)->make();
$timeEntryFake = TimeEntry::factory()->withTags($data->organization)->forOrganization($data->organization)->make();
Passport::actingAs($data->user);
// Act
@@ -595,7 +684,6 @@ class TimeEntryEndpointTest extends ApiEndpointTestAbstract
'end' => $timeEntryFake->end->toIso8601ZuluString(),
'tags' => $timeEntryFake->tags,
'user_id' => $data->user->getKey(),
'task_id' => $timeEntryFake->task_id,
]);
// Assert

View File

@@ -0,0 +1,43 @@
<?php
declare(strict_types=1);
namespace Tests\Unit\Model;
use App\Models\Project;
use App\Models\ProjectMember;
use App\Models\User;
class ProjectMemberModelTest extends ModelTestAbstract
{
public function test_it_belongs_to_a_project(): void
{
// Arrange
$project = Project::factory()->create();
$user = User::factory()->create();
$projectMember = ProjectMember::factory()->forProject($project)->forUser($user)->create();
// Act
$projectMember->refresh();
$projectRel = $projectMember->project;
// Assert
$this->assertNotNull($projectRel);
$this->assertTrue($projectRel->is($project));
}
public function test_it_belongs_to_a_user(): void
{
// Arrange
$user = User::factory()->create();
$projectMember = ProjectMember::factory()->forUser($user)->create();
// Act
$projectMember->refresh();
$userRel = $projectMember->user;
// Assert
$this->assertNotNull($userRel);
$this->assertTrue($userRel->is($user));
}
}

View File

@@ -7,6 +7,7 @@ namespace Tests\Unit\Model;
use App\Models\Client;
use App\Models\Organization;
use App\Models\Project;
use App\Models\ProjectMember;
use App\Models\Task;
class ProjectModelTest extends ModelTestAbstract
@@ -69,4 +70,20 @@ class ProjectModelTest extends ModelTestAbstract
$this->assertCount(3, $tasksRel);
$this->assertTrue($tasksRel->first()->is($tasks->first()));
}
public function test_it_has_many_members(): void
{
// Arrange
$project = Project::factory()->create();
$members = ProjectMember::factory()->forProject($project)->createMany(3);
// Act
$project->refresh();
$membersRel = $project->members;
// Assert
$this->assertNotNull($membersRel);
$this->assertCount(3, $membersRel);
$this->assertTrue($membersRel->first()->is($members->first()));
}
}

View File

@@ -0,0 +1,84 @@
<?php
declare(strict_types=1);
namespace Tests\Unit\Rules;
use App\Rules\CurrencyRule;
use Illuminate\Support\Facades\Validator;
use Tests\TestCase;
class CurrencyRuleTest extends TestCase
{
public function test_validation_passes_if_value_is_valid_currency_code(): void
{
// Arrange
$validator = Validator::make([
'currency' => 'EUR',
], [
'currency' => [new CurrencyRule()],
]);
// Act
$isValid = $validator->passes();
$messages = $validator->messages()->toArray();
// Assert
$this->assertTrue($isValid);
$this->assertArrayNotHasKey('currency', $messages);
}
public function test_validation_fails_if_value_is_not_a_string(): void
{
// Arrange
$validator = Validator::make([
'currency' => true,
], [
'currency' => [new CurrencyRule()],
]);
// Act
$isValid = $validator->passes();
$messages = $validator->messages()->toArray();
// Assert
$this->assertFalse($isValid);
$this->assertEquals('The currency field must be a string.', $messages['currency'][0]);
}
public function test_validation_fails_if_value_is_not_a_valid_currency(): void
{
// Arrange
$validator = Validator::make([
'currency' => 'XXX',
], [
'currency' => [new CurrencyRule()],
]);
// Act
$isValid = $validator->passes();
$messages = $validator->messages()->toArray();
// Assert
$this->assertFalse($isValid);
$this->assertEquals('The currency field must be a valid currency code (ISO 4217).', $messages['currency'][0]);
}
public function test_validation_fails_if_value_is_lower_case(): void
{
// Arrange
$validator = Validator::make([
'currency' => 'eur',
], [
'currency' => [new CurrencyRule()],
]);
// Act
$isValid = $validator->passes();
$messages = $validator->messages()->toArray();
// Assert
$this->assertFalse($isValid);
$this->assertEquals('The currency field must be a valid currency code (ISO 4217).', $messages['currency'][0]);
}
}

View File

@@ -0,0 +1,244 @@
<?php
declare(strict_types=1);
namespace Tests\Unit\Service;
use App\Models\Organization;
use App\Models\Project;
use App\Models\ProjectMember;
use App\Models\TimeEntry;
use App\Models\User;
use App\Service\BillableRateService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class BillableRateServiceTest extends TestCase
{
use RefreshDatabase;
private BillableRateService $billableRateService;
public function setUp(): void
{
parent::setUp();
$this->billableRateService = app(BillableRateService::class);
}
public function test_billable_rate_is_null_if_time_entry_is_not_billable(): void
{
// Arrange
$organization = Organization::factory()->create([
'billable_rate' => 1001,
]);
$user = User::factory()->attachToOrganization($organization, [
'billable_rate' => 2002,
])->create();
$project = Project::factory()->forOrganization($organization)->create([
'billable_rate' => 3003,
]);
$projectMember = ProjectMember::factory()->forUser($user)->forProject($project)->create([
'billable_rate' => 4004,
]);
$timeEntry = TimeEntry::factory()->forProject($project)->forUser($user)->forOrganization($organization)->create([
'billable' => false,
]);
// Act
$billableRate = $this->billableRateService->getBillableRateForTimeEntry($timeEntry);
// Assert
$this->assertSame(null, $billableRate);
}
public function test_billable_rate_uses_project_member_rate_as_first_priority(): void
{
// Arrange
$organization = Organization::factory()->create([
'billable_rate' => 1001,
]);
$user = User::factory()->attachToOrganization($organization, [
'billable_rate' => 2002,
])->create();
$project = Project::factory()->forOrganization($organization)->create([
'billable_rate' => 3003,
]);
$projectMember = ProjectMember::factory()->forUser($user)->forProject($project)->create([
'billable_rate' => 4004,
]);
$timeEntry = TimeEntry::factory()->forProject($project)->forUser($user)->forOrganization($organization)->create([
'billable' => true,
]);
// Act
$billableRate = $this->billableRateService->getBillableRateForTimeEntry($timeEntry);
// Assert
$this->assertSame(4004, $billableRate);
}
public function test_billable_rate_uses_project_rate_as_second_priority_using_null_values_before(): void
{
// Arrange
$organization = Organization::factory()->create([
'billable_rate' => 1001,
]);
$user = User::factory()->attachToOrganization($organization, [
'billable_rate' => 2002,
])->create();
$project = Project::factory()->forOrganization($organization)->create([
'billable_rate' => 3003,
]);
$projectMember = ProjectMember::factory()->forUser($user)->forProject($project)->create([
'billable_rate' => null,
]);
$timeEntry = TimeEntry::factory()->forProject($project)->forUser($user)->forOrganization($organization)->create([
'billable' => true,
]);
// Act
$billableRate = $this->billableRateService->getBillableRateForTimeEntry($timeEntry);
// Assert
$this->assertSame(3003, $billableRate);
}
public function test_billable_rate_uses_project_rate_as_second_priority_using_non_existing_entities_before(): void
{
// Arrange
$organization = Organization::factory()->create([
'billable_rate' => 1001,
]);
$user = User::factory()->attachToOrganization($organization, [
'billable_rate' => 2002,
])->create();
$project = Project::factory()->forOrganization($organization)->create([
'billable_rate' => 3003,
]);
$timeEntry = TimeEntry::factory()->forProject($project)->forUser($user)->forOrganization($organization)->create([
'billable' => true,
]);
// Act
$billableRate = $this->billableRateService->getBillableRateForTimeEntry($timeEntry);
// Assert
$this->assertSame(3003, $billableRate);
}
public function test_billable_rate_uses_organization_member_rate_as_third_priority_using_null_values_before(): void
{
// Arrange
$organization = Organization::factory()->create([
'billable_rate' => 1001,
]);
$user = User::factory()->attachToOrganization($organization, [
'billable_rate' => 2002,
])->create();
$project = Project::factory()->forOrganization($organization)->create([
'billable_rate' => null,
]);
$projectMember = ProjectMember::factory()->forUser($user)->forProject($project)->create([
'billable_rate' => null,
]);
$timeEntry = TimeEntry::factory()->forProject($project)->forUser($user)->forOrganization($organization)->create([
'billable' => true,
]);
// Act
$billableRate = $this->billableRateService->getBillableRateForTimeEntry($timeEntry);
// Assert
$this->assertSame(2002, $billableRate);
}
public function test_billable_rate_uses_organization_member_rate_as_third_priority_using_non_existing_entities_before(): void
{
// Arrange
$organization = Organization::factory()->create([
'billable_rate' => 1001,
]);
$user = User::factory()->attachToOrganization($organization, [
'billable_rate' => 2002,
])->create();
$timeEntry = TimeEntry::factory()->forUser($user)->forOrganization($organization)->create([
'billable' => true,
]);
// Act
$billableRate = $this->billableRateService->getBillableRateForTimeEntry($timeEntry);
// Assert
$this->assertSame(2002, $billableRate);
}
public function test_billable_rate_uses_organization_rate_as_fourth_priority_using_null_values_before(): void
{
// Arrange
$organization = Organization::factory()->create([
'billable_rate' => 1001,
]);
$user = User::factory()->attachToOrganization($organization, [
'billable_rate' => null,
])->create();
$project = Project::factory()->forOrganization($organization)->create([
'billable_rate' => null,
]);
$projectMember = ProjectMember::factory()->forUser($user)->forProject($project)->create([
'billable_rate' => null,
]);
$timeEntry = TimeEntry::factory()->forProject($project)->forUser($user)->forOrganization($organization)->create([
'billable' => true,
]);
// Act
$billableRate = $this->billableRateService->getBillableRateForTimeEntry($timeEntry);
// Assert
$this->assertSame(1001, $billableRate);
}
public function test_billable_rate_uses_organization_rate_as_fourth_priority_using_non_existing_entities_before(): void
{
// Arrange
$organization = Organization::factory()->create([
'billable_rate' => 1001,
]);
$user = User::factory()->create();
$timeEntry = TimeEntry::factory()->forUser($user)->forOrganization($organization)->create([
'billable' => true,
]);
// Act
$billableRate = $this->billableRateService->getBillableRateForTimeEntry($timeEntry);
// Assert
$this->assertSame(1001, $billableRate);
}
public function test_billable_rate_is_null_if_billable_rate_on_all_levels_are_null(): void
{
// Arrange
$organization = Organization::factory()->create([
'billable_rate' => null,
]);
$user = User::factory()->attachToOrganization($organization, [
'billable_rate' => null,
])->create();
$project = Project::factory()->forOrganization($organization)->create([
'billable_rate' => null,
]);
$projectMember = ProjectMember::factory()->forUser($user)->forProject($project)->create([
'billable_rate' => null,
]);
$timeEntry = TimeEntry::factory()->forProject($project)->forUser($user)->forOrganization($organization)->create([
'billable' => true,
]);
// Act
$billableRate = $this->billableRateService->getBillableRateForTimeEntry($timeEntry);
// Assert
$this->assertSame(null, $billableRate);
}
}

View File

@@ -5,6 +5,7 @@ declare(strict_types=1);
namespace Tests\Unit\Service;
use App\Enums\Weekday;
use App\Models\Organization;
use App\Models\TimeEntry;
use App\Models\User;
use App\Service\DashboardService;
@@ -28,22 +29,24 @@ class DashboardServiceTest extends TestCase
{
// Arrange
$this->travelTo(Carbon::create(2024, 1, 1, 12, 0, 0, 'Europe/Vienna'));
$organization = Organization::factory()->create();
$user = User::factory()->create([
'timezone' => 'Europe/Vienna',
]);
$timeEntry1 = TimeEntry::factory()->forUser($user)->create([
$user->organizations()->attach($organization);
$timeEntry1 = TimeEntry::factory()->forUser($user)->forOrganization($organization)->create([
// Note: The start time shifts in timezone Europe/Vienna to the next day
'start' => Carbon::create(2023, 12, 30, 23, 0, 0, 'UTC'),
'end' => Carbon::create(2023, 12, 30, 23, 0, 40, 'UTC'),
]);
$timeEntry2 = TimeEntry::factory()->forUser($user)->create([
$timeEntry2 = TimeEntry::factory()->forUser($user)->forOrganization($organization)->create([
// Note: The start time NOT shifts in timezone Europe/Vienna to the next day
'start' => Carbon::create(2023, 12, 30, 22, 59, 59, 'UTC'),
'end' => Carbon::create(2023, 12, 30, 23, 0, 39, 'UTC'),
]);
// Act
$result = $this->dashboardService->getDailyTrackedHours($user, 5);
$result = $this->dashboardService->getDailyTrackedHours($user, $organization, 5);
// Assert
$this->assertSame([
@@ -75,25 +78,27 @@ class DashboardServiceTest extends TestCase
// Arrange
// Note: Is a Monday
$this->travelTo(Carbon::create(2024, 1, 1, 12, 0, 0, 'Europe/Vienna'));
$organization = Organization::factory()->create();
$user = User::factory()->create([
'timezone' => 'Europe/Vienna',
'week_start' => Weekday::Sunday,
]);
$user->organizations()->attach($organization);
// Note: This is a Sunday
$timeEntry1 = TimeEntry::factory()->forUser($user)->create([
$timeEntry1 = TimeEntry::factory()->forUser($user)->forOrganization($organization)->create([
// Note: The start time shifts in timezone Europe/Vienna to the next day
'start' => Carbon::create(2023, 12, 30, 23, 0, 0, 'UTC'),
'end' => Carbon::create(2023, 12, 30, 23, 0, 40, 'UTC'),
]);
// Note: This is a Saturday
$timeEntry2 = TimeEntry::factory()->forUser($user)->create([
$timeEntry2 = TimeEntry::factory()->forUser($user)->forOrganization($organization)->create([
// Note: The start time NOT shifts in timezone Europe/Vienna to the next day
'start' => Carbon::create(2023, 12, 30, 22, 59, 59, 'UTC'),
'end' => Carbon::create(2023, 12, 30, 23, 0, 39, 'UTC'),
]);
// Act
$result = $this->dashboardService->getWeeklyHistory($user);
$result = $this->dashboardService->getWeeklyHistory($user, $organization);
// Assert
$this->assertSame([
@@ -127,4 +132,123 @@ class DashboardServiceTest extends TestCase
],
], $result);
}
public function test_total_weekly_time_returns_correct_value(): void
{
// Arrange
// Note: Is a Monday
$this->travelTo(Carbon::create(2024, 1, 1, 12, 0, 0, 'Europe/Vienna'));
$organization = Organization::factory()->create();
$user = User::factory()->create([
'timezone' => 'Europe/Vienna',
'week_start' => Weekday::Sunday,
]);
$user->organizations()->attach($organization);
// Note: This is a Sunday
$timeEntry1 = TimeEntry::factory()->forUser($user)->forOrganization($organization)->create([
// Note: The start time shifts in timezone Europe/Vienna to the next day
'start' => Carbon::create(2023, 12, 30, 23, 0, 0, 'UTC'),
'end' => Carbon::create(2023, 12, 30, 23, 0, 40, 'UTC'),
]);
// Note: This is a Saturday
$timeEntry2 = TimeEntry::factory()->forUser($user)->forOrganization($organization)->create([
// Note: The start time NOT shifts in timezone Europe/Vienna to the next day
'start' => Carbon::create(2023, 12, 30, 22, 59, 59, 'UTC'),
'end' => Carbon::create(2023, 12, 30, 23, 0, 39, 'UTC'),
]);
// Act
$result = $this->dashboardService->totalWeeklyTime($user, $organization);
// Assert
$this->assertSame(40, $result);
}
public function test_total_weekly_billable_time_returns_correct_value(): void
{
// Arrange
// Note: Is a Monday
$this->travelTo(Carbon::create(2024, 1, 1, 12, 0, 0, 'Europe/Vienna'));
$organization = Organization::factory()->create();
$user = User::factory()->create([
'timezone' => 'Europe/Vienna',
'week_start' => Weekday::Sunday,
]);
$user->organizations()->attach($organization);
// Note: This is a Sunday
$timeEntry1 = TimeEntry::factory()->forUser($user)->forOrganization($organization)->create([
// Note: The start time shifts in timezone Europe/Vienna to the next day
'billable' => true,
'start' => Carbon::create(2023, 12, 30, 23, 0, 0, 'UTC'),
'end' => Carbon::create(2023, 12, 30, 23, 0, 40, 'UTC'),
]);
// Note: This is a Sunday (non-billable)
$timeEntry1 = TimeEntry::factory()->forUser($user)->forOrganization($organization)->create([
// Note: The start time shifts in timezone Europe/Vienna to the next day
'billable' => false,
'start' => Carbon::create(2023, 12, 30, 23, 0, 40, 'UTC'),
'end' => Carbon::create(2023, 12, 30, 23, 0, 59, 'UTC'),
]);
// Note: This is a Saturday
$timeEntry2 = TimeEntry::factory()->forUser($user)->forOrganization($organization)->create([
// Note: The start time NOT shifts in timezone Europe/Vienna to the next day
'billable' => true,
'start' => Carbon::create(2023, 12, 30, 22, 59, 59, 'UTC'),
'end' => Carbon::create(2023, 12, 30, 23, 0, 39, 'UTC'),
]);
// Act
$result = $this->dashboardService->totalWeeklyBillableTime($user, $organization);
// Assert
$this->assertSame(40, $result);
}
public function test_total_weekly_billable_amount_returns_correct_value(): void
{
// Arrange
// Note: Is a Monday
$this->travelTo(Carbon::create(2024, 1, 1, 12, 0, 0, 'Europe/Vienna'));
$currency = 'USD';
$organization = Organization::factory()->create([
'currency' => $currency,
]);
$user = User::factory()->create([
'timezone' => 'Europe/Vienna',
'week_start' => Weekday::Sunday,
]);
$user->organizations()->attach($organization);
// Note: This is a Sunday
$timeEntry1 = TimeEntry::factory()->forUser($user)->forOrganization($organization)->create([
// Note: The start time shifts in timezone Europe/Vienna to the next day
'billable' => true,
'billable_rate' => 50 * 100,
'start' => Carbon::create(2023, 12, 30, 23, 0, 0, 'UTC'),
'end' => Carbon::create(2023, 12, 31, 0, 0, 0, 'UTC'),
]);
// Note: This is a Sunday (non-billable)
$timeEntry2 = TimeEntry::factory()->forUser($user)->forOrganization($organization)->create([
// Note: The start time shifts in timezone Europe/Vienna to the next day
'billable' => false,
'start' => Carbon::create(2023, 12, 30, 23, 0, 40, 'UTC'),
'end' => Carbon::create(2023, 12, 30, 23, 0, 59, 'UTC'),
]);
// Note: This is a Saturday
$timeEntry3 = TimeEntry::factory()->forUser($user)->forOrganization($organization)->create([
// Note: The start time NOT shifts in timezone Europe/Vienna to the next day
'billable' => true,
'billable_rate' => 100 * 100,
'start' => Carbon::create(2023, 12, 30, 22, 59, 59, 'UTC'),
'end' => Carbon::create(2023, 12, 30, 23, 0, 39, 'UTC'),
]);
// Act
$result = $this->dashboardService->totalWeeklyBillableAmount($user, $organization);
// Assert
$this->assertSame([
'value' => 5000,
'currency' => $currency,
], $result);
}
}