add break time entries and simplified time tracker ui

This commit is contained in:
Gregor Vostrak
2026-07-21 16:48:37 +02:00
parent 114a32536d
commit cbcd1e51f6
128 changed files with 6252 additions and 437 deletions

View File

@@ -353,6 +353,47 @@ class OrganizationEndpointTest extends ApiEndpointTestAbstract
]);
}
public function test_update_endpoint_can_update_the_setting_breaks_enabled(): void
{
// Arrange
$data = $this->createUserWithPermission([
'organizations:update',
]);
$data->organization->breaks_enabled = true;
$data->organization->save();
$this->assertBillableRateServiceIsUnused();
Passport::actingAs($data->user);
// Act
$response = $this->putJson(route('api.v1.organizations.update', [$data->organization->getKey()]), [
'breaks_enabled' => false,
]);
// Assert
$response->assertStatus(200);
$response->assertJsonPath('data.breaks_enabled', false);
$this->assertDatabaseHas(Organization::class, [
'id' => $data->organization->getKey(),
'breaks_enabled' => false,
]);
}
public function test_show_endpoint_returns_breaks_enabled_setting_for_members_with_role_employee(): void
{
// Arrange
$data = $this->createUserWithRole(Role::Employee);
$data->organization->breaks_enabled = false;
$data->organization->save();
Passport::actingAs($data->user);
// Act
$response = $this->getJson(route('api.v1.organizations.show', [$data->organization->getKey()]));
// Assert
$response->assertStatus(200);
$response->assertJsonPath('data.breaks_enabled', false);
}
public function test_update_endpoint_can_update_billable_rate_of_organization_and_update_time_entries(): void
{
// Arrange

View File

@@ -7,6 +7,7 @@ namespace Tests\Unit\Endpoint\Api\V1\Public;
use App\Enums\TagMatchType;
use App\Enums\TimeEntryAggregationType;
use App\Enums\TimeEntryAggregationTypeInterval;
use App\Enums\TimeEntryType;
use App\Enums\Weekday;
use App\Models\Client;
use App\Models\Organization;
@@ -669,6 +670,51 @@ class PublicReportEndpointTest extends ApiEndpointTestAbstract
]);
}
public function test_show_returns_only_entries_matching_the_time_entry_type_filter(): void
{
// Arrange
$organization = Organization::factory()->create();
// Work entry (should be excluded)
TimeEntry::factory()->forOrganization($organization)
->startWithDuration(now()->subDay(), 100)
->create();
// Break entry (should be included)
TimeEntry::factory()->forOrganization($organization)
->isBreak()
->startWithDuration(now()->subDay(), 200)
->create();
$reportDto = new ReportPropertiesDto;
$reportDto->start = now()->subDays(2);
$reportDto->end = now();
$reportDto->group = TimeEntryAggregationType::Project;
$reportDto->subGroup = TimeEntryAggregationType::Task;
$reportDto->historyGroup = TimeEntryAggregationTypeInterval::Day;
$reportDto->weekStart = Weekday::Monday;
$reportDto->timezone = 'Europe/Vienna';
$reportDto->timeEntryType = TimeEntryType::Break;
$report = Report::factory()->forOrganization($organization)->public()->create([
'public_until' => null,
'properties' => $reportDto,
]);
// Act
$response = $this->getJson(route('api.v1.public.reports.show'), [
'X-Api-Key' => $report->share_secret,
]);
// Assert
$response->assertOk();
$response->assertJson([
'data' => [
'seconds' => 200,
'cost' => 0,
'grouped_type' => TimeEntryAggregationType::Project->value,
],
]);
}
public function test_show_applies_not_contains_tag_match_type(): void
{
// Arrange

View File

@@ -7,6 +7,7 @@ namespace Tests\Unit\Endpoint\Api\V1;
use App\Enums\TagMatchType;
use App\Enums\TimeEntryAggregationType;
use App\Enums\TimeEntryRoundingType;
use App\Enums\TimeEntryType;
use App\Enums\Weekday;
use App\Http\Controllers\Api\V1\ReportController;
use App\Models\Client;
@@ -224,6 +225,66 @@ class ReportEndpointTest extends ApiEndpointTestAbstract
$this->assertSame(15, $report->properties->roundingMinutes);
}
public function test_store_endpoint_creates_new_report_with_time_entry_type_filter(): void
{
// Arrange
$data = $this->createUserWithPermission([
'reports:create',
]);
Passport::actingAs($data->user);
// Act
$response = $this->withoutExceptionHandling()->postJson(route('api.v1.reports.store', [$data->organization->getKey()]), [
'name' => 'Test Report',
'is_public' => false,
'properties' => [
'group' => TimeEntryAggregationType::Project->value,
'sub_group' => TimeEntryAggregationType::Task->value,
'history_group' => TimeEntryAggregationType::Day->value,
'start' => Carbon::now()->subDays(30)->toIso8601ZuluString(),
'end' => Carbon::now()->toIso8601ZuluString(),
'time_entry_type' => TimeEntryType::Break->value,
],
]);
// Assert
$response->assertStatus(201);
$response->assertJsonPath(
'data.properties.time_entry_type',
TimeEntryType::Break->value
);
/** @var Report $report */
$report = Report::query()->findOrFail($response->json('data.id'));
$this->assertSame(TimeEntryType::Break, $report->properties->timeEntryType);
}
public function test_store_endpoint_fails_if_time_entry_type_is_invalid(): void
{
// Arrange
$data = $this->createUserWithPermission([
'reports:create',
]);
Passport::actingAs($data->user);
// Act
$response = $this->postJson(route('api.v1.reports.store', [$data->organization->getKey()]), [
'name' => 'Test Report',
'is_public' => false,
'properties' => [
'group' => TimeEntryAggregationType::Project->value,
'sub_group' => TimeEntryAggregationType::Task->value,
'history_group' => TimeEntryAggregationType::Day->value,
'start' => Carbon::now()->subDays(30)->toIso8601ZuluString(),
'end' => Carbon::now()->toIso8601ZuluString(),
'time_entry_type' => 'invalid-type',
],
]);
// Assert
$response->assertStatus(422);
$response->assertJsonValidationErrors(['properties.time_entry_type']);
}
public function test_update_endpoint_fails_if_user_has_no_permission_to_update_report(): void
{
// Arrange

View File

@@ -4750,4 +4750,672 @@ class TimeEntryEndpointTest extends ApiEndpointTestAbstract
$this->assertResponseCode($response, 200);
$response->assertJsonPath('data.seconds', 200);
}
public function test_index_endpoint_can_filter_by_type(): void
{
// Arrange
$data = $this->createUserWithPermission([
'time-entries:view:own',
]);
$regularTimeEntry = TimeEntry::factory()
->forOrganization($data->organization)
->forMember($data->member)
->create();
$breakTimeEntry = TimeEntry::factory()
->forOrganization($data->organization)
->forMember($data->member)
->isBreak()
->create();
Passport::actingAs($data->user);
// Act
$response = $this->getJson(route('api.v1.time-entries.index', [
$data->organization->getKey(),
'member_id' => $data->member->getKey(),
'type' => 'break',
]));
// Assert
$response->assertStatus(200);
$response->assertJsonCount(1, 'data');
$response->assertJsonPath('data.0.id', $breakTimeEntry->getKey());
$response->assertJsonPath('data.0.type', 'break');
}
public function test_aggregate_endpoint_can_group_by_type(): void
{
// Arrange
$data = $this->createUserWithPermission([
'time-entries:view:all',
]);
TimeEntry::factory()
->forOrganization($data->organization)
->forMember($data->member)
->startWithDuration(Carbon::now()->subHours(3), 100)
->create();
TimeEntry::factory()
->forOrganization($data->organization)
->forMember($data->member)
->isBreak()
->startWithDuration(Carbon::now()->subHour(), 200)
->create();
Passport::actingAs($data->user);
// Act
$response = $this->getJson(route('api.v1.time-entries.aggregate', [
$data->organization->getKey(),
'group' => 'type',
'start' => Carbon::now()->subDay()->toIso8601ZuluString(),
'end' => Carbon::now()->addDay()->toIso8601ZuluString(),
]));
// Assert
$response->assertValid();
$this->assertResponseCode($response, 200);
$response->assertJsonPath('data.seconds', 300);
$groupedData = collect($response->json('data.grouped_data'));
$this->assertEqualsCanonicalizing(['work', 'break'], $groupedData->pluck('key')->all());
$this->assertSame(100, $groupedData->firstWhere('key', 'work')['seconds']);
$this->assertSame(200, $groupedData->firstWhere('key', 'break')['seconds']);
}
public function test_store_endpoint_creates_break_time_entry(): void
{
// Arrange
$data = $this->createUserWithPermission([
'time-entries:create:own',
]);
$data->organization->breaks_enabled = true;
$data->organization->save();
Passport::actingAs($data->user);
// Act
$response = $this->postJson(route('api.v1.time-entries.store', [$data->organization->getKey()]), [
'description' => 'Lunch',
'billable' => false,
'type' => 'break',
'start' => Carbon::now()->subHour()->toIso8601ZuluString(),
'end' => Carbon::now()->toIso8601ZuluString(),
'member_id' => $data->member->getKey(),
]);
// Assert
$response->assertStatus(201);
$response->assertJsonPath('data.type', 'break');
$this->assertDatabaseHas(TimeEntry::class, [
'id' => $response->json('data.id'),
'member_id' => $data->member->getKey(),
'type' => 'break',
'billable' => false,
'project_id' => null,
'task_id' => null,
]);
}
public function test_store_endpoint_defaults_to_work_type_if_type_is_missing(): void
{
// Arrange
$data = $this->createUserWithPermission([
'time-entries:create:own',
]);
Passport::actingAs($data->user);
// Act
$response = $this->postJson(route('api.v1.time-entries.store', [$data->organization->getKey()]), [
'billable' => false,
'start' => Carbon::now()->subHour()->toIso8601ZuluString(),
'member_id' => $data->member->getKey(),
]);
// Assert
$response->assertStatus(201);
$response->assertJsonPath('data.type', 'work');
$this->assertDatabaseHas(TimeEntry::class, [
'id' => $response->json('data.id'),
'type' => 'work',
]);
}
public function test_store_endpoint_rejects_null_type(): void
{
// Arrange
$data = $this->createUserWithPermission([
'time-entries:create:own',
]);
Passport::actingAs($data->user);
// Act
$response = $this->postJson(route('api.v1.time-entries.store', [$data->organization->getKey()]), [
'billable' => false,
'type' => null,
'start' => Carbon::now()->subHour()->toIso8601ZuluString(),
'member_id' => $data->member->getKey(),
]);
// Assert
$response->assertStatus(422);
$response->assertJsonValidationErrors(['type']);
}
public function test_store_endpoint_rejects_break_when_breaks_are_disabled_for_organization(): void
{
// Arrange
$data = $this->createUserWithPermission([
'time-entries:create:own',
]);
$data->organization->breaks_enabled = false;
$data->organization->save();
Passport::actingAs($data->user);
// Act
$response = $this->postJson(route('api.v1.time-entries.store', [$data->organization->getKey()]), [
'billable' => false,
'type' => 'break',
'start' => Carbon::now()->subHour()->toIso8601ZuluString(),
'end' => Carbon::now()->toIso8601ZuluString(),
'member_id' => $data->member->getKey(),
]);
// Assert
$response->assertStatus(422);
$response->assertJsonValidationErrors(['type']);
}
public function test_store_endpoint_fails_if_break_time_entry_has_project(): void
{
// Arrange
$data = $this->createUserWithPermission([
'time-entries:create:own',
'projects:view:all',
]);
$data->organization->breaks_enabled = true;
$data->organization->save();
$project = Project::factory()->forOrganization($data->organization)->create();
Passport::actingAs($data->user);
// Act
$response = $this->postJson(route('api.v1.time-entries.store', [$data->organization->getKey()]), [
'billable' => false,
'type' => 'break',
'start' => Carbon::now()->subHour()->toIso8601ZuluString(),
'member_id' => $data->member->getKey(),
'project_id' => $project->getKey(),
]);
// Assert
$response->assertStatus(422);
$response->assertJsonValidationErrors(['project_id']);
}
public function test_store_endpoint_fails_if_break_time_entry_is_billable(): void
{
// Arrange
$data = $this->createUserWithPermission([
'time-entries:create:own',
]);
$data->organization->breaks_enabled = true;
$data->organization->save();
Passport::actingAs($data->user);
// Act
$response = $this->postJson(route('api.v1.time-entries.store', [$data->organization->getKey()]), [
'billable' => true,
'type' => 'break',
'start' => Carbon::now()->subHour()->toIso8601ZuluString(),
'member_id' => $data->member->getKey(),
]);
// Assert
$response->assertStatus(422);
$response->assertJsonValidationErrors(['billable']);
}
public function test_update_endpoint_converting_time_entry_to_break_strips_project_task_tags_and_billable(): void
{
// Arrange
$data = $this->createUserWithPermission([
'time-entries:update:own',
]);
$data->organization->breaks_enabled = true;
$data->organization->save();
$timeEntry = TimeEntry::factory()
->forOrganization($data->organization)
->forMember($data->member)
->withTask($data->organization)
->withTags($data->organization)
->billable()
->create();
Passport::actingAs($data->user);
// Act
$response = $this->putJson(route('api.v1.time-entries.update', [$data->organization->getKey(), $timeEntry->getKey()]), [
'type' => 'break',
]);
// Assert
$response->assertStatus(200);
$response->assertJsonPath('data.type', 'break');
$response->assertJsonPath('data.tags', []);
$this->assertDatabaseHas(TimeEntry::class, [
'id' => $timeEntry->getKey(),
'type' => 'break',
'billable' => false,
'project_id' => null,
'task_id' => null,
'client_id' => null,
]);
}
public function test_update_endpoint_rejects_converting_to_break_when_breaks_are_disabled(): void
{
// Arrange
$data = $this->createUserWithPermission([
'time-entries:update:own',
]);
$data->organization->breaks_enabled = false;
$data->organization->save();
$timeEntry = TimeEntry::factory()
->forOrganization($data->organization)
->forMember($data->member)
->create();
Passport::actingAs($data->user);
// Act
$response = $this->putJson(route('api.v1.time-entries.update', [$data->organization->getKey(), $timeEntry->getKey()]), [
'type' => 'break',
]);
// Assert
$response->assertStatus(422);
$response->assertJsonValidationErrors(['type']);
$this->assertDatabaseHas(TimeEntry::class, [
'id' => $timeEntry->getKey(),
'type' => 'work',
]);
}
public function test_update_endpoint_allows_editing_existing_break_when_breaks_are_disabled(): void
{
// Arrange
$data = $this->createUserWithPermission([
'time-entries:update:own',
]);
$data->organization->breaks_enabled = false;
$data->organization->save();
$timeEntry = TimeEntry::factory()
->forOrganization($data->organization)
->forMember($data->member)
->isBreak()
->create();
Passport::actingAs($data->user);
// Act
$response = $this->putJson(route('api.v1.time-entries.update', [$data->organization->getKey(), $timeEntry->getKey()]), [
'type' => 'break',
'description' => 'Updated break',
]);
// Assert
$response->assertStatus(200);
$response->assertJsonPath('data.type', 'break');
$response->assertJsonPath('data.description', 'Updated break');
}
public function test_store_endpoint_fails_if_break_time_entry_has_tags(): void
{
// Arrange
$data = $this->createUserWithPermission([
'time-entries:create:own',
]);
$data->organization->breaks_enabled = true;
$data->organization->save();
$tag = Tag::factory()->forOrganization($data->organization)->create();
Passport::actingAs($data->user);
// Act
$response = $this->postJson(route('api.v1.time-entries.store', [$data->organization->getKey()]), [
'billable' => false,
'type' => 'break',
'start' => Carbon::now()->subHour()->toIso8601ZuluString(),
'member_id' => $data->member->getKey(),
'tags' => [$tag->getKey()],
]);
// Assert
$response->assertStatus(422);
$response->assertJsonValidationErrors(['tags']);
}
public function test_update_multiple_endpoint_rejects_tags_change_for_break_entries(): void
{
// Arrange
$data = $this->createUserWithPermission([
'time-entries:update:own',
]);
$tag = Tag::factory()->forOrganization($data->organization)->create();
$breakTimeEntry = TimeEntry::factory()
->forOrganization($data->organization)
->forMember($data->member)
->isBreak()
->create();
Passport::actingAs($data->user);
// Act
$response = $this->patchJson(route('api.v1.time-entries.update-multiple', [$data->organization->getKey()]), [
'ids' => [
$breakTimeEntry->getKey(),
],
'changes' => [
'tags' => [$tag->getKey()],
],
]);
// Assert
$response->assertStatus(200);
$this->assertEqualsCanonicalizing([$breakTimeEntry->getKey()], $response->json('error'));
}
public function test_update_endpoint_fails_if_break_time_entry_gets_project(): void
{
// Arrange
$data = $this->createUserWithPermission([
'time-entries:update:own',
'projects:view:all',
]);
$project = Project::factory()->forOrganization($data->organization)->create();
$timeEntry = TimeEntry::factory()
->forOrganization($data->organization)
->forMember($data->member)
->isBreak()
->create();
Passport::actingAs($data->user);
// Act
$response = $this->putJson(route('api.v1.time-entries.update', [$data->organization->getKey(), $timeEntry->getKey()]), [
'type' => 'break',
'project_id' => $project->getKey(),
]);
// Assert
$response->assertStatus(422);
$response->assertJsonValidationErrors(['project_id']);
}
public function test_update_endpoint_fails_if_break_time_entry_gets_project_or_billable_without_type_in_payload(): void
{
// Arrange
$data = $this->createUserWithPermission([
'time-entries:update:own',
'projects:view:all',
]);
$project = Project::factory()->forOrganization($data->organization)->create();
$timeEntry = TimeEntry::factory()
->forOrganization($data->organization)
->forMember($data->member)
->isBreak()
->create();
Passport::actingAs($data->user);
// Act
$response = $this->putJson(route('api.v1.time-entries.update', [$data->organization->getKey(), $timeEntry->getKey()]), [
'project_id' => $project->getKey(),
'billable' => true,
]);
// Assert
$response->assertStatus(422);
$response->assertJsonValidationErrors(['project_id', 'billable']);
$this->assertDatabaseHas(TimeEntry::class, [
'id' => $timeEntry->getKey(),
'project_id' => null,
'billable' => false,
]);
}
public function test_update_endpoint_converting_break_to_work_allows_assigning_project_afterwards(): void
{
// Arrange
$data = $this->createUserWithPermission([
'time-entries:update:own',
'projects:view:all',
]);
$project = Project::factory()->forOrganization($data->organization)->create();
$timeEntry = TimeEntry::factory()
->forOrganization($data->organization)
->forMember($data->member)
->isBreak()
->create();
Passport::actingAs($data->user);
// Act
$response = $this->putJson(route('api.v1.time-entries.update', [$data->organization->getKey(), $timeEntry->getKey()]), [
'type' => 'work',
'project_id' => $project->getKey(),
]);
// Assert
$response->assertStatus(200);
$this->assertDatabaseHas(TimeEntry::class, [
'id' => $timeEntry->getKey(),
'type' => 'work',
'project_id' => $project->getKey(),
]);
}
public function test_update_multiple_endpoint_rejects_project_change_for_break_entries_but_applies_it_to_work_entries(): void
{
// Arrange
$data = $this->createUserWithPermission([
'time-entries:update:own',
'projects:view:all',
]);
$project = Project::factory()->forOrganization($data->organization)->create();
$regularTimeEntry = TimeEntry::factory()
->forOrganization($data->organization)
->forMember($data->member)
->create();
$breakTimeEntry = TimeEntry::factory()
->forOrganization($data->organization)
->forMember($data->member)
->isBreak()
->create();
Passport::actingAs($data->user);
// Act
$response = $this->patchJson(route('api.v1.time-entries.update-multiple', [$data->organization->getKey()]), [
'ids' => [
$regularTimeEntry->getKey(),
$breakTimeEntry->getKey(),
],
'changes' => [
'project_id' => $project->getKey(),
],
]);
// Assert
$response->assertStatus(200);
$this->assertEqualsCanonicalizing([$regularTimeEntry->getKey()], $response->json('success'));
$this->assertEqualsCanonicalizing([$breakTimeEntry->getKey()], $response->json('error'));
$this->assertDatabaseHas(TimeEntry::class, [
'id' => $regularTimeEntry->getKey(),
'project_id' => $project->getKey(),
]);
$this->assertDatabaseHas(TimeEntry::class, [
'id' => $breakTimeEntry->getKey(),
'project_id' => null,
'type' => 'break',
]);
}
public function test_update_multiple_endpoint_rejects_billable_change_for_break_entries(): void
{
// Arrange
$data = $this->createUserWithPermission([
'time-entries:update:own',
]);
$breakTimeEntry = TimeEntry::factory()
->forOrganization($data->organization)
->forMember($data->member)
->isBreak()
->create();
Passport::actingAs($data->user);
// Act
$response = $this->patchJson(route('api.v1.time-entries.update-multiple', [$data->organization->getKey()]), [
'ids' => [
$breakTimeEntry->getKey(),
],
'changes' => [
'billable' => true,
],
]);
// Assert
$response->assertStatus(200);
$this->assertEqualsCanonicalizing([$breakTimeEntry->getKey()], $response->json('error'));
$this->assertDatabaseHas(TimeEntry::class, [
'id' => $breakTimeEntry->getKey(),
'billable' => false,
]);
}
public function test_update_multiple_endpoint_rejects_billable_change_for_break_entries_with_truthy_non_bool_value(): void
{
// Arrange
$data = $this->createUserWithPermission([
'time-entries:update:own',
]);
$breakTimeEntry = TimeEntry::factory()
->forOrganization($data->organization)
->forMember($data->member)
->isBreak()
->create();
Passport::actingAs($data->user);
// Act
$response = $this->patchJson(route('api.v1.time-entries.update-multiple', [$data->organization->getKey()]), [
'ids' => [
$breakTimeEntry->getKey(),
],
'changes' => [
'billable' => 1,
],
]);
// Assert
$response->assertStatus(200);
$this->assertEqualsCanonicalizing([$breakTimeEntry->getKey()], $response->json('error'));
$this->assertDatabaseHas(TimeEntry::class, [
'id' => $breakTimeEntry->getKey(),
'billable' => false,
]);
}
public function test_update_multiple_endpoint_converting_to_break_strips_project_and_billable(): void
{
// Arrange
$data = $this->createUserWithPermission([
'time-entries:update:own',
]);
$data->organization->breaks_enabled = true;
$data->organization->save();
$timeEntry = TimeEntry::factory()
->forOrganization($data->organization)
->forMember($data->member)
->withTask($data->organization)
->billable()
->create();
Passport::actingAs($data->user);
// Act
$response = $this->patchJson(route('api.v1.time-entries.update-multiple', [$data->organization->getKey()]), [
'ids' => [
$timeEntry->getKey(),
],
'changes' => [
'type' => 'break',
],
]);
// Assert
$response->assertStatus(200);
$this->assertEqualsCanonicalizing([$timeEntry->getKey()], $response->json('success'));
$this->assertDatabaseHas(TimeEntry::class, [
'id' => $timeEntry->getKey(),
'type' => 'break',
'billable' => false,
'project_id' => null,
'task_id' => null,
]);
}
public function test_update_multiple_endpoint_rejects_converting_to_break_when_breaks_are_disabled(): void
{
// Arrange
$data = $this->createUserWithPermission([
'time-entries:update:own',
]);
$data->organization->breaks_enabled = false;
$data->organization->save();
$timeEntry = TimeEntry::factory()
->forOrganization($data->organization)
->forMember($data->member)
->create();
Passport::actingAs($data->user);
// Act
$response = $this->patchJson(route('api.v1.time-entries.update-multiple', [$data->organization->getKey()]), [
'ids' => [
$timeEntry->getKey(),
],
'changes' => [
'type' => 'break',
],
]);
// Assert
$response->assertStatus(200);
$this->assertEqualsCanonicalizing([], $response->json('success'));
$this->assertEqualsCanonicalizing([$timeEntry->getKey()], $response->json('error'));
$this->assertDatabaseHas(TimeEntry::class, [
'id' => $timeEntry->getKey(),
'type' => 'work',
]);
}
public function test_update_endpoint_converting_a_billable_entry_to_a_break_clears_the_billable_rate(): void
{
// Arrange
// Regression: converting to a break cleared "billable" but left "billable_rate" set, so the
// break still contributed to cost aggregation (which sums billable_rate without checking billable).
$data = $this->createUserWithPermission([
'time-entries:update:own',
]);
$data->organization->breaks_enabled = true;
$data->organization->save();
$project = Project::factory()->forOrganization($data->organization)->billable(10000)->create();
$timeEntry = TimeEntry::factory()
->forOrganization($data->organization)
->forMember($data->member)
->forProject($project)
->billableRate(10000)
->create();
Passport::actingAs($data->user);
// Act
$response = $this->putJson(route('api.v1.time-entries.update', [$data->organization->getKey(), $timeEntry->getKey()]), [
'type' => 'break',
]);
// Assert
$response->assertStatus(200);
$response->assertJsonPath('data.type', 'break');
$response->assertJsonPath('data.billable', false);
$this->assertDatabaseHas(TimeEntry::class, [
'id' => $timeEntry->getKey(),
'type' => 'break',
'billable' => false,
'billable_rate' => null,
]);
}
}

View File

@@ -16,8 +16,10 @@ use App\Models\TimeEntry;
use App\Models\User;
use App\Service\Export\ExportService;
use Illuminate\Support\Facades\Storage;
use League\Csv\Reader;
use PHPUnit\Framework\Attributes\CoversClass;
use Tests\TestCaseWithDatabase;
use ZipArchive;
#[CoversClass(ExportService::class)]
class ExportServiceTest extends TestCaseWithDatabase
@@ -62,4 +64,32 @@ class ExportServiceTest extends TestCaseWithDatabase
// Assert
Storage::disk(config('filesystems.default'))->assertExists($zip);
}
public function test_export_includes_time_entry_type_in_time_entries_csv(): void
{
// Arrange
$this->mockPrivateStorage();
$user = User::factory()->create();
$organization = Organization::factory()->withOwner($user)->create();
$member = Member::factory()->forUser($user)->forOrganization($organization)->create();
$workEntry = TimeEntry::factory()->forMember($member)->create();
$breakEntry = TimeEntry::factory()->forMember($member)->isBreak()->create();
// Act
$exportService = app(ExportService::class);
$zip = $exportService->export($organization);
// Assert
$zipArchive = new ZipArchive;
$zipArchive->open(Storage::disk(config('filesystems.default'))->path($zip));
$timeEntriesCsv = $zipArchive->getFromName('time_entries.csv');
$zipArchive->close();
$this->assertNotFalse($timeEntriesCsv);
$reader = Reader::createFromString($timeEntriesCsv);
$reader->setHeaderOffset(0);
$this->assertContains('type', $reader->getHeader());
$typesById = collect($reader)->pluck('type', 'id');
$this->assertSame('work', $typesById[$workEntry->getKey()]);
$this->assertSame('break', $typesById[$breakEntry->getKey()]);
}
}

View File

@@ -4,7 +4,11 @@ declare(strict_types=1);
namespace Tests\Unit\Service\Import\Importers;
use App\Enums\TimeEntryType;
use App\Models\Client;
use App\Models\Organization;
use App\Models\Project;
use App\Models\Tag;
use App\Models\TimeEntry;
use App\Service\Import\Importers\ClockifyTimeEntriesImporter;
use App\Service\Import\Importers\DefaultImporter;
@@ -196,4 +200,35 @@ class ClockifyTimeEntriesImporterTest extends ImporterTestAbstract
}
$this->fail();
}
public function test_import_creates_break_time_entry_when_type_is_break(): void
{
// Arrange
// Clockify lets a break carry a project, task, tags and billable status, but those are
// meaningless for non-work time. The break must import stripped of all of them, and must
// NOT create the project/tag it referenced (which would be an orphan).
$organization = Organization::factory()->create();
$importer = new ClockifyTimeEntriesImporter;
$importer->init($organization);
$csv = <<<'CSV'
"Project","Client","Description","Task","User","Group","Email","Tags","Billable","Start Date","Start Time","End Date","End Time","Duration (h)","Duration (decimal)","Billable Rate (USD)","Billable Amount (USD)","Type"
"Break Project","Break Client","Lunch","Design","Peter Tester","","peter.test@email.test","Backend","Yes","03/04/2024","10:00:00 AM","03/04/2024","10:30:00 AM","00:30:00","0.50","0.00","0.00","Break"
CSV;
// Act
$importer->importData($csv, 'Europe/Vienna');
// Assert
$timeEntry = TimeEntry::query()->firstOrFail();
$this->assertSame(TimeEntryType::Break, $timeEntry->type);
$this->assertFalse($timeEntry->billable);
$this->assertNull($timeEntry->project_id);
$this->assertNull($timeEntry->task_id);
$this->assertNull($timeEntry->client_id);
$this->assertSame([], $timeEntry->tags);
// The break's project/tag/client must not have been created as orphans.
$this->assertSame(0, Project::query()->count());
$this->assertSame(0, Tag::query()->count());
$this->assertSame(0, Client::query()->count());
}
}

View File

@@ -4,9 +4,11 @@ declare(strict_types=1);
namespace Tests\Unit\Service\Import\Importers;
use App\Enums\TimeEntryType;
use App\Jobs\RecalculateSpentTimeForProject;
use App\Jobs\RecalculateSpentTimeForTask;
use App\Models\Organization;
use App\Models\TimeEntry;
use App\Service\Import\Importers\DefaultImporter;
use App\Service\Import\Importers\ImportException;
use App\Service\Import\Importers\SolidtimeImporter;
@@ -75,6 +77,44 @@ class SolidtimeImporterTest extends ImporterTestAbstract
Queue::assertPushed(RecalculateSpentTimeForTask::class, 1);
}
public function test_import_of_test_file_with_type_column_imports_breaks(): void
{
// Arrange
$zipPath = $this->createTestZip('solidtime_import_test_2');
$timezone = 'Europe/Vienna';
$organization = Organization::factory()->create();
$importer = new SolidtimeImporter;
$importer->init($organization);
$data = file_get_contents($zipPath);
Queue::fake([
RecalculateSpentTimeForProject::class,
RecalculateSpentTimeForTask::class,
]);
// Act
$importer->importData($data, $timezone);
$report = $importer->getReport();
// Assert
$this->assertSame(3, $report->timeEntriesCreated);
$timeEntries = TimeEntry::all();
$this->assertCount(3, $timeEntries);
// Empty type value falls back to the default type (work)
$timeEntryWithoutType = $timeEntries->firstWhere('description', '');
$this->assertNotNull($timeEntryWithoutType);
$this->assertSame(TimeEntryType::Work, $timeEntryWithoutType->type);
$workEntry = $timeEntries->firstWhere('description', 'Working hard');
$this->assertNotNull($workEntry);
$this->assertSame(TimeEntryType::Work, $workEntry->type);
$breakEntry = $timeEntries->firstWhere('description', 'Lunch break');
$this->assertNotNull($breakEntry);
$this->assertSame(TimeEntryType::Break, $breakEntry->type);
$this->assertFalse($breakEntry->billable);
$this->assertNull($breakEntry->project_id);
$this->assertNull($breakEntry->task_id);
$this->assertSame([], $breakEntry->tags);
}
public function test_import_of_test_file_twice_succeeds(): void
{
// Arrange