feat(users): add event_id to users table and update related resources

This commit is contained in:
2026-10-01 08:56:35 -03:00
parent 6e1f0d731c
commit bb4495c6cc
6 changed files with 101 additions and 3 deletions

View File

@@ -17,6 +17,7 @@ class UserAuthorizationRelationsTest extends TestCase
$this->assertTrue(Schema::hasColumns('users', [
'rol_codigo',
'tenant_codigo',
'event_id',
]));
}
@@ -28,5 +29,7 @@ class UserAuthorizationRelationsTest extends TestCase
$this->assertNull($user->tenant_codigo);
$this->assertSame(RoleCode::User->value, $user->role->codigo);
$this->assertNull($user->tenant);
$this->assertNull($user->event_id);
$this->assertNull($user->event);
}
}

View File

@@ -0,0 +1,57 @@
<?php
namespace Tests\Feature\Migrations;
use App\Domains\Core\Administrator\Resources\AdministratorResource;
use App\Domains\Core\Auth\Models\User;
use App\Domains\Core\Auth\Resources\UserResource;
use Illuminate\Database\QueryException;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Tests\TestCase;
class AddEventIdToUsersTest extends TestCase
{
public function test_event_assignment_preserves_existing_users_and_restricts_event_deletion(): void
{
Schema::create('events', function (Blueprint $table): void {
$table->id();
});
Schema::create('users', function (Blueprint $table): void {
$table->id();
$table->string('tenant_codigo')->nullable();
$table->softDeletes();
$table->timestamps();
});
DB::table('users')->insert(['id' => 1, 'tenant_codigo' => 'legacy']);
DB::table('events')->insert(['id' => 42]);
$migration = require database_path('migrations/2026_10_01_000000_add_event_id_to_users_table.php');
$migration->up();
$legacy = User::query()->findOrFail(1);
$this->assertNull($legacy->event_id);
$this->assertNull($legacy->event);
$this->assertSame('legacy', $legacy->tenant_codigo);
$legacy->update(['event_id' => '42']);
$user = $legacy->fresh();
$this->assertSame(42, $user->event_id);
$this->assertSame(42, $user->event->id);
$this->assertSame(42, UserResource::make($user)->resolve(new Request)['event_id']);
$this->assertSame(42, AdministratorResource::make($user)->resolve(new Request)['event_id']);
try {
DB::table('events')->where('id', 42)->delete();
$this->fail('An assigned event must not be deleted.');
} catch (QueryException $exception) {
$this->assertStringContainsString('FOREIGN KEY', $exception->getMessage());
}
$migration->down();
$this->assertFalse(Schema::hasColumn('users', 'event_id'));
$this->assertSame('legacy', DB::table('users')->where('id', 1)->value('tenant_codigo'));
}
}