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