ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
MarkdownFormattingToHTMLTest.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
28 
29 class MarkdownFormattingToHTMLTest extends TestCase
30 {
33 
34  protected function setUp(): void
35  {
36  $language = $this->getMockBuilder(Language::class)
37  ->disableOriginalConstructor()
38  ->getMock();
39  $group = new Group(new Factory(), $language);
40 
41  $this->markdown = $group->markdown(false)->toHTML();
42  $this->markdown_with_escaped_html = $group->markdown()->toHTML();
43  }
44 
45  public static function stringProvider(): array
46  {
47  return [
48  ["lorem", "<p>lorem</p>\n"],
49  ["lorem **ipsum**", "<p>lorem <strong>ipsum</strong></p>\n"],
50  ["_lorem_ **ipsum**", "<p><em>lorem</em> <strong>ipsum</strong></p>\n"],
51  ["# Headline", "<h1>Headline</h1>\n"],
52  ["## Headline", "<h2>Headline</h2>\n"],
53  ["### Headline", "<h3>Headline</h3>\n"],
54  ["1. Lorem\n2. Ipsum", "<ol>\n<li>Lorem</li>\n<li>Ipsum</li>\n</ol>\n"],
55  ["- Lorem\n- Ipsum", "<ul>\n<li>Lorem</li>\n<li>Ipsum</li>\n</ul>\n"],
56  ["[Link Titel](https://www.ilias.de)", "<p><a href=\"https://www.ilias.de\">Link Titel</a></p>\n"],
57  ];
58  }
59 
63  public function testTransformationToHTML(
64  string $markdown_string,
65  string $expected_html,
66  ): void {
67  $this->assertEquals($expected_html, $this->markdown->transform($markdown_string));
68  }
69 
70  public function testHtmlInputIsRendered(): void
71  {
72  $markdown_with_html = "lorem **ipsum**\n<ul><li>phpunit</li></ul>";
73 
74  $expected = "<p>lorem <strong>ipsum</strong></p>\n<ul><li>phpunit</li></ul>\n";
75 
76  $this->assertSame($expected, $this->markdown->transform($markdown_with_html));
77  }
78 
79  public function testUntrustedLinksAreRemoved(): void
80  {
81  $markdown_with_html = "lorem **ipsum**\n[xss](javascript:alert(1))";
82 
83  $expected = "<p>lorem <strong>ipsum</strong>\n<a>xss</a></p>\n";
84 
85  $this->assertSame($expected, $this->markdown->transform($markdown_with_html));
86  }
87 
88  public function testHtmlInputIsEscapedIfDesired(): void
89  {
90  $markdown_with_html = "lorem **ipsum**\n<ul><li>phpunit</li></ul>";
91 
92  $expected = "<p>lorem <strong>ipsum</strong></p>\n&lt;ul&gt;&lt;li&gt;phpunit&lt;/li&gt;&lt;/ul&gt;\n";
93 
94  $this->assertSame($expected, $this->markdown_with_escaped_html->transform($markdown_with_html));
95  }
96 }
testTransformationToHTML(string $markdown_string, string $expected_html,)
stringProvider
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Builds data types.
Definition: Factory.php:35
A transformation is a function from one datatype to another.