]>
Commit | Line | Data |
---|---|---|
1be4afac | 1 | <?php |
3c66e564 | 2 | use Shaarli\Config\ConfigManager; |
1be4afac A |
3 | |
4 | /** | |
5 | * PluginMarkdownTest.php | |
6 | */ | |
7 | ||
fe3713d2 | 8 | require_once 'application/bookmark/LinkUtils.php'; |
1be4afac A |
9 | require_once 'application/Utils.php'; |
10 | require_once 'plugins/markdown/markdown.php'; | |
11 | ||
12 | /** | |
266e3fe5 A |
13 | * Class PluginMarkdownTest |
14 | * Unit test for the Markdown plugin | |
1be4afac A |
15 | */ |
16 | class PluginMarkdownTest extends PHPUnit_Framework_TestCase | |
17 | { | |
e0376101 A |
18 | /** |
19 | * @var ConfigManager instance. | |
20 | */ | |
21 | protected $conf; | |
22 | ||
1be4afac A |
23 | /** |
24 | * Reset plugin path | |
25 | */ | |
93b1fe54 | 26 | public function setUp() |
1be4afac A |
27 | { |
28 | PluginManager::$PLUGINS_PATH = 'plugins'; | |
e0376101 | 29 | $this->conf = new ConfigManager('tests/utils/config/configJson'); |
86ceea05 | 30 | $this->conf->set('security.allowed_protocols', ['ftp', 'magnet']); |
1be4afac A |
31 | } |
32 | ||
33 | /** | |
34 | * Test render_linklist hook. | |
35 | * Only check that there is basic markdown rendering. | |
36 | */ | |
93b1fe54 | 37 | public function testMarkdownLinklist() |
1be4afac A |
38 | { |
39 | $markdown = '# My title' . PHP_EOL . 'Very interesting content.'; | |
40 | $data = array( | |
41 | 'links' => array( | |
42 | 0 => array( | |
43 | 'description' => $markdown, | |
44 | ), | |
45 | ), | |
46 | ); | |
47 | ||
e0376101 | 48 | $data = hook_markdown_render_linklist($data, $this->conf); |
1be4afac A |
49 | $this->assertNotFalse(strpos($data['links'][0]['description'], '<h1>')); |
50 | $this->assertNotFalse(strpos($data['links'][0]['description'], '<p>')); | |
a120fb29 A |
51 | |
52 | $this->assertEquals($markdown, $data['links'][0]['description_src']); | |
1be4afac A |
53 | } |
54 | ||
dd6794cf A |
55 | /** |
56 | * Test render_feed hook. | |
57 | */ | |
58 | public function testMarkdownFeed() | |
59 | { | |
60 | $markdown = '# My title' . PHP_EOL . 'Very interesting content.'; | |
61 | $markdown .= '— <a href="http://domain.tld/?0oc_VQ" title="Permalien">Permalien</a>'; | |
62 | $data = array( | |
63 | 'links' => array( | |
64 | 0 => array( | |
65 | 'description' => $markdown, | |
66 | ), | |
67 | ), | |
68 | ); | |
69 | ||
70 | $data = hook_markdown_render_feed($data, $this->conf); | |
71 | $this->assertNotFalse(strpos($data['links'][0]['description'], '<h1>')); | |
72 | $this->assertNotFalse(strpos($data['links'][0]['description'], '<p>')); | |
73 | $this->assertStringEndsWith( | |
74 | '— <a href="http://domain.tld/?0oc_VQ">Permalien</a></p></div>', | |
75 | $data['links'][0]['description'] | |
76 | ); | |
77 | } | |
78 | ||
1be4afac A |
79 | /** |
80 | * Test render_daily hook. | |
81 | * Only check that there is basic markdown rendering. | |
82 | */ | |
93b1fe54 | 83 | public function testMarkdownDaily() |
1be4afac A |
84 | { |
85 | $markdown = '# My title' . PHP_EOL . 'Very interesting content.'; | |
86 | $data = array( | |
87 | // Columns data | |
50142efd | 88 | 'linksToDisplay' => array( |
89 | // nth link | |
1be4afac | 90 | 0 => array( |
50142efd | 91 | 'formatedDescription' => $markdown, |
1be4afac A |
92 | ), |
93 | ), | |
94 | ); | |
95 | ||
e0376101 | 96 | $data = hook_markdown_render_daily($data, $this->conf); |
50142efd | 97 | $this->assertNotFalse(strpos($data['linksToDisplay'][0]['formatedDescription'], '<h1>')); |
98 | $this->assertNotFalse(strpos($data['linksToDisplay'][0]['formatedDescription'], '<p>')); | |
1be4afac A |
99 | } |
100 | ||
101 | /** | |
102 | * Test reverse_text2clickable(). | |
103 | */ | |
93b1fe54 | 104 | public function testReverseText2clickable() |
1be4afac A |
105 | { |
106 | $text = 'stuff http://hello.there/is=someone#here otherstuff'; | |
107 | $clickableText = text2clickable($text, ''); | |
108 | $reversedText = reverse_text2clickable($clickableText); | |
109 | $this->assertEquals($text, $reversedText); | |
110 | } | |
111 | ||
cb7940e2 A |
112 | /** |
113 | * Test reverse_text2clickable(). | |
114 | */ | |
115 | public function testReverseText2clickableHashtags() | |
116 | { | |
117 | $text = file_get_contents('tests/plugins/resources/hashtags.raw'); | |
118 | $md = file_get_contents('tests/plugins/resources/hashtags.md'); | |
119 | $clickableText = hashtag_autolink($text); | |
120 | $reversedText = reverse_text2clickable($clickableText); | |
121 | $this->assertEquals($md, $reversedText); | |
122 | } | |
123 | ||
1be4afac A |
124 | /** |
125 | * Test reverse_nl2br(). | |
126 | */ | |
93b1fe54 | 127 | public function testReverseNl2br() |
1be4afac A |
128 | { |
129 | $text = 'stuff' . PHP_EOL . 'otherstuff'; | |
130 | $processedText = nl2br($text); | |
131 | $reversedText = reverse_nl2br($processedText); | |
132 | $this->assertEquals($text, $reversedText); | |
133 | } | |
134 | ||
135 | /** | |
136 | * Test reverse_space2nbsp(). | |
137 | */ | |
93b1fe54 | 138 | public function testReverseSpace2nbsp() |
1be4afac A |
139 | { |
140 | $text = ' stuff' . PHP_EOL . ' otherstuff and another'; | |
141 | $processedText = space2nbsp($text); | |
142 | $reversedText = reverse_space2nbsp($processedText); | |
143 | $this->assertEquals($text, $reversedText); | |
144 | } | |
145 | ||
dd6794cf A |
146 | public function testReverseFeedPermalink() |
147 | { | |
148 | $text = 'Description... '; | |
149 | $text .= '— <a href="http://domain.tld/?0oc_VQ" title="Permalien">Permalien</a>'; | |
150 | $expected = 'Description... — [Permalien](http://domain.tld/?0oc_VQ)'; | |
151 | $processedText = reverse_feed_permalink($text); | |
152 | ||
153 | $this->assertEquals($expected, $processedText); | |
154 | } | |
155 | ||
156 | public function testReverseLastFeedPermalink() | |
157 | { | |
158 | $text = 'Description... '; | |
159 | $text .= '<br>— <a href="http://domain.tld/?0oc_VQ" title="Permalien">Permalien</a>'; | |
160 | $expected = $text; | |
161 | $text .= '<br>— <a href="http://domain.tld/?0oc_VQ" title="Permalien">Permalien</a>'; | |
162 | $expected .= '<br>— [Permalien](http://domain.tld/?0oc_VQ)'; | |
163 | $processedText = reverse_feed_permalink($text); | |
164 | ||
165 | $this->assertEquals($expected, $processedText); | |
166 | } | |
167 | ||
168 | public function testReverseNoFeedPermalink() | |
169 | { | |
170 | $text = 'Hello! Where are you from?'; | |
171 | $expected = $text; | |
172 | $processedText = reverse_feed_permalink($text); | |
173 | ||
174 | $this->assertEquals($expected, $processedText); | |
175 | } | |
176 | ||
1be4afac | 177 | /** |
2925687e | 178 | * Test sanitize_html(). |
1be4afac | 179 | */ |
93b1fe54 | 180 | public function testSanitizeHtml() |
3ce20d9e | 181 | { |
2925687e A |
182 | $input = '< script src="js.js"/>'; |
183 | $input .= '< script attr>alert(\'xss\');</script>'; | |
184 | $input .= '<style> * { display: none }</style>'; | |
185 | $output = escape($input); | |
186 | $input .= '<a href="#" onmouseHover="alert(\'xss\');" attr="tt">link</a>'; | |
187 | $output .= '<a href="#" attr="tt">link</a>'; | |
e0376101 A |
188 | $input .= '<a href="#" onmouseHover=alert(\'xss\'); attr="tt">link</a>'; |
189 | $output .= '<a href="#" attr="tt">link</a>'; | |
2925687e A |
190 | $this->assertEquals($output, sanitize_html($input)); |
191 | // Do not touch escaped HTML. | |
192 | $input = escape($input); | |
193 | $this->assertEquals($input, sanitize_html($input)); | |
1be4afac | 194 | } |
3ce20d9e A |
195 | |
196 | /** | |
197 | * Test the no markdown tag. | |
198 | */ | |
93b1fe54 | 199 | public function testNoMarkdownTag() |
3ce20d9e A |
200 | { |
201 | $str = 'All _work_ and `no play` makes Jack a *dull* boy.'; | |
202 | $data = array( | |
203 | 'links' => array(array( | |
204 | 'description' => $str, | |
8c4e6018 A |
205 | 'tags' => NO_MD_TAG, |
206 | 'taglist' => array(NO_MD_TAG), | |
3ce20d9e A |
207 | )) |
208 | ); | |
209 | ||
e0376101 | 210 | $processed = hook_markdown_render_linklist($data, $this->conf); |
266e3fe5 A |
211 | $this->assertEquals($str, $processed['links'][0]['description']); |
212 | ||
e0376101 | 213 | $processed = hook_markdown_render_feed($data, $this->conf); |
266e3fe5 | 214 | $this->assertEquals($str, $processed['links'][0]['description']); |
3ce20d9e A |
215 | |
216 | $data = array( | |
217 | // Columns data | |
50142efd | 218 | 'linksToDisplay' => array( |
219 | // nth link | |
3ce20d9e | 220 | 0 => array( |
50142efd | 221 | 'formatedDescription' => $str, |
222 | 'tags' => NO_MD_TAG, | |
223 | 'taglist' => array(), | |
3ce20d9e A |
224 | ), |
225 | ), | |
226 | ); | |
227 | ||
e0376101 | 228 | $data = hook_markdown_render_daily($data, $this->conf); |
50142efd | 229 | $this->assertEquals($str, $data['linksToDisplay'][0]['formatedDescription']); |
3ce20d9e | 230 | } |
c5941f31 | 231 | |
266e3fe5 A |
232 | /** |
233 | * Test that a close value to nomarkdown is not understand as nomarkdown (previous value `.nomarkdown`). | |
234 | */ | |
93b1fe54 | 235 | public function testNoMarkdownNotExcactlyMatching() |
266e3fe5 A |
236 | { |
237 | $str = 'All _work_ and `no play` makes Jack a *dull* boy.'; | |
238 | $data = array( | |
239 | 'links' => array(array( | |
240 | 'description' => $str, | |
241 | 'tags' => '.' . NO_MD_TAG, | |
242 | 'taglist' => array('.'. NO_MD_TAG), | |
243 | )) | |
244 | ); | |
245 | ||
e0376101 | 246 | $data = hook_markdown_render_feed($data, $this->conf); |
266e3fe5 A |
247 | $this->assertContains('<em>', $data['links'][0]['description']); |
248 | } | |
249 | ||
c5941f31 | 250 | /** |
86ceea05 | 251 | * Make sure that the generated HTML match the reference HTML file. |
c5941f31 | 252 | */ |
86ceea05 | 253 | public function testMarkdownGlobalProcessDescription() |
c5941f31 A |
254 | { |
255 | $md = file_get_contents('tests/plugins/resources/markdown.md'); | |
256 | $md = format_description($md); | |
257 | $html = file_get_contents('tests/plugins/resources/markdown.html'); | |
258 | ||
86ceea05 A |
259 | $data = process_markdown( |
260 | $md, | |
261 | $this->conf->get('security.markdown_escape', true), | |
262 | $this->conf->get('security.allowed_protocols') | |
263 | ); | |
cb7940e2 | 264 | $this->assertEquals($html, $data . PHP_EOL); |
c5941f31 | 265 | } |
e0376101 A |
266 | |
267 | /** | |
268 | * Make sure that the HTML tags are escaped. | |
269 | */ | |
270 | public function testMarkdownWithHtmlEscape() | |
271 | { | |
272 | $md = '**strong** <strong>strong</strong>'; | |
273 | $html = '<div class="markdown"><p><strong>strong</strong> <strong>strong</strong></p></div>'; | |
274 | $data = array( | |
275 | 'links' => array( | |
276 | 0 => array( | |
277 | 'description' => $md, | |
278 | ), | |
279 | ), | |
280 | ); | |
281 | $data = hook_markdown_render_linklist($data, $this->conf); | |
282 | $this->assertEquals($html, $data['links'][0]['description']); | |
283 | } | |
284 | ||
285 | /** | |
286 | * Make sure that the HTML tags aren't escaped with the setting set to false. | |
287 | */ | |
288 | public function testMarkdownWithHtmlNoEscape() | |
289 | { | |
290 | $this->conf->set('security.markdown_escape', false); | |
291 | $md = '**strong** <strong>strong</strong>'; | |
292 | $html = '<div class="markdown"><p><strong>strong</strong> <strong>strong</strong></p></div>'; | |
293 | $data = array( | |
294 | 'links' => array( | |
295 | 0 => array( | |
296 | 'description' => $md, | |
297 | ), | |
298 | ), | |
299 | ); | |
300 | $data = hook_markdown_render_linklist($data, $this->conf); | |
301 | $this->assertEquals($html, $data['links'][0]['description']); | |
302 | } | |
1be4afac | 303 | } |