mirror of
https://codeberg.org/PostERG/xamxam.git
synced 2026-06-25 08:09:18 +02:00
Add PHPUnit setup (Phase 0) and pure-logic tests (Phase 1): Crypto, EmailObfuscator, SystemController helpers, StudentEmail, TfeController OG tags
This commit is contained in:
140
tests/phpunit/StudentEmailTest.php
Normal file
140
tests/phpunit/StudentEmailTest.php
Normal file
@@ -0,0 +1,140 @@
|
||||
<?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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user