2 namespace Shaarli\Plugin\Markdown
;
4 use Shaarli\Config\ConfigManager
;
5 use Shaarli\Plugin\PluginManager
;
8 * PluginMarkdownTest.php
11 require_once 'application/bookmark/LinkUtils.php';
12 require_once 'application/Utils.php';
13 require_once 'plugins/markdown/markdown.php';
16 * Class PluginMarkdownTest
17 * Unit test for the Markdown plugin
19 class PluginMarkdownTest
extends \PHPUnit\Framework\TestCase
22 * @var ConfigManager instance.
29 public function setUp()
31 PluginManager
::$PLUGINS_PATH = 'plugins';
32 $this->conf
= new ConfigManager('tests/utils/config/configJson');
33 $this->conf
->set('security.allowed_protocols', ['ftp', 'magnet']);
37 * Test render_linklist hook.
38 * Only check that there is basic markdown rendering.
40 public function testMarkdownLinklist()
42 $markdown = '# My title' . PHP_EOL
. 'Very interesting content.';
46 'description' => $markdown,
51 $data = hook_markdown_render_linklist($data, $this->conf
);
52 $this->assertNotFalse(strpos($data['links'][0]['description'], '<h1>'));
53 $this->assertNotFalse(strpos($data['links'][0]['description'], '<p>'));
55 $this->assertEquals($markdown, $data['links'][0]['description_src']);
59 * Test render_feed hook.
61 public function testMarkdownFeed()
63 $markdown = '# My title' . PHP_EOL
. 'Very interesting content.';
64 $markdown .= '— <a href="http://domain.tld/?0oc_VQ" title="Permalien">Permalien</a>';
68 'description' => $markdown,
73 $data = hook_markdown_render_feed($data, $this->conf
);
74 $this->assertNotFalse(strpos($data['links'][0]['description'], '<h1>'));
75 $this->assertNotFalse(strpos($data['links'][0]['description'], '<p>'));
76 $this->assertStringEndsWith(
77 '— <a href="http://domain.tld/?0oc_VQ">Permalien</a></p></div>',
78 $data['links'][0]['description']
83 * Test render_daily hook.
84 * Only check that there is basic markdown rendering.
86 public function testMarkdownDaily()
88 $markdown = '# My title' . PHP_EOL
. 'Very interesting content.';
91 'linksToDisplay' => array(
94 'formatedDescription' => $markdown,
99 $data = hook_markdown_render_daily($data, $this->conf
);
100 $this->assertNotFalse(strpos($data['linksToDisplay'][0]['formatedDescription'], '<h1>'));
101 $this->assertNotFalse(strpos($data['linksToDisplay'][0]['formatedDescription'], '<p>'));
105 * Test reverse_text2clickable().
107 public function testReverseText2clickable()
109 $text = 'stuff http://hello.there/is=someone#here otherstuff';
110 $clickableText = text2clickable($text, '');
111 $reversedText = reverse_text2clickable($clickableText);
112 $this->assertEquals($text, $reversedText);
116 * Test reverse_text2clickable().
118 public function testReverseText2clickableHashtags()
120 $text = file_get_contents('tests/plugins/resources/hashtags.raw');
121 $md = file_get_contents('tests/plugins/resources/hashtags.md');
122 $clickableText = hashtag_autolink($text);
123 $reversedText = reverse_text2clickable($clickableText);
124 $this->assertEquals($md, $reversedText);
128 * Test reverse_nl2br().
130 public function testReverseNl2br()
132 $text = 'stuff' . PHP_EOL
. 'otherstuff';
133 $processedText = nl2br($text);
134 $reversedText = reverse_nl2br($processedText);
135 $this->assertEquals($text, $reversedText);
139 * Test reverse_space2nbsp().
141 public function testReverseSpace2nbsp()
143 $text = ' stuff' . PHP_EOL
. ' otherstuff and another';
144 $processedText = space2nbsp($text);
145 $reversedText = reverse_space2nbsp($processedText);
146 $this->assertEquals($text, $reversedText);
149 public function testReverseFeedPermalink()
151 $text = 'Description... ';
152 $text .= '— <a href="http://domain.tld/?0oc_VQ" title="Permalien">Permalien</a>';
153 $expected = 'Description... — [Permalien](http://domain.tld/?0oc_VQ)';
154 $processedText = reverse_feed_permalink($text);
156 $this->assertEquals($expected, $processedText);
159 public function testReverseLastFeedPermalink()
161 $text = 'Description... ';
162 $text .= '<br>— <a href="http://domain.tld/?0oc_VQ" title="Permalien">Permalien</a>';
164 $text .= '<br>— <a href="http://domain.tld/?0oc_VQ" title="Permalien">Permalien</a>';
165 $expected .= '<br>— [Permalien](http://domain.tld/?0oc_VQ)';
166 $processedText = reverse_feed_permalink($text);
168 $this->assertEquals($expected, $processedText);
171 public function testReverseNoFeedPermalink()
173 $text = 'Hello! Where are you from?';
175 $processedText = reverse_feed_permalink($text);
177 $this->assertEquals($expected, $processedText);
181 * Test sanitize_html().
183 public function testSanitizeHtml()
185 $input = '< script src="js.js"/>';
186 $input .= '< script attr>alert(\'xss\');</script>';
187 $input .= '<style> * { display: none }</style>';
188 $output = escape($input);
189 $input .= '<a href="#" onmouseHover="alert(\'xss\');" attr="tt">link</a>';
190 $output .= '<a href="#" attr="tt">link</a>';
191 $input .= '<a href="#" onmouseHover=alert(\'xss\'); attr="tt">link</a>';
192 $output .= '<a href="#" attr="tt">link</a>';
193 $this->assertEquals($output, sanitize_html($input));
194 // Do not touch escaped HTML.
195 $input = escape($input);
196 $this->assertEquals($input, sanitize_html($input));
200 * Test the no markdown tag.
202 public function testNoMarkdownTag()
204 $str = 'All _work_ and `no play` makes Jack a *dull* boy.';
206 'links' => array(array(
207 'description' => $str,
209 'taglist' => array(NO_MD_TAG
),
213 $processed = hook_markdown_render_linklist($data, $this->conf
);
214 $this->assertEquals($str, $processed['links'][0]['description']);
216 $processed = hook_markdown_render_feed($data, $this->conf
);
217 $this->assertEquals($str, $processed['links'][0]['description']);
221 'linksToDisplay' => array(
224 'formatedDescription' => $str,
226 'taglist' => array(),
231 $data = hook_markdown_render_daily($data, $this->conf
);
232 $this->assertEquals($str, $data['linksToDisplay'][0]['formatedDescription']);
236 * Test that a close value to nomarkdown is not understand as nomarkdown (previous value `.nomarkdown`).
238 public function testNoMarkdownNotExcactlyMatching()
240 $str = 'All _work_ and `no play` makes Jack a *dull* boy.';
242 'links' => array(array(
243 'description' => $str,
244 'tags' => '.' . NO_MD_TAG
,
245 'taglist' => array('.'. NO_MD_TAG
),
249 $data = hook_markdown_render_feed($data, $this->conf
);
250 $this->assertContains('<em>', $data['links'][0]['description']);
254 * Make sure that the generated HTML match the reference HTML file.
256 public function testMarkdownGlobalProcessDescription()
258 $md = file_get_contents('tests/plugins/resources/markdown.md');
259 $md = format_description($md);
260 $html = file_get_contents('tests/plugins/resources/markdown.html');
262 $data = process_markdown(
264 $this->conf
->get('security.markdown_escape', true),
265 $this->conf
->get('security.allowed_protocols')
267 $this->assertEquals($html, $data . PHP_EOL
);
271 * Make sure that the HTML tags are escaped.
273 public function testMarkdownWithHtmlEscape()
275 $md = '**strong** <strong>strong</strong>';
276 $html = '<div class="markdown"><p><strong>strong</strong> <strong>strong</strong></p></div>';
280 'description' => $md,
284 $data = hook_markdown_render_linklist($data, $this->conf
);
285 $this->assertEquals($html, $data['links'][0]['description']);
289 * Make sure that the HTML tags aren't escaped with the setting set to false.
291 public function testMarkdownWithHtmlNoEscape()
293 $this->conf
->set('security.markdown_escape', false);
294 $md = '**strong** <strong>strong</strong>';
295 $html = '<div class="markdown"><p><strong>strong</strong> <strong>strong</strong></p></div>';
299 'description' => $md,
303 $data = hook_markdown_render_linklist($data, $this->conf
);
304 $this->assertEquals($html, $data['links'][0]['description']);