]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - tests/plugins/PluginMarkdownTest.php
Add markdown_escape setting
[github/shaarli/Shaarli.git] / tests / plugins / PluginMarkdownTest.php
CommitLineData
1be4afac
A
1<?php
2
3/**
4 * PluginMarkdownTest.php
5 */
6
7require_once 'application/Utils.php';
8require_once 'plugins/markdown/markdown.php';
9
10/**
266e3fe5
A
11 * Class PluginMarkdownTest
12 * Unit test for the Markdown plugin
1be4afac
A
13 */
14class PluginMarkdownTest extends PHPUnit_Framework_TestCase
15{
9ff17ae2
A
16 /**
17 * @var ConfigManager instance.
18 */
19 protected $conf;
20
1be4afac
A
21 /**
22 * Reset plugin path
23 */
24 function setUp()
25 {
26 PluginManager::$PLUGINS_PATH = 'plugins';
9ff17ae2 27 $this->conf = new ConfigManager('tests/utils/config/configJson');
1be4afac
A
28 }
29
30 /**
31 * Test render_linklist hook.
32 * Only check that there is basic markdown rendering.
33 */
34 function testMarkdownLinklist()
35 {
36 $markdown = '# My title' . PHP_EOL . 'Very interesting content.';
37 $data = array(
38 'links' => array(
39 0 => array(
40 'description' => $markdown,
41 ),
42 ),
43 );
44
9ff17ae2 45 $data = hook_markdown_render_linklist($data, $this->conf);
1be4afac
A
46 $this->assertNotFalse(strpos($data['links'][0]['description'], '<h1>'));
47 $this->assertNotFalse(strpos($data['links'][0]['description'], '<p>'));
48 }
49
50 /**
51 * Test render_daily hook.
52 * Only check that there is basic markdown rendering.
53 */
54 function testMarkdownDaily()
55 {
56 $markdown = '# My title' . PHP_EOL . 'Very interesting content.';
57 $data = array(
58 // Columns data
59 'cols' => array(
60 // First, second, third.
61 0 => array(
62 // nth link
63 0 => array(
64 'formatedDescription' => $markdown,
65 ),
66 ),
67 ),
68 );
69
9ff17ae2 70 $data = hook_markdown_render_daily($data, $this->conf);
1be4afac
A
71 $this->assertNotFalse(strpos($data['cols'][0][0]['formatedDescription'], '<h1>'));
72 $this->assertNotFalse(strpos($data['cols'][0][0]['formatedDescription'], '<p>'));
73 }
74
75 /**
76 * Test reverse_text2clickable().
77 */
78 function testReverseText2clickable()
79 {
80 $text = 'stuff http://hello.there/is=someone#here otherstuff';
81 $clickableText = text2clickable($text, '');
82 $reversedText = reverse_text2clickable($clickableText);
83 $this->assertEquals($text, $reversedText);
84 }
85
86 /**
87 * Test reverse_nl2br().
88 */
89 function testReverseNl2br()
90 {
91 $text = 'stuff' . PHP_EOL . 'otherstuff';
92 $processedText = nl2br($text);
93 $reversedText = reverse_nl2br($processedText);
94 $this->assertEquals($text, $reversedText);
95 }
96
97 /**
98 * Test reverse_space2nbsp().
99 */
100 function testReverseSpace2nbsp()
101 {
102 $text = ' stuff' . PHP_EOL . ' otherstuff and another';
103 $processedText = space2nbsp($text);
104 $reversedText = reverse_space2nbsp($processedText);
105 $this->assertEquals($text, $reversedText);
106 }
107
108 /**
2925687e 109 * Test sanitize_html().
1be4afac 110 */
3ce20d9e
A
111 function testSanitizeHtml()
112 {
2925687e
A
113 $input = '< script src="js.js"/>';
114 $input .= '< script attr>alert(\'xss\');</script>';
115 $input .= '<style> * { display: none }</style>';
116 $output = escape($input);
117 $input .= '<a href="#" onmouseHover="alert(\'xss\');" attr="tt">link</a>';
118 $output .= '<a href="#" attr="tt">link</a>';
9ff17ae2
A
119 $input .= '<a href="#" onmouseHover=alert(\'xss\'); attr="tt">link</a>';
120 $output .= '<a href="#" attr="tt">link</a>';
2925687e
A
121 $this->assertEquals($output, sanitize_html($input));
122 // Do not touch escaped HTML.
123 $input = escape($input);
124 $this->assertEquals($input, sanitize_html($input));
1be4afac 125 }
3ce20d9e
A
126
127 /**
128 * Test the no markdown tag.
129 */
130 function testNoMarkdownTag()
131 {
132 $str = 'All _work_ and `no play` makes Jack a *dull* boy.';
133 $data = array(
134 'links' => array(array(
135 'description' => $str,
8c4e6018
A
136 'tags' => NO_MD_TAG,
137 'taglist' => array(NO_MD_TAG),
3ce20d9e
A
138 ))
139 );
140
9ff17ae2 141 $processed = hook_markdown_render_linklist($data, $this->conf);
266e3fe5
A
142 $this->assertEquals($str, $processed['links'][0]['description']);
143
9ff17ae2 144 $processed = hook_markdown_render_feed($data, $this->conf);
266e3fe5 145 $this->assertEquals($str, $processed['links'][0]['description']);
3ce20d9e
A
146
147 $data = array(
148 // Columns data
149 'cols' => array(
150 // First, second, third.
151 0 => array(
152 // nth link
153 0 => array(
154 'formatedDescription' => $str,
8c4e6018
A
155 'tags' => NO_MD_TAG,
156 'taglist' => array(),
3ce20d9e
A
157 ),
158 ),
159 ),
160 );
161
9ff17ae2 162 $data = hook_markdown_render_daily($data, $this->conf);
3ce20d9e
A
163 $this->assertEquals($str, $data['cols'][0][0]['formatedDescription']);
164 }
c5941f31 165
266e3fe5
A
166 /**
167 * Test that a close value to nomarkdown is not understand as nomarkdown (previous value `.nomarkdown`).
168 */
169 function testNoMarkdownNotExcactlyMatching()
170 {
171 $str = 'All _work_ and `no play` makes Jack a *dull* boy.';
172 $data = array(
173 'links' => array(array(
174 'description' => $str,
175 'tags' => '.' . NO_MD_TAG,
176 'taglist' => array('.'. NO_MD_TAG),
177 ))
178 );
179
9ff17ae2 180 $data = hook_markdown_render_feed($data, $this->conf);
266e3fe5
A
181 $this->assertContains('<em>', $data['links'][0]['description']);
182 }
183
c5941f31
A
184 /**
185 * Test hashtag links processed with markdown.
186 */
187 function testMarkdownHashtagLinks()
188 {
189 $md = file_get_contents('tests/plugins/resources/markdown.md');
190 $md = format_description($md);
191 $html = file_get_contents('tests/plugins/resources/markdown.html');
192
193 $data = process_markdown($md);
194 $this->assertEquals($html, $data);
195 }
9ff17ae2
A
196
197 /**
198 * Make sure that the HTML tags are escaped.
199 */
200 public function testMarkdownWithHtmlEscape()
201 {
202 $md = '**strong** <strong>strong</strong>';
203 $html = '<div class="markdown"><p><strong>strong</strong> &lt;strong&gt;strong&lt;/strong&gt;</p></div>';
204 $data = array(
205 'links' => array(
206 0 => array(
207 'description' => $md,
208 ),
209 ),
210 );
211 $data = hook_markdown_render_linklist($data, $this->conf);
212 $this->assertEquals($html, $data['links'][0]['description']);
213 }
214
215 /**
216 * Make sure that the HTML tags aren't escaped with the setting set to false.
217 */
218 public function testMarkdownWithHtmlNoEscape()
219 {
220 $this->conf->set('security.markdown_escape', false);
221 $md = '**strong** <strong>strong</strong>';
222 $html = '<div class="markdown"><p><strong>strong</strong> <strong>strong</strong></p></div>';
223 $data = array(
224 'links' => array(
225 0 => array(
226 'description' => $md,
227 ),
228 ),
229 );
230 $data = hook_markdown_render_linklist($data, $this->conf);
231 $this->assertEquals($html, $data['links'][0]['description']);
232 }
1be4afac 233}