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