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');
32 * Test render_linklist hook.
33 * Only check that there is basic markdown rendering.
35 public function testMarkdownLinklist()
37 $markdown = '# My title' . PHP_EOL
. 'Very interesting content.';
41 'description' => $markdown,
46 $data = hook_markdown_render_linklist($data, $this->conf
);
47 $this->assertNotFalse(strpos($data['links'][0]['description'], '<h1>'));
48 $this->assertNotFalse(strpos($data['links'][0]['description'], '<p>'));
52 * Test render_daily hook.
53 * Only check that there is basic markdown rendering.
55 public function testMarkdownDaily()
57 $markdown = '# My title' . PHP_EOL
. 'Very interesting content.';
61 // First, second, third.
65 'formatedDescription' => $markdown,
71 $data = hook_markdown_render_daily($data, $this->conf
);
72 $this->assertNotFalse(strpos($data['cols'][0][0]['formatedDescription'], '<h1>'));
73 $this->assertNotFalse(strpos($data['cols'][0][0]['formatedDescription'], '<p>'));
77 * Test reverse_text2clickable().
79 public function testReverseText2clickable()
81 $text = 'stuff http://hello.there/is=someone#here otherstuff';
82 $clickableText = text2clickable($text, '');
83 $reversedText = reverse_text2clickable($clickableText);
84 $this->assertEquals($text, $reversedText);
88 * Test reverse_nl2br().
90 public function testReverseNl2br()
92 $text = 'stuff' . PHP_EOL
. 'otherstuff';
93 $processedText = nl2br($text);
94 $reversedText = reverse_nl2br($processedText);
95 $this->assertEquals($text, $reversedText);
99 * Test reverse_space2nbsp().
101 public function testReverseSpace2nbsp()
103 $text = ' stuff' . PHP_EOL
. ' otherstuff and another';
104 $processedText = space2nbsp($text);
105 $reversedText = reverse_space2nbsp($processedText);
106 $this->assertEquals($text, $reversedText);
110 * Test sanitize_html().
112 public function testSanitizeHtml()
114 $input = '< script src="js.js"/>';
115 $input .= '< script attr>alert(\'xss\');</script>';
116 $input .= '<style> * { display: none }</style>';
117 $output = escape($input);
118 $input .= '<a href="#" onmouseHover="alert(\'xss\');" attr="tt">link</a>';
119 $output .= '<a href="#" attr="tt">link</a>';
120 $input .= '<a href="#" onmouseHover=alert(\'xss\'); attr="tt">link</a>';
121 $output .= '<a href="#" attr="tt">link</a>';
122 $this->assertEquals($output, sanitize_html($input));
123 // Do not touch escaped HTML.
124 $input = escape($input);
125 $this->assertEquals($input, sanitize_html($input));
129 * Test the no markdown tag.
131 public function testNoMarkdownTag()
133 $str = 'All _work_ and `no play` makes Jack a *dull* boy.';
135 'links' => array(array(
136 'description' => $str,
138 'taglist' => array(NO_MD_TAG
),
142 $processed = hook_markdown_render_linklist($data, $this->conf
);
143 $this->assertEquals($str, $processed['links'][0]['description']);
145 $processed = hook_markdown_render_feed($data, $this->conf
);
146 $this->assertEquals($str, $processed['links'][0]['description']);
151 // First, second, third.
155 'formatedDescription' => $str,
157 'taglist' => array(),
163 $data = hook_markdown_render_daily($data, $this->conf
);
164 $this->assertEquals($str, $data['cols'][0][0]['formatedDescription']);
168 * Test that a close value to nomarkdown is not understand as nomarkdown (previous value `.nomarkdown`).
170 public function testNoMarkdownNotExcactlyMatching()
172 $str = 'All _work_ and `no play` makes Jack a *dull* boy.';
174 'links' => array(array(
175 'description' => $str,
176 'tags' => '.' . NO_MD_TAG
,
177 'taglist' => array('.'. NO_MD_TAG
),
181 $data = hook_markdown_render_feed($data, $this->conf
);
182 $this->assertContains('<em>', $data['links'][0]['description']);
186 * Test hashtag links processed with markdown.
188 public function testMarkdownHashtagLinks()
190 $md = file_get_contents('tests/plugins/resources/markdown.md');
191 $md = format_description($md);
192 $html = file_get_contents('tests/plugins/resources/markdown.html');
194 $data = process_markdown($md);
195 $this->assertEquals($html, $data);
199 * Make sure that the HTML tags are escaped.
201 public function testMarkdownWithHtmlEscape()
203 $md = '**strong** <strong>strong</strong>';
204 $html = '<div class="markdown"><p><strong>strong</strong> <strong>strong</strong></p></div>';
208 'description' => $md,
212 $data = hook_markdown_render_linklist($data, $this->conf
);
213 $this->assertEquals($html, $data['links'][0]['description']);
217 * Make sure that the HTML tags aren't escaped with the setting set to false.
219 public function testMarkdownWithHtmlNoEscape()
221 $this->conf
->set('security.markdown_escape', false);
222 $md = '**strong** <strong>strong</strong>';
223 $html = '<div class="markdown"><p><strong>strong</strong> <strong>strong</strong></p></div>';
227 'description' => $md,
231 $data = hook_markdown_render_linklist($data, $this->conf
);
232 $this->assertEquals($html, $data['links'][0]['description']);