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