Files
CABM-IT/tests/Feature/InfrastructureTest.php
mrKamooandClaude Sonnet 5 2e614f2929 Permet de déplacer schémas et dossiers par glisser-déposer
Reprend le mécanisme du module Documents (drag & drop + menu déroulant
"Déplacer vers"), scopé aux documents de type diagram pour rester isolé
du navigateur Documents.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-08-24 08:18:57 +02:00

223 lines
8.7 KiB
PHP

<?php
namespace Tests\Feature;
use App\Models\Location;
use App\Models\Organization;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Livewire;
use Tests\TestCase;
class InfrastructureTest extends TestCase
{
use RefreshDatabase;
public function test_infrastructure_index_page_renders_for_an_authenticated_user(): void
{
$user = User::factory()->create();
$org = Organization::create(['name' => 'Test Commune', 'slug' => 'test-commune', 'org_type' => 'client']);
$response = $this->actingAs($user)->get(route('organizations.infrastructure.index', $org));
$response->assertOk();
$response->assertSee('Infrastructure');
}
public function test_can_create_a_diagram_and_it_opens_in_the_editor(): void
{
$user = User::factory()->create();
$org = Organization::create(['name' => 'Test Commune', 'slug' => 'test-commune', 'org_type' => 'client']);
$this->actingAs($user);
Livewire::test('infrastructure.index', ['organization' => $org])
->set('newDiagramName', 'Schéma réseau - Mairie')
->call('createDiagram')
->assertHasNoErrors();
$this->assertDatabaseHas('documents', [
'organization_id' => $org->id,
'name' => 'Schéma réseau - Mairie',
'type' => 'diagram',
]);
$document = $org->documents()->where('type', 'diagram')->firstOrFail();
$response = $this->get(route('organizations.infrastructure.edit', [$org, $document]));
$response->assertOk();
$response->assertSee('Schéma réseau - Mairie');
}
public function test_saving_a_diagram_creates_a_new_version_only_when_content_changed(): void
{
$user = User::factory()->create();
$org = Organization::create(['name' => 'Test Commune', 'slug' => 'test-commune', 'org_type' => 'client']);
$document = $org->documents()->create([
'name' => 'Schéma test',
'type' => 'diagram',
'content' => '<mxfile>old</mxfile>',
'current_version' => 1,
'created_by' => $user->id,
]);
$document->versions()->create(['version' => 1, 'content' => '<mxfile>old</mxfile>', 'created_by' => $user->id]);
$this->actingAs($user);
Livewire::test('documents.diagram-editor', ['organization' => $org, 'document' => $document])
->call('saveDiagram', '<mxfile>new</mxfile>');
$document->refresh();
$this->assertSame('<mxfile>new</mxfile>', $document->content);
$this->assertSame(2, $document->current_version);
$this->assertCount(2, $document->versions);
}
public function test_diagrams_do_not_appear_in_the_documents_browser(): void
{
$user = User::factory()->create();
$org = Organization::create(['name' => 'Test Commune', 'slug' => 'test-commune', 'org_type' => 'client']);
$org->documents()->create([
'name' => 'Schéma réseau - Mairie',
'type' => 'diagram',
'content' => '<mxfile></mxfile>',
'current_version' => 1,
'created_by' => $user->id,
]);
$org->documents()->create([
'name' => 'Compte-rendu.md',
'type' => 'markdown',
'content' => '',
'current_version' => 1,
'created_by' => $user->id,
]);
$this->actingAs($user);
Livewire::test('documents.index', ['organization' => $org])
->assertDontSee('Schéma réseau - Mairie')
->assertSee('Compte-rendu.md');
}
public function test_can_create_a_folder_and_a_diagram_inside_it(): void
{
$user = User::factory()->create();
$org = Organization::create(['name' => 'Test Commune', 'slug' => 'test-commune', 'org_type' => 'client']);
$this->actingAs($user);
$component = Livewire::test('infrastructure.index', ['organization' => $org])
->set('newFolderName', 'Réseau')
->call('createFolder')
->assertHasNoErrors();
$folder = $org->documents()->where('type', 'diagram')->where('is_folder', true)->firstOrFail();
$this->assertSame('Réseau', $folder->name);
$component->set('folderId', $folder->id)
->set('newDiagramName', 'Schéma coeur de réseau')
->call('createDiagram')
->assertHasNoErrors();
$diagram = $org->documents()->where('type', 'diagram')->where('name', 'Schéma coeur de réseau')->firstOrFail();
$this->assertSame($folder->id, $diagram->folder_id);
// The folder itself must not leak into the general Documents browser.
Livewire::test('documents.index', ['organization' => $org])->assertDontSee('Réseau');
}
public function test_a_diagram_can_be_associated_with_a_site(): void
{
$user = User::factory()->create();
$org = Organization::create(['name' => 'Test Commune', 'slug' => 'test-commune', 'org_type' => 'client']);
$location = Location::create(['organization_id' => $org->id, 'name' => 'Mairie']);
$document = $org->documents()->create([
'name' => 'Schéma test',
'type' => 'diagram',
'content' => '<mxfile></mxfile>',
'current_version' => 1,
'created_by' => $user->id,
]);
$this->actingAs($user);
Livewire::test('documents.diagram-editor', ['organization' => $org, 'document' => $document])
->call('updateLocation', $location->id)
->assertHasNoErrors();
$this->assertSame($location->id, $document->refresh()->location_id);
Livewire::test('infrastructure.index', ['organization' => $org, 'locationId' => $location->id])
->assertSee('Schéma test');
$otherLocation = Location::create(['organization_id' => $org->id, 'name' => 'Annexe']);
Livewire::test('infrastructure.index', ['organization' => $org, 'locationId' => $otherLocation->id])
->assertDontSee('Schéma test');
}
public function test_can_move_a_diagram_into_a_folder_and_back_to_the_root(): void
{
$user = User::factory()->create();
$org = Organization::create(['name' => 'Test Commune', 'slug' => 'test-commune', 'org_type' => 'client']);
$folder = $org->documents()->create(['name' => 'Réseau', 'type' => 'diagram', 'is_folder' => true, 'created_by' => $user->id]);
$diagram = $org->documents()->create([
'name' => 'Schéma test', 'type' => 'diagram', 'content' => '<mxfile></mxfile>',
'current_version' => 1, 'created_by' => $user->id,
]);
$this->actingAs($user);
Livewire::test('infrastructure.index', ['organization' => $org])
->call('moveDocument', $diagram->id, $folder->id);
$this->assertSame($folder->id, $diagram->refresh()->folder_id);
Livewire::test('infrastructure.index', ['organization' => $org])
->call('moveDocument', $diagram->id, null);
$this->assertNull($diagram->refresh()->folder_id);
}
public function test_cannot_move_a_folder_into_its_own_descendant(): void
{
$user = User::factory()->create();
$org = Organization::create(['name' => 'Test Commune', 'slug' => 'test-commune', 'org_type' => 'client']);
$parent = $org->documents()->create(['name' => 'Parent', 'type' => 'diagram', 'is_folder' => true, 'created_by' => $user->id]);
$child = $org->documents()->create(['name' => 'Enfant', 'type' => 'diagram', 'is_folder' => true, 'folder_id' => $parent->id, 'created_by' => $user->id]);
$this->actingAs($user);
Livewire::test('infrastructure.index', ['organization' => $org])
->call('moveDocument', $parent->id, $child->id);
$this->assertNull($parent->refresh()->folder_id);
}
public function test_moving_an_item_is_scoped_to_diagram_type_documents(): void
{
$user = User::factory()->create();
$org = Organization::create(['name' => 'Test Commune', 'slug' => 'test-commune', 'org_type' => 'client']);
$regularFolder = $org->documents()->create(['name' => 'Dossier documents', 'is_folder' => true, 'created_by' => $user->id]);
$diagram = $org->documents()->create([
'name' => 'Schéma test', 'type' => 'diagram', 'content' => '<mxfile></mxfile>',
'current_version' => 1, 'created_by' => $user->id,
]);
$this->actingAs($user);
// A plain (non-diagram) folder must not be a valid drop target from Infrastructure.
Livewire::test('infrastructure.index', ['organization' => $org])
->call('moveDocument', $diagram->id, $regularFolder->id);
$this->assertNull($diagram->refresh()->folder_id);
}
}