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 testReverseFeedDirectLink()
161 $text = 'Description... ';
162 $text .= '— <a href="http://domain.tld/?0oc_VQ" title="Direct link">Direct link</a>';
163 $expected = 'Description... — [Direct link](http://domain.tld/?0oc_VQ)';
164 $processedText = reverse_feed_permalink($text);
166 $this->assertEquals($expected, $processedText);
169 public function testReverseLastFeedPermalink()
171 $text = 'Description... ';
172 $text .= '<br>— <a href="http://domain.tld/?0oc_VQ" title="Permalien">Permalien</a>';
174 $text .= '<br>— <a href="http://domain.tld/?0oc_VQ" title="Permalien">Permalien</a>';
175 $expected .= '<br>— [Permalien](http://domain.tld/?0oc_VQ)';
176 $processedText = reverse_feed_permalink($text);
178 $this->assertEquals($expected, $processedText);
181 public function testReverseNoFeedPermalink()
183 $text = 'Hello! Where are you from?';
185 $processedText = reverse_feed_permalink($text);
187 $this->assertEquals($expected, $processedText);
191 * Test sanitize_html().
193 public function testSanitizeHtml()
195 $input = '< script src="js.js"/>';
196 $input .= '< script attr>alert(\'xss\');</script>';
197 $input .= '<style> * { display: none }</style>';
198 $output = escape($input);
199 $input .= '<a href="#" onmouseHover="alert(\'xss\');" attr="tt">link</a>';
200 $output .= '<a href="#" attr="tt">link</a>';
201 $input .= '<a href="#" onmouseHover=alert(\'xss\'); attr="tt">link</a>';
202 $output .= '<a href="#" attr="tt">link</a>';
203 $this->assertEquals($output, sanitize_html($input));
204 // Do not touch escaped HTML.
205 $input = escape($input);
206 $this->assertEquals($input, sanitize_html($input));
210 * Test the no markdown tag.
212 public function testNoMarkdownTag()
214 $str = 'All _work_ and `no play` makes Jack a *dull* boy.';
216 'links' => array(array(
217 'description' => $str,
219 'taglist' => array(NO_MD_TAG
),
223 $processed = hook_markdown_render_linklist($data, $this->conf
);
224 $this->assertEquals($str, $processed['links'][0]['description']);
226 $processed = hook_markdown_render_feed($data, $this->conf
);
227 $this->assertEquals($str, $processed['links'][0]['description']);
231 'linksToDisplay' => array(
234 'formatedDescription' => $str,
236 'taglist' => array(),
241 $data = hook_markdown_render_daily($data, $this->conf
);
242 $this->assertEquals($str, $data['linksToDisplay'][0]['formatedDescription']);
246 * Test that a close value to nomarkdown is not understand as nomarkdown (previous value `.nomarkdown`).
248 public function testNoMarkdownNotExcactlyMatching()
250 $str = 'All _work_ and `no play` makes Jack a *dull* boy.';
252 'links' => array(array(
253 'description' => $str,
254 'tags' => '.' . NO_MD_TAG
,
255 'taglist' => array('.'. NO_MD_TAG
),
259 $data = hook_markdown_render_feed($data, $this->conf
);
260 $this->assertContains('<em>', $data['links'][0]['description']);
264 * Make sure that the generated HTML match the reference HTML file.
266 public function testMarkdownGlobalProcessDescription()
268 $md = file_get_contents('tests/plugins/resources/markdown.md');
269 $md = format_description($md);
270 $html = file_get_contents('tests/plugins/resources/markdown.html');
272 $data = process_markdown(
274 $this->conf
->get('security.markdown_escape', true),
275 $this->conf
->get('security.allowed_protocols')
277 $this->assertEquals($html, $data . PHP_EOL
);
281 * Make sure that the HTML tags are escaped.
283 public function testMarkdownWithHtmlEscape()
285 $md = '**strong** <strong>strong</strong>';
286 $html = '<div class="markdown"><p><strong>strong</strong> <strong>strong</strong></p></div>';
290 'description' => $md,
294 $data = hook_markdown_render_linklist($data, $this->conf
);
295 $this->assertEquals($html, $data['links'][0]['description']);
299 * Make sure that the HTML tags aren't escaped with the setting set to false.
301 public function testMarkdownWithHtmlNoEscape()
303 $this->conf
->set('security.markdown_escape', false);
304 $md = '**strong** <strong>strong</strong>';
305 $html = '<div class="markdown"><p><strong>strong</strong> <strong>strong</strong></p></div>';
309 'description' => $md,
313 $data = hook_markdown_render_linklist($data, $this->conf
);
314 $this->assertEquals($html, $data['links'][0]['description']);