Files
construprogress/tests/Feature/GlobalTemplatesTest.php
T
javierandClaude Opus 4.7 f8a68312a3 refactor(templates): plantillas globales + asignación por proyecto (pivot)
- Migración: tabla pivot inspection_template_project; drop de phase_id (asociación
  a fase descartada); project_id se mantiene en inspection_templates por compat.
  Migra automáticamente las plantillas existentes (project_id → pivot).
- Modelos: Project::inspectionTemplates() ↔ InspectionTemplate::projects() (BTM);
  retirada la relación phase() de InspectionTemplate.
- GlobalTemplateManager (nuevo, ruta /inspection-templates, permiso manage templates):
  catálogo único global, CRUD + import CSV; sin asociación a fase ni a proyecto.
- ProjectTemplatesPicker (nuevo, ruta projects.templates, permiso edit projects):
  lista global con checkbox para asignar/desasignar plantillas al proyecto.
- ProjectMap: el selector de plantillas del mapa lee SOLO las asignadas al proyecto
  vía pivot. API móvil (bundle + /templates) adaptado al pivot.
- Eliminado el viejo TemplateManager por-proyecto, su vista wrapper y la tabla
  ImportTemplatesTable (ya no aplica: todas son globales).
- Acceso "Plantillas" añadido al menú principal (gate manage templates).

Tests: GlobalTemplatesTest (4). Suite 89 passing.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-06-25 17:26:12 +02:00

102 lines
3.7 KiB
PHP

<?php
namespace Tests\Feature;
use App\Livewire\Inspections\GlobalTemplateManager;
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_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);
}
}