]>
Commit | Line | Data |
---|---|---|
1 | <?php | |
2 | use Shaarli\Config\ConfigManager; | |
3 | ||
4 | /** | |
5 | * PluginMarkdownTest.php | |
6 | */ | |
7 | ||
8 | require_once 'application/bookmark/LinkUtils.php'; | |
9 | require_once 'application/Utils.php'; | |
10 | require_once 'plugins/markdown/markdown.php'; | |
11 | ||
12 | /** | |
13 | * Class PluginMarkdownTest | |
14 | * Unit test for the Markdown plugin | |
15 | */ | |
16 | class PluginMarkdownTest extends PHPUnit_Framework_TestCase | |
17 | { | |
18 | /** | |
19 | * @var ConfigManager instance. | |
20 | */ | |
21 | protected $conf; | |
22 | ||
23 | /** | |
24 | * Reset plugin path | |
25 | */ | |
26 | public function setUp() | |
27 | { | |
28 | PluginManager::$PLUGINS_PATH = 'plugins'; | |
29 | $this->conf = new ConfigManager('tests/utils/config/configJson'); | |
30 | $this->conf->set('security.allowed_protocols', ['ftp', 'magnet']); | |
31 | } | |
32 | ||
33 | /** | |
34 | * Test render_linklist hook. | |
35 | * Only check that there is basic markdown rendering. | |
36 | */ | |
37 | public function testMarkdownLinklist() | |
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 | ||
48 | $data = hook_markdown_render_linklist($data, $this->conf); | |
49 | $this->assertNotFalse(strpos($data['links'][0]['description'], '<h1>')); | |
50 | $this->assertNotFalse(strpos($data['links'][0]['description'], '<p>')); | |
51 | ||
52 | $this->assertEquals($markdown, $data['links'][0]['description_src']); | |
53 | } | |
54 | ||
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 | ||
79 | /** | |
80 | * Test render_daily hook. | |
81 | * Only check that there is basic markdown rendering. | |
82 | */ | |
83 | public function testMarkdownDaily() | |
84 | { | |
85 | $markdown = '# My title' . PHP_EOL . 'Very interesting content.'; | |
86 | $data = array( | |
87 | // Columns data | |
88 | 'linksToDisplay' => array( | |
89 | // nth link | |
90 | 0 => array( | |
91 | 'formatedDescription' => $markdown, | |
92 | ), | |
93 | ), | |
94 | ); | |
95 | ||
96 | $data = hook_markdown_render_daily($data, $this->conf); | |
97 | $this->assertNotFalse(strpos($data['linksToDisplay'][0]['formatedDescription'], '<h1>')); | |
98 | $this->assertNotFalse(strpos($data['linksToDisplay'][0]['formatedDescription'], '<p>')); | |
99 | } | |
100 | ||
101 | /** | |
102 | * Test reverse_text2clickable(). | |
103 | */ | |
104 | public function testReverseText2clickable() | |
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 | ||
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 | ||
124 | /** | |
125 | * Test reverse_nl2br(). | |
126 | */ | |
127 | public function testReverseNl2br() | |
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 | */ | |
138 | public function testReverseSpace2nbsp() | |
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 | ||
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 | ||
177 | /** | |
178 | * Test sanitize_html(). | |
179 | */ | |
180 | public function testSanitizeHtml() | |
181 | { | |
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>'; | |
188 | $input .= '<a href="#" onmouseHover=alert(\'xss\'); attr="tt">link</a>'; | |
189 | $output .= '<a href="#" attr="tt">link</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)); | |
194 | } | |
195 | ||
196 | /** | |
197 | * Test the no markdown tag. | |
198 | */ | |
199 | public function testNoMarkdownTag() | |
200 | { | |
201 | $str = 'All _work_ and `no play` makes Jack a *dull* boy.'; | |
202 | $data = array( | |
203 | 'links' => array(array( | |
204 | 'description' => $str, | |
205 | 'tags' => NO_MD_TAG, | |
206 | 'taglist' => array(NO_MD_TAG), | |
207 | )) | |
208 | ); | |
209 | ||
210 | $processed = hook_markdown_render_linklist($data, $this->conf); | |
211 | $this->assertEquals($str, $processed['links'][0]['description']); | |
212 | ||
213 | $processed = hook_markdown_render_feed($data, $this->conf); | |
214 | $this->assertEquals($str, $processed['links'][0]['description']); | |
215 | ||
216 | $data = array( | |
217 | // Columns data | |
218 | 'linksToDisplay' => array( | |
219 | // nth link | |
220 | 0 => array( | |
221 | 'formatedDescription' => $str, | |
222 | 'tags' => NO_MD_TAG, | |
223 | 'taglist' => array(), | |
224 | ), | |
225 | ), | |
226 | ); | |
227 | ||
228 | $data = hook_markdown_render_daily($data, $this->conf); | |
229 | $this->assertEquals($str, $data['linksToDisplay'][0]['formatedDescription']); | |
230 | } | |
231 | ||
232 | /** | |
233 | * Test that a close value to nomarkdown is not understand as nomarkdown (previous value `.nomarkdown`). | |
234 | */ | |
235 | public function testNoMarkdownNotExcactlyMatching() | |
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 | ||
246 | $data = hook_markdown_render_feed($data, $this->conf); | |
247 | $this->assertContains('<em>', $data['links'][0]['description']); | |
248 | } | |
249 | ||
250 | /** | |
251 | * Make sure that the generated HTML match the reference HTML file. | |
252 | */ | |
253 | public function testMarkdownGlobalProcessDescription() | |
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 | ||
259 | $data = process_markdown( | |
260 | $md, | |
261 | $this->conf->get('security.markdown_escape', true), | |
262 | $this->conf->get('security.allowed_protocols') | |
263 | ); | |
264 | $this->assertEquals($html, $data . PHP_EOL); | |
265 | } | |
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 | } | |
303 | } |