2 use Shaarli\Config\ConfigManager
;
5 * PluginMarkdownTest.php
8 require_once 'application/Utils.php';
9 require_once 'plugins/markdown/markdown.php';
12 * Class PluginMarkdownTest
13 * Unit test for the Markdown plugin
15 class PluginMarkdownTest
extends PHPUnit_Framework_TestCase
18 * @var ConfigManager instance.
25 public function setUp()
27 PluginManager
::$PLUGINS_PATH = 'plugins';
28 $this->conf
= new ConfigManager('tests/utils/config/configJson');
29 $this->conf
->set('security.allowed_protocols', ['ftp', 'magnet']);
33 * Test render_linklist hook.
34 * Only check that there is basic markdown rendering.
36 public function testMarkdownLinklist()
38 $markdown = '# My title' . PHP_EOL
. 'Very interesting content.';
42 'description' => $markdown,
47 $data = hook_markdown_render_linklist($data, $this->conf
);
48 $this->assertNotFalse(strpos($data['links'][0]['description'], '<h1>'));
49 $this->assertNotFalse(strpos($data['links'][0]['description'], '<p>'));
51 $this->assertEquals($markdown, $data['links'][0]['description_src']);
55 * Test render_feed hook.
57 public function testMarkdownFeed()
59 $markdown = '# My title' . PHP_EOL
. 'Very interesting content.';
60 $markdown .= '— <a href="http://domain.tld/?0oc_VQ" title="Permalien">Permalien</a>';
64 'description' => $markdown,
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 '— <a href="http://domain.tld/?0oc_VQ">Permalien</a></p></div>',
74 $data['links'][0]['description']
79 * Test render_daily hook.
80 * Only check that there is basic markdown rendering.
82 public function testMarkdownDaily()
84 $markdown = '# My title' . PHP_EOL
. 'Very interesting content.';
87 'linksToDisplay' => array(
90 'formatedDescription' => $markdown,
95 $data = hook_markdown_render_daily($data, $this->conf
);
96 $this->assertNotFalse(strpos($data['linksToDisplay'][0]['formatedDescription'], '<h1>'));
97 $this->assertNotFalse(strpos($data['linksToDisplay'][0]['formatedDescription'], '<p>'));
101 * Test reverse_text2clickable().
103 public function testReverseText2clickable()
105 $text = 'stuff http://hello.there/is=someone#here otherstuff';
106 $clickableText = text2clickable($text, '');
107 $reversedText = reverse_text2clickable($clickableText);
108 $this->assertEquals($text, $reversedText);
112 * Test reverse_text2clickable().
114 public function testReverseText2clickableHashtags()
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);
124 * Test reverse_nl2br().
126 public function testReverseNl2br()
128 $text = 'stuff' . PHP_EOL
. 'otherstuff';
129 $processedText = nl2br($text);
130 $reversedText = reverse_nl2br($processedText);
131 $this->assertEquals($text, $reversedText);
135 * Test reverse_space2nbsp().
137 public function testReverseSpace2nbsp()
139 $text = ' stuff' . PHP_EOL
. ' otherstuff and another';
140 $processedText = space2nbsp($text);
141 $reversedText = reverse_space2nbsp($processedText);
142 $this->assertEquals($text, $reversedText);
145 public function testReverseFeedPermalink()
147 $text = 'Description... ';
148 $text .= '— <a href="http://domain.tld/?0oc_VQ" title="Permalien">Permalien</a>';
149 $expected = 'Description... — [Permalien](http://domain.tld/?0oc_VQ)';
150 $processedText = reverse_feed_permalink($text);
152 $this->assertEquals($expected, $processedText);
155 public function testReverseLastFeedPermalink()
157 $text = 'Description... ';
158 $text .= '<br>— <a href="http://domain.tld/?0oc_VQ" title="Permalien">Permalien</a>';
160 $text .= '<br>— <a href="http://domain.tld/?0oc_VQ" title="Permalien">Permalien</a>';
161 $expected .= '<br>— [Permalien](http://domain.tld/?0oc_VQ)';
162 $processedText = reverse_feed_permalink($text);
164 $this->assertEquals($expected, $processedText);
167 public function testReverseNoFeedPermalink()
169 $text = 'Hello! Where are you from?';
171 $processedText = reverse_feed_permalink($text);
173 $this->assertEquals($expected, $processedText);
177 * Test sanitize_html().
179 public function testSanitizeHtml()
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>';
187 $input .= '<a href="#" onmouseHover=alert(\'xss\'); attr="tt">link</a>';
188 $output .= '<a href="#" attr="tt">link</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));
196 * Test the no markdown tag.
198 public function testNoMarkdownTag()
200 $str = 'All _work_ and `no play` makes Jack a *dull* boy.';
202 'links' => array(array(
203 'description' => $str,
205 'taglist' => array(NO_MD_TAG
),
209 $processed = hook_markdown_render_linklist($data, $this->conf
);
210 $this->assertEquals($str, $processed['links'][0]['description']);
212 $processed = hook_markdown_render_feed($data, $this->conf
);
213 $this->assertEquals($str, $processed['links'][0]['description']);
217 'linksToDisplay' => array(
220 'formatedDescription' => $str,
222 'taglist' => array(),
227 $data = hook_markdown_render_daily($data, $this->conf
);
228 $this->assertEquals($str, $data['linksToDisplay'][0]['formatedDescription']);
232 * Test that a close value to nomarkdown is not understand as nomarkdown (previous value `.nomarkdown`).
234 public function testNoMarkdownNotExcactlyMatching()
236 $str = 'All _work_ and `no play` makes Jack a *dull* boy.';
238 'links' => array(array(
239 'description' => $str,
240 'tags' => '.' . NO_MD_TAG
,
241 'taglist' => array('.'. NO_MD_TAG
),
245 $data = hook_markdown_render_feed($data, $this->conf
);
246 $this->assertContains('<em>', $data['links'][0]['description']);
250 * Make sure that the generated HTML match the reference HTML file.
252 public function testMarkdownGlobalProcessDescription()
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');
258 $data = process_markdown(
260 $this->conf
->get('security.markdown_escape', true),
261 $this->conf
->get('security.allowed_protocols')
263 $this->assertEquals($html, $data . PHP_EOL
);
267 * Make sure that the HTML tags are escaped.
269 public function testMarkdownWithHtmlEscape()
271 $md = '**strong** <strong>strong</strong>';
272 $html = '<div class="markdown"><p><strong>strong</strong> <strong>strong</strong></p></div>';
276 'description' => $md,
280 $data = hook_markdown_render_linklist($data, $this->conf
);
281 $this->assertEquals($html, $data['links'][0]['description']);
285 * Make sure that the HTML tags aren't escaped with the setting set to false.
287 public function testMarkdownWithHtmlNoEscape()
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>';
295 'description' => $md,
299 $data = hook_markdown_render_linklist($data, $this->conf
);
300 $this->assertEquals($html, $data['links'][0]['description']);