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