- Nueva InspectionTemplatesTable (DataTableComponent): columnas Plantilla,
Descripción, Campos, Proyectos (uso) y Acciones; filtros en cabecera por nombre,
descripción y uso (en uso / sin uso). Gateada por manage templates.
- GlobalTemplateManager: el listado HTML se sustituye por la tabla embebida.
Acciones Editar/Borrar viajan por eventos (template-edit/template-delete);
el manager emite templates-changed al guardar/borrar/importar para que la tabla
se refresque (#[On('templates-changed')]).
Tests: GlobalTemplatesTest amplía con tabla (render + permiso, evento edit). Suite 91 passing.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
139 lines
5.1 KiB
PHP
139 lines
5.1 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature;
|
|
|
|
use App\Livewire\Inspections\GlobalTemplateManager;
|
|
use App\Livewire\Inspections\InspectionTemplatesTable;
|
|
use App\Livewire\Projects\ProjectMap;
|
|
use App\Livewire\Projects\ProjectTemplatesPicker;
|
|
use App\Models\InspectionTemplate;
|
|
use App\Models\Layer;
|
|
use App\Models\Phase;
|
|
use App\Models\Project;
|
|
use App\Models\User;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Livewire\Livewire;
|
|
use Spatie\Permission\Models\Permission;
|
|
use Tests\TestCase;
|
|
|
|
class GlobalTemplatesTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
private function project(User $owner, string $ref = 'P'): Project
|
|
{
|
|
$p = Project::create([
|
|
'reference' => $ref, 'name' => 'P-' . $ref, 'address' => 'x', 'lat' => 40, 'lng' => -3,
|
|
'start_date' => now()->toDateString(), 'end_date_estimated' => now()->addMonth()->toDateString(),
|
|
'status' => 'in_progress', 'created_by' => $owner->id,
|
|
]);
|
|
$p->users()->attach($owner->id, ['role_in_project' => 'supervisor']);
|
|
return $p;
|
|
}
|
|
|
|
public function test_global_manager_requires_manage_templates(): void
|
|
{
|
|
Permission::findOrCreate('manage templates');
|
|
$user = User::factory()->create(); // sin permiso
|
|
Livewire::actingAs($user)
|
|
->test(GlobalTemplateManager::class)
|
|
->assertForbidden();
|
|
}
|
|
|
|
public function test_global_manager_creates_template_without_project(): void
|
|
{
|
|
Permission::findOrCreate('manage templates');
|
|
$admin = User::factory()->create();
|
|
$admin->givePermissionTo('manage templates');
|
|
|
|
Livewire::actingAs($admin)
|
|
->test(GlobalTemplateManager::class)
|
|
->call('newTemplate')
|
|
->set('form.name', 'Inspección obra')
|
|
->call('saveTemplate')
|
|
->assertHasNoErrors();
|
|
|
|
$this->assertDatabaseHas('inspection_templates', [
|
|
'name' => 'Inspección obra', 'project_id' => null,
|
|
]);
|
|
}
|
|
|
|
public function test_picker_toggles_project_assignment(): void
|
|
{
|
|
Permission::findOrCreate('edit projects');
|
|
$user = User::factory()->create();
|
|
$user->givePermissionTo('edit projects');
|
|
$project = $this->project($user);
|
|
$tpl = InspectionTemplate::create(['name' => 'Recep', 'fields' => []]);
|
|
|
|
Livewire::actingAs($user)
|
|
->test(ProjectTemplatesPicker::class, ['project' => $project])
|
|
->call('toggle', $tpl->id);
|
|
|
|
$this->assertTrue($project->fresh()->inspectionTemplates()->where('inspection_templates.id', $tpl->id)->exists());
|
|
|
|
// Toggle de nuevo → desasigna
|
|
Livewire::actingAs($user)
|
|
->test(ProjectTemplatesPicker::class, ['project' => $project])
|
|
->call('toggle', $tpl->id);
|
|
|
|
$this->assertFalse($project->fresh()->inspectionTemplates()->where('inspection_templates.id', $tpl->id)->exists());
|
|
}
|
|
|
|
public function test_templates_table_lists_and_requires_permission(): void
|
|
{
|
|
Permission::findOrCreate('manage templates');
|
|
InspectionTemplate::create(['name' => 'Recepción acero', 'fields' => []]);
|
|
|
|
// Sin permiso → 403
|
|
$weak = User::factory()->create();
|
|
Livewire::actingAs($weak)
|
|
->test(InspectionTemplatesTable::class)
|
|
->assertForbidden();
|
|
|
|
// Con permiso → lista
|
|
$admin = User::factory()->create();
|
|
$admin->givePermissionTo('manage templates');
|
|
Livewire::actingAs($admin)
|
|
->test(InspectionTemplatesTable::class)
|
|
->assertOk()
|
|
->assertSee('Recepción acero');
|
|
}
|
|
|
|
public function test_table_emits_edit_event_handled_by_manager(): void
|
|
{
|
|
Permission::findOrCreate('manage templates');
|
|
$admin = User::factory()->create();
|
|
$admin->givePermissionTo('manage templates');
|
|
$tpl = InspectionTemplate::create(['name' => 'Editame', 'fields' => []]);
|
|
|
|
// El manager escucha #[On('template-edit')] y abre el form
|
|
Livewire::actingAs($admin)
|
|
->test(GlobalTemplateManager::class)
|
|
->assertSet('showForm', false)
|
|
->call('editTemplate', $tpl->id) // simula el dispatch
|
|
->assertSet('showForm', true)
|
|
->assertSet('editingTemplate', $tpl->id);
|
|
}
|
|
|
|
public function test_map_template_selector_only_shows_assigned_templates(): void
|
|
{
|
|
$user = User::factory()->create();
|
|
$project = $this->project($user, 'M');
|
|
|
|
$assigned = InspectionTemplate::create(['name' => 'Asignada', 'fields' => []]);
|
|
$unassigned = InspectionTemplate::create(['name' => 'Otra', 'fields' => []]);
|
|
$project->inspectionTemplates()->attach($assigned->id);
|
|
|
|
// Necesita una phase/layer al menos
|
|
Phase::create(['project_id' => $project->id, 'name' => 'F', 'order' => 1, 'color' => '#000', 'progress_percent' => 0]);
|
|
|
|
$cmp = Livewire::actingAs($user)
|
|
->test(ProjectMap::class, ['project' => $project]);
|
|
|
|
$names = collect($cmp->get('templates'))->pluck('name')->all();
|
|
$this->assertContains('Asignada', $names);
|
|
$this->assertNotContains('Otra', $names);
|
|
}
|
|
}
|