This commit is contained in:
mrKamoo
2026-09-30 16:27:32 +02:00
commit 310724708f
147 changed files with 21327 additions and 0 deletions
+16
View File
@@ -0,0 +1,16 @@
<?php
namespace Tests\Unit;
use PHPUnit\Framework\TestCase;
class ExampleTest extends TestCase
{
/**
* A basic test example.
*/
public function test_that_true_is_true(): void
{
$this->assertTrue(true);
}
}
@@ -0,0 +1,54 @@
<?php
namespace Tests\Unit;
use App\Signature\TemplateRenderer;
use PHPUnit\Framework\TestCase;
class TemplateRendererTest extends TestCase
{
private TemplateRenderer $r;
protected function setUp(): void
{
$this->r = new TemplateRenderer;
}
public function test_substitue_et_echappe_les_variables(): void
{
$this->assertSame('Bonjour Jean &lt;b&gt;', $this->r->render('Bonjour {{prenom}}', ['prenom' => 'Jean <b>']));
}
public function test_valeur_brute_avec_triple_accolade(): void
{
$this->assertSame('<img src="x">', $this->r->render('{{{banniere}}}', ['banniere' => '<img src="x">']));
}
public function test_bloc_if_masque_si_vide(): void
{
$tpl = 'A{{#if mobile}} Mob. {{mobile}}{{/if}}B';
$this->assertSame('AB', $this->r->render($tpl, ['mobile' => ' ']));
$this->assertSame('A Mob. 06B', $this->r->render($tpl, ['mobile' => '06']));
}
public function test_blocs_imbriques_et_unless(): void
{
$tpl = '{{#if adresse}}{{adresse}}{{#if ville}}, {{ville}}{{/if}}{{/if}}{{#unless adresse}}-{{/unless}}';
$this->assertSame('1 rue X, Béziers', $this->r->render($tpl, ['adresse' => '1 rue X', 'ville' => 'Béziers']));
$this->assertSame('1 rue X', $this->r->render($tpl, ['adresse' => '1 rue X']));
$this->assertSame('-', $this->r->render($tpl, []));
}
public function test_variable_inconnue_vide_et_casse_ignoree(): void
{
$this->assertSame('[]', $this->r->render('[{{Inconnue}}]', []));
$this->assertSame('x', $this->r->render('{{PRENOM}}', ['prenom' => 'x']));
}
public function test_liste_des_variables(): void
{
$vars = $this->r->variables('{{prenom}} {{#if mobile}}{{mobile}}{{/if}} {{{banniere}}}');
sort($vars);
$this->assertSame(['banniere', 'mobile', 'prenom'], $vars);
}
}
@@ -0,0 +1,42 @@
<?php
namespace Tests\Unit;
use App\Signature\TemplateSanitizer;
use PHPUnit\Framework\TestCase;
class TemplateSanitizerTest extends TestCase
{
private TemplateSanitizer $s;
protected function setUp(): void
{
$this->s = new TemplateSanitizer;
}
public function test_retire_scripts_et_balises_actives(): void
{
$out = $this->s->clean('<p>A</p><script>alert(1)</script><iframe src="x"></iframe><form><input></form><b>B</b>');
$this->assertSame('<p>A</p><b>B</b>', $out);
}
public function test_retire_gestionnaires_evenements(): void
{
$this->assertSame('<img src="a.png">', $this->s->clean('<img src="a.png" onerror="alert(1)">'));
$this->assertSame('<a href="https://x">x</a>', $this->s->clean("<a href=\"https://x\" onClick='y()'>x</a>"));
}
public function test_retire_url_javascript_mais_garde_https_mailto_tel(): void
{
$this->assertSame('<a>x</a>', $this->s->clean('<a href="javascript:alert(1)">x</a>'));
$this->assertSame('<a>x</a>', $this->s->clean('<a href=" JaVa&#115;cript:alert(1)">x</a>'));
$ok = '<a href="mailto:a@b.fr">m</a><a href="tel:+33400">t</a><img src="https://s/img/l.png">';
$this->assertSame($ok, $this->s->clean($ok));
}
public function test_conserve_marqueurs_de_template_entre_lignes_de_tableau(): void
{
$tpl = '<table><tr><td>{{prenom}}</td></tr>{{#if banniere}}<tr><td>{{{banniere}}}</td></tr>{{/if}}</table>';
$this->assertSame($tpl, $this->s->clean($tpl));
}
}
@@ -0,0 +1,31 @@
<?php
namespace Tests\Unit;
use App\Signature\TemplateSelector;
use PHPUnit\Framework\TestCase;
class TemplateSelectorTest extends TestCase
{
private array $assignments = [
['id' => 1, 'template_id' => 10, 'group_id' => 100, 'priority' => 10], // agents
['id' => 2, 'template_id' => 20, 'group_id' => 200, 'priority' => 100], // directeurs
['id' => 3, 'template_id' => 30, 'group_id' => 300, 'priority' => 100], // même priorité, plus récent
];
public function test_priorite_la_plus_haute_gagne(): void
{
$this->assertSame(20, (new TemplateSelector)->select($this->assignments, [100, 200], 99));
}
public function test_egalite_departagee_par_anciennete(): void
{
$this->assertSame(20, (new TemplateSelector)->select($this->assignments, [300, 200], 99));
}
public function test_template_par_defaut_hors_groupe(): void
{
$this->assertSame(99, (new TemplateSelector)->select($this->assignments, [555], 99));
$this->assertNull((new TemplateSelector)->select($this->assignments, [], null));
}
}