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