- Deleted the `vetro.json` file containing glass configurations. - Introduced `ImportaTitoliAction` to facilitate importing model titles from Excel. - Created `TitoliModelliImporter` for handling title import logic and updating existing models. - Updated `SistemaResource` to include the new title import action. - Modified `ConfigurazioneModello` to adjust the model name retrieval logic. - Changed `ListinoImporter` to make the alias constant public. - Added tests for title import functionality, ensuring correct updates and creation of new models.
111 lines
4.6 KiB
PHP
111 lines
4.6 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature;
|
|
|
|
use App\Filament\Resources\SistemaResource\Pages\ListSistemi;
|
|
use App\Models\Modello;
|
|
use App\Models\Sistema;
|
|
use App\Models\User;
|
|
use App\Support\Listini\TitoliModelliImporter;
|
|
use Database\Seeders\CatalogoSeeder;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Illuminate\Http\UploadedFile;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Livewire\Livewire;
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
|
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
|
|
use Tests\TestCase;
|
|
|
|
class ImportazioneTitoliTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
private Sistema $pvc;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
|
|
Storage::fake('public');
|
|
$this->seed(CatalogoSeeder::class);
|
|
$this->pvc = Sistema::where('codice', 'pvc')->firstOrFail();
|
|
$this->actingAs(User::factory()->create(['is_superadmin' => true]));
|
|
}
|
|
|
|
/** Stesso formato del foglio "Nomi": intestazione, riga vuota, poi codice e titolo. */
|
|
private function fileExcel(): UploadedFile
|
|
{
|
|
$spreadsheet = new Spreadsheet;
|
|
$spreadsheet->getActiveSheet()->fromArray([
|
|
['ORDINE', 'TITOLO', 'CERNIERE A SCOMPARSA'],
|
|
[null, null, null],
|
|
['finestra_1_anta', 'FINESTRA 1 ANTA, A/R ', 'SI'],
|
|
['finestra_wasistas', 'WASISTAS CON MARTELLINA', 'SI'],
|
|
['modello_nuovo', 'MODELLO NUOVO', 'NO'],
|
|
['bilico', null, 'NO'],
|
|
], null, 'A1', true);
|
|
$path = tempnam(sys_get_temp_dir(), 'titoli');
|
|
(new Xlsx($spreadsheet))->save($path);
|
|
|
|
return UploadedFile::fake()->createWithContent('titoli.xlsx', file_get_contents($path));
|
|
}
|
|
|
|
public function test_legge_codici_e_titoli_dal_file_di_riferimento(): void
|
|
{
|
|
$titoli = TitoliModelliImporter::leggi(base_path('../reference/OrdineSerramenti_Descrizioni_NuoveOpzioni.xlsx'));
|
|
|
|
$this->assertCount(28, $titoli);
|
|
$this->assertSame('FINESTRA 2 ANTE, A/R', $titoli['finestra_2_ante']);
|
|
$this->assertSame('TELAIO FISSO', $titoli['finestra_anta_fissa']);
|
|
}
|
|
|
|
public function test_aggiorna_solo_il_titolo_dei_modelli_esistenti(): void
|
|
{
|
|
$finestra = Modello::where('sistema_id', $this->pvc->id)->where('codice', 'finestra_1_anta')->firstOrFail();
|
|
$prima = $finestra->only(['nome', 'descrizione', 'attivo', 'ordine', 'moduli']);
|
|
|
|
$esito = TitoliModelliImporter::importa($this->pvc, [
|
|
'finestra_1_anta' => 'FINESTRA 1 ANTA, A/R',
|
|
'finestra_wasistas' => 'WASISTAS CON MARTELLINA',
|
|
]);
|
|
|
|
$finestra->refresh();
|
|
$this->assertSame('FINESTRA 1 ANTA, A/R', $finestra->titolo);
|
|
$this->assertSame($prima, $finestra->only(['nome', 'descrizione', 'attivo', 'ordine', 'moduli']));
|
|
// Codice con refuso nel file: aggiorna il modello tramite alias, senza crearne uno nuovo.
|
|
$this->assertSame('WASISTAS CON MARTELLINA', Modello::where('codice', 'finestra_vasistas')->value('titolo'));
|
|
$this->assertFalse(Modello::where('codice', 'finestra_wasistas')->exists());
|
|
$this->assertSame(['finestra_1_anta', 'finestra_vasistas'], $esito['aggiornati']);
|
|
$this->assertSame([], $esito['creati']);
|
|
|
|
$this->assertSame(2, TitoliModelliImporter::importa($this->pvc, [
|
|
'finestra_1_anta' => 'FINESTRA 1 ANTA, A/R',
|
|
'finestra_wasistas' => 'WASISTAS CON MARTELLINA',
|
|
])['invariati']);
|
|
}
|
|
|
|
public function test_un_codice_nuovo_crea_un_modello_disattivo(): void
|
|
{
|
|
$esito = TitoliModelliImporter::importa($this->pvc, ['modello_nuovo' => 'MODELLO NUOVO']);
|
|
|
|
$nuovo = Modello::where('sistema_id', $this->pvc->id)->where('codice', 'modello_nuovo')->firstOrFail();
|
|
$this->assertSame('MODELLO NUOVO', $nuovo->titolo);
|
|
$this->assertFalse($nuovo->attivo);
|
|
$this->assertSame(['modello_nuovo'], $esito['creati']);
|
|
}
|
|
|
|
public function test_l_azione_importa_i_titoli_dal_pannello(): void
|
|
{
|
|
Livewire::test(ListSistemi::class)
|
|
->callTableAction('importaTitoli', $this->pvc, data: ['file' => $this->fileExcel()])
|
|
->assertHasNoTableActionErrors()
|
|
->assertNotified('Titoli importati');
|
|
|
|
$this->assertSame('FINESTRA 1 ANTA, A/R', Modello::where('codice', 'finestra_1_anta')->value('titolo'));
|
|
$this->assertSame('WASISTAS CON MARTELLINA', Modello::where('codice', 'finestra_vasistas')->value('titolo'));
|
|
$this->assertFalse(Modello::where('codice', 'modello_nuovo')->value('attivo'));
|
|
// Riga senza titolo: ignorata.
|
|
$this->assertNotSame('', Modello::where('codice', 'bilico')->value('titolo'));
|
|
}
|
|
}
|