ajout images

This commit is contained in:
mrKamoo
2026-10-01 08:45:49 +02:00
parent e133c741b8
commit fb9e9ee54d
8 changed files with 304 additions and 0 deletions
+88
View File
@@ -0,0 +1,88 @@
<?php
namespace Tests\Feature;
use App\Models\Template;
use App\Models\Tenant;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\File;
use Tests\TestCase;
class AdminImagesTest extends TestCase
{
use RefreshDatabase;
private Tenant $tenant;
private string $dir;
// PNG 1×1 valide
private const PNG = 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=';
protected function setUp(): void
{
parent::setUp();
$this->dir = storage_path('framework/testing/img-'.uniqid());
config(['signature.admin_allowed_cidrs' => [], 'signature.image_path' => $this->dir]);
File::ensureDirectoryExists($this->dir);
$this->tenant = Tenant::create(['name' => 'CABM', 'entra_tenant_id' => '11111111-1111-1111-1111-111111111111']);
}
protected function tearDown(): void
{
File::deleteDirectory($this->dir);
parent::tearDown();
}
private function user(string $role): User
{
return User::create(['name' => $role, 'email' => "{$role}@x.fr", 'tenant_id' => $this->tenant->id, 'entra_oid' => '22222222-2222-2222-2222-222222222222', 'role' => $role]);
}
private function png(string $client = 'Logo CABM.png'): UploadedFile
{
$tmp = tempnam(sys_get_temp_dir(), 'png');
file_put_contents($tmp, base64_decode(self::PNG));
return new UploadedFile($tmp, $client, 'image/png', null, true);
}
public function test_editeur_depose_une_image_au_nom_nettoye_sans_ecraser(): void
{
$editor = $this->user('editor');
$this->actingAs($editor)->post('/admin/images', ['image' => $this->png()])->assertRedirect()->assertSessionHasNoErrors();
$this->actingAs($editor)->post('/admin/images', ['image' => $this->png()])->assertRedirect();
$this->assertFileExists($this->dir.'/logo-cabm.png');
$this->assertFileExists($this->dir.'/logo-cabm-2.png');
$this->actingAs($editor)->get('/admin/images')->assertOk()->assertSee('logo-cabm-2.png');
}
public function test_refuse_un_faux_png_et_le_svg(): void
{
$tmp = tempnam(sys_get_temp_dir(), 'x');
file_put_contents($tmp, '<svg xmlns="http://www.w3.org/2000/svg"><script>alert(1)</script></svg>');
$this->actingAs($this->user('editor'))
->post('/admin/images', ['image' => new UploadedFile($tmp, 'logo.png', 'image/png', null, true)])
->assertSessionHasErrors('image');
$this->assertSame([], File::files($this->dir));
}
public function test_suppression_reservee_admin_et_bloquee_si_utilisee(): void
{
File::ensureDirectoryExists($this->dir);
file_put_contents($this->dir.'/logo.png', base64_decode(self::PNG));
$this->actingAs($this->user('editor'))->delete('/admin/images/logo.png')->assertForbidden();
$admin = User::create(['name' => 'a', 'email' => 'a@x.fr', 'tenant_id' => $this->tenant->id, 'entra_oid' => '33333333-3333-3333-3333-333333333333', 'role' => 'admin']);
$t = Template::create(['tenant_id' => $this->tenant->id, 'name' => 'Défaut', 'draft_html_full' => '<img src="{{site_url}}/img/logo.png">']);
$this->actingAs($admin)->delete('/admin/images/logo.png')->assertSessionHasErrors('image');
$this->assertFileExists($this->dir.'/logo.png');
$t->update(['draft_html_full' => 'texte']);
$this->actingAs($admin)->delete('/admin/images/logo.png')->assertSessionHasNoErrors();
$this->assertFileDoesNotExist($this->dir.'/logo.png');
}
}