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