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>'));
53 * Test render_feed hook.
55 public function testMarkdownFeed()
57 $markdown = '# My title' . PHP_EOL
. 'Very interesting content.';
58 $markdown .= '— <a href="http://domain.tld/?0oc_VQ" title="Permalien">Permalien</a>';
62 'description' => $markdown,
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 '— <a href="http://domain.tld/?0oc_VQ">Permalien</a></p></div>',
72 $data['links'][0]['description']
77 * Test render_daily hook.
78 * Only check that there is basic markdown rendering.
80 public function testMarkdownDaily()
82 $markdown = '# My title' . PHP_EOL
. 'Very interesting content.';
85 'linksToDisplay' => array(
88 'formatedDescription' => $markdown,
93 $data = hook_markdown_render_daily($data, $this->conf
);
94 $this->assertNotFalse(strpos($data['linksToDisplay'][0]['formatedDescription'], '<h1>'));
95 $this->assertNotFalse(strpos($data['linksToDisplay'][0]['formatedDescription'], '<p>'));
99 * Test reverse_text2clickable().
101 public function testReverseText2clickable()
103 $text = 'stuff http://hello.there/is=someone#here otherstuff';
104 $clickableText = text2clickable($text, '');
105 $reversedText = reverse_text2clickable($clickableText);
106 $this->assertEquals($text, $reversedText);
110 * Test reverse_text2clickable().
112 public function testReverseText2clickableHashtags()
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);
122 * Test reverse_nl2br().
124 public function testReverseNl2br()
126 $text = 'stuff' . PHP_EOL
. 'otherstuff';
127 $processedText = nl2br($text);
128 $reversedText = reverse_nl2br($processedText);
129 $this->assertEquals($text, $reversedText);
133 * Test reverse_space2nbsp().
135 public function testReverseSpace2nbsp()
137 $text = ' stuff' . PHP_EOL
. ' otherstuff and another';
138 $processedText = space2nbsp($text);
139 $reversedText = reverse_space2nbsp($processedText);
140 $this->assertEquals($text, $reversedText);
143 public function testReverseFeedPermalink()
145 $text = 'Description... ';
146 $text .= '— <a href="http://domain.tld/?0oc_VQ" title="Permalien">Permalien</a>';
147 $expected = 'Description... — [Permalien](http://domain.tld/?0oc_VQ)';
148 $processedText = reverse_feed_permalink($text);
150 $this->assertEquals($expected, $processedText);
153 public function testReverseLastFeedPermalink()
155 $text = 'Description... ';
156 $text .= '<br>— <a href="http://domain.tld/?0oc_VQ" title="Permalien">Permalien</a>';
158 $text .= '<br>— <a href="http://domain.tld/?0oc_VQ" title="Permalien">Permalien</a>';
159 $expected .= '<br>— [Permalien](http://domain.tld/?0oc_VQ)';
160 $processedText = reverse_feed_permalink($text);
162 $this->assertEquals($expected, $processedText);
165 public function testReverseNoFeedPermalink()
167 $text = 'Hello! Where are you from?';
169 $processedText = reverse_feed_permalink($text);
171 $this->assertEquals($expected, $processedText);
175 * Test sanitize_html().
177 public function testSanitizeHtml()
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>';
185 $input .= '<a href="#" onmouseHover=alert(\'xss\'); attr="tt">link</a>';
186 $output .= '<a href="#" attr="tt">link</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));
194 * Test the no markdown tag.
196 public function testNoMarkdownTag()
198 $str = 'All _work_ and `no play` makes Jack a *dull* boy.';
200 'links' => array(array(
201 'description' => $str,
203 'taglist' => array(NO_MD_TAG
),
207 $processed = hook_markdown_render_linklist($data, $this->conf
);
208 $this->assertEquals($str, $processed['links'][0]['description']);
210 $processed = hook_markdown_render_feed($data, $this->conf
);
211 $this->assertEquals($str, $processed['links'][0]['description']);
215 'linksToDisplay' => array(
218 'formatedDescription' => $str,
220 'taglist' => array(),
225 $data = hook_markdown_render_daily($data, $this->conf
);
226 $this->assertEquals($str, $data['linksToDisplay'][0]['formatedDescription']);
230 * Test that a close value to nomarkdown is not understand as nomarkdown (previous value `.nomarkdown`).
232 public function testNoMarkdownNotExcactlyMatching()
234 $str = 'All _work_ and `no play` makes Jack a *dull* boy.';
236 'links' => array(array(
237 'description' => $str,
238 'tags' => '.' . NO_MD_TAG
,
239 'taglist' => array('.'. NO_MD_TAG
),
243 $data = hook_markdown_render_feed($data, $this->conf
);
244 $this->assertContains('<em>', $data['links'][0]['description']);
248 * Make sure that the generated HTML match the reference HTML file.
250 public function testMarkdownGlobalProcessDescription()
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');
256 $data = process_markdown(
258 $this->conf
->get('security.markdown_escape', true),
259 $this->conf
->get('security.allowed_protocols')
261 $this->assertEquals($html, $data . PHP_EOL
);
265 * Make sure that the HTML tags are escaped.
267 public function testMarkdownWithHtmlEscape()
269 $md = '**strong** <strong>strong</strong>';
270 $html = '<div class="markdown"><p><strong>strong</strong> <strong>strong</strong></p></div>';
274 'description' => $md,
278 $data = hook_markdown_render_linklist($data, $this->conf
);
279 $this->assertEquals($html, $data['links'][0]['description']);
283 * Make sure that the HTML tags aren't escaped with the setting set to false.
285 public function testMarkdownWithHtmlNoEscape()
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>';
293 'description' => $md,
297 $data = hook_markdown_render_linklist($data, $this->conf
);
298 $this->assertEquals($html, $data['links'][0]['description']);