2026-07-26 20:22:05 +04:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* Tests unitaires pour markdown_to_html (includes/lib/markdown.php)
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
// --- Échappement XSS ---------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
assertEquals(
|
|
|
|
|
'<script>alert("x")</script>',
|
|
|
|
|
markdown_to_html('<script>alert("x")</script>'),
|
|
|
|
|
'markdown_to_html échappe le HTML brut'
|
|
|
|
|
);
|
|
|
|
|
assertNotContains(
|
|
|
|
|
'<script>',
|
|
|
|
|
markdown_to_html('<script>alert("x")</script>'),
|
|
|
|
|
'markdown_to_html ne laisse passer aucune balise script'
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// --- Gras et italique --------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
assertEquals(
|
|
|
|
|
'<strong>gras</strong>',
|
|
|
|
|
markdown_to_html('**gras**'),
|
|
|
|
|
'markdown_to_html convertit **texte** en <strong>'
|
|
|
|
|
);
|
|
|
|
|
assertEquals(
|
|
|
|
|
'<em>ital</em>',
|
|
|
|
|
markdown_to_html('*ital*'),
|
|
|
|
|
'markdown_to_html convertit *texte* en <em>'
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// --- Liens Markdown ----------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
assertEquals(
|
|
|
|
|
'<a href="https://exemple.com" target="_blank" rel="noopener noreferrer">exemple</a>',
|
|
|
|
|
markdown_to_html('[exemple](https://exemple.com)'),
|
|
|
|
|
'markdown_to_html convertit un lien Markdown avec attributs de sécurité'
|
|
|
|
|
);
|
|
|
|
|
assertEquals(
|
|
|
|
|
'<a href="http://exemple.com" target="_blank" rel="noopener noreferrer">site</a>',
|
|
|
|
|
markdown_to_html('[site](exemple.com)'),
|
|
|
|
|
'markdown_to_html ajoute http:// aux liens sans protocole'
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// --- URLs brutes -------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
assertEquals(
|
|
|
|
|
'Visite <a href="https://exemple.com/page" target="_blank" rel="noopener noreferrer">https://exemple.com/page</a> pour info',
|
|
|
|
|
markdown_to_html('Visite https://exemple.com/page pour info'),
|
|
|
|
|
'markdown_to_html rend les URLs http(s) brutes cliquables'
|
|
|
|
|
);
|
|
|
|
|
assertEquals(
|
|
|
|
|
'va sur <a href="http://o-k-i.net" target="_blank" rel="noopener noreferrer">o-k-i.net</a> maintenant',
|
|
|
|
|
markdown_to_html('va sur o-k-i.net maintenant'),
|
|
|
|
|
'markdown_to_html rend les domaines nus cliquables (avec http://)'
|
|
|
|
|
);
|
|
|
|
|
assertEquals(
|
|
|
|
|
'version v1.2 dispo',
|
|
|
|
|
markdown_to_html('version v1.2 dispo'),
|
|
|
|
|
'markdown_to_html ne transforme pas un numéro de version en lien'
|
|
|
|
|
);
|
|
|
|
|
|
2026-07-27 01:55:21 +04:00
|
|
|
// --- URLs contenant « & » : pas de double encodage ---------------------------
|
|
|
|
|
|
|
|
|
|
// Le texte est échappé une seule fois : « & » devient « & », jamais « &amp; »
|
|
|
|
|
assertEquals(
|
|
|
|
|
'Voir <a href="https://exemple.com/page?a=1&b=2" target="_blank" rel="noopener noreferrer">https://exemple.com/page?a=1&b=2</a> suite',
|
|
|
|
|
markdown_to_html('Voir https://exemple.com/page?a=1&b=2 suite'),
|
|
|
|
|
'markdown_to_html n\'encode pas deux fois le & des URLs brutes'
|
|
|
|
|
);
|
|
|
|
|
assertEquals(
|
|
|
|
|
'<a href="https://exemple.com/?x=1&y=2" target="_blank" rel="noopener noreferrer">lien</a>',
|
|
|
|
|
markdown_to_html('[lien](https://exemple.com/?x=1&y=2)'),
|
|
|
|
|
'markdown_to_html n\'encode pas deux fois le & des liens Markdown'
|
|
|
|
|
);
|
|
|
|
|
assertNotContains(
|
|
|
|
|
'&amp;',
|
|
|
|
|
markdown_to_html('https://exemple.com/page?a=1&b=2'),
|
|
|
|
|
'markdown_to_html ne produit jamais de &amp;'
|
|
|
|
|
);
|
|
|
|
|
|
2026-07-26 20:22:05 +04:00
|
|
|
// --- Listes ------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
assertEquals(
|
2026-07-27 01:55:21 +04:00
|
|
|
"<ul>\n<li>a</li>\n<li>b</li>\n</ul>",
|
2026-07-26 20:22:05 +04:00
|
|
|
markdown_to_html("- a\n- b"),
|
2026-07-27 01:55:21 +04:00
|
|
|
'markdown_to_html convertit les listes à puces en <ul> uniquement'
|
2026-07-26 20:22:05 +04:00
|
|
|
);
|
|
|
|
|
assertEquals(
|
2026-07-27 01:55:21 +04:00
|
|
|
"<ol>\n<li>a</li>\n<li>b</li>\n</ol>",
|
2026-07-26 20:22:05 +04:00
|
|
|
markdown_to_html("1. a\n2. b"),
|
|
|
|
|
'markdown_to_html convertit les listes numérotées en <ol>'
|
|
|
|
|
);
|
2026-07-27 01:55:21 +04:00
|
|
|
assertEquals(
|
|
|
|
|
"<ul>\n<li>a</li>\n</ul><br />\n<ol>\n<li>b</li>\n</ol><br />\n<ul>\n<li>c</li>\n</ul>",
|
|
|
|
|
markdown_to_html("- a\n1. b\n- c"),
|
|
|
|
|
'markdown_to_html sépare les listes de types différents'
|
|
|
|
|
);
|
|
|
|
|
assertEquals(
|
|
|
|
|
"<ul>\n<li>a</li>\n</ul><br />\ntexte",
|
|
|
|
|
markdown_to_html("- a\ntexte"),
|
|
|
|
|
'markdown_to_html ferme la liste avant le texte qui suit'
|
|
|
|
|
);
|
2026-07-26 20:22:05 +04:00
|
|
|
|
|
|
|
|
// --- Retours à la ligne ------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
assertEquals(
|
|
|
|
|
"ligne1<br />\nligne2",
|
|
|
|
|
markdown_to_html("ligne1\nligne2"),
|
|
|
|
|
'markdown_to_html convertit les sauts de ligne en <br />'
|
|
|
|
|
);
|