createUserWithPermission(); Passport::actingAs($data->user); // Act $response = $this->getJson(route('api.v1.time-entries.index', [$data->organization->getKey()])); // Assert $response->assertForbidden(); } public function test_index_endpoint_fails_if_user_has_no_permission_to_view_time_entries_for_others_but_wants_all_entries(): void { // Arrange $data = $this->createUserWithPermission([ 'time-entries:view:own', ]); Passport::actingAs($data->user); // Act $response = $this->getJson(route('api.v1.time-entries.index', [$data->organization->getKey()])); // Assert $response->assertForbidden(); } public function test_index_endpoint_returns_time_entries_for_current_user(): void { // Arrange $data = $this->createUserWithPermission([ 'time-entries:view:own', ]); $timeEntry = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)->create(); Passport::actingAs($data->user); // Act $response = $this->getJson(route('api.v1.time-entries.index', [ $data->organization->getKey(), 'member_id' => $data->member->getKey(), ])); // Assert $this->assertResponseCode($response, 200); $response->assertJsonPath('data.0.id', $timeEntry->getKey()); } public function test_index_endpoint_filters_by_member_id_instead_of_legacy_user_id(): void { // Arrange $data = $this->createUserWithPermission([ 'time-entries:view:own', ]); $legacyUser = User::factory()->create(); $timeEntry = TimeEntry::factory()->forMember($data->member)->create([ 'user_id' => $legacyUser->getKey(), ]); Passport::actingAs($data->user); // Act $response = $this->getJson(route('api.v1.time-entries.index', [ $data->organization->getKey(), 'member_id' => $data->member->getKey(), ])); // Assert $this->assertResponseCode($response, 200); $response->assertJsonCount(1, 'data'); $response->assertJsonPath('data.0.id', $timeEntry->getKey()); } public function test_index_endpoint_fails_if_user_filter_is_from_different_organization(): void { // Arrange $data = $this->createUserWithPermission([ 'time-entries:view:all', ]); $otherData = $this->createUserWithPermission([ 'time-entries:view:all', ]); Passport::actingAs($data->user); // Act $response = $this->getJson(route('api.v1.time-entries.index', [ $data->organization->getKey(), 'member_id' => $otherData->member->getKey(), ])); // Assert $response->assertStatus(422); $response->assertJsonValidationErrorFor('member_id'); } public function test_index_endpoint_returns_time_entries_for_other_user_in_organization(): void { // Arrange $data = $this->createUserWithPermission([ 'time-entries:view:all', ]); $user = User::factory()->create(); $member = Member::factory()->forOrganization($data->organization)->forUser($user)->role(Role::Employee)->create(); $timeEntry = TimeEntry::factory()->forOrganization($data->organization)->forMember($member)->create(); Passport::actingAs($data->user); // Act $response = $this->getJson(route('api.v1.time-entries.index', [ $data->organization->getKey(), 'member_id' => $member->getKey(), ])); // Assert $this->assertResponseCode($response, 200); $response->assertJsonPath('data.0.id', $timeEntry->getKey()); } public function test_index_endpoint_returns_time_entries_for_all_users_in_organization_default_sort_by_start_date_desc(): void { // Arrange $data = $this->createUserWithPermission([ 'time-entries:view:all', ]); $user = User::factory()->create(); $member = Member::factory()->forOrganization($data->organization)->forUser($user)->role(Role::Employee)->create(); $timeEntry1 = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)->create([ 'start' => Carbon::now()->subDay(), ]); $timeEntry2 = TimeEntry::factory()->forOrganization($data->organization)->forMember($member)->create([ 'start' => Carbon::now()->subDays(2), ]); $timeEntry3 = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)->create([ 'start' => Carbon::now()->subDays(3), ]); Passport::actingAs($data->user); // Act $response = $this->getJson(route('api.v1.time-entries.index', [$data->organization->getKey()])); // Assert $this->assertResponseCode($response, 200); $response->assertJsonPath('data.0.id', $timeEntry1->getKey()); $response->assertJsonPath('data.1.id', $timeEntry2->getKey()); $response->assertJsonPath('data.2.id', $timeEntry3->getKey()); } public function test_index_endpoint_returns_only_active_time_entries(): void { // Arrange $data = $this->createUserWithPermission([ 'time-entries:view:own', ]); $activeTimeEntry = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)->active()->create(); $nonActiveTimeEntries = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)->createMany(3); Passport::actingAs($data->user); // Act $response = $this->getJson(route('api.v1.time-entries.index', [ $data->organization->getKey(), 'active' => 'true', 'member_id' => $data->member->getKey(), ])); // Assert $this->assertResponseCode($response, 200); $response->assertJsonCount(1, 'data'); $response->assertJsonPath('meta.total', 1); $response->assertJsonPath('data.0.id', $activeTimeEntry->getKey()); } public function test_index_endpoint_returns_only_non_active_time_entries(): void { // Arrange $data = $this->createUserWithPermission([ 'time-entries:view:own', ]); $activeTimeEntry = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)->active()->createMany(3); $nonActiveTimeEntries = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)->create(); Passport::actingAs($data->user); // Act $response = $this->getJson(route('api.v1.time-entries.index', [ $data->organization->getKey(), 'active' => 'false', 'member_id' => $data->member->getKey(), ])); // Assert $this->assertResponseCode($response, 200); $response->assertJsonCount(1, 'data'); $response->assertJsonPath('meta.total', 1); $response->assertJsonPath('data.0.id', $nonActiveTimeEntries->getKey()); } public function test_index_endpoint_filter_only_full_dates_returns_time_entries_for_the_whole_day_case_less_time_entries_than_limit(): void { // Arrange $data = $this->createUserWithPermission([ 'time-entries:view:own', ]); $timeEntries = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)->createMany(3); Passport::actingAs($data->user); // Act $response = $this->getJson(route('api.v1.time-entries.index', [ $data->organization->getKey(), 'only_full_dates' => 'true', 'limit' => 5, 'member_id' => $data->member->getKey(), ])); // Assert $this->assertResponseCode($response, 200); $response->assertJsonCount(3, 'data'); $response->assertJsonPath('meta.total', 3); } public function test_index_endpoint_filter_only_full_dates_returns_time_entries_for_the_whole_day_case_more_time_entries_than_limit(): void { // Arrange $data = $this->createUserWithPermission([ 'time-entries:view:own', ]); $timeEntriesDay1 = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member) ->startBetween(Carbon::now($data->user->timezone)->subDay()->startOfDay(), Carbon::now($data->user->timezone)->subDay()->endOfDay()) ->createMany(3); $timeEntriesDay2 = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member) ->startBetween(Carbon::now($data->user->timezone)->subDays(2)->startOfDay(), Carbon::now($data->user->timezone)->subDays(2)->endOfDay()) ->createMany(3); Passport::actingAs($data->user); // Act $response = $this->getJson(route('api.v1.time-entries.index', [ $data->organization->getKey(), 'only_full_dates' => 'true', 'limit' => 5, 'member_id' => $data->member->getKey(), ])); // Assert $this->assertResponseCode($response, 200); $response->assertJsonCount(3, 'data'); $response->assertJsonPath('meta.total', 6); } public function test_index_endpoint_filter_only_full_dates_returns_time_entries_for_the_whole_day_case_more_time_entries_than_limit_with_a_timezone_edge_case(): void { // Arrange $now = Carbon::create(2024, 1, 1, 12, 0, 0, 'Europe/Vienna'); $this->travelTo($now); $data = $this->createUserWithPermission([ 'time-entries:view:own', ]); $data->user->timezone = 'America/New_York'; $data->user->save(); /** * We create in the eyes of the users timezone 2 time entries yesterday, 2 time entries two days ago, and 3 time entries three days ago * The time entries are created in a way that they jump to the next day if the endpoint ignores the users timezone and just uses UTC */ // Note: This entry is yesterday in user timezone and yesterday in UTC $timeEntriesDay1InUserTimeZone = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member) ->state([ 'start' => Carbon::now()->timezone($data->user->timezone)->subDay()->startOfDay()->utc(), ]) ->createMany(2); // Note: This entry is yesterday in UTC timezone, but two days ago in user timezone $timeEntriesDay1InUTC = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member) ->state([ 'start' => Carbon::now()->utc()->subDay()->startOfDay()->utc(), ]) ->createMany(2); // Note: This entry is two days ago in user timezone $timeEntriesDay2InUserTimeZone = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member) ->state([ 'start' => Carbon::now()->timezone($data->user->timezone)->subDays(2)->startOfDay()->utc(), ]) ->createMany(3); Passport::actingAs($data->user); // Act $response = $this->getJson(route('api.v1.time-entries.index', [ $data->organization->getKey(), 'only_full_dates' => 'true', 'limit' => 5, 'member_id' => $data->member->getKey(), ])); // Assert $this->assertResponseCode($response, 200); $response->assertJsonCount(2, 'data'); $response->assertJsonPath('meta.total', 7); } public function test_index_endpoint_filter_only_full_dates_returns_time_entries_for_the_whole_day_case_more_time_entries_in_latest_day_than_limit(): void { // Arrange $now = Carbon::create(2024, 1, 1, 12, 0, 0, 'Europe/Vienna'); $this->travelTo($now); $data = $this->createUserWithPermission([ 'time-entries:view:own', ]); $timeEntriesDay1 = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member) ->state([ 'start' => Carbon::now()->timezone($data->user->timezone)->subDay()->startOfDay()->utc(), ]) ->createMany(7); $timeEntriesDay2 = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member) ->state([ 'start' => Carbon::now()->timezone($data->user->timezone)->subDays(2)->endOfDay()->utc(), ]) ->createMany(3); Passport::actingAs($data->user); // Act $response = $this->getJson(route('api.v1.time-entries.index', [ $data->organization->getKey(), 'only_full_dates' => 'true', 'limit' => 5, 'member_id' => $data->member->getKey(), ])); // Assert $this->assertResponseCode($response, 200); $response->assertJsonCount(7, 'data'); $response->assertJsonPath('meta.total', 10); Log::assertLogged(fn (LogEntry $log) => $log->level === 'warning' && $log->message === 'User has has more than 5 time entries on one date' ); } public function test_index_endpoint_before_filter_returns_time_entries_before_date(): void { // Arrange $data = $this->createUserWithPermission([ 'time-entries:view:own', ]); $timeEntriesAfter = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member) ->startBetween( Carbon::now()->timezone($data->user->timezone)->subDay()->startOfDay()->utc(), Carbon::now()->timezone($data->user->timezone)->utc() ) ->createMany(3); $timeEntriesBefore = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member) ->startBetween( Carbon::now()->timezone($data->user->timezone)->subDays(2)->startOfDay()->utc(), 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(), ]); Passport::actingAs($data->user); // Act $response = $this->getJson(route('api.v1.time-entries.index', [ $data->organization->getKey(), 'end' => Carbon::now()->timezone($data->user->timezone)->subDay()->startOfDay()->toIso8601ZuluString(), 'member_id' => $data->member->getKey(), ])); // Assert $this->assertResponseCode($response, 200); $response->assertJson(fn (AssertableJson $json) => $json ->has('data') ->has('meta') ->where('meta.total', 4) ->count('data', 4) ->where('data.0.id', $timeEntriesDirectlyBeforeLimit->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()) ); } public function test_index_endpoint_pagination_returns_every_time_entry_exactly_once_with_rounding(): void { // Arrange $data = $this->createUserWithPermission([ 'time-entries:view:own', ]); // Bulk import: 300 time entries that all share the exact same start. $sharedStart = Carbon::createFromFormat('Y-m-d H:i:s', '2020-01-01 00:00:07'); $rows = []; for ($i = 0; $i < 300; $i++) { $rows[] = [ 'id' => (string) Str::uuid(), 'description' => 'Entry '.$i, 'start' => $sharedStart, 'end' => $sharedStart, 'billable' => false, 'is_imported' => true, 'user_id' => $data->member->user_id, 'member_id' => $data->member->getKey(), 'organization_id' => $data->organization->getKey(), 'created_at' => $sharedStart, 'updated_at' => $sharedStart, ]; } DB::table('time_entries')->insert($rows); $this->actAsOrganizationWithSubscription(); Passport::actingAs($data->user); // Act - walk every page like the client does (limit/offset), with rounding enabled. $orgId = $data->organization->getKey(); $limit = 15; $collected = collect(); $offset = 0; do { $response = $this->getJson(route('api.v1.time-entries.index', [ $orgId, 'member_id' => $data->member->getKey(), 'rounding_type' => TimeEntryRoundingType::Nearest, 'rounding_minutes' => 6, 'limit' => $limit, 'offset' => $offset, ])); $this->assertResponseCode($response, 200); $ids = $response->json('data.*.id'); $collected = $collected->concat($ids); $offset += $limit; } while (count($ids) === $limit); // Assert - every time entry appears exactly once, none duplicated or missing. $this->assertEqualsCanonicalizing(array_column($rows, 'id'), $collected->all(), 'Some time entries were duplicated or missing across pages'); } public function test_index_endpoint_can_round_up(): void { // Arrange $this->travelTo(Carbon::createFromFormat('Y-m-d H:i:s', '2020-01-01 00:15:04')); $data = $this->createUserWithPermission([ 'time-entries:view:own', ]); $timeEntry1 = TimeEntry::factory()->forOrganization($data->organization) ->forMember($data->member) ->create([ 'start' => Carbon::createFromFormat('Y-m-d H:i:s', '2020-01-01 00:00:08'), 'end' => Carbon::createFromFormat('Y-m-d H:i:s', '2020-01-01 00:00:01'), ]); $timeEntry2 = TimeEntry::factory()->forOrganization($data->organization) ->forMember($data->member) ->create([ 'start' => Carbon::createFromFormat('Y-m-d H:i:s', '2020-01-01 00:00:07'), 'end' => null, ]); $this->actAsOrganizationWithSubscription(); Passport::actingAs($data->user); // Act $response = $this->getJson(route('api.v1.time-entries.index', [ $data->organization->getKey(), 'member_id' => $data->member->getKey(), 'rounding_type' => TimeEntryRoundingType::Up, 'rounding_minutes' => 6, ])); // Assert $this->assertResponseCode($response, 200); $response->assertJson(fn (AssertableJson $json) => $json ->has('data') ->has('meta') ->where('meta.total', 2) ->count('data', 2) ->where('data.0.id', $timeEntry1->getKey()) ->where('data.0.start', '2020-01-01T00:00:00Z') ->where('data.0.end', '2020-01-01T00:06:00Z') ->where('data.1.id', $timeEntry2->getKey()) ->where('data.1.start', '2020-01-01T00:00:00Z') ->where('data.1.end', '2020-01-01T00:18:00Z') ); } public function test_index_endpoint_can_round_up_but_does_not_round_up_if_already_on_border(): void { // Arrange $this->travelTo(Carbon::createFromFormat('Y-m-d H:i:s', '2020-01-01 00:15:04')); $data = $this->createUserWithPermission([ 'time-entries:view:own', ]); $timeEntry1 = TimeEntry::factory()->forOrganization($data->organization) ->forMember($data->member) ->create([ 'start' => Carbon::createFromFormat('Y-m-d H:i:s', '2020-01-01 00:00:08'), 'end' => Carbon::createFromFormat('Y-m-d H:i:s', '2020-01-01 00:06:00'), ]); $timeEntry2 = TimeEntry::factory()->forOrganization($data->organization) ->forMember($data->member) ->create([ 'start' => Carbon::createFromFormat('Y-m-d H:i:s', '2020-01-01 00:00:07'), 'end' => null, ]); $this->actAsOrganizationWithSubscription(); Passport::actingAs($data->user); // Act $response = $this->getJson(route('api.v1.time-entries.index', [ $data->organization->getKey(), 'member_id' => $data->member->getKey(), 'rounding_type' => TimeEntryRoundingType::Up, 'rounding_minutes' => 6, ])); // Assert $this->assertResponseCode($response, 200); $response->assertJson(fn (AssertableJson $json) => $json ->has('data') ->has('meta') ->where('meta.total', 2) ->count('data', 2) ->where('data.0.id', $timeEntry1->getKey()) ->where('data.0.start', '2020-01-01T00:00:00Z') ->where('data.0.end', '2020-01-01T00:06:00Z') ->where('data.1.id', $timeEntry2->getKey()) ->where('data.1.start', '2020-01-01T00:00:00Z') ->where('data.1.end', '2020-01-01T00:18:00Z') ); } public function test_index_endpoint_ignores_rounding_if_organization_has_no_premium_features(): void { // Arrange $this->travelTo(Carbon::createFromFormat('Y-m-d H:i:s', '2020-01-01 00:15:04')); $data = $this->createUserWithPermission([ 'time-entries:view:own', ]); $timeEntry1 = TimeEntry::factory()->forOrganization($data->organization) ->forMember($data->member) ->create([ 'start' => Carbon::createFromFormat('Y-m-d H:i:s', '2020-01-01 00:00:08'), 'end' => Carbon::createFromFormat('Y-m-d H:i:s', '2020-01-01 00:00:01'), ]); $timeEntry2 = TimeEntry::factory()->forOrganization($data->organization) ->forMember($data->member) ->create([ 'start' => Carbon::createFromFormat('Y-m-d H:i:s', '2020-01-01 00:00:07'), 'end' => null, ]); $this->actAsOrganizationWithoutSubscriptionAndWithoutTrial(); Passport::actingAs($data->user); // Act $response = $this->getJson(route('api.v1.time-entries.index', [ $data->organization->getKey(), 'member_id' => $data->member->getKey(), 'rounding_type' => TimeEntryRoundingType::Up, 'rounding_minutes' => 6, ])); // Assert $this->assertResponseCode($response, 200); $response->assertJson(fn (AssertableJson $json) => $json ->has('data') ->has('meta') ->where('meta.total', 2) ->count('data', 2) ->where('data.0.id', $timeEntry1->getKey()) ->where('data.0.start', '2020-01-01T00:00:08Z') ->where('data.0.end', '2020-01-01T00:00:01Z') ->where('data.1.id', $timeEntry2->getKey()) ->where('data.1.start', '2020-01-01T00:00:07Z') ->where('data.1.end', null) ); } public function test_index_endpoint_can_round_down(): void { // Arrange $this->travelTo(Carbon::createFromFormat('Y-m-d H:i:s', '2020-01-01 00:15:04')); $data = $this->createUserWithPermission([ 'time-entries:view:own', ]); $timeEntry1 = TimeEntry::factory()->forOrganization($data->organization) ->forMember($data->member) ->create([ 'start' => Carbon::createFromFormat('Y-m-d H:i:s', '2020-01-01 00:00:08'), 'end' => Carbon::createFromFormat('Y-m-d H:i:s', '2020-01-01 00:00:01'), ]); $timeEntry2 = TimeEntry::factory()->forOrganization($data->organization) ->forMember($data->member) ->create([ 'start' => Carbon::createFromFormat('Y-m-d H:i:s', '2020-01-01 00:00:07'), 'end' => null, ]); $this->actAsOrganizationWithSubscription(); Passport::actingAs($data->user); // Act $response = $this->getJson(route('api.v1.time-entries.index', [ $data->organization->getKey(), 'member_id' => $data->member->getKey(), 'rounding_type' => TimeEntryRoundingType::Down, 'rounding_minutes' => 6, ])); // Assert $this->assertResponseCode($response, 200); $response->assertJson(fn (AssertableJson $json) => $json ->has('data') ->has('meta') ->where('meta.total', 2) ->count('data', 2) ->where('data.0.id', $timeEntry1->getKey()) ->where('data.0.start', '2020-01-01T00:00:00Z') ->where('data.0.end', '2020-01-01T00:00:00Z') ->where('data.1.id', $timeEntry2->getKey()) ->where('data.1.start', '2020-01-01T00:00:00Z') ->where('data.1.end', '2020-01-01T00:12:00Z') ); } public function test_index_endpoint_can_round_nearest(): void { // Arrange $this->travelTo(Carbon::createFromFormat('Y-m-d H:i:s', '2020-01-01 00:15:00')); $data = $this->createUserWithPermission([ 'time-entries:view:own', ]); $timeEntry1 = TimeEntry::factory()->forOrganization($data->organization) ->forMember($data->member) ->create([ 'start' => Carbon::createFromFormat('Y-m-d H:i:s', '2020-01-01 00:00:08'), 'end' => Carbon::createFromFormat('Y-m-d H:i:s', '2020-01-01 00:02:59'), ]); $timeEntry2 = TimeEntry::factory()->forOrganization($data->organization) ->forMember($data->member) ->create([ 'start' => Carbon::createFromFormat('Y-m-d H:i:s', '2020-01-01 00:00:07'), 'end' => null, ]); $this->actAsOrganizationWithSubscription(); Passport::actingAs($data->user); // Act $response = $this->getJson(route('api.v1.time-entries.index', [ $data->organization->getKey(), 'member_id' => $data->member->getKey(), 'rounding_type' => TimeEntryRoundingType::Nearest, 'rounding_minutes' => 6, ])); // Assert $this->assertResponseCode($response, 200); $response->assertJson(fn (AssertableJson $json) => $json ->has('data') ->has('meta') ->where('meta.total', 2) ->count('data', 2) ->where('data.0.id', $timeEntry1->getKey()) ->where('data.0.start', '2020-01-01T00:00:00Z') ->where('data.0.end', '2020-01-01T00:00:00Z') ->where('data.1.id', $timeEntry2->getKey()) ->where('data.1.start', '2020-01-01T00:00:00Z') ->where('data.1.end', '2020-01-01T00:18:00Z') ); } public function test_index_endpoint_after_filter_returns_time_entries_after_date(): void { // Arrange $data = $this->createUserWithPermission([ 'time-entries:view:own', ]); $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); $timeEntriesDirectlyAfterLimit = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member) ->create([ 'start' => Carbon::now($data->user->timezone)->startOfDay()->utc(), ]); Passport::actingAs($data->user); // Act $response = $this->getJson(route('api.v1.time-entries.index', [ $data->organization->getKey(), 'start' => Carbon::now($data->user->timezone)->subDay()->endOfDay()->toIso8601ZuluString(), // yesterday 'member_id' => $data->member->getKey(), ])); // Assert $this->assertResponseCode($response, 200); $response->assertJson(fn (AssertableJson $json) => $json ->has('data') ->has('meta') ->where('meta.total', 4) ->count('data', 4) ->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()], 'start' => Carbon::now()->subDay()->toIso8601ZuluString(), 'end' => Carbon::now()->toIso8601ZuluString(), 'active' => 'true', 'only_full_dates' => 'true', 'limit' => 1, ])); // Assert $response->assertValid(); $this->assertResponseCode($response, 200); $response->assertJson(fn (AssertableJson $json) => $json ->has('data') ->has('meta') ->where('meta.total', 1) ->count('data', 1) ->where('data.0.id', $timeEntry1->getKey()) ); } public function test_index_endpoint_with_limit_offset_and_only_full_dates_deactivated(): void { // Arrange $data = $this->createUserWithPermission([ 'time-entries:view:own', ]); $project1 = Project::factory()->forOrganization($data->organization)->create(); $project2 = Project::factory()->forOrganization($data->organization)->create(); TimeEntry::factory()->forMember($data->member)->forProject($project1)->forOrganization($data->organization)->create([ 'start' => Carbon::now()->subDays(2), ]); $timeEntry = TimeEntry::factory()->forMember($data->member)->forProject($project1)->forOrganization($data->organization)->create([ 'start' => Carbon::now()->subDays(3), ]); TimeEntry::factory()->forMember($data->member)->forProject($project1)->forOrganization($data->organization)->create([ 'start' => Carbon::now()->subDays(4), ]); TimeEntry::factory()->forMember($data->member)->forProject($project2)->forOrganization($data->organization)->create(); Passport::actingAs($data->user); // Act $response = $this->getJson(route('api.v1.time-entries.index', [ $data->organization->getKey(), 'member_id' => $data->member->getKey(), 'project_ids' => [$project1->getKey()], 'limit' => 1, 'offset' => 1, 'only_full_dates' => 'false', ])); // Assert $this->assertResponseCode($response, 200); $response->assertJsonCount(1, 'data'); $response->assertJsonPath('meta.total', 3); $response->assertJsonPath('data.*.id', [$timeEntry->getKey()]); } public function test_index_export_endpoint_fails_if_user_has_no_permission_to_view_time_entries(): void { // Arrange $data = $this->createUserWithPermission(); Passport::actingAs($data->user); // Act $response = $this->getJson(route('api.v1.time-entries.index-export', [ $data->organization->getKey(), 'format' => ExportFormat::CSV, 'start' => Carbon::now()->startOfYear()->toIso8601ZuluString(), 'end' => Carbon::now()->endOfYear()->toIso8601ZuluString(), ])); // Assert $response->assertForbidden(); } public function test_index_export_endpoint_fails_if_pdf_renderer_is_not_configured_but_a_user_want_a_pdf_report(): void { // Arrange $data = $this->createUserWithPermission([ 'time-entries:view:all', ]); Passport::actingAs($data->user); Config::set('services.gotenberg.url', null); $this->actAsOrganizationWithSubscription(); // Act $response = $this->getJson(route('api.v1.time-entries.index-export', [ $data->organization->getKey(), 'format' => ExportFormat::PDF, 'start' => Carbon::now()->startOfYear()->toIso8601ZuluString(), 'end' => Carbon::now()->endOfYear()->toIso8601ZuluString(), ])); // Assert $response->assertStatus(400); $response->assertExactJson([ 'error' => true, 'key' => 'pdf_renderer_is_not_configured', 'message' => 'PDF renderer is not configured', ]); } public function test_index_export_endpoint_fails_if_user_wants_a_pdf_export_but_has_no_subscription(): void { // Arrange $data = $this->createUserWithPermission([ 'time-entries:view:all', ]); Passport::actingAs($data->user); $this->actAsOrganizationWithoutSubscriptionAndWithoutTrial(); // Act $response = $this->getJson(route('api.v1.time-entries.index-export', [ $data->organization->getKey(), 'format' => ExportFormat::PDF, 'start' => Carbon::now()->startOfYear()->toIso8601ZuluString(), 'end' => Carbon::now()->endOfYear()->toIso8601ZuluString(), ])); // Assert $response->assertStatus(400); $response->assertExactJson([ 'error' => true, 'key' => 'feature_is_not_available_in_free_plan', 'message' => 'Feature is not available in free plan', ]); } public function test_index_export_endpoint_fails_if_user_has_only_access_to_own_time_entries_but_does_not_filter_for_this(): void { // Arrange $data = $this->createUserWithPermission([ 'time-entries:view:own', ]); Passport::actingAs($data->user); // Act $response = $this->getJson(route('api.v1.time-entries.index-export', [ $data->organization->getKey(), 'format' => ExportFormat::CSV, 'start' => Carbon::now()->startOfYear()->toIso8601ZuluString(), 'end' => Carbon::now()->endOfYear()->toIso8601ZuluString(), ])); // Assert $response->assertForbidden(); } public function test_index_export_endpoint_can_create_a_detailed_time_entry_report_in_format_csv(): void { // Arrange $data = $this->createUserWithPermission([ 'time-entries:view:all', ]); $client = Client::factory()->forOrganization($data->organization)->create(); $project = Project::factory()->forOrganization($data->organization)->forClient($client)->create(); $timeEntry1 = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)->startWithDuration(Carbon::now(), 100)->create(); $timeEntry2 = TimeEntry::factory()->forOrganization($data->organization)->forProject($project)->forMember($data->member)->startWithDuration(Carbon::now(), 100)->create(); Passport::actingAs($data->user); // Act $response = $this->getJson(route('api.v1.time-entries.index-export', [ $data->organization->getKey(), 'format' => ExportFormat::CSV, 'start' => Carbon::now()->startOfYear()->toIso8601ZuluString(), 'end' => Carbon::now()->endOfYear()->toIso8601ZuluString(), ])); // Assert $this->assertResponseCode($response, 200); } public function test_index_export_endpoint_can_create_a_detailed_time_entry_report_in_format_ods(): void { // Arrange $data = $this->createUserWithPermission([ 'time-entries:view:all', ]); $client = Client::factory()->forOrganization($data->organization)->create(); $project = Project::factory()->forOrganization($data->organization)->forClient($client)->create(); $timeEntry1 = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)->startWithDuration(Carbon::now(), 100)->create(); $timeEntry2 = TimeEntry::factory()->forOrganization($data->organization)->forProject($project)->forMember($data->member)->startWithDuration(Carbon::now(), 100)->create(); Passport::actingAs($data->user); // Act $response = $this->getJson(route('api.v1.time-entries.index-export', [ $data->organization->getKey(), 'format' => ExportFormat::ODS, 'start' => Carbon::now()->startOfYear()->toIso8601ZuluString(), 'end' => Carbon::now()->endOfYear()->toIso8601ZuluString(), ])); // Assert $this->assertResponseCode($response, 200); } public function test_index_export_endpoint_can_create_a_detailed_time_entry_report_in_format_xlxs(): void { // Arrange $data = $this->createUserWithPermission([ 'time-entries:view:all', ]); $client = Client::factory()->forOrganization($data->organization)->create(); $project = Project::factory()->forOrganization($data->organization)->forClient($client)->create(); $timeEntry1 = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)->startWithDuration(Carbon::now(), 100)->create(); $timeEntry2 = TimeEntry::factory()->forOrganization($data->organization)->forProject($project)->forMember($data->member)->startWithDuration(Carbon::now(), 100)->create(); Passport::actingAs($data->user); // Act $response = $this->getJson(route('api.v1.time-entries.index-export', [ $data->organization->getKey(), 'format' => ExportFormat::XLSX, 'start' => Carbon::now()->startOfYear()->toIso8601ZuluString(), 'end' => Carbon::now()->endOfYear()->toIso8601ZuluString(), ])); // Assert $this->assertResponseCode($response, 200); } public function test_index_export_endpoint_can_create_a_detailed_time_entry_report_in_format_pdf(): void { // Arrange $data = $this->createUserWithPermission([ 'time-entries:view:all', ]); Passport::actingAs($data->user); $client = Client::factory()->forOrganization($data->organization)->create(); $project = Project::factory()->forOrganization($data->organization)->forClient($client)->create(); $timeEntry1 = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)->startWithDuration(Carbon::now(), 100)->create(); $timeEntry2 = TimeEntry::factory()->forOrganization($data->organization)->forProject($project)->forMember($data->member)->startWithDuration(Carbon::now(), 100)->create(); $this->actAsOrganizationWithSubscription(); // Act $response = $this->getJson(route('api.v1.time-entries.index-export', [ $data->organization->getKey(), 'format' => ExportFormat::PDF, 'start' => Carbon::now()->startOfYear()->toIso8601ZuluString(), 'end' => Carbon::now()->endOfYear()->toIso8601ZuluString(), ])); // Assert $this->assertResponseCode($response, 200); } public function test_index_export_endpoint_can_create_a_detailed_time_entry_report_in_format_csv_as_employee_role_with_show_billable_rate(): void { // Arrange $data = $this->createUserWithRole(Role::Employee, true); $client = Client::factory()->forOrganization($data->organization)->create(); $project = Project::factory()->forOrganization($data->organization)->forClient($client)->create(); $timeEntry1 = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)->startWithDuration(Carbon::now(), 100)->create(); $timeEntry2 = TimeEntry::factory()->forOrganization($data->organization)->forProject($project)->forMember($data->member)->startWithDuration(Carbon::now(), 100)->create(); Passport::actingAs($data->user); // Act $response = $this->getJson(route('api.v1.time-entries.index-export', [ $data->organization->getKey(), 'format' => ExportFormat::CSV, 'start' => Carbon::now()->startOfYear()->toIso8601ZuluString(), 'end' => Carbon::now()->endOfYear()->toIso8601ZuluString(), 'member_id' => $data->member->id, ])); // Assert $this->assertResponseCode($response, 200); } public function test_index_export_endpoint_can_create_a_detailed_time_entry_report_in_format_ods_as_employee_role_with_show_billable_rate(): void { // Arrange $data = $this->createUserWithRole(Role::Employee, true); $client = Client::factory()->forOrganization($data->organization)->create(); $project = Project::factory()->forOrganization($data->organization)->forClient($client)->create(); $timeEntry1 = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)->startWithDuration(Carbon::now(), 100)->create(); $timeEntry2 = TimeEntry::factory()->forOrganization($data->organization)->forProject($project)->forMember($data->member)->startWithDuration(Carbon::now(), 100)->create(); Passport::actingAs($data->user); // Act $response = $this->getJson(route('api.v1.time-entries.index-export', [ $data->organization->getKey(), 'format' => ExportFormat::ODS, 'start' => Carbon::now()->startOfYear()->toIso8601ZuluString(), 'end' => Carbon::now()->endOfYear()->toIso8601ZuluString(), 'member_id' => $data->member->id, ])); // Assert $this->assertResponseCode($response, 200); } public function test_index_export_endpoint_can_create_a_detailed_time_entry_report_in_format_xlxs_as_employee_role_with_show_billable_rate(): void { // Arrange $data = $this->createUserWithRole(Role::Employee, true); $client = Client::factory()->forOrganization($data->organization)->create(); $project = Project::factory()->forOrganization($data->organization)->forClient($client)->create(); $timeEntry1 = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)->startWithDuration(Carbon::now(), 100)->create(); $timeEntry2 = TimeEntry::factory()->forOrganization($data->organization)->forProject($project)->forMember($data->member)->startWithDuration(Carbon::now(), 100)->create(); Passport::actingAs($data->user); // Act $response = $this->getJson(route('api.v1.time-entries.index-export', [ $data->organization->getKey(), 'format' => ExportFormat::XLSX, 'start' => Carbon::now()->startOfYear()->toIso8601ZuluString(), 'end' => Carbon::now()->endOfYear()->toIso8601ZuluString(), 'member_id' => $data->member->id, ])); // Assert $this->assertResponseCode($response, 200); } public function test_index_export_endpoint_can_create_a_detailed_time_entry_report_in_format_pdf_as_employee_role_with_show_billable_rate(): void { // Arrange $data = $this->createUserWithRole(Role::Employee, true); Passport::actingAs($data->user); $client = Client::factory()->forOrganization($data->organization)->create(); $project = Project::factory()->forOrganization($data->organization)->forClient($client)->create(); $timeEntry1 = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)->startWithDuration(Carbon::now(), 100)->create(); $timeEntry2 = TimeEntry::factory()->forOrganization($data->organization)->forProject($project)->forMember($data->member)->startWithDuration(Carbon::now(), 100)->create(); $this->actAsOrganizationWithSubscription(); // Act $response = $this->getJson(route('api.v1.time-entries.index-export', [ $data->organization->getKey(), 'format' => ExportFormat::PDF, 'start' => Carbon::now()->startOfYear()->toIso8601ZuluString(), 'end' => Carbon::now()->endOfYear()->toIso8601ZuluString(), 'member_id' => $data->member->id, ])); // Assert $this->assertResponseCode($response, 200); } public function test_index_export_endpoint_can_create_a_detailed_time_entry_report_in_format_csv_as_employee_role_without_show_billable_rate(): void { // Arrange $data = $this->createUserWithRole(Role::Employee, false); $client = Client::factory()->forOrganization($data->organization)->create(); $project = Project::factory()->forOrganization($data->organization)->forClient($client)->create(); $timeEntry1 = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)->startWithDuration(Carbon::now(), 100)->create(); $timeEntry2 = TimeEntry::factory()->forOrganization($data->organization)->forProject($project)->forMember($data->member)->startWithDuration(Carbon::now(), 100)->create(); Passport::actingAs($data->user); // Act $response = $this->getJson(route('api.v1.time-entries.index-export', [ $data->organization->getKey(), 'format' => ExportFormat::CSV, 'start' => Carbon::now()->startOfYear()->toIso8601ZuluString(), 'end' => Carbon::now()->endOfYear()->toIso8601ZuluString(), 'member_id' => $data->member->id, ])); // Assert $this->assertResponseCode($response, 200); } public function test_index_export_endpoint_can_create_a_detailed_time_entry_report_in_format_ods_as_employee_role_without_show_billable_rate(): void { // Arrange $data = $this->createUserWithRole(Role::Employee, false); $client = Client::factory()->forOrganization($data->organization)->create(); $project = Project::factory()->forOrganization($data->organization)->forClient($client)->create(); $timeEntry1 = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)->startWithDuration(Carbon::now(), 100)->create(); $timeEntry2 = TimeEntry::factory()->forOrganization($data->organization)->forProject($project)->forMember($data->member)->startWithDuration(Carbon::now(), 100)->create(); Passport::actingAs($data->user); // Act $response = $this->getJson(route('api.v1.time-entries.index-export', [ $data->organization->getKey(), 'format' => ExportFormat::ODS, 'start' => Carbon::now()->startOfYear()->toIso8601ZuluString(), 'end' => Carbon::now()->endOfYear()->toIso8601ZuluString(), 'member_id' => $data->member->id, ])); // Assert $this->assertResponseCode($response, 200); } public function test_index_export_endpoint_can_create_a_detailed_time_entry_report_in_format_xlxs_as_employee_role_without_show_billable_rate(): void { // Arrange $data = $this->createUserWithRole(Role::Employee, false); $client = Client::factory()->forOrganization($data->organization)->create(); $project = Project::factory()->forOrganization($data->organization)->forClient($client)->create(); $timeEntry1 = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)->startWithDuration(Carbon::now(), 100)->create(); $timeEntry2 = TimeEntry::factory()->forOrganization($data->organization)->forProject($project)->forMember($data->member)->startWithDuration(Carbon::now(), 100)->create(); Passport::actingAs($data->user); // Act $response = $this->getJson(route('api.v1.time-entries.index-export', [ $data->organization->getKey(), 'format' => ExportFormat::XLSX, 'start' => Carbon::now()->startOfYear()->toIso8601ZuluString(), 'end' => Carbon::now()->endOfYear()->toIso8601ZuluString(), 'member_id' => $data->member->id, ])); // Assert $this->assertResponseCode($response, 200); } public function test_index_export_endpoint_can_create_a_detailed_time_entry_report_in_format_pdf_as_employee_role_without_show_billable_rate(): void { // Arrange $data = $this->createUserWithRole(Role::Employee, false); Passport::actingAs($data->user); $client = Client::factory()->forOrganization($data->organization)->create(); $project = Project::factory()->forOrganization($data->organization)->forClient($client)->create(); $timeEntry1 = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)->startWithDuration(Carbon::now(), 100)->create(); $timeEntry2 = TimeEntry::factory()->forOrganization($data->organization)->forProject($project)->forMember($data->member)->startWithDuration(Carbon::now(), 100)->create(); $this->actAsOrganizationWithSubscription(); // Act $response = $this->getJson(route('api.v1.time-entries.index-export', [ $data->organization->getKey(), 'format' => ExportFormat::PDF, 'start' => Carbon::now()->startOfYear()->toIso8601ZuluString(), 'end' => Carbon::now()->endOfYear()->toIso8601ZuluString(), 'member_id' => $data->member->id, ])); // Assert $this->assertResponseCode($response, 200); } public function test_aggregate_export_endpoints_fails_if_user_no_permission_to_view_time_entries(): void { // Arrange $data = $this->createUserWithPermission(); Passport::actingAs($data->user); // Act $response = $this->getJson(route('api.v1.time-entries.aggregate-export', [ $data->organization->getKey(), 'format' => ExportFormat::CSV, 'group' => TimeEntryAggregationType::Client, 'sub_group' => TimeEntryAggregationType::Project, 'history_group' => TimeEntryAggregationTypeInterval::Month, 'start' => Carbon::now()->startOfYear()->toIso8601ZuluString(), 'end' => Carbon::now()->endOfYear()->toIso8601ZuluString(), ])); // Assert $response->assertForbidden(); } public function test_aggregate_endpoint_fails_if_user_has_no_permission_to_view_time_entries(): void { // Arrange $data = $this->createUserWithPermission(); Passport::actingAs($data->user); // Act $response = $this->getJson(route('api.v1.time-entries.aggregate', [ $data->organization->getKey(), ])); // Assert $response->assertForbidden(); } public function test_aggregate_export_endpoint_fails_if_user_wants_a_pdf_export_but_has_no_subscription(): void { // Arrange $data = $this->createUserWithPermission([ 'time-entries:view:all', ]); Passport::actingAs($data->user); $this->actAsOrganizationWithoutSubscriptionAndWithoutTrial(); // Act $response = $this->getJson(route('api.v1.time-entries.aggregate-export', [ $data->organization->getKey(), 'format' => ExportFormat::PDF, 'group' => TimeEntryAggregationType::Client, 'sub_group' => TimeEntryAggregationType::Project, 'history_group' => TimeEntryAggregationTypeInterval::Month, 'start' => Carbon::now()->startOfYear()->toIso8601ZuluString(), 'end' => Carbon::now()->endOfYear()->toIso8601ZuluString(), ])); // Assert $response->assertStatus(400); $response->assertExactJson([ 'error' => true, 'key' => 'feature_is_not_available_in_free_plan', 'message' => 'Feature is not available in free plan', ]); } public function test_aggregate_export_endpoint_fails_if_user_has_only_access_to_own_time_entries_but_does_not_filter_for_this(): void { // Arrange $data = $this->createUserWithPermission([ 'time-entries:view:own', ]); Passport::actingAs($data->user); // Act $response = $this->getJson(route('api.v1.time-entries.aggregate-export', [ $data->organization->getKey(), 'format' => ExportFormat::CSV, 'group' => TimeEntryAggregationType::Client, 'sub_group' => TimeEntryAggregationType::Project, 'history_group' => TimeEntryAggregationTypeInterval::Month, 'start' => Carbon::now()->startOfYear()->toIso8601ZuluString(), 'end' => Carbon::now()->endOfYear()->toIso8601ZuluString(), ])); // Assert $response->assertForbidden(); } public function test_aggregate_export_endpoints_can_create_a_csv_report(): void { // Arrange $data = $this->createUserWithPermission([ 'time-entries:view:all', ]); $client = Client::factory()->forOrganization($data->organization)->create(); $project = Project::factory()->forOrganization($data->organization)->forClient($client)->create(); $timeEntry1 = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)->startWithDuration(Carbon::now(), 100)->create(); $timeEntry2 = TimeEntry::factory()->forOrganization($data->organization)->forProject($project)->forMember($data->member)->startWithDuration(Carbon::now(), 100)->create(); Passport::actingAs($data->user); // Act $response = $this->getJson(route('api.v1.time-entries.aggregate-export', [ $data->organization->getKey(), 'format' => ExportFormat::CSV, 'group' => TimeEntryAggregationType::Client, 'sub_group' => TimeEntryAggregationType::Project, 'history_group' => TimeEntryAggregationTypeInterval::Month, 'start' => Carbon::now()->startOfYear()->toIso8601ZuluString(), 'end' => Carbon::now()->endOfYear()->toIso8601ZuluString(), ])); // Assert $this->assertResponseCode($response, 200); } public function test_aggregate_export_endpoints_can_create_a_csv_report_as_employee_role_with_show_billable_rate(): void { // Arrange $data = $this->createUserWithRole(Role::Employee, true); $client = Client::factory()->forOrganization($data->organization)->create(); $project = Project::factory()->forOrganization($data->organization)->forClient($client)->create(); $timeEntry1 = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)->startWithDuration(Carbon::now(), 100)->create(); $timeEntry2 = TimeEntry::factory()->forOrganization($data->organization)->forProject($project)->forMember($data->member)->startWithDuration(Carbon::now(), 100)->create(); Passport::actingAs($data->user); // Act $response = $this->getJson(route('api.v1.time-entries.aggregate-export', [ $data->organization->getKey(), 'format' => ExportFormat::CSV, 'group' => TimeEntryAggregationType::Client, 'sub_group' => TimeEntryAggregationType::Project, 'history_group' => TimeEntryAggregationTypeInterval::Month, 'start' => Carbon::now()->startOfYear()->toIso8601ZuluString(), 'end' => Carbon::now()->endOfYear()->toIso8601ZuluString(), 'member_id' => $data->member->getKey(), ])); // Assert $this->assertResponseCode($response, 200); } public function test_aggregate_export_endpoints_can_create_a_csv_report_as_employee_role_without_show_billable_rate(): void { // Arrange $data = $this->createUserWithRole(Role::Employee, false); $client = Client::factory()->forOrganization($data->organization)->create(); $project = Project::factory()->forOrganization($data->organization)->forClient($client)->create(); $timeEntry1 = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)->startWithDuration(Carbon::now(), 100)->create(); $timeEntry2 = TimeEntry::factory()->forOrganization($data->organization)->forProject($project)->forMember($data->member)->startWithDuration(Carbon::now(), 100)->create(); Passport::actingAs($data->user); // Act $response = $this->getJson(route('api.v1.time-entries.aggregate-export', [ $data->organization->getKey(), 'format' => ExportFormat::CSV, 'group' => TimeEntryAggregationType::Client, 'sub_group' => TimeEntryAggregationType::Project, 'history_group' => TimeEntryAggregationTypeInterval::Month, 'start' => Carbon::now()->startOfYear()->toIso8601ZuluString(), 'end' => Carbon::now()->endOfYear()->toIso8601ZuluString(), 'member_id' => $data->member->getKey(), ])); // Assert $this->assertResponseCode($response, 200); } public function test_aggregate_export_endpoints_can_create_a_xlsx_report(): void { // Arrange $data = $this->createUserWithPermission([ 'time-entries:view:all', ]); $client = Client::factory()->forOrganization($data->organization)->create(); $project = Project::factory()->forOrganization($data->organization)->forClient($client)->create(); $timeEntry1 = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)->startWithDuration(Carbon::now(), 100)->create(); $timeEntry2 = TimeEntry::factory()->forOrganization($data->organization)->forProject($project)->forMember($data->member)->startWithDuration(Carbon::now(), 100)->create(); Passport::actingAs($data->user); // Act $response = $this->getJson(route('api.v1.time-entries.aggregate-export', [ $data->organization->getKey(), 'format' => ExportFormat::XLSX, 'group' => TimeEntryAggregationType::Client, 'sub_group' => TimeEntryAggregationType::Project, 'history_group' => TimeEntryAggregationTypeInterval::Month, 'start' => Carbon::now()->startOfYear()->toIso8601ZuluString(), 'end' => Carbon::now()->endOfYear()->toIso8601ZuluString(), ])); // Assert $this->assertResponseCode($response, 200); } public function test_aggregate_export_endpoints_can_create_a_xlsx_report_as_employee_role_with_show_billable_rate(): void { // Arrange $data = $this->createUserWithRole(Role::Employee, true); $client = Client::factory()->forOrganization($data->organization)->create(); $project = Project::factory()->forOrganization($data->organization)->forClient($client)->create(); $timeEntry1 = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)->startWithDuration(Carbon::now(), 100)->create(); $timeEntry2 = TimeEntry::factory()->forOrganization($data->organization)->forProject($project)->forMember($data->member)->startWithDuration(Carbon::now(), 100)->create(); Passport::actingAs($data->user); // Act $response = $this->getJson(route('api.v1.time-entries.aggregate-export', [ $data->organization->getKey(), 'format' => ExportFormat::XLSX, 'group' => TimeEntryAggregationType::Client, 'sub_group' => TimeEntryAggregationType::Project, 'history_group' => TimeEntryAggregationTypeInterval::Month, 'start' => Carbon::now()->startOfYear()->toIso8601ZuluString(), 'end' => Carbon::now()->endOfYear()->toIso8601ZuluString(), 'member_id' => $data->member->getKey(), ])); // Assert $this->assertResponseCode($response, 200); } public function test_aggregate_export_endpoints_can_create_a_xlsx_report_as_employee_role_without_show_billable_rate(): void { // Arrange $data = $this->createUserWithRole(Role::Employee, false); $client = Client::factory()->forOrganization($data->organization)->create(); $project = Project::factory()->forOrganization($data->organization)->forClient($client)->create(); $timeEntry1 = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)->startWithDuration(Carbon::now(), 100)->create(); $timeEntry2 = TimeEntry::factory()->forOrganization($data->organization)->forProject($project)->forMember($data->member)->startWithDuration(Carbon::now(), 100)->create(); Passport::actingAs($data->user); // Act $response = $this->getJson(route('api.v1.time-entries.aggregate-export', [ $data->organization->getKey(), 'format' => ExportFormat::XLSX, 'group' => TimeEntryAggregationType::Client, 'sub_group' => TimeEntryAggregationType::Project, 'history_group' => TimeEntryAggregationTypeInterval::Month, 'start' => Carbon::now()->startOfYear()->toIso8601ZuluString(), 'end' => Carbon::now()->endOfYear()->toIso8601ZuluString(), 'member_id' => $data->member->getKey(), ])); // Assert $this->assertResponseCode($response, 200); } public function test_aggregate_export_endpoints_can_create_a_ods_report(): void { // Arrange $data = $this->createUserWithPermission([ 'time-entries:view:all', ]); $client = Client::factory()->forOrganization($data->organization)->create(); $project = Project::factory()->forOrganization($data->organization)->forClient($client)->create(); $timeEntry1 = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)->startWithDuration(Carbon::now(), 100)->create(); $timeEntry2 = TimeEntry::factory()->forOrganization($data->organization)->forProject($project)->forMember($data->member)->startWithDuration(Carbon::now(), 100)->create(); Passport::actingAs($data->user); // Act $response = $this->getJson(route('api.v1.time-entries.aggregate-export', [ $data->organization->getKey(), 'format' => ExportFormat::ODS, 'group' => TimeEntryAggregationType::User, 'sub_group' => TimeEntryAggregationType::Project, 'history_group' => TimeEntryAggregationTypeInterval::Month, 'start' => Carbon::now()->startOfYear()->toIso8601ZuluString(), 'end' => Carbon::now()->endOfYear()->toIso8601ZuluString(), ])); // Assert $this->assertResponseCode($response, 200); } public function test_aggregate_export_endpoints_can_create_a_ods_report_as_employee_role_with_show_billable_rate(): void { // Arrange $data = $this->createUserWithRole(Role::Employee, true); $client = Client::factory()->forOrganization($data->organization)->create(); $project = Project::factory()->forOrganization($data->organization)->forClient($client)->create(); $timeEntry1 = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)->startWithDuration(Carbon::now(), 100)->create(); $timeEntry2 = TimeEntry::factory()->forOrganization($data->organization)->forProject($project)->forMember($data->member)->startWithDuration(Carbon::now(), 100)->create(); Passport::actingAs($data->user); // Act $response = $this->getJson(route('api.v1.time-entries.aggregate-export', [ $data->organization->getKey(), 'format' => ExportFormat::ODS, 'group' => TimeEntryAggregationType::User, 'sub_group' => TimeEntryAggregationType::Project, 'history_group' => TimeEntryAggregationTypeInterval::Month, 'start' => Carbon::now()->startOfYear()->toIso8601ZuluString(), 'end' => Carbon::now()->endOfYear()->toIso8601ZuluString(), 'member_id' => $data->member->getKey(), ])); // Assert $this->assertResponseCode($response, 200); } public function test_aggregate_export_endpoints_can_create_a_ods_report_as_employee_role_without_show_billable_rate(): void { // Arrange $data = $this->createUserWithRole(Role::Employee, false); $client = Client::factory()->forOrganization($data->organization)->create(); $project = Project::factory()->forOrganization($data->organization)->forClient($client)->create(); $timeEntry1 = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)->startWithDuration(Carbon::now(), 100)->create(); $timeEntry2 = TimeEntry::factory()->forOrganization($data->organization)->forProject($project)->forMember($data->member)->startWithDuration(Carbon::now(), 100)->create(); Passport::actingAs($data->user); // Act $response = $this->getJson(route('api.v1.time-entries.aggregate-export', [ $data->organization->getKey(), 'format' => ExportFormat::ODS, 'group' => TimeEntryAggregationType::User, 'sub_group' => TimeEntryAggregationType::Project, 'history_group' => TimeEntryAggregationTypeInterval::Month, 'start' => Carbon::now()->startOfYear()->toIso8601ZuluString(), 'end' => Carbon::now()->endOfYear()->toIso8601ZuluString(), 'member_id' => $data->member->getKey(), ])); // Assert $this->assertResponseCode($response, 200); } public function test_aggregate_export_endpoint_fails_if_pdf_renderer_is_not_configured_but_a_user_want_a_pdf_report(): void { // Arrange $data = $this->createUserWithPermission([ 'time-entries:view:all', ]); Passport::actingAs($data->user); $this->actAsOrganizationWithSubscription(); Config::set('services.gotenberg.url', null); // Act $response = $this->getJson(route('api.v1.time-entries.aggregate-export', [ $data->organization->getKey(), 'format' => ExportFormat::PDF, 'group' => TimeEntryAggregationType::User, 'sub_group' => TimeEntryAggregationType::Project, 'history_group' => TimeEntryAggregationTypeInterval::Month, 'start' => Carbon::now()->startOfYear()->toIso8601ZuluString(), 'end' => Carbon::now()->endOfYear()->toIso8601ZuluString(), ])); // Assert $response->assertStatus(400); $response->assertExactJson([ 'error' => true, 'key' => 'pdf_renderer_is_not_configured', 'message' => 'PDF renderer is not configured', ]); } public function test_aggregate_export_endpoints_can_create_a_pdf_report(): void { // Arrange $data = $this->createUserWithPermission([ 'time-entries:view:all', ]); $client = Client::factory()->forOrganization($data->organization)->create(); $project = Project::factory()->forOrganization($data->organization)->forClient($client)->create(); $timeEntry1 = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)->startWithDuration(Carbon::now(), 100)->create(); $timeEntry2 = TimeEntry::factory()->forOrganization($data->organization)->forProject($project)->forMember($data->member)->startWithDuration(Carbon::now(), 100)->create(); Passport::actingAs($data->user); $this->actAsOrganizationWithSubscription(); // Act $response = $this->getJson(route('api.v1.time-entries.aggregate-export', [ $data->organization->getKey(), 'format' => ExportFormat::PDF, 'group' => TimeEntryAggregationType::User, 'sub_group' => TimeEntryAggregationType::Project, 'history_group' => TimeEntryAggregationTypeInterval::Month, 'start' => Carbon::now()->startOfYear()->toIso8601ZuluString(), 'end' => Carbon::now()->endOfYear()->toIso8601ZuluString(), ])); // Assert $this->assertResponseCode($response, 200); } public function test_aggregate_export_endpoints_can_create_a_pdf_report_as_employee_role_with_show_billable_rate(): void { // Arrange $data = $this->createUserWithRole(Role::Employee, true); $client = Client::factory()->forOrganization($data->organization)->create(); $project = Project::factory()->forOrganization($data->organization)->forClient($client)->create(); $timeEntry1 = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)->startWithDuration(Carbon::now(), 100)->create(); $timeEntry2 = TimeEntry::factory()->forOrganization($data->organization)->forProject($project)->forMember($data->member)->startWithDuration(Carbon::now(), 100)->create(); Passport::actingAs($data->user); $this->actAsOrganizationWithSubscription(); // Act $response = $this->getJson(route('api.v1.time-entries.aggregate-export', [ $data->organization->getKey(), 'format' => ExportFormat::PDF, 'group' => TimeEntryAggregationType::User, 'sub_group' => TimeEntryAggregationType::Project, 'history_group' => TimeEntryAggregationTypeInterval::Month, 'start' => Carbon::now()->startOfYear()->toIso8601ZuluString(), 'end' => Carbon::now()->endOfYear()->toIso8601ZuluString(), 'member_id' => $data->member->getKey(), ])); // Assert $this->assertResponseCode($response, 200); } public function test_aggregate_export_endpoints_can_create_a_pdf_report_as_employee_role_without_show_billable_rate(): void { // Arrange $data = $this->createUserWithRole(Role::Employee, false); $client = Client::factory()->forOrganization($data->organization)->create(); $project = Project::factory()->forOrganization($data->organization)->forClient($client)->create(); $timeEntry1 = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)->startWithDuration(Carbon::now(), 100)->create(); $timeEntry2 = TimeEntry::factory()->forOrganization($data->organization)->forProject($project)->forMember($data->member)->startWithDuration(Carbon::now(), 100)->create(); Passport::actingAs($data->user); $this->actAsOrganizationWithSubscription(); // Act $response = $this->getJson(route('api.v1.time-entries.aggregate-export', [ $data->organization->getKey(), 'format' => ExportFormat::PDF, 'group' => TimeEntryAggregationType::User, 'sub_group' => TimeEntryAggregationType::Project, 'history_group' => TimeEntryAggregationTypeInterval::Month, 'start' => Carbon::now()->startOfYear()->toIso8601ZuluString(), 'end' => Carbon::now()->endOfYear()->toIso8601ZuluString(), 'member_id' => $data->member->getKey(), ])); // Assert $this->assertResponseCode($response, 200); } public function test_aggregate_export_endpoints_can_create_a_pdf_report_grouped_by_date(): void { // Arrange $data = $this->createUserWithPermission([ 'time-entries:view:all', ]); $client = Client::factory()->forOrganization($data->organization)->create(); $project = Project::factory()->forOrganization($data->organization)->forClient($client)->create(); $timeEntry1 = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)->startWithDuration(Carbon::now(), 100)->create(); $timeEntry2 = TimeEntry::factory()->forOrganization($data->organization)->forProject($project)->forMember($data->member)->startWithDuration(Carbon::now(), 100)->create(); Passport::actingAs($data->user); $this->actAsOrganizationWithSubscription(); // Act $response = $this->getJson(route('api.v1.time-entries.aggregate-export', [ $data->organization->getKey(), 'format' => ExportFormat::PDF, 'group' => TimeEntryAggregationType::Day, 'sub_group' => TimeEntryAggregationType::Project, 'history_group' => TimeEntryAggregationTypeInterval::Day, 'start' => Carbon::now()->startOfYear()->toIso8601ZuluString(), 'end' => Carbon::now()->endOfYear()->toIso8601ZuluString(), ])); // Assert $this->assertResponseCode($response, 200); } public function test_aggregate_export_pdf_renders_date_group_labels_in_organization_date_format(): void { // Arrange $this->travelTo(Carbon::create(2024, 3, 15, 12, 0, 0, 'UTC')); $data = $this->createUserWithPermission([ 'time-entries:view:all', ]); // Note: the organization factory randomizes the date format, so pin it $data->organization->update(['date_format' => DateFormat::SlashSeparatedDDMMYYYY]); $project = Project::factory()->forOrganization($data->organization)->create(); TimeEntry::factory()->forOrganization($data->organization)->forProject($project)->forMember($data->member) ->startWithDuration(Carbon::now()->subDay(), 3600)->create(); Passport::actingAs($data->user); $this->actAsOrganizationWithSubscription(); // Act // Note: debug=true returns the rendered HTML instead of handing it to the PDF renderer. $response = $this->getJson(route('api.v1.time-entries.aggregate-export', [ $data->organization->getKey(), 'format' => ExportFormat::PDF, 'group' => TimeEntryAggregationType::Day, 'sub_group' => TimeEntryAggregationType::Project, 'history_group' => TimeEntryAggregationTypeInterval::Day, 'start' => Carbon::now()->subDays(7)->toIso8601ZuluString(), 'end' => Carbon::now()->toIso8601ZuluString(), 'debug' => 'true', ])); // Assert $this->assertResponseCode($response, 200); $html = $response->json('html'); $this->assertIsString($html); $this->assertStringContainsString('14/03/2024', $html); $this->assertStringNotContainsString('2024-03-14', $html); } public function test_aggregate_export_csv_renders_date_group_labels_in_organization_date_format(): void { // Arrange $this->travelTo(Carbon::create(2024, 3, 15, 12, 0, 0, 'UTC')); $data = $this->createUserWithPermission([ 'time-entries:view:all', ]); // Note: the organization factory randomizes the date format, so pin it $data->organization->update(['date_format' => DateFormat::SlashSeparatedDDMMYYYY]); $project = Project::factory()->forOrganization($data->organization)->create(); TimeEntry::factory()->forOrganization($data->organization)->forProject($project)->forMember($data->member) ->startWithDuration(Carbon::now()->subDay(), 3600)->create(); Passport::actingAs($data->user); // Act $response = $this->getJson(route('api.v1.time-entries.aggregate-export', [ $data->organization->getKey(), 'format' => ExportFormat::CSV, 'group' => TimeEntryAggregationType::Day, 'sub_group' => TimeEntryAggregationType::Project, 'history_group' => TimeEntryAggregationTypeInterval::Day, 'start' => Carbon::now()->subDays(7)->toIso8601ZuluString(), 'end' => Carbon::now()->toIso8601ZuluString(), ])); // Assert $this->assertResponseCode($response, 200); $disk = Storage::disk(config('filesystems.private')); $files = $disk->files('exports'); $this->assertCount(1, $files); $csv = $disk->get($files[0]); $this->assertIsString($csv); $this->assertStringContainsString('14/03/2024', $csv); $this->assertStringNotContainsString('2024-03-14', $csv); } /** * @return array{0: Organization, 1: Member, 2: User} */ private function createWeeksSpanningNewYear(): array { $data = $this->createUserWithPermission([ 'time-entries:view:all', ]); // Note: the organization factory randomizes the date format, so pin it $data->organization->update(['date_format' => DateFormat::SlashSeparatedDDMMYYYY]); $project = Project::factory()->forOrganization($data->organization)->create(); // Note: the user factory pins the week start to Monday, so these land in predictable buckets foreach (['2025-12-22', '2025-12-29', '2026-01-05'] as $day) { TimeEntry::factory()->forOrganization($data->organization)->forProject($project)->forMember($data->member) ->startWithDuration(Carbon::parse($day.' 09:00:00', 'UTC'), 3600)->create(); } return [$data->organization, $data->member, $data->user]; } public function test_aggregate_export_csv_labels_a_week_group_with_its_date_range(): void { // Arrange $this->travelTo(Carbon::create(2026, 1, 15, 12, 0, 0, 'UTC')); [$organization, , $user] = $this->createWeeksSpanningNewYear(); Passport::actingAs($user); // Act $response = $this->getJson(route('api.v1.time-entries.aggregate-export', [ $organization->getKey(), 'format' => ExportFormat::CSV, 'group' => TimeEntryAggregationType::Week, 'sub_group' => TimeEntryAggregationType::Project, 'history_group' => TimeEntryAggregationTypeInterval::Week, 'start' => Carbon::parse('2025-12-15 00:00:00', 'UTC')->toIso8601ZuluString(), 'end' => Carbon::parse('2026-01-15 23:59:59', 'UTC')->toIso8601ZuluString(), ])); // Assert $this->assertResponseCode($response, 200); $disk = Storage::disk(config('filesystems.private')); $files = $disk->files('exports'); $this->assertCount(1, $files); $csv = $disk->get($files[0]); $this->assertIsString($csv); $this->assertStringContainsString('22/12/2025 - 28/12/2025', $csv); $this->assertStringContainsString('29/12/2025 - 04/01/2026', $csv); $this->assertStringContainsString('05/01/2026 - 11/01/2026', $csv); $this->assertStringNotContainsString('2025-12-29', $csv); $this->assertStringNotContainsString('2025-12-22', $csv); } public function test_aggregate_export_endpoints_can_create_a_pdf_report_grouped_by_week_spanning_new_year(): void { // Arrange $this->travelTo(Carbon::create(2026, 1, 15, 12, 0, 0, 'UTC')); [$organization, , $user] = $this->createWeeksSpanningNewYear(); Passport::actingAs($user); $this->actAsOrganizationWithSubscription(); // Act // Note: a week 1 label carries a comma and reaches the echarts series in a