]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - tests/plugins/PluginMarkdownTest.php
Fix hashtags with markdown escape enabled
[github/shaarli/Shaarli.git] / tests / plugins / PluginMarkdownTest.php
CommitLineData
1be4afac 1<?php
3c66e564 2use Shaarli\Config\ConfigManager;
1be4afac
A
3
4/**
5 * PluginMarkdownTest.php
6 */
7
8require_once 'application/Utils.php';
9require_once 'plugins/markdown/markdown.php';
10
11/**
266e3fe5
A
12 * Class PluginMarkdownTest
13 * Unit test for the Markdown plugin
1be4afac
A
14 */
15class 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
dd6794cf
A
52 /**
53 * Test render_feed hook.
54 */
55 public function testMarkdownFeed()
56 {
57 $markdown = '# My title' . PHP_EOL . 'Very interesting content.';
58 $markdown .= '&#8212; <a href="http://domain.tld/?0oc_VQ" title="Permalien">Permalien</a>';
59 $data = array(
60 'links' => array(
61 0 => array(
62 'description' => $markdown,
63 ),
64 ),
65 );
66
67 $data = hook_markdown_render_feed($data, $this->conf);
68 $this->assertNotFalse(strpos($data['links'][0]['description'], '<h1>'));
69 $this->assertNotFalse(strpos($data['links'][0]['description'], '<p>'));
70 $this->assertStringEndsWith(
71 '&#8212; <a href="http://domain.tld/?0oc_VQ">Permalien</a></p></div>',
72 $data['links'][0]['description']
73 );
74 }
75
1be4afac
A
76 /**
77 * Test render_daily hook.
78 * Only check that there is basic markdown rendering.
79 */
93b1fe54 80 public function testMarkdownDaily()
1be4afac
A
81 {
82 $markdown = '# My title' . PHP_EOL . 'Very interesting content.';
83 $data = array(
84 // Columns data
50142efd 85 'linksToDisplay' => array(
86 // nth link
1be4afac 87 0 => array(
50142efd 88 'formatedDescription' => $markdown,
1be4afac
A
89 ),
90 ),
91 );
92
e0376101 93 $data = hook_markdown_render_daily($data, $this->conf);
50142efd 94 $this->assertNotFalse(strpos($data['linksToDisplay'][0]['formatedDescription'], '<h1>'));
95 $this->assertNotFalse(strpos($data['linksToDisplay'][0]['formatedDescription'], '<p>'));
1be4afac
A
96 }
97
98 /**
99 * Test reverse_text2clickable().
100 */
93b1fe54 101 public function testReverseText2clickable()
1be4afac
A
102 {
103 $text = 'stuff http://hello.there/is=someone#here otherstuff';
104 $clickableText = text2clickable($text, '');
105 $reversedText = reverse_text2clickable($clickableText);
106 $this->assertEquals($text, $reversedText);
107 }
108
cb7940e2
A
109 /**
110 * Test reverse_text2clickable().
111 */
112 public function testReverseText2clickableHashtags()
113 {
114 $text = file_get_contents('tests/plugins/resources/hashtags.raw');
115 $md = file_get_contents('tests/plugins/resources/hashtags.md');
116 $clickableText = hashtag_autolink($text);
117 $reversedText = reverse_text2clickable($clickableText);
118 $this->assertEquals($md, $reversedText);
119 }
120
1be4afac
A
121 /**
122 * Test reverse_nl2br().
123 */
93b1fe54 124 public function testReverseNl2br()
1be4afac
A
125 {
126 $text = 'stuff' . PHP_EOL . 'otherstuff';
127 $processedText = nl2br($text);
128 $reversedText = reverse_nl2br($processedText);
129 $this->assertEquals($text, $reversedText);
130 }
131
132 /**
133 * Test reverse_space2nbsp().
134 */
93b1fe54 135 public function testReverseSpace2nbsp()
1be4afac
A
136 {
137 $text = ' stuff' . PHP_EOL . ' otherstuff and another';
138 $processedText = space2nbsp($text);
139 $reversedText = reverse_space2nbsp($processedText);
140 $this->assertEquals($text, $reversedText);
141 }
142
dd6794cf
A
143 public function testReverseFeedPermalink()
144 {
145 $text = 'Description... ';
146 $text .= '&#8212; <a href="http://domain.tld/?0oc_VQ" title="Permalien">Permalien</a>';
147 $expected = 'Description... &#8212; [Permalien](http://domain.tld/?0oc_VQ)';
148 $processedText = reverse_feed_permalink($text);
149
150 $this->assertEquals($expected, $processedText);
151 }
152
153 public function testReverseLastFeedPermalink()
154 {
155 $text = 'Description... ';
156 $text .= '<br>&#8212; <a href="http://domain.tld/?0oc_VQ" title="Permalien">Permalien</a>';
157 $expected = $text;
158 $text .= '<br>&#8212; <a href="http://domain.tld/?0oc_VQ" title="Permalien">Permalien</a>';
159 $expected .= '<br>&#8212; [Permalien](http://domain.tld/?0oc_VQ)';
160 $processedText = reverse_feed_permalink($text);
161
162 $this->assertEquals($expected, $processedText);
163 }
164
165 public function testReverseNoFeedPermalink()
166 {
167 $text = 'Hello! Where are you from?';
168 $expected = $text;
169 $processedText = reverse_feed_permalink($text);
170
171 $this->assertEquals($expected, $processedText);
172 }
173
1be4afac 174 /**
2925687e 175 * Test sanitize_html().
1be4afac 176 */
93b1fe54 177 public function testSanitizeHtml()
3ce20d9e 178 {
2925687e
A
179 $input = '< script src="js.js"/>';
180 $input .= '< script attr>alert(\'xss\');</script>';
181 $input .= '<style> * { display: none }</style>';
182 $output = escape($input);
183 $input .= '<a href="#" onmouseHover="alert(\'xss\');" attr="tt">link</a>';
184 $output .= '<a href="#" attr="tt">link</a>';
e0376101
A
185 $input .= '<a href="#" onmouseHover=alert(\'xss\'); attr="tt">link</a>';
186 $output .= '<a href="#" attr="tt">link</a>';
2925687e
A
187 $this->assertEquals($output, sanitize_html($input));
188 // Do not touch escaped HTML.
189 $input = escape($input);
190 $this->assertEquals($input, sanitize_html($input));
1be4afac 191 }
3ce20d9e
A
192
193 /**
194 * Test the no markdown tag.
195 */
93b1fe54 196 public function testNoMarkdownTag()
3ce20d9e
A
197 {
198 $str = 'All _work_ and `no play` makes Jack a *dull* boy.';
199 $data = array(
200 'links' => array(array(
201 'description' => $str,
8c4e6018
A
202 'tags' => NO_MD_TAG,
203 'taglist' => array(NO_MD_TAG),
3ce20d9e
A
204 ))
205 );
206
e0376101 207 $processed = hook_markdown_render_linklist($data, $this->conf);
266e3fe5
A
208 $this->assertEquals($str, $processed['links'][0]['description']);
209
e0376101 210 $processed = hook_markdown_render_feed($data, $this->conf);
266e3fe5 211 $this->assertEquals($str, $processed['links'][0]['description']);
3ce20d9e
A
212
213 $data = array(
214 // Columns data
50142efd 215 'linksToDisplay' => array(
216 // nth link
3ce20d9e 217 0 => array(
50142efd 218 'formatedDescription' => $str,
219 'tags' => NO_MD_TAG,
220 'taglist' => array(),
3ce20d9e
A
221 ),
222 ),
223 );
224
e0376101 225 $data = hook_markdown_render_daily($data, $this->conf);
50142efd 226 $this->assertEquals($str, $data['linksToDisplay'][0]['formatedDescription']);
3ce20d9e 227 }
c5941f31 228
266e3fe5
A
229 /**
230 * Test that a close value to nomarkdown is not understand as nomarkdown (previous value `.nomarkdown`).
231 */
93b1fe54 232 public function testNoMarkdownNotExcactlyMatching()
266e3fe5
A
233 {
234 $str = 'All _work_ and `no play` makes Jack a *dull* boy.';
235 $data = array(
236 'links' => array(array(
237 'description' => $str,
238 'tags' => '.' . NO_MD_TAG,
239 'taglist' => array('.'. NO_MD_TAG),
240 ))
241 );
242
e0376101 243 $data = hook_markdown_render_feed($data, $this->conf);
266e3fe5
A
244 $this->assertContains('<em>', $data['links'][0]['description']);
245 }
246
c5941f31 247 /**
86ceea05 248 * Make sure that the generated HTML match the reference HTML file.
c5941f31 249 */
86ceea05 250 public function testMarkdownGlobalProcessDescription()
c5941f31
A
251 {
252 $md = file_get_contents('tests/plugins/resources/markdown.md');
253 $md = format_description($md);
254 $html = file_get_contents('tests/plugins/resources/markdown.html');
255
86ceea05
A
256 $data = process_markdown(
257 $md,
258 $this->conf->get('security.markdown_escape', true),
259 $this->conf->get('security.allowed_protocols')
260 );
cb7940e2 261 $this->assertEquals($html, $data . PHP_EOL);
c5941f31 262 }
e0376101
A
263
264 /**
265 * Make sure that the HTML tags are escaped.
266 */
267 public function testMarkdownWithHtmlEscape()
268 {
269 $md = '**strong** <strong>strong</strong>';
270 $html = '<div class="markdown"><p><strong>strong</strong> &lt;strong&gt;strong&lt;/strong&gt;</p></div>';
271 $data = array(
272 'links' => array(
273 0 => array(
274 'description' => $md,
275 ),
276 ),
277 );
278 $data = hook_markdown_render_linklist($data, $this->conf);
279 $this->assertEquals($html, $data['links'][0]['description']);
280 }
281
282 /**
283 * Make sure that the HTML tags aren't escaped with the setting set to false.
284 */
285 public function testMarkdownWithHtmlNoEscape()
286 {
287 $this->conf->set('security.markdown_escape', false);
288 $md = '**strong** <strong>strong</strong>';
289 $html = '<div class="markdown"><p><strong>strong</strong> <strong>strong</strong></p></div>';
290 $data = array(
291 'links' => array(
292 0 => array(
293 'description' => $md,
294 ),
295 ),
296 );
297 $data = hook_markdown_render_linklist($data, $this->conf);
298 $this->assertEquals($html, $data['links'][0]['description']);
299 }
1be4afac 300}