Sustituye el modal (proyecto + checkboxes) por una tabla Rappasoft: - ImportTemplatesTable lista todas las plantillas de proyectos accesibles (excluye el destino), con filtros en cabecera (nombre, proyecto) y checkbox de selección (bulk). - Acción masiva "Importar seleccionadas" copia al proyecto destino (dedupe por nombre) y avisa a TemplateManager por evento (templates-imported) para refrescar/cerrar. - TemplateManager: eliminada la lógica vieja (importProjectId/importableTemplates/ selectedImportTemplateIds/importFromProject); el modal embebe la tabla. Tests: TemplateImportTest adaptado (selección+import, excluye destino). Suite 92 passing. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
92 lines
3.3 KiB
PHP
92 lines
3.3 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature;
|
|
|
|
use App\Livewire\Inspections\TemplateManager;
|
|
use App\Livewire\Projects\ImportTemplatesTable;
|
|
use App\Models\InspectionTemplate;
|
|
use App\Models\Project;
|
|
use App\Models\User;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Illuminate\Http\UploadedFile;
|
|
use Livewire\Livewire;
|
|
use Tests\TestCase;
|
|
|
|
class TemplateImportTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
private function project(User $member, string $ref): Project
|
|
{
|
|
$p = Project::create([
|
|
'reference' => $ref, 'name' => 'Proyecto ' . $ref, 'address' => 'x',
|
|
'lat' => 40.0, 'lng' => -3.0, 'start_date' => now()->toDateString(),
|
|
'end_date_estimated' => now()->addMonth()->toDateString(),
|
|
'status' => 'in_progress', 'created_by' => $member->id,
|
|
]);
|
|
$p->users()->attach($member->id, ['role_in_project' => 'supervisor']);
|
|
return $p;
|
|
}
|
|
|
|
public function test_import_templates_from_another_project(): void
|
|
{
|
|
$user = User::factory()->create();
|
|
$source = $this->project($user, 'SRC');
|
|
$target = $this->project($user, 'DST');
|
|
|
|
$tpl = InspectionTemplate::create([
|
|
'project_id' => $source->id, 'name' => 'Recepción acero',
|
|
'fields' => [['name' => 'ok', 'label' => 'OK', 'type' => 'boolean']],
|
|
]);
|
|
|
|
// Tabla Rappasoft: lista plantillas de otros proyectos, selección con checkbox.
|
|
Livewire::actingAs($user)
|
|
->test(ImportTemplatesTable::class, ['projectId' => $target->id])
|
|
->assertSee('Recepción acero')
|
|
->set('selected', [(string) $tpl->id])
|
|
->call('importSelected')
|
|
->assertDispatched('templates-imported');
|
|
|
|
$this->assertDatabaseHas('inspection_templates', [
|
|
'project_id' => $target->id, 'name' => 'Recepción acero',
|
|
]);
|
|
}
|
|
|
|
public function test_import_table_excludes_target_project_templates(): void
|
|
{
|
|
$user = User::factory()->create();
|
|
$target = $this->project($user, 'DST2');
|
|
InspectionTemplate::create(['project_id' => $target->id, 'name' => 'Propia del destino', 'fields' => []]);
|
|
|
|
Livewire::actingAs($user)
|
|
->test(ImportTemplatesTable::class, ['projectId' => $target->id])
|
|
->assertDontSee('Propia del destino');
|
|
}
|
|
|
|
public function test_import_template_from_csv(): void
|
|
{
|
|
$user = User::factory()->create();
|
|
$project = $this->project($user, 'CSV');
|
|
|
|
$csv = "name,label,type,required,options,min,max,step\n"
|
|
. "resistencia,Resistencia,integer,1,,,,\n"
|
|
. "acabado,Acabado,select,0,bueno;regular;malo,,,\n";
|
|
$file = UploadedFile::fake()->createWithContent('campos.csv', $csv);
|
|
|
|
Livewire::actingAs($user)
|
|
->test(TemplateManager::class, ['project' => $project])
|
|
->call('openImportFileModal')
|
|
->set('importTemplateName', 'Plantilla CSV')
|
|
->set('importFile', $file)
|
|
->call('parseImportFile')
|
|
->assertHasNoErrors()
|
|
->call('confirmImportFile');
|
|
|
|
$this->assertDatabaseHas('inspection_templates', [
|
|
'project_id' => $project->id, 'name' => 'Plantilla CSV',
|
|
]);
|
|
$tpl = InspectionTemplate::where('name', 'Plantilla CSV')->first();
|
|
$this->assertCount(2, $tpl->fields);
|
|
}
|
|
}
|