Files
AggloSign/backend/tests/Feature/AdminImagesTest.php
T

116 lines
5.0 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?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_extension_deduite_du_contenu_et_non_du_nom_envoye(): void
{
$this->actingAs($this->user('editor'))->post('/admin/images', ['image' => $this->png('photo.gif')])->assertSessionHasNoErrors();
$this->assertFileExists($this->dir.'/photo.png');
$this->assertFileDoesNotExist($this->dir.'/photo.gif');
}
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');
}
public function test_image_utilisee_par_une_campagne_ou_une_autre_collectivite_non_supprimable(): void
{
file_put_contents($this->dir.'/banniere.png', base64_decode(self::PNG));
$admin = $this->user('admin');
$other = Tenant::create(['name' => 'Commune', 'entra_tenant_id' => '99999999-9999-9999-9999-999999999999']);
$campaign = \App\Models\Campaign::create(['tenant_id' => $other->id, 'name' => 'Ailleurs', 'image_path' => 'banniere.png',
'alt_text' => 'x', 'starts_at' => now(), 'ends_at' => now()->addDay()]);
$this->actingAs($admin)->delete('/admin/images/banniere.png')->assertSessionHasErrors('image');
$this->assertFileExists($this->dir.'/banniere.png');
$campaign->update(['tenant_id' => $this->tenant->id]);
$this->actingAs($admin)->delete('/admin/images/banniere.png')->assertSessionHasErrors('image');
$campaign->delete();
$this->actingAs($admin)->delete('/admin/images/banniere.png')->assertSessionHasNoErrors();
$this->assertFileDoesNotExist($this->dir.'/banniere.png');
}
}