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_nl2br().
114 public function testReverseNl2br()
116 $text = 'stuff' . PHP_EOL
. 'otherstuff';
117 $processedText = nl2br($text);
118 $reversedText = reverse_nl2br($processedText);
119 $this->assertEquals($text, $reversedText);
123 * Test reverse_space2nbsp().
125 public function testReverseSpace2nbsp()
127 $text = ' stuff' . PHP_EOL
. ' otherstuff and another';
128 $processedText = space2nbsp($text);
129 $reversedText = reverse_space2nbsp($processedText);
130 $this->assertEquals($text, $reversedText);
133 public function testReverseFeedPermalink()
135 $text = 'Description... ';
136 $text .= '— <a href="http://domain.tld/?0oc_VQ" title="Permalien">Permalien</a>';
137 $expected = 'Description... — [Permalien](http://domain.tld/?0oc_VQ)';
138 $processedText = reverse_feed_permalink($text);
140 $this->assertEquals($expected, $processedText);
143 public function testReverseLastFeedPermalink()
145 $text = 'Description... ';
146 $text .= '<br>— <a href="http://domain.tld/?0oc_VQ" title="Permalien">Permalien</a>';
148 $text .= '<br>— <a href="http://domain.tld/?0oc_VQ" title="Permalien">Permalien</a>';
149 $expected .= '<br>— [Permalien](http://domain.tld/?0oc_VQ)';
150 $processedText = reverse_feed_permalink($text);
152 $this->assertEquals($expected, $processedText);
155 public function testReverseNoFeedPermalink()
157 $text = 'Hello! Where are you from?';
159 $processedText = reverse_feed_permalink($text);
161 $this->assertEquals($expected, $processedText);
165 * Test sanitize_html().
167 public function testSanitizeHtml()
169 $input = '< script src="js.js"/>';
170 $input .= '< script attr>alert(\'xss\');</script>';
171 $input .= '<style> * { display: none }</style>';
172 $output = escape($input);
173 $input .= '<a href="#" onmouseHover="alert(\'xss\');" attr="tt">link</a>';
174 $output .= '<a href="#" attr="tt">link</a>';
175 $input .= '<a href="#" onmouseHover=alert(\'xss\'); attr="tt">link</a>';
176 $output .= '<a href="#" attr="tt">link</a>';
177 $this->assertEquals($output, sanitize_html($input));
178 // Do not touch escaped HTML.
179 $input = escape($input);
180 $this->assertEquals($input, sanitize_html($input));
184 * Test the no markdown tag.
186 public function testNoMarkdownTag()
188 $str = 'All _work_ and `no play` makes Jack a *dull* boy.';
190 'links' => array(array(
191 'description' => $str,
193 'taglist' => array(NO_MD_TAG
),
197 $processed = hook_markdown_render_linklist($data, $this->conf
);
198 $this->assertEquals($str, $processed['links'][0]['description']);
200 $processed = hook_markdown_render_feed($data, $this->conf
);
201 $this->assertEquals($str, $processed['links'][0]['description']);
205 'linksToDisplay' => array(
208 'formatedDescription' => $str,
210 'taglist' => array(),
215 $data = hook_markdown_render_daily($data, $this->conf
);
216 $this->assertEquals($str, $data['linksToDisplay'][0]['formatedDescription']);
220 * Test that a close value to nomarkdown is not understand as nomarkdown (previous value `.nomarkdown`).
222 public function testNoMarkdownNotExcactlyMatching()
224 $str = 'All _work_ and `no play` makes Jack a *dull* boy.';
226 'links' => array(array(
227 'description' => $str,
228 'tags' => '.' . NO_MD_TAG
,
229 'taglist' => array('.'. NO_MD_TAG
),
233 $data = hook_markdown_render_feed($data, $this->conf
);
234 $this->assertContains('<em>', $data['links'][0]['description']);
238 * Make sure that the generated HTML match the reference HTML file.
240 public function testMarkdownGlobalProcessDescription()
242 $md = file_get_contents('tests/plugins/resources/markdown.md');
243 $md = format_description($md);
244 $html = file_get_contents('tests/plugins/resources/markdown.html');
246 $data = process_markdown(
248 $this->conf
->get('security.markdown_escape', true),
249 $this->conf
->get('security.allowed_protocols')
251 $this->assertEquals($html, $data);
255 * Make sure that the HTML tags are escaped.
257 public function testMarkdownWithHtmlEscape()
259 $md = '**strong** <strong>strong</strong>';
260 $html = '<div class="markdown"><p><strong>strong</strong> <strong>strong</strong></p></div>';
264 'description' => $md,
268 $data = hook_markdown_render_linklist($data, $this->conf
);
269 $this->assertEquals($html, $data['links'][0]['description']);
273 * Make sure that the HTML tags aren't escaped with the setting set to false.
275 public function testMarkdownWithHtmlNoEscape()
277 $this->conf
->set('security.markdown_escape', false);
278 $md = '**strong** <strong>strong</strong>';
279 $html = '<div class="markdown"><p><strong>strong</strong> <strong>strong</strong></p></div>';
283 'description' => $md,
287 $data = hook_markdown_render_linklist($data, $this->conf
);
288 $this->assertEquals($html, $data['links'][0]['description']);