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