]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - tests/plugins/PluginMarkdownTest.php
Add a whitelist of protocols for URLs
[github/shaarli/Shaarli.git] / tests / plugins / PluginMarkdownTest.php
CommitLineData
1be4afac 1<?php
3c66e564 2use Shaarli\Config\ConfigManager;
1be4afac
A
3
4/**
5 * PluginMarkdownTest.php
6 */
7
8require_once 'application/Utils.php';
9require_once 'plugins/markdown/markdown.php';
10
11/**
266e3fe5
A
12 * Class PluginMarkdownTest
13 * Unit test for the Markdown plugin
1be4afac
A
14 */
15class PluginMarkdownTest extends PHPUnit_Framework_TestCase
16{
e0376101
A
17 /**
18 * @var ConfigManager instance.
19 */
20 protected $conf;
21
1be4afac
A
22 /**
23 * Reset plugin path
24 */
93b1fe54 25 public function setUp()
1be4afac
A
26 {
27 PluginManager::$PLUGINS_PATH = 'plugins';
e0376101 28 $this->conf = new ConfigManager('tests/utils/config/configJson');
86ceea05 29 $this->conf->set('security.allowed_protocols', ['ftp', 'magnet']);
1be4afac
A
30 }
31
32 /**
33 * Test render_linklist hook.
34 * Only check that there is basic markdown rendering.
35 */
93b1fe54 36 public function testMarkdownLinklist()
1be4afac
A
37 {
38 $markdown = '# My title' . PHP_EOL . 'Very interesting content.';
39 $data = array(
40 'links' => array(
41 0 => array(
42 'description' => $markdown,
43 ),
44 ),
45 );
46
e0376101 47 $data = hook_markdown_render_linklist($data, $this->conf);
1be4afac
A
48 $this->assertNotFalse(strpos($data['links'][0]['description'], '<h1>'));
49 $this->assertNotFalse(strpos($data['links'][0]['description'], '<p>'));
50 }
51
52 /**
53 * Test render_daily hook.
54 * Only check that there is basic markdown rendering.
55 */
93b1fe54 56 public function testMarkdownDaily()
1be4afac
A
57 {
58 $markdown = '# My title' . PHP_EOL . 'Very interesting content.';
59 $data = array(
60 // Columns data
61 'cols' => array(
62 // First, second, third.
63 0 => array(
64 // nth link
65 0 => array(
66 'formatedDescription' => $markdown,
67 ),
68 ),
69 ),
70 );
71
e0376101 72 $data = hook_markdown_render_daily($data, $this->conf);
1be4afac
A
73 $this->assertNotFalse(strpos($data['cols'][0][0]['formatedDescription'], '<h1>'));
74 $this->assertNotFalse(strpos($data['cols'][0][0]['formatedDescription'], '<p>'));
75 }
76
77 /**
78 * Test reverse_text2clickable().
79 */
93b1fe54 80 public function testReverseText2clickable()
1be4afac
A
81 {
82 $text = 'stuff http://hello.there/is=someone#here otherstuff';
83 $clickableText = text2clickable($text, '');
84 $reversedText = reverse_text2clickable($clickableText);
85 $this->assertEquals($text, $reversedText);
86 }
87
88 /**
89 * Test reverse_nl2br().
90 */
93b1fe54 91 public function testReverseNl2br()
1be4afac
A
92 {
93 $text = 'stuff' . PHP_EOL . 'otherstuff';
94 $processedText = nl2br($text);
95 $reversedText = reverse_nl2br($processedText);
96 $this->assertEquals($text, $reversedText);
97 }
98
99 /**
100 * Test reverse_space2nbsp().
101 */
93b1fe54 102 public function testReverseSpace2nbsp()
1be4afac
A
103 {
104 $text = ' stuff' . PHP_EOL . ' otherstuff and another';
105 $processedText = space2nbsp($text);
106 $reversedText = reverse_space2nbsp($processedText);
107 $this->assertEquals($text, $reversedText);
108 }
109
110 /**
2925687e 111 * Test sanitize_html().
1be4afac 112 */
93b1fe54 113 public function testSanitizeHtml()
3ce20d9e 114 {
2925687e
A
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>';
e0376101
A
121 $input .= '<a href="#" onmouseHover=alert(\'xss\'); attr="tt">link</a>';
122 $output .= '<a href="#" attr="tt">link</a>';
2925687e
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));
1be4afac 127 }
3ce20d9e
A
128
129 /**
130 * Test the no markdown tag.
131 */
93b1fe54 132 public function testNoMarkdownTag()
3ce20d9e
A
133 {
134 $str = 'All _work_ and `no play` makes Jack a *dull* boy.';
135 $data = array(
136 'links' => array(array(
137 'description' => $str,
8c4e6018
A
138 'tags' => NO_MD_TAG,
139 'taglist' => array(NO_MD_TAG),
3ce20d9e
A
140 ))
141 );
142
e0376101 143 $processed = hook_markdown_render_linklist($data, $this->conf);
266e3fe5
A
144 $this->assertEquals($str, $processed['links'][0]['description']);
145
e0376101 146 $processed = hook_markdown_render_feed($data, $this->conf);
266e3fe5 147 $this->assertEquals($str, $processed['links'][0]['description']);
3ce20d9e
A
148
149 $data = array(
150 // Columns data
151 'cols' => array(
152 // First, second, third.
153 0 => array(
154 // nth link
155 0 => array(
156 'formatedDescription' => $str,
8c4e6018
A
157 'tags' => NO_MD_TAG,
158 'taglist' => array(),
3ce20d9e
A
159 ),
160 ),
161 ),
162 );
163
e0376101 164 $data = hook_markdown_render_daily($data, $this->conf);
3ce20d9e
A
165 $this->assertEquals($str, $data['cols'][0][0]['formatedDescription']);
166 }
c5941f31 167
266e3fe5
A
168 /**
169 * Test that a close value to nomarkdown is not understand as nomarkdown (previous value `.nomarkdown`).
170 */
93b1fe54 171 public function testNoMarkdownNotExcactlyMatching()
266e3fe5
A
172 {
173 $str = 'All _work_ and `no play` makes Jack a *dull* boy.';
174 $data = array(
175 'links' => array(array(
176 'description' => $str,
177 'tags' => '.' . NO_MD_TAG,
178 'taglist' => array('.'. NO_MD_TAG),
179 ))
180 );
181
e0376101 182 $data = hook_markdown_render_feed($data, $this->conf);
266e3fe5
A
183 $this->assertContains('<em>', $data['links'][0]['description']);
184 }
185
c5941f31 186 /**
86ceea05 187 * Make sure that the generated HTML match the reference HTML file.
c5941f31 188 */
86ceea05 189 public function testMarkdownGlobalProcessDescription()
c5941f31
A
190 {
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');
194
86ceea05
A
195 $data = process_markdown(
196 $md,
197 $this->conf->get('security.markdown_escape', true),
198 $this->conf->get('security.allowed_protocols')
199 );
c5941f31
A
200 $this->assertEquals($html, $data);
201 }
e0376101
A
202
203 /**
204 * Make sure that the HTML tags are escaped.
205 */
206 public function testMarkdownWithHtmlEscape()
207 {
208 $md = '**strong** <strong>strong</strong>';
209 $html = '<div class="markdown"><p><strong>strong</strong> &lt;strong&gt;strong&lt;/strong&gt;</p></div>';
210 $data = array(
211 'links' => array(
212 0 => array(
213 'description' => $md,
214 ),
215 ),
216 );
217 $data = hook_markdown_render_linklist($data, $this->conf);
218 $this->assertEquals($html, $data['links'][0]['description']);
219 }
220
221 /**
222 * Make sure that the HTML tags aren't escaped with the setting set to false.
223 */
224 public function testMarkdownWithHtmlNoEscape()
225 {
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>';
229 $data = array(
230 'links' => array(
231 0 => array(
232 'description' => $md,
233 ),
234 ),
235 );
236 $data = hook_markdown_render_linklist($data, $this->conf);
237 $this->assertEquals($html, $data['links'][0]['description']);
238 }
1be4afac 239}