]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - tests/plugins/PluginMarkdownTest.php
namespacing: add plugin tests to \Shaarli\Plugin\[...]
[github/shaarli/Shaarli.git] / tests / plugins / PluginMarkdownTest.php
1 <?php
2 namespace Shaarli\Plugin\Markdown;
3
4 use Shaarli\Config\ConfigManager;
5 use Shaarli\Plugin\PluginManager;
6
7 /**
8 * PluginMarkdownTest.php
9 */
10
11 require_once 'application/bookmark/LinkUtils.php';
12 require_once 'application/Utils.php';
13 require_once 'plugins/markdown/markdown.php';
14
15 /**
16 * Class PluginMarkdownTest
17 * Unit test for the Markdown plugin
18 */
19 class PluginMarkdownTest extends \PHPUnit\Framework\TestCase
20 {
21 /**
22 * @var ConfigManager instance.
23 */
24 protected $conf;
25
26 /**
27 * Reset plugin path
28 */
29 public function setUp()
30 {
31 PluginManager::$PLUGINS_PATH = 'plugins';
32 $this->conf = new ConfigManager('tests/utils/config/configJson');
33 $this->conf->set('security.allowed_protocols', ['ftp', 'magnet']);
34 }
35
36 /**
37 * Test render_linklist hook.
38 * Only check that there is basic markdown rendering.
39 */
40 public function testMarkdownLinklist()
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
51 $data = hook_markdown_render_linklist($data, $this->conf);
52 $this->assertNotFalse(strpos($data['links'][0]['description'], '<h1>'));
53 $this->assertNotFalse(strpos($data['links'][0]['description'], '<p>'));
54
55 $this->assertEquals($markdown, $data['links'][0]['description_src']);
56 }
57
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
82 /**
83 * Test render_daily hook.
84 * Only check that there is basic markdown rendering.
85 */
86 public function testMarkdownDaily()
87 {
88 $markdown = '# My title' . PHP_EOL . 'Very interesting content.';
89 $data = array(
90 // Columns data
91 'linksToDisplay' => array(
92 // nth link
93 0 => array(
94 'formatedDescription' => $markdown,
95 ),
96 ),
97 );
98
99 $data = hook_markdown_render_daily($data, $this->conf);
100 $this->assertNotFalse(strpos($data['linksToDisplay'][0]['formatedDescription'], '<h1>'));
101 $this->assertNotFalse(strpos($data['linksToDisplay'][0]['formatedDescription'], '<p>'));
102 }
103
104 /**
105 * Test reverse_text2clickable().
106 */
107 public function testReverseText2clickable()
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
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
127 /**
128 * Test reverse_nl2br().
129 */
130 public function testReverseNl2br()
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 */
141 public function testReverseSpace2nbsp()
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
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
180 /**
181 * Test sanitize_html().
182 */
183 public function testSanitizeHtml()
184 {
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>';
191 $input .= '<a href="#" onmouseHover=alert(\'xss\'); attr="tt">link</a>';
192 $output .= '<a href="#" attr="tt">link</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));
197 }
198
199 /**
200 * Test the no markdown tag.
201 */
202 public function testNoMarkdownTag()
203 {
204 $str = 'All _work_ and `no play` makes Jack a *dull* boy.';
205 $data = array(
206 'links' => array(array(
207 'description' => $str,
208 'tags' => NO_MD_TAG,
209 'taglist' => array(NO_MD_TAG),
210 ))
211 );
212
213 $processed = hook_markdown_render_linklist($data, $this->conf);
214 $this->assertEquals($str, $processed['links'][0]['description']);
215
216 $processed = hook_markdown_render_feed($data, $this->conf);
217 $this->assertEquals($str, $processed['links'][0]['description']);
218
219 $data = array(
220 // Columns data
221 'linksToDisplay' => array(
222 // nth link
223 0 => array(
224 'formatedDescription' => $str,
225 'tags' => NO_MD_TAG,
226 'taglist' => array(),
227 ),
228 ),
229 );
230
231 $data = hook_markdown_render_daily($data, $this->conf);
232 $this->assertEquals($str, $data['linksToDisplay'][0]['formatedDescription']);
233 }
234
235 /**
236 * Test that a close value to nomarkdown is not understand as nomarkdown (previous value `.nomarkdown`).
237 */
238 public function testNoMarkdownNotExcactlyMatching()
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
249 $data = hook_markdown_render_feed($data, $this->conf);
250 $this->assertContains('<em>', $data['links'][0]['description']);
251 }
252
253 /**
254 * Make sure that the generated HTML match the reference HTML file.
255 */
256 public function testMarkdownGlobalProcessDescription()
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
262 $data = process_markdown(
263 $md,
264 $this->conf->get('security.markdown_escape', true),
265 $this->conf->get('security.allowed_protocols')
266 );
267 $this->assertEquals($html, $data . PHP_EOL);
268 }
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 }
306 }