146 lines
4.9 KiB
PHP
146 lines
4.9 KiB
PHP
<?php
|
|
|
|
use App\Livewire\Gescon\RubricaUniversaleTable;
|
|
use App\Models\Persona;
|
|
use App\Models\PersonaUnitaRelazione;
|
|
use App\Models\RubricaUniversale;
|
|
use App\Models\User;
|
|
use App\Models\Stabile;
|
|
use App\Support\StabileContext;
|
|
use Illuminate\Support\Facades\Artisan;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Livewire\Livewire;
|
|
|
|
beforeEach(function () {
|
|
$user = User::first();
|
|
if (! $user) {
|
|
$user = User::factory()->create(['email' => 'cecilia.tordini@gmail.com']);
|
|
}
|
|
|
|
try {
|
|
\Spatie\Permission\Models\Role::firstOrCreate(['name' => 'amministratore', 'guard_name' => 'web']);
|
|
$user->assignRole('amministratore');
|
|
} catch (\Throwable $e) {}
|
|
|
|
$adminId = DB::table('amministratori')->insertGetId([
|
|
'user_id' => $user->id,
|
|
'nome' => 'Admin Test',
|
|
'cognome' => 'Test',
|
|
'codice_amministratore' => 'ADM00001',
|
|
'created_at' => now(),
|
|
'updated_at' => now(),
|
|
]);
|
|
|
|
$stabile = Stabile::firstOrCreate(['codice_stabile' => '0021'], [
|
|
'denominazione' => 'SUPERCONDOMINIO MILIZIE 3',
|
|
'amministratore_id' => $adminId,
|
|
'indirizzo' => 'Viale delle Milizie 3',
|
|
'cap' => '00192',
|
|
'citta' => 'Roma',
|
|
'provincia' => 'RM',
|
|
]);
|
|
if ($stabile->amministratore_id !== $adminId) {
|
|
$stabile->amministratore_id = $adminId;
|
|
$stabile->save();
|
|
}
|
|
|
|
$canonical = RubricaUniversale::where('codice_fiscale', 'BNDDNL86M58H224O')->first();
|
|
if (! $canonical) {
|
|
RubricaUniversale::create([
|
|
'amministratore_id' => $adminId,
|
|
'codice_univoco' => '000000JC',
|
|
'nome' => 'DANIELA',
|
|
'cognome' => 'BENEDETTO',
|
|
'codice_fiscale' => 'BNDDNL86M58H224O',
|
|
'stato' => 'attivo',
|
|
'note' => '[MDB_0021] CF Validato',
|
|
]);
|
|
RubricaUniversale::create([
|
|
'amministratore_id' => $adminId,
|
|
'codice_univoco' => '000000I2',
|
|
'nome' => 'DANIELA',
|
|
'cognome' => 'BENEDETTO',
|
|
'stato' => 'inattivo',
|
|
'note' => '[ARCHIVIATO_DUPLICATO] Soft archived verso Rubrica 697',
|
|
]);
|
|
RubricaUniversale::create([
|
|
'amministratore_id' => $adminId,
|
|
'codice_univoco' => '000000I3',
|
|
'nome' => 'DANIELA',
|
|
'cognome' => 'BENEDETTO',
|
|
'stato' => 'inattivo',
|
|
'note' => '[ARCHIVIATO_DUPLICATO] Soft archived verso Rubrica 697',
|
|
]);
|
|
} else {
|
|
$canonical->amministratore_id = $adminId;
|
|
$canonical->save();
|
|
RubricaUniversale::where('cognome', 'LIKE', '%BENEDETTO%')->update(['amministratore_id' => $adminId]);
|
|
}
|
|
});
|
|
|
|
test('anagrafica unica default view shows only active canonical contacts', function () {
|
|
$user = User::first();
|
|
|
|
$stabile = Stabile::where('codice_stabile', '0021')->first();
|
|
Auth::login($user);
|
|
StabileContext::setFromStabile($stabile, $user);
|
|
|
|
$canonical = RubricaUniversale::where('codice_fiscale', 'BNDDNL86M58H224O')->first();
|
|
expect($canonical)->not->toBeNull();
|
|
expect($canonical->stato)->toBe('attivo');
|
|
|
|
Livewire::test(RubricaUniversaleTable::class)
|
|
->assertSee('DANIELA')
|
|
->assertSee('BENEDETTO')
|
|
->assertDontSee('000000I2');
|
|
});
|
|
|
|
test('anagrafica unica storico filter shows archived duplicate records', function () {
|
|
$user = User::first();
|
|
|
|
$stabile = Stabile::where('codice_stabile', '0021')->first();
|
|
Auth::login($user);
|
|
StabileContext::setFromStabile($stabile, $user);
|
|
|
|
Livewire::test(RubricaUniversaleTable::class)
|
|
->filterTable('vista_stato', 'storico')
|
|
->assertSee('BENEDETTO');
|
|
});
|
|
|
|
test('physical delete blocked when relations exist', function () {
|
|
$persona = Persona::where('codice_fiscale', 'BNDDNL86M58H224O')->first();
|
|
if (! $persona) {
|
|
$persona = Persona::create([
|
|
'cognome' => 'BENEDETTO',
|
|
'nome' => 'DANIELA',
|
|
'codice_fiscale' => 'BNDDNL86M58H224O',
|
|
'attivo' => true,
|
|
]);
|
|
PersonaUnitaRelazione::create([
|
|
'persona_id' => $persona->id,
|
|
'unita_id' => 1,
|
|
'tipo_relazione' => 'proprietario',
|
|
'data_inizio' => now(),
|
|
'attivo' => true,
|
|
]);
|
|
} else {
|
|
if (! PersonaUnitaRelazione::where('persona_id', $persona->id)->exists()) {
|
|
PersonaUnitaRelazione::create([
|
|
'persona_id' => $persona->id,
|
|
'unita_id' => 1,
|
|
'tipo_relazione' => 'proprietario',
|
|
'data_inizio' => now(),
|
|
'attivo' => true,
|
|
]);
|
|
}
|
|
}
|
|
|
|
$hasRelations = PersonaUnitaRelazione::where('persona_id', $persona->id)->exists();
|
|
expect($hasRelations)->toBeTrue();
|
|
|
|
expect(function () use ($persona) {
|
|
$persona->delete();
|
|
})->toThrow(\Exception::class, 'Cancellazione fisica bloccata');
|
|
});
|