2 use Shaarli\Config\ConfigManager
;
3 use Shaarli\Plugin\PluginManager
;
6 * PluginMarkdownTest.php
9 require_once 'application/bookmark/LinkUtils.php';
10 require_once 'application/Utils.php';
11 require_once 'plugins/markdown/markdown.php';
14 * Class PluginMarkdownTest
15 * Unit test for the Markdown plugin
17 class PluginMarkdownTest
extends PHPUnit_Framework_TestCase
20 * @var ConfigManager instance.
27 public function setUp()
29 PluginManager
::$PLUGINS_PATH = 'plugins';
30 $this->conf
= new ConfigManager('tests/utils/config/configJson');
31 $this->conf
->set('security.allowed_protocols', ['ftp', 'magnet']);
35 * Test render_linklist hook.
36 * Only check that there is basic markdown rendering.
38 public function testMarkdownLinklist()
40 $markdown = '# My title' . PHP_EOL
. 'Very interesting content.';
44 'description' => $markdown,
49 $data = hook_markdown_render_linklist($data, $this->conf
);
50 $this->assertNotFalse(strpos($data['links'][0]['description'], '<h1>'));
51 $this->assertNotFalse(strpos($data['links'][0]['description'], '<p>'));
53 $this->assertEquals($markdown, $data['links'][0]['description_src']);
57 * Test render_feed hook.
59 public function testMarkdownFeed()
61 $markdown = '# My title' . PHP_EOL
. 'Very interesting content.';
62 $markdown .= '— <a href="http://domain.tld/?0oc_VQ" title="Permalien">Permalien</a>';
66 'description' => $markdown,
71 $data = hook_markdown_render_feed($data, $this->conf
);
72 $this->assertNotFalse(strpos($data['links'][0]['description'], '<h1>'));
73 $this->assertNotFalse(strpos($data['links'][0]['description'], '<p>'));
74 $this->assertStringEndsWith(
75 '— <a href="http://domain.tld/?0oc_VQ">Permalien</a></p></div>',
76 $data['links'][0]['description']
81 * Test render_daily hook.
82 * Only check that there is basic markdown rendering.
84 public function testMarkdownDaily()
86 $markdown = '# My title' . PHP_EOL
. 'Very interesting content.';
89 'linksToDisplay' => array(
92 'formatedDescription' => $markdown,
97 $data = hook_markdown_render_daily($data, $this->conf
);
98 $this->assertNotFalse(strpos($data['linksToDisplay'][0]['formatedDescription'], '<h1>'));
99 $this->assertNotFalse(strpos($data['linksToDisplay'][0]['formatedDescription'], '<p>'));
103 * Test reverse_text2clickable().
105 public function testReverseText2clickable()
107 $text = 'stuff http://hello.there/is=someone#here otherstuff';
108 $clickableText = text2clickable($text, '');
109 $reversedText = reverse_text2clickable($clickableText);
110 $this->assertEquals($text, $reversedText);
114 * Test reverse_text2clickable().
116 public function testReverseText2clickableHashtags()
118 $text = file_get_contents('tests/plugins/resources/hashtags.raw');
119 $md = file_get_contents('tests/plugins/resources/hashtags.md');
120 $clickableText = hashtag_autolink($text);
121 $reversedText = reverse_text2clickable($clickableText);
122 $this->assertEquals($md, $reversedText);
126 * Test reverse_nl2br().
128 public function testReverseNl2br()
130 $text = 'stuff' . PHP_EOL
. 'otherstuff';
131 $processedText = nl2br($text);
132 $reversedText = reverse_nl2br($processedText);
133 $this->assertEquals($text, $reversedText);
137 * Test reverse_space2nbsp().
139 public function testReverseSpace2nbsp()
141 $text = ' stuff' . PHP_EOL
. ' otherstuff and another';
142 $processedText = space2nbsp($text);
143 $reversedText = reverse_space2nbsp($processedText);
144 $this->assertEquals($text, $reversedText);
147 public function testReverseFeedPermalink()
149 $text = 'Description... ';
150 $text .= '— <a href="http://domain.tld/?0oc_VQ" title="Permalien">Permalien</a>';
151 $expected = 'Description... — [Permalien](http://domain.tld/?0oc_VQ)';
152 $processedText = reverse_feed_permalink($text);
154 $this->assertEquals($expected, $processedText);
157 public function testReverseLastFeedPermalink()
159 $text = 'Description... ';
160 $text .= '<br>— <a href="http://domain.tld/?0oc_VQ" title="Permalien">Permalien</a>';
162 $text .= '<br>— <a href="http://domain.tld/?0oc_VQ" title="Permalien">Permalien</a>';
163 $expected .= '<br>— [Permalien](http://domain.tld/?0oc_VQ)';
164 $processedText = reverse_feed_permalink($text);
166 $this->assertEquals($expected, $processedText);
169 public function testReverseNoFeedPermalink()
171 $text = 'Hello! Where are you from?';
173 $processedText = reverse_feed_permalink($text);
175 $this->assertEquals($expected, $processedText);
179 * Test sanitize_html().
181 public function testSanitizeHtml()
183 $input = '< script src="js.js"/>';
184 $input .= '< script attr>alert(\'xss\');</script>';
185 $input .= '<style> * { display: none }</style>';
186 $output = escape($input);
187 $input .= '<a href="#" onmouseHover="alert(\'xss\');" attr="tt">link</a>';
188 $output .= '<a href="#" attr="tt">link</a>';
189 $input .= '<a href="#" onmouseHover=alert(\'xss\'); attr="tt">link</a>';
190 $output .= '<a href="#" attr="tt">link</a>';
191 $this->assertEquals($output, sanitize_html($input));
192 // Do not touch escaped HTML.
193 $input = escape($input);
194 $this->assertEquals($input, sanitize_html($input));
198 * Test the no markdown tag.
200 public function testNoMarkdownTag()
202 $str = 'All _work_ and `no play` makes Jack a *dull* boy.';
204 'links' => array(array(
205 'description' => $str,
207 'taglist' => array(NO_MD_TAG
),
211 $processed = hook_markdown_render_linklist($data, $this->conf
);
212 $this->assertEquals($str, $processed['links'][0]['description']);
214 $processed = hook_markdown_render_feed($data, $this->conf
);
215 $this->assertEquals($str, $processed['links'][0]['description']);
219 'linksToDisplay' => array(
222 'formatedDescription' => $str,
224 'taglist' => array(),
229 $data = hook_markdown_render_daily($data, $this->conf
);
230 $this->assertEquals($str, $data['linksToDisplay'][0]['formatedDescription']);
234 * Test that a close value to nomarkdown is not understand as nomarkdown (previous value `.nomarkdown`).
236 public function testNoMarkdownNotExcactlyMatching()
238 $str = 'All _work_ and `no play` makes Jack a *dull* boy.';
240 'links' => array(array(
241 'description' => $str,
242 'tags' => '.' . NO_MD_TAG
,
243 'taglist' => array('.'. NO_MD_TAG
),
247 $data = hook_markdown_render_feed($data, $this->conf
);
248 $this->assertContains('<em>', $data['links'][0]['description']);
252 * Make sure that the generated HTML match the reference HTML file.
254 public function testMarkdownGlobalProcessDescription()
256 $md = file_get_contents('tests/plugins/resources/markdown.md');
257 $md = format_description($md);
258 $html = file_get_contents('tests/plugins/resources/markdown.html');
260 $data = process_markdown(
262 $this->conf
->get('security.markdown_escape', true),
263 $this->conf
->get('security.allowed_protocols')
265 $this->assertEquals($html, $data . PHP_EOL
);
269 * Make sure that the HTML tags are escaped.
271 public function testMarkdownWithHtmlEscape()
273 $md = '**strong** <strong>strong</strong>';
274 $html = '<div class="markdown"><p><strong>strong</strong> <strong>strong</strong></p></div>';
278 'description' => $md,
282 $data = hook_markdown_render_linklist($data, $this->conf
);
283 $this->assertEquals($html, $data['links'][0]['description']);
287 * Make sure that the HTML tags aren't escaped with the setting set to false.
289 public function testMarkdownWithHtmlNoEscape()
291 $this->conf
->set('security.markdown_escape', false);
292 $md = '**strong** <strong>strong</strong>';
293 $html = '<div class="markdown"><p><strong>strong</strong> <strong>strong</strong></p></div>';
297 'description' => $md,
301 $data = hook_markdown_render_linklist($data, $this->conf
);
302 $this->assertEquals($html, $data['links'][0]['description']);