]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - tests/plugins/PluginMarkdownTest.php
Merge pull request #1143 from ArthurHoaro/sort-equal-tags
[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
52 /**
53 * Test render_feed hook.
54 */
55 public function testMarkdownFeed()
56 {
57 $markdown = '# My title' . PHP_EOL . 'Very interesting content.';
58 $markdown .= '&#8212; <a href="http://domain.tld/?0oc_VQ" title="Permalien">Permalien</a>';
59 $data = array(
60 'links' => array(
61 0 => array(
62 'description' => $markdown,
63 ),
64 ),
65 );
66
67 $data = hook_markdown_render_feed($data, $this->conf);
68 $this->assertNotFalse(strpos($data['links'][0]['description'], '<h1>'));
69 $this->assertNotFalse(strpos($data['links'][0]['description'], '<p>'));
70 $this->assertStringEndsWith(
71 '&#8212; <a href="http://domain.tld/?0oc_VQ">Permalien</a></p></div>',
72 $data['links'][0]['description']
73 );
74 }
75
76 /**
77 * Test render_daily hook.
78 * Only check that there is basic markdown rendering.
79 */
80 public function testMarkdownDaily()
81 {
82 $markdown = '# My title' . PHP_EOL . 'Very interesting content.';
83 $data = array(
84 // Columns data
85 'linksToDisplay' => array(
86 // nth link
87 0 => array(
88 'formatedDescription' => $markdown,
89 ),
90 ),
91 );
92
93 $data = hook_markdown_render_daily($data, $this->conf);
94 $this->assertNotFalse(strpos($data['linksToDisplay'][0]['formatedDescription'], '<h1>'));
95 $this->assertNotFalse(strpos($data['linksToDisplay'][0]['formatedDescription'], '<p>'));
96 }
97
98 /**
99 * Test reverse_text2clickable().
100 */
101 public function testReverseText2clickable()
102 {
103 $text = 'stuff http://hello.there/is=someone#here otherstuff';
104 $clickableText = text2clickable($text, '');
105 $reversedText = reverse_text2clickable($clickableText);
106 $this->assertEquals($text, $reversedText);
107 }
108
109 /**
110 * Test reverse_nl2br().
111 */
112 public function testReverseNl2br()
113 {
114 $text = 'stuff' . PHP_EOL . 'otherstuff';
115 $processedText = nl2br($text);
116 $reversedText = reverse_nl2br($processedText);
117 $this->assertEquals($text, $reversedText);
118 }
119
120 /**
121 * Test reverse_space2nbsp().
122 */
123 public function testReverseSpace2nbsp()
124 {
125 $text = ' stuff' . PHP_EOL . ' otherstuff and another';
126 $processedText = space2nbsp($text);
127 $reversedText = reverse_space2nbsp($processedText);
128 $this->assertEquals($text, $reversedText);
129 }
130
131 public function testReverseFeedPermalink()
132 {
133 $text = 'Description... ';
134 $text .= '&#8212; <a href="http://domain.tld/?0oc_VQ" title="Permalien">Permalien</a>';
135 $expected = 'Description... &#8212; [Permalien](http://domain.tld/?0oc_VQ)';
136 $processedText = reverse_feed_permalink($text);
137
138 $this->assertEquals($expected, $processedText);
139 }
140
141 public function testReverseLastFeedPermalink()
142 {
143 $text = 'Description... ';
144 $text .= '<br>&#8212; <a href="http://domain.tld/?0oc_VQ" title="Permalien">Permalien</a>';
145 $expected = $text;
146 $text .= '<br>&#8212; <a href="http://domain.tld/?0oc_VQ" title="Permalien">Permalien</a>';
147 $expected .= '<br>&#8212; [Permalien](http://domain.tld/?0oc_VQ)';
148 $processedText = reverse_feed_permalink($text);
149
150 $this->assertEquals($expected, $processedText);
151 }
152
153 public function testReverseNoFeedPermalink()
154 {
155 $text = 'Hello! Where are you from?';
156 $expected = $text;
157 $processedText = reverse_feed_permalink($text);
158
159 $this->assertEquals($expected, $processedText);
160 }
161
162 /**
163 * Test sanitize_html().
164 */
165 public function testSanitizeHtml()
166 {
167 $input = '< script src="js.js"/>';
168 $input .= '< script attr>alert(\'xss\');</script>';
169 $input .= '<style> * { display: none }</style>';
170 $output = escape($input);
171 $input .= '<a href="#" onmouseHover="alert(\'xss\');" attr="tt">link</a>';
172 $output .= '<a href="#" attr="tt">link</a>';
173 $input .= '<a href="#" onmouseHover=alert(\'xss\'); attr="tt">link</a>';
174 $output .= '<a href="#" attr="tt">link</a>';
175 $this->assertEquals($output, sanitize_html($input));
176 // Do not touch escaped HTML.
177 $input = escape($input);
178 $this->assertEquals($input, sanitize_html($input));
179 }
180
181 /**
182 * Test the no markdown tag.
183 */
184 public function testNoMarkdownTag()
185 {
186 $str = 'All _work_ and `no play` makes Jack a *dull* boy.';
187 $data = array(
188 'links' => array(array(
189 'description' => $str,
190 'tags' => NO_MD_TAG,
191 'taglist' => array(NO_MD_TAG),
192 ))
193 );
194
195 $processed = hook_markdown_render_linklist($data, $this->conf);
196 $this->assertEquals($str, $processed['links'][0]['description']);
197
198 $processed = hook_markdown_render_feed($data, $this->conf);
199 $this->assertEquals($str, $processed['links'][0]['description']);
200
201 $data = array(
202 // Columns data
203 'linksToDisplay' => array(
204 // nth link
205 0 => array(
206 'formatedDescription' => $str,
207 'tags' => NO_MD_TAG,
208 'taglist' => array(),
209 ),
210 ),
211 );
212
213 $data = hook_markdown_render_daily($data, $this->conf);
214 $this->assertEquals($str, $data['linksToDisplay'][0]['formatedDescription']);
215 }
216
217 /**
218 * Test that a close value to nomarkdown is not understand as nomarkdown (previous value `.nomarkdown`).
219 */
220 public function testNoMarkdownNotExcactlyMatching()
221 {
222 $str = 'All _work_ and `no play` makes Jack a *dull* boy.';
223 $data = array(
224 'links' => array(array(
225 'description' => $str,
226 'tags' => '.' . NO_MD_TAG,
227 'taglist' => array('.'. NO_MD_TAG),
228 ))
229 );
230
231 $data = hook_markdown_render_feed($data, $this->conf);
232 $this->assertContains('<em>', $data['links'][0]['description']);
233 }
234
235 /**
236 * Make sure that the generated HTML match the reference HTML file.
237 */
238 public function testMarkdownGlobalProcessDescription()
239 {
240 $md = file_get_contents('tests/plugins/resources/markdown.md');
241 $md = format_description($md);
242 $html = file_get_contents('tests/plugins/resources/markdown.html');
243
244 $data = process_markdown(
245 $md,
246 $this->conf->get('security.markdown_escape', true),
247 $this->conf->get('security.allowed_protocols')
248 );
249 $this->assertEquals($html, $data);
250 }
251
252 /**
253 * Make sure that the HTML tags are escaped.
254 */
255 public function testMarkdownWithHtmlEscape()
256 {
257 $md = '**strong** <strong>strong</strong>';
258 $html = '<div class="markdown"><p><strong>strong</strong> &lt;strong&gt;strong&lt;/strong&gt;</p></div>';
259 $data = array(
260 'links' => array(
261 0 => array(
262 'description' => $md,
263 ),
264 ),
265 );
266 $data = hook_markdown_render_linklist($data, $this->conf);
267 $this->assertEquals($html, $data['links'][0]['description']);
268 }
269
270 /**
271 * Make sure that the HTML tags aren't escaped with the setting set to false.
272 */
273 public function testMarkdownWithHtmlNoEscape()
274 {
275 $this->conf->set('security.markdown_escape', false);
276 $md = '**strong** <strong>strong</strong>';
277 $html = '<div class="markdown"><p><strong>strong</strong> <strong>strong</strong></p></div>';
278 $data = array(
279 'links' => array(
280 0 => array(
281 'description' => $md,
282 ),
283 ),
284 );
285 $data = hook_markdown_render_linklist($data, $this->conf);
286 $this->assertEquals($html, $data['links'][0]['description']);
287 }
288 }