mirror of
https://github.com/solidtime-io/solidtime.git
synced 2026-08-08 00:02:15 +01:00
Added filled gaps to time entry aggregation; Moved aggregation to service
This commit is contained in:
43
tests/TestCaseWithDatabase.php
Normal file
43
tests/TestCaseWithDatabase.php
Normal file
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests;
|
||||
|
||||
use App\Models\Member;
|
||||
use App\Models\Organization;
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Str;
|
||||
use Laravel\Jetstream\Jetstream;
|
||||
|
||||
abstract class TestCaseWithDatabase extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
/**
|
||||
* @param array<string> $permissions
|
||||
* @return object{user: User, organization: Organization, member: Member}
|
||||
*/
|
||||
protected function createUserWithPermission(array $permissions = [], bool $isOwner = false): object
|
||||
{
|
||||
$roleName = 'custom-test-'.Str::uuid();
|
||||
Jetstream::role($roleName, 'Custom Test', $permissions)
|
||||
->description('Role custom for testing');
|
||||
$user = User::factory()->create();
|
||||
if ($isOwner) {
|
||||
$organization = Organization::factory()->withOwner($user)->create();
|
||||
} else {
|
||||
$organization = Organization::factory()->create();
|
||||
}
|
||||
$member = Member::factory()->forUser($user)->forOrganization($organization)->create([
|
||||
'role' => $roleName,
|
||||
]);
|
||||
|
||||
return (object) [
|
||||
'user' => $user,
|
||||
'organization' => $organization,
|
||||
'member' => $member,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -4,41 +4,8 @@ declare(strict_types=1);
|
||||
|
||||
namespace Tests\Unit\Endpoint\Api\V1;
|
||||
|
||||
use App\Models\Member;
|
||||
use App\Models\Organization;
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Str;
|
||||
use Laravel\Jetstream\Jetstream;
|
||||
use Tests\TestCase;
|
||||
use Tests\TestCaseWithDatabase;
|
||||
|
||||
class ApiEndpointTestAbstract extends TestCase
|
||||
class ApiEndpointTestAbstract extends TestCaseWithDatabase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
/**
|
||||
* @param array<string> $permissions
|
||||
* @return object{user: User, organization: Organization, member: Member}
|
||||
*/
|
||||
protected function createUserWithPermission(array $permissions = [], bool $isOwner = false): object
|
||||
{
|
||||
$roleName = 'custom-test-'.Str::uuid();
|
||||
Jetstream::role($roleName, 'Custom Test', $permissions)
|
||||
->description('Role custom for testing');
|
||||
$user = User::factory()->create();
|
||||
if ($isOwner) {
|
||||
$organization = Organization::factory()->withOwner($user)->create();
|
||||
} else {
|
||||
$organization = Organization::factory()->create();
|
||||
}
|
||||
$member = Member::factory()->forUser($user)->forOrganization($organization)->create([
|
||||
'role' => $roleName,
|
||||
]);
|
||||
|
||||
return (object) [
|
||||
'user' => $user,
|
||||
'organization' => $organization,
|
||||
'member' => $member,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,6 +32,7 @@ class InvitationEndpointTest extends ApiEndpointTestAbstract
|
||||
$data = $this->createUserWithPermission([
|
||||
'invitations:view',
|
||||
]);
|
||||
$invitation1 = OrganizationInvitation::factory()->forOrganization($data->organization)->create();
|
||||
Passport::actingAs($data->user);
|
||||
|
||||
// Act
|
||||
@@ -39,6 +40,15 @@ class InvitationEndpointTest extends ApiEndpointTestAbstract
|
||||
|
||||
// Assert
|
||||
$response->assertStatus(200);
|
||||
$response->assertJson([
|
||||
'data' => [
|
||||
[
|
||||
'id' => $invitation1->getKey(),
|
||||
'email' => $invitation1->email,
|
||||
'role' => $invitation1->role,
|
||||
],
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_store_fails_if_user_has_no_permission_to_create_invitations(): void
|
||||
|
||||
@@ -7,6 +7,7 @@ namespace Tests\Unit\Endpoint\Api\V1;
|
||||
use App\Models\Client;
|
||||
use App\Models\Organization;
|
||||
use App\Models\Project;
|
||||
use App\Models\ProjectMember;
|
||||
use App\Models\Task;
|
||||
use App\Models\TimeEntry;
|
||||
use Laravel\Passport\Passport;
|
||||
@@ -377,13 +378,14 @@ class ProjectEndpointTest extends ApiEndpointTestAbstract
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_destroy_endpoint_deletes_project(): void
|
||||
public function test_destroy_endpoint_deletes_project_with_project_members(): void
|
||||
{
|
||||
// Arrange
|
||||
$data = $this->createUserWithPermission([
|
||||
'projects:delete',
|
||||
]);
|
||||
$project = Project::factory()->forOrganization($data->organization)->create();
|
||||
$projectMember = ProjectMember::factory()->forMember($data->member)->forProject($project)->create();
|
||||
Passport::actingAs($data->user);
|
||||
|
||||
// Act
|
||||
@@ -395,5 +397,8 @@ class ProjectEndpointTest extends ApiEndpointTestAbstract
|
||||
$this->assertDatabaseMissing(Project::class, [
|
||||
'id' => $project->getKey(),
|
||||
]);
|
||||
$this->assertDatabaseMissing(ProjectMember::class, [
|
||||
'id' => $projectMember->getKey(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -327,7 +327,7 @@ class ProjectMemberEndpointTest extends ApiEndpointTestAbstract
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_destroy_endpoint_fails_if_user_has_no_permission_to_delete_projects(): void
|
||||
public function test_destroy_endpoint_fails_if_user_has_no_permission_to_delete_project_members(): void
|
||||
{
|
||||
// Arrange
|
||||
$data = $this->createUserWithPermission([
|
||||
@@ -346,14 +346,14 @@ class ProjectMemberEndpointTest extends ApiEndpointTestAbstract
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_destroy_endpoint_deletes_project(): void
|
||||
public function test_destroy_endpoint_deletes_project_member(): void
|
||||
{
|
||||
// Arrange
|
||||
$data = $this->createUserWithPermission([
|
||||
'project-members:delete',
|
||||
]);
|
||||
$project = Project::factory()->forOrganization($data->organization)->create();
|
||||
$projectMember = ProjectMember::factory()->forProject($project)->create();
|
||||
$projectMember = ProjectMember::factory()->forProject($project)->forMember($data->member)->create();
|
||||
Passport::actingAs($data->user);
|
||||
|
||||
// Act
|
||||
|
||||
@@ -8,10 +8,11 @@ use App\Enums\Role;
|
||||
use App\Exceptions\Api\TimeEntryCanNotBeRestartedApiException;
|
||||
use App\Models\Member;
|
||||
use App\Models\Project;
|
||||
use App\Models\Tag;
|
||||
use App\Models\Task;
|
||||
use App\Models\TimeEntry;
|
||||
use App\Models\User;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\Testing\Fluent\AssertableJson;
|
||||
@@ -293,7 +294,7 @@ class TimeEntryEndpointTest extends ApiEndpointTestAbstract
|
||||
'time-entries:view:own',
|
||||
]);
|
||||
$timeEntriesDay1 = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)
|
||||
->startBetween(Carbon::now()->subDay()->startOfDay(), Carbon::now()->subDay()->endOfDay())
|
||||
->startBetween(Carbon::now()->subDay()->startOfDay(), Carbon::now()->subDay()->endOfDay(), true)
|
||||
->createMany(7);
|
||||
Passport::actingAs($data->user);
|
||||
|
||||
@@ -306,11 +307,11 @@ class TimeEntryEndpointTest extends ApiEndpointTestAbstract
|
||||
]));
|
||||
|
||||
// Assert
|
||||
$response->assertStatus(200);
|
||||
$response->assertJsonCount(7, 'data');
|
||||
Log::assertLogged(fn (LogEntry $log) => $log->level === 'warning'
|
||||
&& $log->message === 'User has has more than 5 time entries on one date'
|
||||
);
|
||||
$response->assertStatus(200);
|
||||
$response->assertJsonCount(7, 'data');
|
||||
}
|
||||
|
||||
public function test_index_endpoint_before_filter_returns_time_entries_before_date(): void
|
||||
@@ -331,6 +332,7 @@ class TimeEntryEndpointTest extends ApiEndpointTestAbstract
|
||||
Carbon::now()->timezone($data->user->timezone)->subDays(2)->endOfDay()->utc()
|
||||
)
|
||||
->createMany(3);
|
||||
$timeEntriesBeforeSorted = $timeEntriesBefore->sortByDesc('start')->values();
|
||||
$timeEntriesDirectlyBeforeLimit = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)
|
||||
->create([
|
||||
'start' => Carbon::now()->timezone($data->user->timezone)->subDays(2)->endOfDay()->utc(),
|
||||
@@ -350,9 +352,9 @@ class TimeEntryEndpointTest extends ApiEndpointTestAbstract
|
||||
->has('data')
|
||||
->count('data', 4)
|
||||
->where('data.0.id', $timeEntriesDirectlyBeforeLimit->getKey())
|
||||
->where('data.1.id', $timeEntriesBefore->sortByDesc('start')->get(0)->getKey())
|
||||
->where('data.2.id', $timeEntriesBefore->sortByDesc('start')->get(1)->getKey())
|
||||
->where('data.3.id', $timeEntriesBefore->sortByDesc('start')->get(2)->getKey())
|
||||
->where('data.1.id', $timeEntriesBeforeSorted->get(0)->getKey())
|
||||
->where('data.2.id', $timeEntriesBeforeSorted->get(1)->getKey())
|
||||
->where('data.3.id', $timeEntriesBeforeSorted->get(2)->getKey())
|
||||
);
|
||||
}
|
||||
|
||||
@@ -365,6 +367,7 @@ class TimeEntryEndpointTest extends ApiEndpointTestAbstract
|
||||
$timeEntriesAfter = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)
|
||||
->startBetween(Carbon::now($data->user->timezone)->startOfDay()->utc(), Carbon::now($data->user->timezone)->utc())
|
||||
->createMany(3);
|
||||
$timeEntriesAfterSorted = $timeEntriesAfter->sortByDesc('start')->values();
|
||||
$timeEntriesBefore = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)
|
||||
->startBetween(Carbon::now($data->user->timezone)->subDay()->startOfDay()->utc(), Carbon::now($data->user->timezone)->subDay()->endOfDay()->utc())
|
||||
->createMany(3);
|
||||
@@ -386,13 +389,61 @@ class TimeEntryEndpointTest extends ApiEndpointTestAbstract
|
||||
$response->assertJson(fn (AssertableJson $json) => $json
|
||||
->has('data')
|
||||
->count('data', 4)
|
||||
->where('data.0.id', $timeEntriesAfter->sortByDesc('start')->get(0)->getKey())
|
||||
->where('data.1.id', $timeEntriesAfter->sortByDesc('start')->get(1)->getKey())
|
||||
->where('data.2.id', $timeEntriesAfter->sortByDesc('start')->get(2)->getKey())
|
||||
->where('data.0.id', $timeEntriesAfterSorted->get(0)->getKey())
|
||||
->where('data.1.id', $timeEntriesAfterSorted->get(1)->getKey())
|
||||
->where('data.2.id', $timeEntriesAfterSorted->get(2)->getKey())
|
||||
->where('data.3.id', $timeEntriesDirectlyAfterLimit->getKey())
|
||||
);
|
||||
}
|
||||
|
||||
public function test_index_endpoint_with_all_available_filters(): void
|
||||
{
|
||||
// Arrange
|
||||
$data = $this->createUserWithPermission([
|
||||
'time-entries:view:all',
|
||||
'time-entries:view:own',
|
||||
]);
|
||||
$project = Project::factory()->forOrganization($data->organization)->create();
|
||||
$task = Task::factory()->forOrganization($data->organization)->forProject($project)->create();
|
||||
$tag = Tag::factory()->forOrganization($data->organization)->create();
|
||||
$timeEntry1 = TimeEntry::factory()
|
||||
->forOrganization($data->organization)
|
||||
->forProject($project)
|
||||
->forTask($task)
|
||||
->forMember($data->member)
|
||||
->billable()
|
||||
->active()
|
||||
->create([
|
||||
'start' => Carbon::now()->subHour(),
|
||||
'tags' => [$tag->getKey()],
|
||||
]);
|
||||
Passport::actingAs($data->user);
|
||||
|
||||
// Act
|
||||
$response = $this->getJson(route('api.v1.time-entries.index', [
|
||||
$data->organization->getKey(),
|
||||
'member_id' => $data->member->getKey(),
|
||||
'member_ids' => [$data->member->getKey()],
|
||||
'project_ids' => [$project->getKey()],
|
||||
'task_ids' => [$task->getKey()],
|
||||
'tag_ids' => [$tag->getKey()],
|
||||
'before' => Carbon::now()->toIso8601ZuluString(),
|
||||
'after' => Carbon::now()->subDay()->toIso8601ZuluString(),
|
||||
'active' => 'true',
|
||||
'only_full_dates' => 'true',
|
||||
'limit' => 1,
|
||||
]));
|
||||
|
||||
// Assert
|
||||
$response->assertValid();
|
||||
$response->assertStatus(200);
|
||||
$response->assertJson(fn (AssertableJson $json) => $json
|
||||
->has('data')
|
||||
->count('data', 1)
|
||||
->where('data.0.id', $timeEntry1->getKey())
|
||||
);
|
||||
}
|
||||
|
||||
public function test_aggregate_endpoint_fails_if_user_has_no_permission_to_view_time_entries(): void
|
||||
{
|
||||
// Arrange
|
||||
@@ -413,12 +464,13 @@ class TimeEntryEndpointTest extends ApiEndpointTestAbstract
|
||||
$data = $this->createUserWithPermission([
|
||||
'time-entries:view:all',
|
||||
]);
|
||||
$timeEntries = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)->createMany(3);
|
||||
$project = Project::factory()->forOrganization($data->organization)->create();
|
||||
$timeEntries = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)->forProject($project)->createMany(3);
|
||||
$timeEntries = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)->state([
|
||||
'start' => $timeEntries->get(0)->start,
|
||||
])->createMany(3);
|
||||
$day1 = Carbon::now()->timezone($data->user->timezone)->subDays(1)->utc();
|
||||
$day2 = Carbon::now()->timezone($data->user->timezone)->subDays(3)->utc();
|
||||
$timeEntry1NoProject = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)->startWithDuration($day1, 10)->create();
|
||||
$timeEntry2NoProject = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)->startWithDuration($day2, 10)->create();
|
||||
$timeEntry1WithProject = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)->forProject($project)->startWithDuration($day1, 10)->create();
|
||||
$timeEntry2WithProject = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)->forProject($project)->startWithDuration($day2, 10)->create();
|
||||
Passport::actingAs($data->user);
|
||||
|
||||
// Act
|
||||
@@ -430,6 +482,154 @@ class TimeEntryEndpointTest extends ApiEndpointTestAbstract
|
||||
|
||||
// Assert
|
||||
$response->assertSuccessful();
|
||||
$response->assertExactJson([
|
||||
'data' => [
|
||||
'seconds' => 40,
|
||||
'cost' => 0,
|
||||
'grouped_data' => [
|
||||
0 => [
|
||||
'key' => $day2->format('Y-m-d'),
|
||||
'seconds' => 20,
|
||||
'cost' => 0,
|
||||
'grouped_type' => 'project',
|
||||
'grouped_data' => [
|
||||
0 => [
|
||||
'key' => $project->getKey(),
|
||||
'seconds' => 10,
|
||||
'cost' => 0,
|
||||
'grouped_type' => null,
|
||||
'grouped_data' => null,
|
||||
],
|
||||
1 => [
|
||||
'key' => null,
|
||||
'seconds' => 10,
|
||||
'cost' => 0,
|
||||
'grouped_type' => null,
|
||||
'grouped_data' => null,
|
||||
],
|
||||
],
|
||||
],
|
||||
1 => [
|
||||
'key' => $day1->format('Y-m-d'),
|
||||
'seconds' => 20,
|
||||
'cost' => 0,
|
||||
'grouped_type' => 'project',
|
||||
'grouped_data' => [
|
||||
0 => [
|
||||
'key' => $project->getKey(),
|
||||
'seconds' => 10,
|
||||
'cost' => 0,
|
||||
'grouped_type' => null,
|
||||
'grouped_data' => null,
|
||||
],
|
||||
1 => [
|
||||
'key' => null,
|
||||
'seconds' => 10,
|
||||
'cost' => 0,
|
||||
'grouped_type' => null,
|
||||
'grouped_data' => null,
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
'grouped_type' => 'day',
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_aggregate_endpoint_groups_by_two_groups_with_fill_gaps_argument(): void
|
||||
{
|
||||
// Arrange
|
||||
$data = $this->createUserWithPermission([
|
||||
'time-entries:view:all',
|
||||
]);
|
||||
$project = Project::factory()->forOrganization($data->organization)->create();
|
||||
$day1 = Carbon::now()->timezone($data->user->timezone)->subDays(1)->utc();
|
||||
$day2 = Carbon::now()->timezone($data->user->timezone)->subDays(3)->utc();
|
||||
$timeEntry1NoProject = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)->startWithDuration($day1, 10)->create();
|
||||
$timeEntry2NoProject = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)->startWithDuration($day2, 10)->create();
|
||||
$timeEntry1WithProject = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)->forProject($project)->startWithDuration($day1, 10)->create();
|
||||
$timeEntry2WithProject = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)->forProject($project)->startWithDuration($day2, 10)->create();
|
||||
Passport::actingAs($data->user);
|
||||
|
||||
// Act
|
||||
$response = $this->getJson(route('api.v1.time-entries.aggregate', [
|
||||
$data->organization->getKey(),
|
||||
'group' => 'project',
|
||||
'sub_group' => 'day',
|
||||
'fill_gaps_in_time_groups' => 'true',
|
||||
'after' => $day2->copy()->subSecond()->toIso8601ZuluString(),
|
||||
'before' => $day1->copy()->addSecond()->toIso8601ZuluString(),
|
||||
]));
|
||||
|
||||
// Assert
|
||||
$response->assertSuccessful();
|
||||
$response->assertExactJson(['data' => [
|
||||
'seconds' => 40,
|
||||
'cost' => 0,
|
||||
'grouped_type' => 'project',
|
||||
'grouped_data' => [
|
||||
0 => [
|
||||
'key' => $project->getKey(),
|
||||
'seconds' => 20,
|
||||
'cost' => 0,
|
||||
'grouped_type' => 'day',
|
||||
'grouped_data' => [
|
||||
0 => [
|
||||
'key' => $day2->format('Y-m-d'),
|
||||
'seconds' => 10,
|
||||
'cost' => 0,
|
||||
'grouped_type' => null,
|
||||
'grouped_data' => null,
|
||||
],
|
||||
1 => [
|
||||
'key' => $day2->copy()->addDay()->format('Y-m-d'),
|
||||
'seconds' => 0,
|
||||
'cost' => 0,
|
||||
'grouped_type' => null,
|
||||
'grouped_data' => null,
|
||||
],
|
||||
2 => [
|
||||
'key' => $day1->format('Y-m-d'),
|
||||
'seconds' => 10,
|
||||
'cost' => 0,
|
||||
'grouped_type' => null,
|
||||
'grouped_data' => null,
|
||||
],
|
||||
],
|
||||
],
|
||||
1 => [
|
||||
'key' => null,
|
||||
'seconds' => 20,
|
||||
'cost' => 0,
|
||||
'grouped_type' => 'day',
|
||||
'grouped_data' => [
|
||||
0 => [
|
||||
'key' => $day2->format('Y-m-d'),
|
||||
'seconds' => 10,
|
||||
'cost' => 0,
|
||||
'grouped_type' => null,
|
||||
'grouped_data' => null,
|
||||
],
|
||||
1 => [
|
||||
'key' => $day2->copy()->addDay()->format('Y-m-d'),
|
||||
'seconds' => 0,
|
||||
'cost' => 0,
|
||||
'grouped_type' => null,
|
||||
'grouped_data' => null,
|
||||
],
|
||||
2 => [
|
||||
'key' => $day1->format('Y-m-d'),
|
||||
'seconds' => 10,
|
||||
'cost' => 0,
|
||||
'grouped_type' => null,
|
||||
'grouped_data' => null,
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_aggregate_endpoint_groups_by_one_group(): void
|
||||
@@ -438,12 +638,12 @@ class TimeEntryEndpointTest extends ApiEndpointTestAbstract
|
||||
$data = $this->createUserWithPermission([
|
||||
'time-entries:view:all',
|
||||
]);
|
||||
$timeEntries = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)->createMany(3);
|
||||
$project = Project::factory()->forOrganization($data->organization)->create();
|
||||
$timeEntries = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)->forProject($project)->createMany(3);
|
||||
$timeEntries = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)->state([
|
||||
'start' => $timeEntries->get(0)->start,
|
||||
])->createMany(3);
|
||||
$week1 = Carbon::now()->timezone($data->user->timezone)->startOfWeek($data->user->week_start->carbonWeekDay())->utc();
|
||||
$week2 = Carbon::now()->timezone($data->user->timezone)->subWeeks(2)->startOfWeek($data->user->week_start->carbonWeekDay())->utc();
|
||||
$timeEntry1Week1 = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)->startWithDuration($week1->copy()->addDays(1), 10)->create();
|
||||
$timeEntry2Week1 = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)->startWithDuration($week1->copy()->addDays(2), 10)->create();
|
||||
$timeEntry1Week2 = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)->startWithDuration($week2->copy()->addDays(3), 10)->create();
|
||||
$timeEntry2Week2 = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)->startWithDuration($week2->copy()->addDays(4), 10)->create();
|
||||
Passport::actingAs($data->user);
|
||||
|
||||
// Act
|
||||
@@ -454,6 +654,88 @@ class TimeEntryEndpointTest extends ApiEndpointTestAbstract
|
||||
|
||||
// Assert
|
||||
$response->assertSuccessful();
|
||||
$response->assertExactJson([
|
||||
'data' => [
|
||||
'seconds' => 40,
|
||||
'cost' => 0,
|
||||
'grouped_type' => 'week',
|
||||
'grouped_data' => [
|
||||
0 => [
|
||||
'key' => $week2->format('Y-m-d H:i:s'),
|
||||
'seconds' => 20,
|
||||
'cost' => 0,
|
||||
'grouped_type' => null,
|
||||
'grouped_data' => null,
|
||||
],
|
||||
1 => [
|
||||
'key' => $week1->format('Y-m-d H:i:s'),
|
||||
'seconds' => 20,
|
||||
'cost' => 0,
|
||||
'grouped_type' => null,
|
||||
'grouped_data' => null,
|
||||
],
|
||||
],
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_aggregate_endpoint_groups_by_one_group_with_fill_gaps_argument(): void
|
||||
{
|
||||
// Arrange
|
||||
$data = $this->createUserWithPermission([
|
||||
'time-entries:view:all',
|
||||
]);
|
||||
$laterWeekEnd = Carbon::now()->timezone($data->user->timezone)->endOfWeek($data->user->week_start->toEndOfWeek()->carbonWeekDay())->utc();
|
||||
$earlierWeekStart = Carbon::now()->timezone($data->user->timezone)->subWeeks(2)->startOfWeek($data->user->week_start->carbonWeekDay())->utc();
|
||||
|
||||
$timeEntry1 = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)->startWithDuration($laterWeekEnd->copy()->subDays(1), 10)->create();
|
||||
$timeEntry2 = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)->startWithDuration($laterWeekEnd->copy()->subDays(2), 10)->create();
|
||||
$timeEntry3 = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)->startWithDuration($earlierWeekStart->copy()->addDays(1), 10)->create();
|
||||
$timeEntry4 = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)->startWithDuration($earlierWeekStart->copy()->addDays(2), 10)->create();
|
||||
Passport::actingAs($data->user);
|
||||
|
||||
// Act
|
||||
$response = $this->getJson(route('api.v1.time-entries.aggregate', [
|
||||
$data->organization->getKey(),
|
||||
'group' => 'week',
|
||||
'fill_gaps_in_time_groups' => 'true',
|
||||
'after' => $earlierWeekStart->toIso8601ZuluString(),
|
||||
'before' => $laterWeekEnd->toIso8601ZuluString(),
|
||||
]));
|
||||
|
||||
// Assert
|
||||
$response->assertSuccessful();
|
||||
$response->assertExactJson([
|
||||
'data' => [
|
||||
'seconds' => 40,
|
||||
'cost' => 0,
|
||||
'grouped_type' => 'week',
|
||||
'grouped_data' => [
|
||||
0 => [
|
||||
'key' => '2024-05-05 22:00:00',
|
||||
'seconds' => 20,
|
||||
'cost' => 0,
|
||||
'grouped_type' => null,
|
||||
'grouped_data' => null,
|
||||
],
|
||||
1 => [
|
||||
'key' => '2024-05-12 22:00:00',
|
||||
'seconds' => 0,
|
||||
'cost' => 0,
|
||||
'grouped_type' => null,
|
||||
'grouped_data' => null,
|
||||
],
|
||||
2 => [
|
||||
'key' => '2024-05-19 22:00:00',
|
||||
'seconds' => 20,
|
||||
'cost' => 0,
|
||||
'grouped_type' => null,
|
||||
'grouped_data' => null,
|
||||
],
|
||||
],
|
||||
],
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
public function test_aggregate_endpoint_with_no_group(): void
|
||||
|
||||
@@ -5,7 +5,10 @@ declare(strict_types=1);
|
||||
namespace Tests\Unit\Endpoint\Api\V1;
|
||||
|
||||
use App\Models\TimeEntry;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Laravel\Passport\Passport;
|
||||
use TiMacDonald\Log\LogEntry;
|
||||
|
||||
class UserTimeEntryEndpointTest extends ApiEndpointTestAbstract
|
||||
{
|
||||
@@ -36,6 +39,28 @@ class UserTimeEntryEndpointTest extends ApiEndpointTestAbstract
|
||||
|
||||
// Assert
|
||||
$response->assertSuccessful();
|
||||
$response->assertJsonPath('data.id', $activeTimeEntry->getKey());
|
||||
}
|
||||
|
||||
public function test_my_active_endpoint_logs_a_warning_if_user_has_multiple_active_time_entries_and_return_the_latest_one(): void
|
||||
{
|
||||
// Arrange
|
||||
$data = $this->createUserWithPermission([
|
||||
]);
|
||||
$activeTimeEntry1 = TimeEntry::factory()->forMember($data->member)->active()->start(Carbon::now()->subDay())->create();
|
||||
$activeTimeEntry2 = TimeEntry::factory()->forMember($data->member)->active()->start(Carbon::now())->create();
|
||||
Passport::actingAs($data->user);
|
||||
|
||||
// Act
|
||||
$response = $this->getJson(route('api.v1.users.time-entries.my-active'));
|
||||
|
||||
// Assert
|
||||
Log::assertLogged(fn (LogEntry $log) => $log->level === 'warning'
|
||||
&& $log->message === 'User has more than one active time entry.'
|
||||
&& $log->context === ['user' => $data->user->getKey()]
|
||||
);
|
||||
$response->assertSuccessful();
|
||||
$response->assertJsonPath('data.id', $activeTimeEntry2->getKey());
|
||||
}
|
||||
|
||||
public function test_my_active_endpoint_returns_not_found_if_user_has_no_active_time_entry(): void
|
||||
|
||||
@@ -98,7 +98,7 @@ class ProjectModelTest extends ModelTestAbstract
|
||||
ProjectMember::factory()->forProject($projectPrivateButMember)->forMember($member)->create();
|
||||
|
||||
// Act
|
||||
$projectsVisible = Project::query()->visibleByUser($member->user)->get();
|
||||
$projectsVisible = Project::query()->visibleByEmployee($member->user)->get();
|
||||
$allProjects = Project::query()->get();
|
||||
|
||||
// Assert
|
||||
|
||||
@@ -72,7 +72,7 @@ class TaskModelTest extends ModelTestAbstract
|
||||
$taskPrivateButMember = Task::factory()->forProject($projectPrivateButMember)->create();
|
||||
|
||||
// Act
|
||||
$tasksVisible = Task::query()->visibleByUser($member->user)->get();
|
||||
$tasksVisible = Task::query()->visibleByEmployee($member->user)->get();
|
||||
$allTasks = Task::query()->get();
|
||||
|
||||
// Assert
|
||||
|
||||
@@ -10,7 +10,7 @@ use App\Models\Tag;
|
||||
use App\Models\Task;
|
||||
use App\Models\TimeEntry;
|
||||
use App\Models\User;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Support\Carbon;
|
||||
|
||||
class TimeEntryModelTest extends ModelTestAbstract
|
||||
{
|
||||
|
||||
@@ -13,9 +13,9 @@ use App\Models\Task;
|
||||
use App\Models\TimeEntry;
|
||||
use App\Models\User;
|
||||
use App\Service\DashboardService;
|
||||
use Carbon\Carbon;
|
||||
use Carbon\CarbonImmutable;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Tests\TestCase;
|
||||
|
||||
class DashboardServiceTest extends TestCase
|
||||
|
||||
142
tests/Unit/Service/TimeEntryAggregationServiceTest.php
Normal file
142
tests/Unit/Service/TimeEntryAggregationServiceTest.php
Normal file
@@ -0,0 +1,142 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\Unit\Service;
|
||||
|
||||
use App\Enums\TimeEntryAggregationType;
|
||||
use App\Enums\Weekday;
|
||||
use App\Models\TimeEntry;
|
||||
use App\Service\TimeEntryAggregationService;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Tests\TestCaseWithDatabase;
|
||||
|
||||
class TimeEntryAggregationServiceTest extends TestCaseWithDatabase
|
||||
{
|
||||
private TimeEntryAggregationService $service;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
$this->service = app(TimeEntryAggregationService::class);
|
||||
}
|
||||
|
||||
public function test_aggregate_time_entries_by_day_and_project_returns_empty_array_if_no_time_entries_given(): void
|
||||
{
|
||||
// Arrange
|
||||
$query = TimeEntry::query();
|
||||
|
||||
// Act
|
||||
$result = $this->service->getAggregatedTimeEntries(
|
||||
$query,
|
||||
TimeEntryAggregationType::Day,
|
||||
TimeEntryAggregationType::Project,
|
||||
'Europe/Vienna',
|
||||
Weekday::Monday,
|
||||
false,
|
||||
null,
|
||||
null
|
||||
);
|
||||
|
||||
// Assert
|
||||
$this->assertSame([
|
||||
'seconds' => 0,
|
||||
'cost' => 0,
|
||||
'grouped_type' => 'day',
|
||||
'grouped_data' => [],
|
||||
], $result);
|
||||
}
|
||||
|
||||
public function test_aggregate_time_entries_by_day_and_project_with_filled_gaps(): void
|
||||
{
|
||||
// Arrange
|
||||
$query = TimeEntry::query();
|
||||
|
||||
// Act
|
||||
$result = $this->service->getAggregatedTimeEntries(
|
||||
$query,
|
||||
TimeEntryAggregationType::Day,
|
||||
TimeEntryAggregationType::Project,
|
||||
'Europe/Vienna',
|
||||
Weekday::Monday,
|
||||
true,
|
||||
Carbon::now()->subDays(2)->utc(),
|
||||
Carbon::now()->subDays(1)->utc(),
|
||||
);
|
||||
|
||||
// Assert
|
||||
$this->assertSame([
|
||||
'seconds' => 0,
|
||||
'cost' => 0,
|
||||
'grouped_type' => 'day',
|
||||
'grouped_data' => [
|
||||
[
|
||||
'key' => Carbon::now()->subDays(2)->utc()->format('Y-m-d'),
|
||||
'seconds' => 0,
|
||||
'cost' => 0,
|
||||
'grouped_type' => 'project',
|
||||
'grouped_data' => [],
|
||||
],
|
||||
[
|
||||
'key' => Carbon::now()->subDays(1)->utc()->format('Y-m-d'),
|
||||
'seconds' => 0,
|
||||
'cost' => 0,
|
||||
'grouped_type' => 'project',
|
||||
'grouped_data' => [],
|
||||
],
|
||||
],
|
||||
], $result);
|
||||
}
|
||||
|
||||
public function test_aggregate_time_entries_by_user_and_project_with_filled_gaps(): void
|
||||
{
|
||||
// Arrange
|
||||
$query = TimeEntry::query();
|
||||
|
||||
// Act
|
||||
$result = $this->service->getAggregatedTimeEntries(
|
||||
$query,
|
||||
TimeEntryAggregationType::User,
|
||||
TimeEntryAggregationType::Project,
|
||||
'Europe/Vienna',
|
||||
Weekday::Monday,
|
||||
true,
|
||||
Carbon::now()->subDays(2),
|
||||
Carbon::now()->subDays(1),
|
||||
);
|
||||
|
||||
// Assert
|
||||
$this->assertSame([
|
||||
'seconds' => 0,
|
||||
'cost' => 0,
|
||||
'grouped_type' => 'user',
|
||||
'grouped_data' => [],
|
||||
], $result);
|
||||
}
|
||||
|
||||
public function test_aggregate_time_entries_by_user_and_day_with_filled_gaps(): void
|
||||
{
|
||||
// Arrange
|
||||
$query = TimeEntry::query();
|
||||
|
||||
// Act
|
||||
$result = $this->service->getAggregatedTimeEntries(
|
||||
$query,
|
||||
TimeEntryAggregationType::User,
|
||||
TimeEntryAggregationType::Day,
|
||||
'Europe/Vienna',
|
||||
Weekday::Monday,
|
||||
true,
|
||||
Carbon::now()->subDays(2),
|
||||
Carbon::now()->subDays(1),
|
||||
);
|
||||
|
||||
// Assert
|
||||
$this->assertSame([
|
||||
'seconds' => 0,
|
||||
'cost' => 0,
|
||||
'grouped_type' => 'user',
|
||||
'grouped_data' => [],
|
||||
], $result);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user