mirror of
https://github.com/solidtime-io/solidtime.git
synced 2026-10-07 21:33:18 +01:00
- Rename the audit actor columns user_type/user_id to actor_type/actor_id - Add owner_organization_id and owner_user_id to the audits table as foreign keys with cascade on delete, so that the audits of an organization or user are deleted together with it. The indexes and foreign keys are created without blocking writes on the large table. - Fill the owner columns for new audits via CustomAuditable. Organizations and users no longer record their own deletion audit, since it would reference the already deleted owner.
123 lines
4.1 KiB
PHP
123 lines
4.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Tests\Unit\Model;
|
|
|
|
use App\Models\Audit;
|
|
use App\Models\Member;
|
|
use App\Models\Organization;
|
|
use App\Models\Project;
|
|
use App\Models\ProjectMember;
|
|
use App\Models\TimeEntry;
|
|
use App\Models\User;
|
|
use PHPUnit\Framework\Attributes\CoversClass;
|
|
|
|
#[CoversClass(Audit::class)]
|
|
class AuditModelTest extends ModelTestAbstract
|
|
{
|
|
public function test_it_belongs_to_an_owner_organization(): void
|
|
{
|
|
// Arrange
|
|
$organization = Organization::factory()->create();
|
|
$audit = Audit::factory()->create([
|
|
'owner_organization_id' => $organization->getKey(),
|
|
]);
|
|
|
|
// Act
|
|
$audit->refresh();
|
|
$ownerOrganizationRel = $audit->ownerOrganization;
|
|
|
|
// Assert
|
|
$this->assertNotNull($ownerOrganizationRel);
|
|
$this->assertTrue($ownerOrganizationRel->is($organization));
|
|
}
|
|
|
|
public function test_it_belongs_to_an_owner_user(): void
|
|
{
|
|
// Arrange
|
|
$user = User::factory()->create();
|
|
$audit = Audit::factory()->create([
|
|
'owner_user_id' => $user->getKey(),
|
|
]);
|
|
|
|
// Act
|
|
$audit->refresh();
|
|
$ownerUserRel = $audit->ownerUser;
|
|
|
|
// Assert
|
|
$this->assertNotNull($ownerUserRel);
|
|
$this->assertTrue($ownerUserRel->is($user));
|
|
}
|
|
|
|
public function test_audits_of_models_with_organization_have_the_organization_as_owner(): void
|
|
{
|
|
// Arrange
|
|
$organization = Organization::factory()->create();
|
|
$member = Member::factory()->forOrganization($organization)->create();
|
|
|
|
// Act
|
|
$timeEntry = TimeEntry::factory()->forOrganization($organization)->forMember($member)->create();
|
|
|
|
// Assert
|
|
$audit = Audit::query()->where('auditable_id', $timeEntry->getKey())->sole();
|
|
$this->assertSame($organization->getKey(), $audit->owner_organization_id);
|
|
$this->assertNull($audit->owner_user_id);
|
|
}
|
|
|
|
public function test_audits_of_project_members_have_the_organization_of_the_project_as_owner(): void
|
|
{
|
|
// Arrange
|
|
$organization = Organization::factory()->create();
|
|
$project = Project::factory()->forOrganization($organization)->create();
|
|
$member = Member::factory()->forOrganization($organization)->create();
|
|
|
|
// Act
|
|
$projectMember = ProjectMember::factory()->forProject($project)->forMember($member)->create();
|
|
|
|
// Assert
|
|
$audit = Audit::query()->where('auditable_id', $projectMember->getKey())->sole();
|
|
$this->assertSame($organization->getKey(), $audit->owner_organization_id);
|
|
$this->assertNull($audit->owner_user_id);
|
|
}
|
|
|
|
public function test_audits_of_an_organization_have_the_organization_itself_as_owner(): void
|
|
{
|
|
// Act
|
|
$organization = Organization::factory()->create();
|
|
|
|
// Assert
|
|
$audit = Audit::query()->where('auditable_id', $organization->getKey())->sole();
|
|
$this->assertSame($organization->getKey(), $audit->owner_organization_id);
|
|
$this->assertNull($audit->owner_user_id);
|
|
}
|
|
|
|
public function test_audits_of_a_user_have_the_user_itself_as_owner(): void
|
|
{
|
|
// Act
|
|
$user = User::factory()->create();
|
|
|
|
// Assert
|
|
$audit = Audit::query()->where('auditable_id', $user->getKey())->sole();
|
|
$this->assertSame($user->getKey(), $audit->owner_user_id);
|
|
$this->assertNull($audit->owner_organization_id);
|
|
}
|
|
|
|
public function test_deleting_an_owner_deletes_its_audits_and_does_not_create_a_deletion_audit(): void
|
|
{
|
|
// Arrange
|
|
$user = User::factory()->create();
|
|
$organization = Organization::factory()->create();
|
|
$otherUser = User::factory()->create();
|
|
|
|
// Act
|
|
$user->delete();
|
|
$organization->delete();
|
|
|
|
// Assert
|
|
$this->assertSame(0, Audit::query()->where('auditable_id', $user->getKey())->count());
|
|
$this->assertSame(0, Audit::query()->where('auditable_id', $organization->getKey())->count());
|
|
$this->assertSame(1, Audit::query()->where('auditable_id', $otherUser->getKey())->count());
|
|
}
|
|
}
|