]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - tests/plugins/PluginMarkdownTest.php
Add OpenGraph meta tags on permalink page
[github/shaarli/Shaarli.git] / tests / plugins / PluginMarkdownTest.php
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 .= '&#8212; <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 '&#8212; <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_nl2br().
113 */
114 public function testReverseNl2br()
115 {
116 $text = 'stuff' . PHP_EOL . 'otherstuff';
117 $processedText = nl2br($text);
118 $reversedText = reverse_nl2br($processedText);
119 $this->assertEquals($text, $reversedText);
120 }
121
122 /**
123 * Test reverse_space2nbsp().
124 */
125 public function testReverseSpace2nbsp()
126 {
127 $text = ' stuff' . PHP_EOL . ' otherstuff and another';
128 $processedText = space2nbsp($text);
129 $reversedText = reverse_space2nbsp($processedText);
130 $this->assertEquals($text, $reversedText);
131 }
132
133 public function testReverseFeedPermalink()
134 {
135 $text = 'Description... ';
136 $text .= '&#8212; <a href="http://domain.tld/?0oc_VQ" title="Permalien">Permalien</a>';
137 $expected = 'Description... &#8212; [Permalien](http://domain.tld/?0oc_VQ)';
138 $processedText = reverse_feed_permalink($text);
139
140 $this->assertEquals($expected, $processedText);
141 }
142
143 public function testReverseLastFeedPermalink()
144 {
145 $text = 'Description... ';
146 $text .= '<br>&#8212; <a href="http://domain.tld/?0oc_VQ" title="Permalien">Permalien</a>';
147 $expected = $text;
148 $text .= '<br>&#8212; <a href="http://domain.tld/?0oc_VQ" title="Permalien">Permalien</a>';
149 $expected .= '<br>&#8212; [Permalien](http://domain.tld/?0oc_VQ)';
150 $processedText = reverse_feed_permalink($text);
151
152 $this->assertEquals($expected, $processedText);
153 }
154
155 public function testReverseNoFeedPermalink()
156 {
157 $text = 'Hello! Where are you from?';
158 $expected = $text;
159 $processedText = reverse_feed_permalink($text);
160
161 $this->assertEquals($expected, $processedText);
162 }
163
164 /**
165 * Test sanitize_html().
166 */
167 public function testSanitizeHtml()
168 {
169 $input = '< script src="js.js"/>';
170 $input .= '< script attr>alert(\'xss\');</script>';
171 $input .= '<style> * { display: none }</style>';
172 $output = escape($input);
173 $input .= '<a href="#" onmouseHover="alert(\'xss\');" attr="tt">link</a>';
174 $output .= '<a href="#" attr="tt">link</a>';
175 $input .= '<a href="#" onmouseHover=alert(\'xss\'); attr="tt">link</a>';
176 $output .= '<a href="#" attr="tt">link</a>';
177 $this->assertEquals($output, sanitize_html($input));
178 // Do not touch escaped HTML.
179 $input = escape($input);
180 $this->assertEquals($input, sanitize_html($input));
181 }
182
183 /**
184 * Test the no markdown tag.
185 */
186 public function testNoMarkdownTag()
187 {
188 $str = 'All _work_ and `no play` makes Jack a *dull* boy.';
189 $data = array(
190 'links' => array(array(
191 'description' => $str,
192 'tags' => NO_MD_TAG,
193 'taglist' => array(NO_MD_TAG),
194 ))
195 );
196
197 $processed = hook_markdown_render_linklist($data, $this->conf);
198 $this->assertEquals($str, $processed['links'][0]['description']);
199
200 $processed = hook_markdown_render_feed($data, $this->conf);
201 $this->assertEquals($str, $processed['links'][0]['description']);
202
203 $data = array(
204 // Columns data
205 'linksToDisplay' => array(
206 // nth link
207 0 => array(
208 'formatedDescription' => $str,
209 'tags' => NO_MD_TAG,
210 'taglist' => array(),
211 ),
212 ),
213 );
214
215 $data = hook_markdown_render_daily($data, $this->conf);
216 $this->assertEquals($str, $data['linksToDisplay'][0]['formatedDescription']);
217 }
218
219 /**
220 * Test that a close value to nomarkdown is not understand as nomarkdown (previous value `.nomarkdown`).
221 */
222 public function testNoMarkdownNotExcactlyMatching()
223 {
224 $str = 'All _work_ and `no play` makes Jack a *dull* boy.';
225 $data = array(
226 'links' => array(array(
227 'description' => $str,
228 'tags' => '.' . NO_MD_TAG,
229 'taglist' => array('.'. NO_MD_TAG),
230 ))
231 );
232
233 $data = hook_markdown_render_feed($data, $this->conf);
234 $this->assertContains('<em>', $data['links'][0]['description']);
235 }
236
237 /**
238 * Make sure that the generated HTML match the reference HTML file.
239 */
240 public function testMarkdownGlobalProcessDescription()
241 {
242 $md = file_get_contents('tests/plugins/resources/markdown.md');
243 $md = format_description($md);
244 $html = file_get_contents('tests/plugins/resources/markdown.html');
245
246 $data = process_markdown(
247 $md,
248 $this->conf->get('security.markdown_escape', true),
249 $this->conf->get('security.allowed_protocols')
250 );
251 $this->assertEquals($html, $data);
252 }
253
254 /**
255 * Make sure that the HTML tags are escaped.
256 */
257 public function testMarkdownWithHtmlEscape()
258 {
259 $md = '**strong** <strong>strong</strong>';
260 $html = '<div class="markdown"><p><strong>strong</strong> &lt;strong&gt;strong&lt;/strong&gt;</p></div>';
261 $data = array(
262 'links' => array(
263 0 => array(
264 'description' => $md,
265 ),
266 ),
267 );
268 $data = hook_markdown_render_linklist($data, $this->conf);
269 $this->assertEquals($html, $data['links'][0]['description']);
270 }
271
272 /**
273 * Make sure that the HTML tags aren't escaped with the setting set to false.
274 */
275 public function testMarkdownWithHtmlNoEscape()
276 {
277 $this->conf->set('security.markdown_escape', false);
278 $md = '**strong** <strong>strong</strong>';
279 $html = '<div class="markdown"><p><strong>strong</strong> <strong>strong</strong></p></div>';
280 $data = array(
281 'links' => array(
282 0 => array(
283 'description' => $md,
284 ),
285 ),
286 );
287 $data = hook_markdown_render_linklist($data, $this->conf);
288 $this->assertEquals($html, $data['links'][0]['description']);
289 }
290 }