Add spend_time to projects and tasks

This commit is contained in:
Constantin Graf
2024-09-18 22:01:55 +02:00
committed by Gregor Vostrak
parent 2e8da98287
commit bff766d363
14 changed files with 734 additions and 3 deletions

View File

@@ -7,6 +7,8 @@ namespace Tests\Unit\Endpoint\Api\V1;
use App\Enums\Role;
use App\Exceptions\Api\TimeEntryCanNotBeRestartedApiException;
use App\Http\Controllers\Api\V1\TimeEntryController;
use App\Jobs\RecalculateSpentTimeForProject;
use App\Jobs\RecalculateSpentTimeForTask;
use App\Models\Client;
use App\Models\Member;
use App\Models\Project;
@@ -16,6 +18,7 @@ use App\Models\TimeEntry;
use App\Models\User;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Queue;
use Illuminate\Support\Str;
use Illuminate\Testing\Fluent\AssertableJson;
use Laravel\Passport\Passport;
@@ -1050,6 +1053,45 @@ class TimeEntryEndpointTest extends ApiEndpointTestAbstract
]);
}
public function test_create_endpoint_recalculates_project_and_task_spent_time_if_time_entry_has_project_and_task(): void
{
// Arrange
$data = $this->createUserWithPermission([
'time-entries:create:own',
]);
$project = Project::factory()->forOrganization($data->organization)->create();
$task = Task::factory()->forOrganization($data->organization)->forProject($project)->create();
TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)->forProject($project)->forTask($task)->create();
$timeEntryFake = TimeEntry::factory()->forOrganization($data->organization)->make();
Passport::actingAs($data->user);
Queue::fake([
RecalculateSpentTimeForProject::class,
RecalculateSpentTimeForTask::class,
]);
// Act
$response = $this->postJson(route('api.v1.time-entries.store', [$data->organization->getKey()]), [
'description' => $timeEntryFake->description,
'billable' => $timeEntryFake->billable,
'start' => Carbon::now()->toIso8601ZuluString(),
'end' => Carbon::now()->addHour()->toIso8601ZuluString(),
'member_id' => $data->member->getKey(),
'project_id' => $project->getKey(),
'task_id' => $task->getKey(),
]);
// Assert
$response->assertStatus(201);
Queue::assertPushed(RecalculateSpentTimeForProject::class, 1);
Queue::assertPushed(RecalculateSpentTimeForTask::class, 1);
Queue::assertPushed(RecalculateSpentTimeForProject::class, function (RecalculateSpentTimeForProject $job) use ($project): bool {
return $job->project->is($project);
});
Queue::assertPushed(RecalculateSpentTimeForTask::class, function (RecalculateSpentTimeForTask $job) use ($task): bool {
return $job->task->is($task);
});
}
public function test_update_endpoint_fails_if_user_has_no_permission_to_update_own_time_entries(): void
{
// Arrange
@@ -1385,6 +1427,82 @@ class TimeEntryEndpointTest extends ApiEndpointTestAbstract
]);
}
public function test_update_endpoint_recalculates_project_and_task_spend_time_after_updating_time_entry_settings_a_project_and_a_task(): void
{
// Arrange
$data = $this->createUserWithPermission([
'time-entries:update:own',
]);
$project = Project::factory()->forOrganization($data->organization)->create();
$task = Task::factory()->forOrganization($data->organization)->forProject($project)->create();
$timeEntry = TimeEntry::factory()->forOrganization($data->organization)->forProject(null)->forTask(null)->forMember($data->member)->create();
TimeEntry::factory()->forOrganization($data->organization)->make();
Passport::actingAs($data->user);
Queue::fake([
RecalculateSpentTimeForProject::class,
RecalculateSpentTimeForTask::class,
]);
// Act
$response = $this->putJson(route('api.v1.time-entries.update', [$data->organization->getKey(), $timeEntry->getKey()]), [
'project_id' => $project->getKey(),
'task_id' => $task->getKey(),
]);
// Assert
$response->assertStatus(200);
Queue::assertPushed(RecalculateSpentTimeForProject::class, 1);
Queue::assertPushed(RecalculateSpentTimeForTask::class, 1);
Queue::assertPushed(function (RecalculateSpentTimeForProject $job) use ($project): bool {
return $job->project->is($project);
}, 1);
Queue::assertPushed(function (RecalculateSpentTimeForTask $job) use ($task): bool {
return $job->task->is($task);
}, 1);
}
public function test_update_endpoint_recalculates_project_and_task_spend_time_after_updating_time_entry_settings_a_new_project_and_a_new_task(): void
{
// Arrange
$data = $this->createUserWithPermission([
'time-entries:update:own',
]);
$oldProject = Project::factory()->forOrganization($data->organization)->create();
$oldTask = Task::factory()->forOrganization($data->organization)->forProject($oldProject)->create();
$timeEntry = TimeEntry::factory()->forOrganization($data->organization)->forProject($oldProject)->forTask($oldTask)->forMember($data->member)->create();
$project = Project::factory()->forOrganization($data->organization)->create();
$task = Task::factory()->forOrganization($data->organization)->forProject($project)->create();
TimeEntry::factory()->forOrganization($data->organization)->make();
Passport::actingAs($data->user);
Queue::fake([
RecalculateSpentTimeForProject::class,
RecalculateSpentTimeForTask::class,
]);
// Act
$response = $this->putJson(route('api.v1.time-entries.update', [$data->organization->getKey(), $timeEntry->getKey()]), [
'project_id' => $project->getKey(),
'task_id' => $task->getKey(),
]);
// Assert
$response->assertStatus(200);
Queue::assertPushed(RecalculateSpentTimeForProject::class, 2);
Queue::assertPushed(RecalculateSpentTimeForTask::class, 2);
Queue::assertPushed(function (RecalculateSpentTimeForProject $job) use ($project): bool {
return $job->project->is($project);
}, 1);
Queue::assertPushed(function (RecalculateSpentTimeForProject $job) use ($oldProject): bool {
return $job->project->is($oldProject);
}, 1);
Queue::assertPushed(function (RecalculateSpentTimeForTask $job) use ($task): bool {
return $job->task->is($task);
}, 1);
Queue::assertPushed(function (RecalculateSpentTimeForTask $job) use ($oldTask): bool {
return $job->task->is($oldTask);
}, 1);
}
public function test_destroy_endpoint_fails_if_user_tries_to_delete_time_entry_in_organization_that_they_does_belong_to(): void
{
// Arrange
@@ -1493,6 +1611,68 @@ class TimeEntryEndpointTest extends ApiEndpointTestAbstract
]);
}
public function test_destroy_endpoint_recalculates_project_and_task_spend_time_after_deleting_time_entry(): void
{
// Arrange
$data = $this->createUserWithPermission([
'time-entries:delete:own',
]);
$project = Project::factory()->forOrganization($data->organization)->create();
$task = Task::factory()->forOrganization($data->organization)->create();
$timeEntry = TimeEntry::factory()->forOrganization($data->organization)->forProject($project)->forTask($task)->forMember($data->member)->create();
$project = $timeEntry->project;
$task = $timeEntry->task;
Passport::actingAs($data->user);
Queue::fake([
RecalculateSpentTimeForProject::class,
RecalculateSpentTimeForTask::class,
]);
// Act
$response = $this->deleteJson(route('api.v1.time-entries.destroy', [$data->organization->getKey(), $timeEntry->getKey()]));
// Assert
$response->assertStatus(204);
$response->assertNoContent();
$this->assertDatabaseMissing(TimeEntry::class, [
'id' => $timeEntry->getKey(),
]);
Queue::assertPushed(RecalculateSpentTimeForProject::class, 1);
Queue::assertPushed(RecalculateSpentTimeForTask::class, 1);
Queue::assertPushed(RecalculateSpentTimeForProject::class, function (RecalculateSpentTimeForProject $job) use ($project) {
return $job->project->is($project);
});
Queue::assertPushed(RecalculateSpentTimeForTask::class, function (RecalculateSpentTimeForTask $job) use ($task) {
return $job->task->is($task);
});
}
public function test_destroy_endpoint_does_not_recalculate_project_and_task_spend_time_after_deleting_time_entry_if_time_entry_had_no_project_and_task(): void
{
// Arrange
$data = $this->createUserWithPermission([
'time-entries:delete:own',
]);
$timeEntry = TimeEntry::factory()->forOrganization($data->organization)->forProject(null)->forTask(null)->forMember($data->member)->create();
Passport::actingAs($data->user);
Queue::fake([
RecalculateSpentTimeForProject::class,
RecalculateSpentTimeForTask::class,
]);
// Act
$response = $this->deleteJson(route('api.v1.time-entries.destroy', [$data->organization->getKey(), $timeEntry->getKey()]));
// Assert
$response->assertStatus(204);
$response->assertNoContent();
$this->assertDatabaseMissing(TimeEntry::class, [
'id' => $timeEntry->getKey(),
]);
Queue::assertNotPushed(RecalculateSpentTimeForProject::class);
Queue::assertNotPushed(RecalculateSpentTimeForTask::class);
}
public function test_update_multiple_endpoint_fails_if_user_has_no_permission_to_update_own_time_entries_or_all_time_entries(): void
{
// Arrange

View File

@@ -0,0 +1,58 @@
<?php
declare(strict_types=1);
namespace Tests\Unit\Jobs;
use App\Jobs\RecalculateSpentTimeForProject;
use App\Models\Project;
use App\Models\TimeEntry;
use Illuminate\Support\Facades\DB;
use Tests\TestCaseWithDatabase;
class RecalculateSpentTimeForProjectTest extends TestCaseWithDatabase
{
public function test_recalculates_spent_time_for_project(): void
{
// Arrange
$project = Project::factory()->create([
'spent_time' => 0,
]);
TimeEntry::factory()->startWithDuration(now(), 10)->forProject($project)->create();
TimeEntry::factory()->startWithDuration(now(), 11)->forProject($project)->create();
$project->refresh();
$recalculateSpentTimeForProject = new RecalculateSpentTimeForProject($project);
DB::enableQueryLog();
// Act
$recalculateSpentTimeForProject->handle();
// Assert
self::assertCount(2, DB::getQueryLog());
$project->refresh();
self::assertEquals(21, $project->spent_time);
}
public function test_does_not_save_project_if_value_is_already_correct(): void
{
// Arrange
$project = Project::factory()->create([
'spent_time' => 21,
]);
TimeEntry::factory()->startWithDuration(now(), 10)->forProject($project)->create();
TimeEntry::factory()->startWithDuration(now(), 11)->forProject($project)->create();
$project->refresh();
$recalculateSpentTimeForProject = new RecalculateSpentTimeForProject($project);
DB::enableQueryLog();
// Act
$recalculateSpentTimeForProject->handle();
// Assert
self::assertCount(1, DB::getQueryLog());
$project->refresh();
self::assertEquals(21, $project->spent_time);
}
}

View File

@@ -0,0 +1,58 @@
<?php
declare(strict_types=1);
namespace Tests\Unit\Jobs;
use App\Jobs\RecalculateSpentTimeForTask;
use App\Models\Task;
use App\Models\TimeEntry;
use Illuminate\Support\Facades\DB;
use Tests\TestCaseWithDatabase;
class RecalculateSpentTimeForTaskTest extends TestCaseWithDatabase
{
public function test_recalculates_spent_time_for_task(): void
{
// Arrange
$task = Task::factory()->create([
'spent_time' => 0,
]);
TimeEntry::factory()->startWithDuration(now(), 10)->forTask($task)->create();
TimeEntry::factory()->startWithDuration(now(), 11)->forTask($task)->create();
$task->refresh();
$recalculateSpentTimeForTask = new RecalculateSpentTimeForTask($task);
DB::enableQueryLog();
// Act
$recalculateSpentTimeForTask->handle();
// Assert
self::assertCount(2, DB::getQueryLog());
$task->refresh();
self::assertEquals(21, $task->spent_time);
}
public function test_does_not_save_task_if_value_is_already_correct(): void
{
// Arrange
$task = Task::factory()->create([
'spent_time' => 21,
]);
TimeEntry::factory()->startWithDuration(now(), 10)->forTask($task)->create();
TimeEntry::factory()->startWithDuration(now(), 11)->forTask($task)->create();
$task->refresh();
$recalculateSpentTimeForTask = new RecalculateSpentTimeForTask($task);
DB::enableQueryLog();
// Act
$recalculateSpentTimeForTask->handle();
// Assert
self::assertCount(1, DB::getQueryLog());
$task->refresh();
self::assertEquals(21, $task->spent_time);
}
}

View File

@@ -0,0 +1,51 @@
<?php
declare(strict_types=1);
namespace Tests\Unit\Jobs\Test;
use App\Jobs\Test\TestJob;
use App\Models\User;
use Illuminate\Support\Facades\Log;
use Tests\TestCaseWithDatabase;
use TiMacDonald\Log\LogEntry;
class TestJobTest extends TestCaseWithDatabase
{
public function test_logs_debug_message(): void
{
// Arrange
$user = User::factory()->create();
$message = 'Test message';
$job = new TestJob($user, $message);
// Act
$job->handle();
// Assert
Log::assertLoggedTimes(fn (LogEntry $log) => $log->level === 'debug'
&& $log->message === 'TestJob: '.$message
&& $log->context['user'] === $user->getKey(),
1
);
}
public function test_can_fail_if_parameter_fail_is_true(): void
{
// Arrange
$user = User::factory()->create();
$message = 'Test message';
$job = new TestJob($user, $message, true);
// Act
try {
$job->handle();
} catch (\Exception $e) {
// Assert
$this->assertEquals('TestJob failed.', $e->getMessage());
return;
}
$this->fail('Expected exception not thrown');
}
}

View File

@@ -10,6 +10,8 @@ use App\Models\Organization;
use App\Models\Project;
use App\Models\ProjectMember;
use App\Models\Task;
use App\Models\TimeEntry;
use Illuminate\Support\Facades\DB;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\UsesClass;
@@ -117,6 +119,47 @@ class ProjectModelTest extends ModelTestAbstract
], $allProjects);
}
public function test_computed_spent_time_returns_the_sum_of_all_time_entries_excl_running_timers(): void
{
// Arrange
$project = Project::factory()->create();
$otherProject = Project::factory()->create();
TimeEntry::factory()->forProject($project)->startWithDuration(now(), 10)->create();
TimeEntry::factory()->forProject($project)->startWithDuration(now(), 10)->create();
TimeEntry::factory()->forProject($project)->startWithDuration(now(), 10)->create();
TimeEntry::factory()->forProject($otherProject)->startWithDuration(now(), 10)->create();
TimeEntry::factory()->forProject($otherProject)->start(now())->active()->create();
// Act
$project->refresh();
$spentTime = $project->getSpentTimeComputed();
// Assert
$this->assertEquals(30, $spentTime);
}
public function test_computed_spent_time_returns_already_computed_value_if_present(): void
{
// Arrange
$project = Project::factory()->create();
$otherProject = Project::factory()->create();
TimeEntry::factory()->forProject($project)->startWithDuration(now(), 10)->create();
TimeEntry::factory()->forProject($project)->startWithDuration(now(), 10)->create();
TimeEntry::factory()->forProject($project)->startWithDuration(now(), 10)->create();
TimeEntry::factory()->forProject($otherProject)->startWithDuration(now(), 10)->create();
TimeEntry::factory()->forProject($otherProject)->start(now())->active()->create();
$timeEntries = Project::query()
->withAggregate('timeEntries as spent_time_computed', DB::raw('extract(epoch from ("end" - start))'), 'sum')
->get();
// Act
$project->refresh();
$spentTime = $timeEntries->first()->getSpentTimeComputed();
// Assert
$this->assertEquals(30, $spentTime);
}
public function test_accessor_is_archived_is_true_if_archived_at_is_not_null(): void
{
// Arrange