]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - tests/plugins/PluginMarkdownTest.php
namespacing: add plugin tests to \Shaarli\Plugin\[...]
[github/shaarli/Shaarli.git] / tests / plugins / PluginMarkdownTest.php
CommitLineData
1be4afac 1<?php
95854417
V
2namespace Shaarli\Plugin\Markdown;
3
3c66e564 4use Shaarli\Config\ConfigManager;
e1850388 5use Shaarli\Plugin\PluginManager;
1be4afac
A
6
7/**
8 * PluginMarkdownTest.php
9 */
10
fe3713d2 11require_once 'application/bookmark/LinkUtils.php';
1be4afac
A
12require_once 'application/Utils.php';
13require_once 'plugins/markdown/markdown.php';
14
15/**
266e3fe5
A
16 * Class PluginMarkdownTest
17 * Unit test for the Markdown plugin
1be4afac 18 */
95854417 19class PluginMarkdownTest extends \PHPUnit\Framework\TestCase
1be4afac 20{
e0376101
A
21 /**
22 * @var ConfigManager instance.
23 */
24 protected $conf;
25
1be4afac
A
26 /**
27 * Reset plugin path
28 */
93b1fe54 29 public function setUp()
1be4afac
A
30 {
31 PluginManager::$PLUGINS_PATH = 'plugins';
e0376101 32 $this->conf = new ConfigManager('tests/utils/config/configJson');
86ceea05 33 $this->conf->set('security.allowed_protocols', ['ftp', 'magnet']);
1be4afac
A
34 }
35
36 /**
37 * Test render_linklist hook.
38 * Only check that there is basic markdown rendering.
39 */
93b1fe54 40 public function testMarkdownLinklist()
1be4afac
A
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
e0376101 51 $data = hook_markdown_render_linklist($data, $this->conf);
1be4afac
A
52 $this->assertNotFalse(strpos($data['links'][0]['description'], '<h1>'));
53 $this->assertNotFalse(strpos($data['links'][0]['description'], '<p>'));
a120fb29
A
54
55 $this->assertEquals($markdown, $data['links'][0]['description_src']);
1be4afac
A
56 }
57
dd6794cf
A
58 /**
59 * Test render_feed hook.
60 */
61 public function testMarkdownFeed()
62 {
63 $markdown = '# My title' . PHP_EOL . 'Very interesting content.';
64 $markdown .= '&#8212; <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 '&#8212; <a href="http://domain.tld/?0oc_VQ">Permalien</a></p></div>',
78 $data['links'][0]['description']
79 );
80 }
81
1be4afac
A
82 /**
83 * Test render_daily hook.
84 * Only check that there is basic markdown rendering.
85 */
93b1fe54 86 public function testMarkdownDaily()
1be4afac
A
87 {
88 $markdown = '# My title' . PHP_EOL . 'Very interesting content.';
89 $data = array(
90 // Columns data
50142efd 91 'linksToDisplay' => array(
92 // nth link
1be4afac 93 0 => array(
50142efd 94 'formatedDescription' => $markdown,
1be4afac
A
95 ),
96 ),
97 );
98
e0376101 99 $data = hook_markdown_render_daily($data, $this->conf);
50142efd 100 $this->assertNotFalse(strpos($data['linksToDisplay'][0]['formatedDescription'], '<h1>'));
101 $this->assertNotFalse(strpos($data['linksToDisplay'][0]['formatedDescription'], '<p>'));
1be4afac
A
102 }
103
104 /**
105 * Test reverse_text2clickable().
106 */
93b1fe54 107 public function testReverseText2clickable()
1be4afac
A
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
cb7940e2
A
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
1be4afac
A
127 /**
128 * Test reverse_nl2br().
129 */
93b1fe54 130 public function testReverseNl2br()
1be4afac
A
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 */
93b1fe54 141 public function testReverseSpace2nbsp()
1be4afac
A
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
dd6794cf
A
149 public function testReverseFeedPermalink()
150 {
151 $text = 'Description... ';
152 $text .= '&#8212; <a href="http://domain.tld/?0oc_VQ" title="Permalien">Permalien</a>';
153 $expected = 'Description... &#8212; [Permalien](http://domain.tld/?0oc_VQ)';
154 $processedText = reverse_feed_permalink($text);
155
156 $this->assertEquals($expected, $processedText);
157 }
158
159 public function testReverseLastFeedPermalink()
160 {
161 $text = 'Description... ';
162 $text .= '<br>&#8212; <a href="http://domain.tld/?0oc_VQ" title="Permalien">Permalien</a>';
163 $expected = $text;
164 $text .= '<br>&#8212; <a href="http://domain.tld/?0oc_VQ" title="Permalien">Permalien</a>';
165 $expected .= '<br>&#8212; [Permalien](http://domain.tld/?0oc_VQ)';
166 $processedText = reverse_feed_permalink($text);
167
168 $this->assertEquals($expected, $processedText);
169 }
170
171 public function testReverseNoFeedPermalink()
172 {
173 $text = 'Hello! Where are you from?';
174 $expected = $text;
175 $processedText = reverse_feed_permalink($text);
176
177 $this->assertEquals($expected, $processedText);
178 }
179
1be4afac 180 /**
2925687e 181 * Test sanitize_html().
1be4afac 182 */
93b1fe54 183 public function testSanitizeHtml()
3ce20d9e 184 {
2925687e
A
185 $input = '< script src="js.js"/>';
186 $input .= '< script attr>alert(\'xss\');</script>';
187 $input .= '<style> * { display: none }</style>';
188 $output = escape($input);
189 $input .= '<a href="#" onmouseHover="alert(\'xss\');" attr="tt">link</a>';
190 $output .= '<a href="#" attr="tt">link</a>';
e0376101
A
191 $input .= '<a href="#" onmouseHover=alert(\'xss\'); attr="tt">link</a>';
192 $output .= '<a href="#" attr="tt">link</a>';
2925687e
A
193 $this->assertEquals($output, sanitize_html($input));
194 // Do not touch escaped HTML.
195 $input = escape($input);
196 $this->assertEquals($input, sanitize_html($input));
1be4afac 197 }
3ce20d9e
A
198
199 /**
200 * Test the no markdown tag.
201 */
93b1fe54 202 public function testNoMarkdownTag()
3ce20d9e
A
203 {
204 $str = 'All _work_ and `no play` makes Jack a *dull* boy.';
205 $data = array(
206 'links' => array(array(
207 'description' => $str,
8c4e6018
A
208 'tags' => NO_MD_TAG,
209 'taglist' => array(NO_MD_TAG),
3ce20d9e
A
210 ))
211 );
212
e0376101 213 $processed = hook_markdown_render_linklist($data, $this->conf);
266e3fe5
A
214 $this->assertEquals($str, $processed['links'][0]['description']);
215
e0376101 216 $processed = hook_markdown_render_feed($data, $this->conf);
266e3fe5 217 $this->assertEquals($str, $processed['links'][0]['description']);
3ce20d9e
A
218
219 $data = array(
220 // Columns data
50142efd 221 'linksToDisplay' => array(
222 // nth link
3ce20d9e 223 0 => array(
50142efd 224 'formatedDescription' => $str,
225 'tags' => NO_MD_TAG,
226 'taglist' => array(),
3ce20d9e
A
227 ),
228 ),
229 );
230
e0376101 231 $data = hook_markdown_render_daily($data, $this->conf);
50142efd 232 $this->assertEquals($str, $data['linksToDisplay'][0]['formatedDescription']);
3ce20d9e 233 }
c5941f31 234
266e3fe5
A
235 /**
236 * Test that a close value to nomarkdown is not understand as nomarkdown (previous value `.nomarkdown`).
237 */
93b1fe54 238 public function testNoMarkdownNotExcactlyMatching()
266e3fe5
A
239 {
240 $str = 'All _work_ and `no play` makes Jack a *dull* boy.';
241 $data = array(
242 'links' => array(array(
243 'description' => $str,
244 'tags' => '.' . NO_MD_TAG,
245 'taglist' => array('.'. NO_MD_TAG),
246 ))
247 );
248
e0376101 249 $data = hook_markdown_render_feed($data, $this->conf);
266e3fe5
A
250 $this->assertContains('<em>', $data['links'][0]['description']);
251 }
252
c5941f31 253 /**
86ceea05 254 * Make sure that the generated HTML match the reference HTML file.
c5941f31 255 */
86ceea05 256 public function testMarkdownGlobalProcessDescription()
c5941f31
A
257 {
258 $md = file_get_contents('tests/plugins/resources/markdown.md');
259 $md = format_description($md);
260 $html = file_get_contents('tests/plugins/resources/markdown.html');
261
86ceea05
A
262 $data = process_markdown(
263 $md,
264 $this->conf->get('security.markdown_escape', true),
265 $this->conf->get('security.allowed_protocols')
266 );
cb7940e2 267 $this->assertEquals($html, $data . PHP_EOL);
c5941f31 268 }
e0376101
A
269
270 /**
271 * Make sure that the HTML tags are escaped.
272 */
273 public function testMarkdownWithHtmlEscape()
274 {
275 $md = '**strong** <strong>strong</strong>';
276 $html = '<div class="markdown"><p><strong>strong</strong> &lt;strong&gt;strong&lt;/strong&gt;</p></div>';
277 $data = array(
278 'links' => array(
279 0 => array(
280 'description' => $md,
281 ),
282 ),
283 );
284 $data = hook_markdown_render_linklist($data, $this->conf);
285 $this->assertEquals($html, $data['links'][0]['description']);
286 }
287
288 /**
289 * Make sure that the HTML tags aren't escaped with the setting set to false.
290 */
291 public function testMarkdownWithHtmlNoEscape()
292 {
293 $this->conf->set('security.markdown_escape', false);
294 $md = '**strong** <strong>strong</strong>';
295 $html = '<div class="markdown"><p><strong>strong</strong> <strong>strong</strong></p></div>';
296 $data = array(
297 'links' => array(
298 0 => array(
299 'description' => $md,
300 ),
301 ),
302 );
303 $data = hook_markdown_render_linklist($data, $this->conf);
304 $this->assertEquals($html, $data['links'][0]['description']);
305 }
1be4afac 306}