]>
Commit | Line | Data |
---|---|---|
1be4afac | 1 | <?php |
3c66e564 | 2 | use Shaarli\Config\ConfigManager; |
1be4afac A |
3 | |
4 | /** | |
5 | * PluginMarkdownTest.php | |
6 | */ | |
7 | ||
8 | require_once 'application/Utils.php'; | |
9 | require_once 'plugins/markdown/markdown.php'; | |
10 | ||
11 | /** | |
266e3fe5 A |
12 | * Class PluginMarkdownTest |
13 | * Unit test for the Markdown plugin | |
1be4afac A |
14 | */ |
15 | class PluginMarkdownTest extends PHPUnit_Framework_TestCase | |
16 | { | |
e0376101 A |
17 | /** |
18 | * @var ConfigManager instance. | |
19 | */ | |
20 | protected $conf; | |
21 | ||
1be4afac A |
22 | /** |
23 | * Reset plugin path | |
24 | */ | |
93b1fe54 | 25 | public function setUp() |
1be4afac A |
26 | { |
27 | PluginManager::$PLUGINS_PATH = 'plugins'; | |
e0376101 | 28 | $this->conf = new ConfigManager('tests/utils/config/configJson'); |
86ceea05 | 29 | $this->conf->set('security.allowed_protocols', ['ftp', 'magnet']); |
1be4afac A |
30 | } |
31 | ||
32 | /** | |
33 | * Test render_linklist hook. | |
34 | * Only check that there is basic markdown rendering. | |
35 | */ | |
93b1fe54 | 36 | public function testMarkdownLinklist() |
1be4afac A |
37 | { |
38 | $markdown = '# My title' . PHP_EOL . 'Very interesting content.'; | |
39 | $data = array( | |
40 | 'links' => array( | |
41 | 0 => array( | |
42 | 'description' => $markdown, | |
43 | ), | |
44 | ), | |
45 | ); | |
46 | ||
e0376101 | 47 | $data = hook_markdown_render_linklist($data, $this->conf); |
1be4afac A |
48 | $this->assertNotFalse(strpos($data['links'][0]['description'], '<h1>')); |
49 | $this->assertNotFalse(strpos($data['links'][0]['description'], '<p>')); | |
50 | } | |
51 | ||
52 | /** | |
53 | * Test render_daily hook. | |
54 | * Only check that there is basic markdown rendering. | |
55 | */ | |
93b1fe54 | 56 | public function testMarkdownDaily() |
1be4afac A |
57 | { |
58 | $markdown = '# My title' . PHP_EOL . 'Very interesting content.'; | |
59 | $data = array( | |
60 | // Columns data | |
50142efd | 61 | 'linksToDisplay' => array( |
62 | // nth link | |
1be4afac | 63 | 0 => array( |
50142efd | 64 | 'formatedDescription' => $markdown, |
1be4afac A |
65 | ), |
66 | ), | |
67 | ); | |
68 | ||
e0376101 | 69 | $data = hook_markdown_render_daily($data, $this->conf); |
50142efd | 70 | $this->assertNotFalse(strpos($data['linksToDisplay'][0]['formatedDescription'], '<h1>')); |
71 | $this->assertNotFalse(strpos($data['linksToDisplay'][0]['formatedDescription'], '<p>')); | |
1be4afac A |
72 | } |
73 | ||
74 | /** | |
75 | * Test reverse_text2clickable(). | |
76 | */ | |
93b1fe54 | 77 | public function testReverseText2clickable() |
1be4afac A |
78 | { |
79 | $text = 'stuff http://hello.there/is=someone#here otherstuff'; | |
80 | $clickableText = text2clickable($text, ''); | |
81 | $reversedText = reverse_text2clickable($clickableText); | |
82 | $this->assertEquals($text, $reversedText); | |
83 | } | |
84 | ||
85 | /** | |
86 | * Test reverse_nl2br(). | |
87 | */ | |
93b1fe54 | 88 | public function testReverseNl2br() |
1be4afac A |
89 | { |
90 | $text = 'stuff' . PHP_EOL . 'otherstuff'; | |
91 | $processedText = nl2br($text); | |
92 | $reversedText = reverse_nl2br($processedText); | |
93 | $this->assertEquals($text, $reversedText); | |
94 | } | |
95 | ||
96 | /** | |
97 | * Test reverse_space2nbsp(). | |
98 | */ | |
93b1fe54 | 99 | public function testReverseSpace2nbsp() |
1be4afac A |
100 | { |
101 | $text = ' stuff' . PHP_EOL . ' otherstuff and another'; | |
102 | $processedText = space2nbsp($text); | |
103 | $reversedText = reverse_space2nbsp($processedText); | |
104 | $this->assertEquals($text, $reversedText); | |
105 | } | |
106 | ||
107 | /** | |
2925687e | 108 | * Test sanitize_html(). |
1be4afac | 109 | */ |
93b1fe54 | 110 | public function testSanitizeHtml() |
3ce20d9e | 111 | { |
2925687e A |
112 | $input = '< script src="js.js"/>'; |
113 | $input .= '< script attr>alert(\'xss\');</script>'; | |
114 | $input .= '<style> * { display: none }</style>'; | |
115 | $output = escape($input); | |
116 | $input .= '<a href="#" onmouseHover="alert(\'xss\');" attr="tt">link</a>'; | |
117 | $output .= '<a href="#" attr="tt">link</a>'; | |
e0376101 A |
118 | $input .= '<a href="#" onmouseHover=alert(\'xss\'); attr="tt">link</a>'; |
119 | $output .= '<a href="#" attr="tt">link</a>'; | |
2925687e A |
120 | $this->assertEquals($output, sanitize_html($input)); |
121 | // Do not touch escaped HTML. | |
122 | $input = escape($input); | |
123 | $this->assertEquals($input, sanitize_html($input)); | |
1be4afac | 124 | } |
3ce20d9e A |
125 | |
126 | /** | |
127 | * Test the no markdown tag. | |
128 | */ | |
93b1fe54 | 129 | public function testNoMarkdownTag() |
3ce20d9e A |
130 | { |
131 | $str = 'All _work_ and `no play` makes Jack a *dull* boy.'; | |
132 | $data = array( | |
133 | 'links' => array(array( | |
134 | 'description' => $str, | |
8c4e6018 A |
135 | 'tags' => NO_MD_TAG, |
136 | 'taglist' => array(NO_MD_TAG), | |
3ce20d9e A |
137 | )) |
138 | ); | |
139 | ||
e0376101 | 140 | $processed = hook_markdown_render_linklist($data, $this->conf); |
266e3fe5 A |
141 | $this->assertEquals($str, $processed['links'][0]['description']); |
142 | ||
e0376101 | 143 | $processed = hook_markdown_render_feed($data, $this->conf); |
266e3fe5 | 144 | $this->assertEquals($str, $processed['links'][0]['description']); |
3ce20d9e A |
145 | |
146 | $data = array( | |
147 | // Columns data | |
50142efd | 148 | 'linksToDisplay' => array( |
149 | // nth link | |
3ce20d9e | 150 | 0 => array( |
50142efd | 151 | 'formatedDescription' => $str, |
152 | 'tags' => NO_MD_TAG, | |
153 | 'taglist' => array(), | |
3ce20d9e A |
154 | ), |
155 | ), | |
156 | ); | |
157 | ||
e0376101 | 158 | $data = hook_markdown_render_daily($data, $this->conf); |
50142efd | 159 | $this->assertEquals($str, $data['linksToDisplay'][0]['formatedDescription']); |
3ce20d9e | 160 | } |
c5941f31 | 161 | |
266e3fe5 A |
162 | /** |
163 | * Test that a close value to nomarkdown is not understand as nomarkdown (previous value `.nomarkdown`). | |
164 | */ | |
93b1fe54 | 165 | public function testNoMarkdownNotExcactlyMatching() |
266e3fe5 A |
166 | { |
167 | $str = 'All _work_ and `no play` makes Jack a *dull* boy.'; | |
168 | $data = array( | |
169 | 'links' => array(array( | |
170 | 'description' => $str, | |
171 | 'tags' => '.' . NO_MD_TAG, | |
172 | 'taglist' => array('.'. NO_MD_TAG), | |
173 | )) | |
174 | ); | |
175 | ||
e0376101 | 176 | $data = hook_markdown_render_feed($data, $this->conf); |
266e3fe5 A |
177 | $this->assertContains('<em>', $data['links'][0]['description']); |
178 | } | |
179 | ||
c5941f31 | 180 | /** |
86ceea05 | 181 | * Make sure that the generated HTML match the reference HTML file. |
c5941f31 | 182 | */ |
86ceea05 | 183 | public function testMarkdownGlobalProcessDescription() |
c5941f31 A |
184 | { |
185 | $md = file_get_contents('tests/plugins/resources/markdown.md'); | |
186 | $md = format_description($md); | |
187 | $html = file_get_contents('tests/plugins/resources/markdown.html'); | |
188 | ||
86ceea05 A |
189 | $data = process_markdown( |
190 | $md, | |
191 | $this->conf->get('security.markdown_escape', true), | |
192 | $this->conf->get('security.allowed_protocols') | |
193 | ); | |
c5941f31 A |
194 | $this->assertEquals($html, $data); |
195 | } | |
e0376101 A |
196 | |
197 | /** | |
198 | * Make sure that the HTML tags are escaped. | |
199 | */ | |
200 | public function testMarkdownWithHtmlEscape() | |
201 | { | |
202 | $md = '**strong** <strong>strong</strong>'; | |
203 | $html = '<div class="markdown"><p><strong>strong</strong> <strong>strong</strong></p></div>'; | |
204 | $data = array( | |
205 | 'links' => array( | |
206 | 0 => array( | |
207 | 'description' => $md, | |
208 | ), | |
209 | ), | |
210 | ); | |
211 | $data = hook_markdown_render_linklist($data, $this->conf); | |
212 | $this->assertEquals($html, $data['links'][0]['description']); | |
213 | } | |
214 | ||
215 | /** | |
216 | * Make sure that the HTML tags aren't escaped with the setting set to false. | |
217 | */ | |
218 | public function testMarkdownWithHtmlNoEscape() | |
219 | { | |
220 | $this->conf->set('security.markdown_escape', false); | |
221 | $md = '**strong** <strong>strong</strong>'; | |
222 | $html = '<div class="markdown"><p><strong>strong</strong> <strong>strong</strong></p></div>'; | |
223 | $data = array( | |
224 | 'links' => array( | |
225 | 0 => array( | |
226 | 'description' => $md, | |
227 | ), | |
228 | ), | |
229 | ); | |
230 | $data = hook_markdown_render_linklist($data, $this->conf); | |
231 | $this->assertEquals($html, $data['links'][0]['description']); | |
232 | } | |
1be4afac | 233 | } |