2026-06-25 11:53:51 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Tests\Feature;
|
|
|
|
|
|
|
|
|
|
use App\Livewire\Inspections\TemplateManager;
|
|
|
|
|
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 TemplateCreationTest extends TestCase
|
|
|
|
|
{
|
|
|
|
|
use RefreshDatabase;
|
|
|
|
|
|
|
|
|
|
private function setup_(): array
|
|
|
|
|
{
|
|
|
|
|
Permission::findOrCreate('edit projects');
|
|
|
|
|
$user = User::factory()->create();
|
|
|
|
|
$user->givePermissionTo('edit projects');
|
|
|
|
|
$project = Project::create([
|
|
|
|
|
'reference' => 'TPL-2', 'name' => 'Proyecto TPL', 'address' => 'x',
|
|
|
|
|
'lat' => 40.0, 'lng' => -3.0, 'start_date' => now()->toDateString(),
|
|
|
|
|
'end_date_estimated' => now()->addMonth()->toDateString(),
|
|
|
|
|
'status' => 'in_progress', 'created_by' => $user->id,
|
|
|
|
|
]);
|
|
|
|
|
return [$user, $project];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function test_templates_page_embeds_the_manager_component(): void
|
|
|
|
|
{
|
|
|
|
|
[$user, $project] = $this->setup_();
|
|
|
|
|
|
|
|
|
|
$this->actingAs($user)
|
|
|
|
|
->get(route('projects.templates', $project))
|
|
|
|
|
->assertOk()
|
|
|
|
|
->assertSeeLivewire(TemplateManager::class);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function test_new_template_button_opens_form_and_saves(): void
|
|
|
|
|
{
|
|
|
|
|
[$user, $project] = $this->setup_();
|
|
|
|
|
|
|
|
|
|
Livewire::actingAs($user)
|
|
|
|
|
->test(TemplateManager::class, ['project' => $project])
|
|
|
|
|
->assertSet('showForm', false)
|
|
|
|
|
->call('newTemplate')
|
|
|
|
|
->assertSet('showForm', true)
|
|
|
|
|
->set('form.name', 'Recepción de hormigón')
|
|
|
|
|
->call('saveTemplate')
|
|
|
|
|
->assertHasNoErrors();
|
|
|
|
|
|
|
|
|
|
$this->assertDatabaseHas('inspection_templates', [
|
|
|
|
|
'project_id' => $project->id,
|
|
|
|
|
'name' => 'Recepción de hormigón',
|
|
|
|
|
]);
|
|
|
|
|
}
|
2026-06-25 16:45:21 +02:00
|
|
|
|
|
|
|
|
public function test_field_keeps_group_question_and_help(): void
|
|
|
|
|
{
|
|
|
|
|
[$user, $project] = $this->setup_();
|
|
|
|
|
|
|
|
|
|
Livewire::actingAs($user)
|
|
|
|
|
->test(TemplateManager::class, ['project' => $project])
|
|
|
|
|
->call('newTemplate')
|
|
|
|
|
->set('form.name', 'Con grupos')
|
|
|
|
|
->call('addField')
|
|
|
|
|
->set('form.fields.0.group', 'Geometría')
|
|
|
|
|
->set('form.fields.0.name', 'altura')
|
|
|
|
|
->set('form.fields.0.label', 'Altura (m)')
|
|
|
|
|
->set('form.fields.0.question', '¿Cumple la cota?')
|
|
|
|
|
->set('form.fields.0.help', 'Medir con láser')
|
|
|
|
|
->call('saveTemplate')
|
|
|
|
|
->assertHasNoErrors();
|
|
|
|
|
|
|
|
|
|
$tpl = \App\Models\InspectionTemplate::where('name', 'Con grupos')->first();
|
|
|
|
|
$this->assertSame('Geometría', $tpl->fields[0]['group']);
|
|
|
|
|
$this->assertSame('¿Cumple la cota?', $tpl->fields[0]['question']);
|
|
|
|
|
$this->assertSame('Medir con láser', $tpl->fields[0]['help']);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function test_global_template_has_no_project_and_shows_in_other_projects(): void
|
|
|
|
|
{
|
|
|
|
|
[$user, $project] = $this->setup_();
|
|
|
|
|
|
|
|
|
|
Livewire::actingAs($user)
|
|
|
|
|
->test(TemplateManager::class, ['project' => $project])
|
|
|
|
|
->call('newTemplate')
|
|
|
|
|
->set('form.name', 'Plantilla global')
|
|
|
|
|
->set('form.is_global', true)
|
|
|
|
|
->call('saveTemplate')
|
|
|
|
|
->assertHasNoErrors();
|
|
|
|
|
|
|
|
|
|
$this->assertDatabaseHas('inspection_templates', [
|
|
|
|
|
'name' => 'Plantilla global', 'project_id' => null,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
// Otro proyecto del mismo usuario ve la plantilla global en su listado
|
|
|
|
|
$other = \App\Models\Project::create([
|
|
|
|
|
'reference' => 'OTH', 'name' => 'Otro', 'address' => 'x', 'lat' => 40, 'lng' => -3,
|
|
|
|
|
'start_date' => now()->toDateString(), 'end_date_estimated' => now()->addMonth()->toDateString(),
|
|
|
|
|
'status' => 'in_progress', 'created_by' => $user->id,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
Livewire::actingAs($user)
|
|
|
|
|
->test(TemplateManager::class, ['project' => $other])
|
|
|
|
|
->assertSee('Plantilla global');
|
|
|
|
|
}
|
2026-06-25 11:53:51 +02:00
|
|
|
}
|