mirror of
https://codeberg.org/PostERG/xamxam.git
synced 2026-06-25 16:19:19 +02:00
141 lines
4.6 KiB
PHP
141 lines
4.6 KiB
PHP
<?php
|
||
|
||
use PHPUnit\Framework\TestCase;
|
||
|
||
/**
|
||
* StudentEmailTest — Pure logic tests for StudentEmail::buildHtml().
|
||
*
|
||
* buildHtml() is private but we can test it via reflection.
|
||
*/
|
||
class StudentEmailTest extends TestCase
|
||
{
|
||
/**
|
||
* Invoke the private buildHtml() via reflection.
|
||
*/
|
||
private function buildHtml(array $thesis): string
|
||
{
|
||
$ref = new ReflectionMethod(StudentEmail::class, 'buildHtml');
|
||
$ref->setAccessible(true);
|
||
|
||
return $ref->invoke(null, $thesis);
|
||
}
|
||
|
||
// ── Basic output ──────────────────────────────────────────────────────────
|
||
|
||
public function testBuildHtmlReturnsNonEmptyString(): void
|
||
{
|
||
$thesis = [
|
||
'identifier' => '2024-001',
|
||
'title' => 'My Thesis',
|
||
'authors' => 'John Doe',
|
||
'year' => '2024',
|
||
'synopsis' => 'A brief synopsis.',
|
||
'keywords' => 'art, design',
|
||
];
|
||
|
||
$html = $this->buildHtml($thesis);
|
||
|
||
$this->assertIsString($html);
|
||
$this->assertNotEmpty($html);
|
||
$this->assertStringContainsString('My Thesis', $html);
|
||
$this->assertStringContainsString('2024-001', $html);
|
||
$this->assertStringContainsString('John Doe', $html);
|
||
}
|
||
|
||
public function testBuildHtmlContainsKeyFields(): void
|
||
{
|
||
$thesis = [
|
||
'title' => 'Test Title',
|
||
'authors' => 'Alice',
|
||
'year' => '2025',
|
||
'identifier' => '2025-042',
|
||
'orientation' => 'BD',
|
||
];
|
||
|
||
$html = $this->buildHtml($thesis);
|
||
|
||
$this->assertStringContainsString('Test Title', $html);
|
||
$this->assertStringContainsString('Alice', $html);
|
||
$this->assertStringContainsString('2025', $html);
|
||
$this->assertStringContainsString('2025-042', $html);
|
||
$this->assertStringContainsString('BD', $html);
|
||
}
|
||
|
||
// ── HTML escaping ─────────────────────────────────────────────────────────
|
||
|
||
public function testBuildHtmlEscapesSpecialCharacters(): void
|
||
{
|
||
$thesis = [
|
||
'identifier' => '2024-999',
|
||
'title' => '<script>alert("xss")</script>',
|
||
'authors' => 'Evil & Co.',
|
||
'year' => '2024',
|
||
];
|
||
|
||
$html = $this->buildHtml($thesis);
|
||
|
||
$this->assertStringNotContainsString('<script>', $html);
|
||
$this->assertStringContainsString('<script>', $html);
|
||
$this->assertStringContainsString('Evil & Co.', $html);
|
||
}
|
||
|
||
// ── Missing / null optional fields ────────────────────────────────────────
|
||
|
||
public function testBuildHtmlHandlesMissingOptionalFields(): void
|
||
{
|
||
// Minimal thesis — only title and authors
|
||
$thesis = [
|
||
'title' => 'Solo',
|
||
'authors' => 'One Author',
|
||
];
|
||
|
||
$html = $this->buildHtml($thesis);
|
||
|
||
$this->assertIsString($html);
|
||
$this->assertNotEmpty($html);
|
||
// Should contain the "–" dash placeholders for empty fields
|
||
$this->assertStringContainsString('Solo', $html);
|
||
$this->assertStringContainsString('One Author', $html);
|
||
}
|
||
|
||
public function testBuildHtmlHandlesNullFieldsGracefully(): void
|
||
{
|
||
$thesis = [
|
||
'identifier' => null,
|
||
'title' => 'Null Test',
|
||
'authors' => null,
|
||
'year' => null,
|
||
'subtitle' => null,
|
||
'orientation' => null,
|
||
];
|
||
|
||
$html = $this->buildHtml($thesis);
|
||
|
||
$this->assertIsString($html);
|
||
$this->assertNotEmpty($html);
|
||
$this->assertStringContainsString('Null Test', $html);
|
||
}
|
||
|
||
public function testBuildHtmlHandlesEmptyArray(): void
|
||
{
|
||
$html = $this->buildHtml([]);
|
||
|
||
$this->assertIsString($html);
|
||
$this->assertNotEmpty($html);
|
||
// Should not crash — just fill all fields with "–"
|
||
}
|
||
|
||
public function testBuildHtmlContainsLabelFields(): void
|
||
{
|
||
$thesis = ['title' => 'X', 'authors' => 'Y'];
|
||
|
||
$html = $this->buildHtml($thesis);
|
||
|
||
$this->assertStringContainsString('Identifiant', $html);
|
||
$this->assertStringContainsString('Titre', $html);
|
||
$this->assertStringContainsString('Auteur·ice(s)', $html);
|
||
$this->assertStringContainsString('Année', $html);
|
||
$this->assertStringContainsString('Mots-clés', $html);
|
||
}
|
||
}
|