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_daily hook.
54 * Only check that there is basic markdown rendering.
56 public function testMarkdownDaily()
58 $markdown = '# My title' . PHP_EOL
. 'Very interesting content.';
61 'linksToDisplay' => array(
64 'formatedDescription' => $markdown,
69 $data = hook_markdown_render_daily($data, $this->conf
);
70 $this->assertNotFalse(strpos($data['linksToDisplay'][0]['formatedDescription'], '<h1>'));
71 $this->assertNotFalse(strpos($data['linksToDisplay'][0]['formatedDescription'], '<p>'));
75 * Test reverse_text2clickable().
77 public function testReverseText2clickable()
79 $text = 'stuff http://hello.there/is=someone#here otherstuff';
80 $clickableText = text2clickable($text, '');
81 $reversedText = reverse_text2clickable($clickableText);
82 $this->assertEquals($text, $reversedText);
86 * Test reverse_nl2br().
88 public function testReverseNl2br()
90 $text = 'stuff' . PHP_EOL
. 'otherstuff';
91 $processedText = nl2br($text);
92 $reversedText = reverse_nl2br($processedText);
93 $this->assertEquals($text, $reversedText);
97 * Test reverse_space2nbsp().
99 public function testReverseSpace2nbsp()
101 $text = ' stuff' . PHP_EOL
. ' otherstuff and another';
102 $processedText = space2nbsp($text);
103 $reversedText = reverse_space2nbsp($processedText);
104 $this->assertEquals($text, $reversedText);
108 * Test sanitize_html().
110 public function testSanitizeHtml()
112 $input = '< script src="js.js"/>';
113 $input .= '< script attr>alert(\'xss\');</script>';
114 $input .= '<style> * { display: none }</style>';
115 $output = escape($input);
116 $input .= '<a href="#" onmouseHover="alert(\'xss\');" attr="tt">link</a>';
117 $output .= '<a href="#" attr="tt">link</a>';
118 $input .= '<a href="#" onmouseHover=alert(\'xss\'); attr="tt">link</a>';
119 $output .= '<a href="#" attr="tt">link</a>';
120 $this->assertEquals($output, sanitize_html($input));
121 // Do not touch escaped HTML.
122 $input = escape($input);
123 $this->assertEquals($input, sanitize_html($input));
127 * Test the no markdown tag.
129 public function testNoMarkdownTag()
131 $str = 'All _work_ and `no play` makes Jack a *dull* boy.';
133 'links' => array(array(
134 'description' => $str,
136 'taglist' => array(NO_MD_TAG
),
140 $processed = hook_markdown_render_linklist($data, $this->conf
);
141 $this->assertEquals($str, $processed['links'][0]['description']);
143 $processed = hook_markdown_render_feed($data, $this->conf
);
144 $this->assertEquals($str, $processed['links'][0]['description']);
148 'linksToDisplay' => array(
151 'formatedDescription' => $str,
153 'taglist' => array(),
158 $data = hook_markdown_render_daily($data, $this->conf
);
159 $this->assertEquals($str, $data['linksToDisplay'][0]['formatedDescription']);
163 * Test that a close value to nomarkdown is not understand as nomarkdown (previous value `.nomarkdown`).
165 public function testNoMarkdownNotExcactlyMatching()
167 $str = 'All _work_ and `no play` makes Jack a *dull* boy.';
169 'links' => array(array(
170 'description' => $str,
171 'tags' => '.' . NO_MD_TAG
,
172 'taglist' => array('.'. NO_MD_TAG
),
176 $data = hook_markdown_render_feed($data, $this->conf
);
177 $this->assertContains('<em>', $data['links'][0]['description']);
181 * Make sure that the generated HTML match the reference HTML file.
183 public function testMarkdownGlobalProcessDescription()
185 $md = file_get_contents('tests/plugins/resources/markdown.md');
186 $md = format_description($md);
187 $html = file_get_contents('tests/plugins/resources/markdown.html');
189 $data = process_markdown(
191 $this->conf
->get('security.markdown_escape', true),
192 $this->conf
->get('security.allowed_protocols')
194 $this->assertEquals($html, $data);
198 * Make sure that the HTML tags are escaped.
200 public function testMarkdownWithHtmlEscape()
202 $md = '**strong** <strong>strong</strong>';
203 $html = '<div class="markdown"><p><strong>strong</strong> <strong>strong</strong></p></div>';
207 'description' => $md,
211 $data = hook_markdown_render_linklist($data, $this->conf
);
212 $this->assertEquals($html, $data['links'][0]['description']);
216 * Make sure that the HTML tags aren't escaped with the setting set to false.
218 public function testMarkdownWithHtmlNoEscape()
220 $this->conf
->set('security.markdown_escape', false);
221 $md = '**strong** <strong>strong</strong>';
222 $html = '<div class="markdown"><p><strong>strong</strong> <strong>strong</strong></p></div>';
226 'description' => $md,
230 $data = hook_markdown_render_linklist($data, $this->conf
);
231 $this->assertEquals($html, $data['links'][0]['description']);