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.';
62 // First, second, third.
66 'formatedDescription' => $markdown,
72 $data = hook_markdown_render_daily($data, $this->conf
);
73 $this->assertNotFalse(strpos($data['cols'][0][0]['formatedDescription'], '<h1>'));
74 $this->assertNotFalse(strpos($data['cols'][0][0]['formatedDescription'], '<p>'));
78 * Test reverse_text2clickable().
80 public function testReverseText2clickable()
82 $text = 'stuff http://hello.there/is=someone#here otherstuff';
83 $clickableText = text2clickable($text, '');
84 $reversedText = reverse_text2clickable($clickableText);
85 $this->assertEquals($text, $reversedText);
89 * Test reverse_nl2br().
91 public function testReverseNl2br()
93 $text = 'stuff' . PHP_EOL
. 'otherstuff';
94 $processedText = nl2br($text);
95 $reversedText = reverse_nl2br($processedText);
96 $this->assertEquals($text, $reversedText);
100 * Test reverse_space2nbsp().
102 public function testReverseSpace2nbsp()
104 $text = ' stuff' . PHP_EOL
. ' otherstuff and another';
105 $processedText = space2nbsp($text);
106 $reversedText = reverse_space2nbsp($processedText);
107 $this->assertEquals($text, $reversedText);
111 * Test sanitize_html().
113 public function testSanitizeHtml()
115 $input = '< script src="js.js"/>';
116 $input .= '< script attr>alert(\'xss\');</script>';
117 $input .= '<style> * { display: none }</style>';
118 $output = escape($input);
119 $input .= '<a href="#" onmouseHover="alert(\'xss\');" attr="tt">link</a>';
120 $output .= '<a href="#" attr="tt">link</a>';
121 $input .= '<a href="#" onmouseHover=alert(\'xss\'); attr="tt">link</a>';
122 $output .= '<a href="#" attr="tt">link</a>';
123 $this->assertEquals($output, sanitize_html($input));
124 // Do not touch escaped HTML.
125 $input = escape($input);
126 $this->assertEquals($input, sanitize_html($input));
130 * Test the no markdown tag.
132 public function testNoMarkdownTag()
134 $str = 'All _work_ and `no play` makes Jack a *dull* boy.';
136 'links' => array(array(
137 'description' => $str,
139 'taglist' => array(NO_MD_TAG
),
143 $processed = hook_markdown_render_linklist($data, $this->conf
);
144 $this->assertEquals($str, $processed['links'][0]['description']);
146 $processed = hook_markdown_render_feed($data, $this->conf
);
147 $this->assertEquals($str, $processed['links'][0]['description']);
152 // First, second, third.
156 'formatedDescription' => $str,
158 'taglist' => array(),
164 $data = hook_markdown_render_daily($data, $this->conf
);
165 $this->assertEquals($str, $data['cols'][0][0]['formatedDescription']);
169 * Test that a close value to nomarkdown is not understand as nomarkdown (previous value `.nomarkdown`).
171 public function testNoMarkdownNotExcactlyMatching()
173 $str = 'All _work_ and `no play` makes Jack a *dull* boy.';
175 'links' => array(array(
176 'description' => $str,
177 'tags' => '.' . NO_MD_TAG
,
178 'taglist' => array('.'. NO_MD_TAG
),
182 $data = hook_markdown_render_feed($data, $this->conf
);
183 $this->assertContains('<em>', $data['links'][0]['description']);
187 * Make sure that the generated HTML match the reference HTML file.
189 public function testMarkdownGlobalProcessDescription()
191 $md = file_get_contents('tests/plugins/resources/markdown.md');
192 $md = format_description($md);
193 $html = file_get_contents('tests/plugins/resources/markdown.html');
195 $data = process_markdown(
197 $this->conf
->get('security.markdown_escape', true),
198 $this->conf
->get('security.allowed_protocols')
200 $this->assertEquals($html, $data);
204 * Make sure that the HTML tags are escaped.
206 public function testMarkdownWithHtmlEscape()
208 $md = '**strong** <strong>strong</strong>';
209 $html = '<div class="markdown"><p><strong>strong</strong> <strong>strong</strong></p></div>';
213 'description' => $md,
217 $data = hook_markdown_render_linklist($data, $this->conf
);
218 $this->assertEquals($html, $data['links'][0]['description']);
222 * Make sure that the HTML tags aren't escaped with the setting set to false.
224 public function testMarkdownWithHtmlNoEscape()
226 $this->conf
->set('security.markdown_escape', false);
227 $md = '**strong** <strong>strong</strong>';
228 $html = '<div class="markdown"><p><strong>strong</strong> <strong>strong</strong></p></div>';
232 'description' => $md,
236 $data = hook_markdown_render_linklist($data, $this->conf
);
237 $this->assertEquals($html, $data['links'][0]['description']);