2 use Shaarli\Config\ConfigManager
;
5 * PluginMarkdownTest.php
8 require_once 'application/bookmark/LinkUtils.php';
9 require_once 'application/Utils.php';
10 require_once 'plugins/markdown/markdown.php';
13 * Class PluginMarkdownTest
14 * Unit test for the Markdown plugin
16 class PluginMarkdownTest
extends PHPUnit_Framework_TestCase
19 * @var ConfigManager instance.
26 public function setUp()
28 PluginManager
::$PLUGINS_PATH = 'plugins';
29 $this->conf
= new ConfigManager('tests/utils/config/configJson');
30 $this->conf
->set('security.allowed_protocols', ['ftp', 'magnet']);
34 * Test render_linklist hook.
35 * Only check that there is basic markdown rendering.
37 public function testMarkdownLinklist()
39 $markdown = '# My title' . PHP_EOL
. 'Very interesting content.';
43 'description' => $markdown,
48 $data = hook_markdown_render_linklist($data, $this->conf
);
49 $this->assertNotFalse(strpos($data['links'][0]['description'], '<h1>'));
50 $this->assertNotFalse(strpos($data['links'][0]['description'], '<p>'));
52 $this->assertEquals($markdown, $data['links'][0]['description_src']);
56 * Test render_feed hook.
58 public function testMarkdownFeed()
60 $markdown = '# My title' . PHP_EOL
. 'Very interesting content.';
61 $markdown .= '— <a href="http://domain.tld/?0oc_VQ" title="Permalien">Permalien</a>';
65 'description' => $markdown,
70 $data = hook_markdown_render_feed($data, $this->conf
);
71 $this->assertNotFalse(strpos($data['links'][0]['description'], '<h1>'));
72 $this->assertNotFalse(strpos($data['links'][0]['description'], '<p>'));
73 $this->assertStringEndsWith(
74 '— <a href="http://domain.tld/?0oc_VQ">Permalien</a></p></div>',
75 $data['links'][0]['description']
80 * Test render_daily hook.
81 * Only check that there is basic markdown rendering.
83 public function testMarkdownDaily()
85 $markdown = '# My title' . PHP_EOL
. 'Very interesting content.';
88 'linksToDisplay' => array(
91 'formatedDescription' => $markdown,
96 $data = hook_markdown_render_daily($data, $this->conf
);
97 $this->assertNotFalse(strpos($data['linksToDisplay'][0]['formatedDescription'], '<h1>'));
98 $this->assertNotFalse(strpos($data['linksToDisplay'][0]['formatedDescription'], '<p>'));
102 * Test reverse_text2clickable().
104 public function testReverseText2clickable()
106 $text = 'stuff http://hello.there/is=someone#here otherstuff';
107 $clickableText = text2clickable($text, '');
108 $reversedText = reverse_text2clickable($clickableText);
109 $this->assertEquals($text, $reversedText);
113 * Test reverse_text2clickable().
115 public function testReverseText2clickableHashtags()
117 $text = file_get_contents('tests/plugins/resources/hashtags.raw');
118 $md = file_get_contents('tests/plugins/resources/hashtags.md');
119 $clickableText = hashtag_autolink($text);
120 $reversedText = reverse_text2clickable($clickableText);
121 $this->assertEquals($md, $reversedText);
125 * Test reverse_nl2br().
127 public function testReverseNl2br()
129 $text = 'stuff' . PHP_EOL
. 'otherstuff';
130 $processedText = nl2br($text);
131 $reversedText = reverse_nl2br($processedText);
132 $this->assertEquals($text, $reversedText);
136 * Test reverse_space2nbsp().
138 public function testReverseSpace2nbsp()
140 $text = ' stuff' . PHP_EOL
. ' otherstuff and another';
141 $processedText = space2nbsp($text);
142 $reversedText = reverse_space2nbsp($processedText);
143 $this->assertEquals($text, $reversedText);
146 public function testReverseFeedPermalink()
148 $text = 'Description... ';
149 $text .= '— <a href="http://domain.tld/?0oc_VQ" title="Permalien">Permalien</a>';
150 $expected = 'Description... — [Permalien](http://domain.tld/?0oc_VQ)';
151 $processedText = reverse_feed_permalink($text);
153 $this->assertEquals($expected, $processedText);
156 public function testReverseLastFeedPermalink()
158 $text = 'Description... ';
159 $text .= '<br>— <a href="http://domain.tld/?0oc_VQ" title="Permalien">Permalien</a>';
161 $text .= '<br>— <a href="http://domain.tld/?0oc_VQ" title="Permalien">Permalien</a>';
162 $expected .= '<br>— [Permalien](http://domain.tld/?0oc_VQ)';
163 $processedText = reverse_feed_permalink($text);
165 $this->assertEquals($expected, $processedText);
168 public function testReverseNoFeedPermalink()
170 $text = 'Hello! Where are you from?';
172 $processedText = reverse_feed_permalink($text);
174 $this->assertEquals($expected, $processedText);
178 * Test sanitize_html().
180 public function testSanitizeHtml()
182 $input = '< script src="js.js"/>';
183 $input .= '< script attr>alert(\'xss\');</script>';
184 $input .= '<style> * { display: none }</style>';
185 $output = escape($input);
186 $input .= '<a href="#" onmouseHover="alert(\'xss\');" attr="tt">link</a>';
187 $output .= '<a href="#" attr="tt">link</a>';
188 $input .= '<a href="#" onmouseHover=alert(\'xss\'); attr="tt">link</a>';
189 $output .= '<a href="#" attr="tt">link</a>';
190 $this->assertEquals($output, sanitize_html($input));
191 // Do not touch escaped HTML.
192 $input = escape($input);
193 $this->assertEquals($input, sanitize_html($input));
197 * Test the no markdown tag.
199 public function testNoMarkdownTag()
201 $str = 'All _work_ and `no play` makes Jack a *dull* boy.';
203 'links' => array(array(
204 'description' => $str,
206 'taglist' => array(NO_MD_TAG
),
210 $processed = hook_markdown_render_linklist($data, $this->conf
);
211 $this->assertEquals($str, $processed['links'][0]['description']);
213 $processed = hook_markdown_render_feed($data, $this->conf
);
214 $this->assertEquals($str, $processed['links'][0]['description']);
218 'linksToDisplay' => array(
221 'formatedDescription' => $str,
223 'taglist' => array(),
228 $data = hook_markdown_render_daily($data, $this->conf
);
229 $this->assertEquals($str, $data['linksToDisplay'][0]['formatedDescription']);
233 * Test that a close value to nomarkdown is not understand as nomarkdown (previous value `.nomarkdown`).
235 public function testNoMarkdownNotExcactlyMatching()
237 $str = 'All _work_ and `no play` makes Jack a *dull* boy.';
239 'links' => array(array(
240 'description' => $str,
241 'tags' => '.' . NO_MD_TAG
,
242 'taglist' => array('.'. NO_MD_TAG
),
246 $data = hook_markdown_render_feed($data, $this->conf
);
247 $this->assertContains('<em>', $data['links'][0]['description']);
251 * Make sure that the generated HTML match the reference HTML file.
253 public function testMarkdownGlobalProcessDescription()
255 $md = file_get_contents('tests/plugins/resources/markdown.md');
256 $md = format_description($md);
257 $html = file_get_contents('tests/plugins/resources/markdown.html');
259 $data = process_markdown(
261 $this->conf
->get('security.markdown_escape', true),
262 $this->conf
->get('security.allowed_protocols')
264 $this->assertEquals($html, $data . PHP_EOL
);
268 * Make sure that the HTML tags are escaped.
270 public function testMarkdownWithHtmlEscape()
272 $md = '**strong** <strong>strong</strong>';
273 $html = '<div class="markdown"><p><strong>strong</strong> <strong>strong</strong></p></div>';
277 'description' => $md,
281 $data = hook_markdown_render_linklist($data, $this->conf
);
282 $this->assertEquals($html, $data['links'][0]['description']);
286 * Make sure that the HTML tags aren't escaped with the setting set to false.
288 public function testMarkdownWithHtmlNoEscape()
290 $this->conf
->set('security.markdown_escape', false);
291 $md = '**strong** <strong>strong</strong>';
292 $html = '<div class="markdown"><p><strong>strong</strong> <strong>strong</strong></p></div>';
296 'description' => $md,
300 $data = hook_markdown_render_linklist($data, $this->conf
);
301 $this->assertEquals($html, $data['links'][0]['description']);