]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - tests/plugins/PluginMarkdownTest.php
Add markdown_escape setting
[github/shaarli/Shaarli.git] / tests / plugins / PluginMarkdownTest.php
1 <?php
2
3 /**
4 * PluginMarkdownTest.php
5 */
6
7 require_once 'application/Utils.php';
8 require_once 'plugins/markdown/markdown.php';
9
10 /**
11 * Class PluginMarkdownTest
12 * Unit test for the Markdown plugin
13 */
14 class PluginMarkdownTest extends PHPUnit_Framework_TestCase
15 {
16 /**
17 * @var ConfigManager instance.
18 */
19 protected $conf;
20
21 /**
22 * Reset plugin path
23 */
24 function setUp()
25 {
26 PluginManager::$PLUGINS_PATH = 'plugins';
27 $this->conf = new ConfigManager('tests/utils/config/configJson');
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
45 $data = hook_markdown_render_linklist($data, $this->conf);
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
70 $data = hook_markdown_render_daily($data, $this->conf);
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 /**
109 * Test sanitize_html().
110 */
111 function testSanitizeHtml()
112 {
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>';
119 $input .= '<a href="#" onmouseHover=alert(\'xss\'); attr="tt">link</a>';
120 $output .= '<a href="#" attr="tt">link</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));
125 }
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,
136 'tags' => NO_MD_TAG,
137 'taglist' => array(NO_MD_TAG),
138 ))
139 );
140
141 $processed = hook_markdown_render_linklist($data, $this->conf);
142 $this->assertEquals($str, $processed['links'][0]['description']);
143
144 $processed = hook_markdown_render_feed($data, $this->conf);
145 $this->assertEquals($str, $processed['links'][0]['description']);
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,
155 'tags' => NO_MD_TAG,
156 'taglist' => array(),
157 ),
158 ),
159 ),
160 );
161
162 $data = hook_markdown_render_daily($data, $this->conf);
163 $this->assertEquals($str, $data['cols'][0][0]['formatedDescription']);
164 }
165
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
180 $data = hook_markdown_render_feed($data, $this->conf);
181 $this->assertContains('<em>', $data['links'][0]['description']);
182 }
183
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 }
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 }
233 }