]>
Commit | Line | Data |
---|---|---|
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_daily hook. | |
54 | * Only check that there is basic markdown rendering. | |
55 | */ | |
56 | public function testMarkdownDaily() | |
57 | { | |
58 | $markdown = '# My title' . PHP_EOL . 'Very interesting content.'; | |
59 | $data = array( | |
60 | // Columns data | |
61 | 'cols' => array( | |
62 | // First, second, third. | |
63 | 0 => array( | |
64 | // nth link | |
65 | 0 => array( | |
66 | 'formatedDescription' => $markdown, | |
67 | ), | |
68 | ), | |
69 | ), | |
70 | ); | |
71 | ||
72 | $data = hook_markdown_render_daily($data, $this->conf); | |
73 | $this->assertNotFalse(strpos($data['cols'][0][0]['formatedDescription'], '<h1>')); | |
74 | $this->assertNotFalse(strpos($data['cols'][0][0]['formatedDescription'], '<p>')); | |
75 | } | |
76 | ||
77 | /** | |
78 | * Test reverse_text2clickable(). | |
79 | */ | |
80 | public function testReverseText2clickable() | |
81 | { | |
82 | $text = 'stuff http://hello.there/is=someone#here otherstuff'; | |
83 | $clickableText = text2clickable($text, ''); | |
84 | $reversedText = reverse_text2clickable($clickableText); | |
85 | $this->assertEquals($text, $reversedText); | |
86 | } | |
87 | ||
88 | /** | |
89 | * Test reverse_nl2br(). | |
90 | */ | |
91 | public function testReverseNl2br() | |
92 | { | |
93 | $text = 'stuff' . PHP_EOL . 'otherstuff'; | |
94 | $processedText = nl2br($text); | |
95 | $reversedText = reverse_nl2br($processedText); | |
96 | $this->assertEquals($text, $reversedText); | |
97 | } | |
98 | ||
99 | /** | |
100 | * Test reverse_space2nbsp(). | |
101 | */ | |
102 | public function testReverseSpace2nbsp() | |
103 | { | |
104 | $text = ' stuff' . PHP_EOL . ' otherstuff and another'; | |
105 | $processedText = space2nbsp($text); | |
106 | $reversedText = reverse_space2nbsp($processedText); | |
107 | $this->assertEquals($text, $reversedText); | |
108 | } | |
109 | ||
110 | /** | |
111 | * Test sanitize_html(). | |
112 | */ | |
113 | public function testSanitizeHtml() | |
114 | { | |
115 | $input = '< script src="js.js"/>'; | |
116 | $input .= '< script attr>alert(\'xss\');</script>'; | |
117 | $input .= '<style> * { display: none }</style>'; | |
118 | $output = escape($input); | |
119 | $input .= '<a href="#" onmouseHover="alert(\'xss\');" attr="tt">link</a>'; | |
120 | $output .= '<a href="#" attr="tt">link</a>'; | |
121 | $input .= '<a href="#" onmouseHover=alert(\'xss\'); attr="tt">link</a>'; | |
122 | $output .= '<a href="#" attr="tt">link</a>'; | |
123 | $this->assertEquals($output, sanitize_html($input)); | |
124 | // Do not touch escaped HTML. | |
125 | $input = escape($input); | |
126 | $this->assertEquals($input, sanitize_html($input)); | |
127 | } | |
128 | ||
129 | /** | |
130 | * Test the no markdown tag. | |
131 | */ | |
132 | public function testNoMarkdownTag() | |
133 | { | |
134 | $str = 'All _work_ and `no play` makes Jack a *dull* boy.'; | |
135 | $data = array( | |
136 | 'links' => array(array( | |
137 | 'description' => $str, | |
138 | 'tags' => NO_MD_TAG, | |
139 | 'taglist' => array(NO_MD_TAG), | |
140 | )) | |
141 | ); | |
142 | ||
143 | $processed = hook_markdown_render_linklist($data, $this->conf); | |
144 | $this->assertEquals($str, $processed['links'][0]['description']); | |
145 | ||
146 | $processed = hook_markdown_render_feed($data, $this->conf); | |
147 | $this->assertEquals($str, $processed['links'][0]['description']); | |
148 | ||
149 | $data = array( | |
150 | // Columns data | |
151 | 'cols' => array( | |
152 | // First, second, third. | |
153 | 0 => array( | |
154 | // nth link | |
155 | 0 => array( | |
156 | 'formatedDescription' => $str, | |
157 | 'tags' => NO_MD_TAG, | |
158 | 'taglist' => array(), | |
159 | ), | |
160 | ), | |
161 | ), | |
162 | ); | |
163 | ||
164 | $data = hook_markdown_render_daily($data, $this->conf); | |
165 | $this->assertEquals($str, $data['cols'][0][0]['formatedDescription']); | |
166 | } | |
167 | ||
168 | /** | |
169 | * Test that a close value to nomarkdown is not understand as nomarkdown (previous value `.nomarkdown`). | |
170 | */ | |
171 | public function testNoMarkdownNotExcactlyMatching() | |
172 | { | |
173 | $str = 'All _work_ and `no play` makes Jack a *dull* boy.'; | |
174 | $data = array( | |
175 | 'links' => array(array( | |
176 | 'description' => $str, | |
177 | 'tags' => '.' . NO_MD_TAG, | |
178 | 'taglist' => array('.'. NO_MD_TAG), | |
179 | )) | |
180 | ); | |
181 | ||
182 | $data = hook_markdown_render_feed($data, $this->conf); | |
183 | $this->assertContains('<em>', $data['links'][0]['description']); | |
184 | } | |
185 | ||
186 | /** | |
187 | * Make sure that the generated HTML match the reference HTML file. | |
188 | */ | |
189 | public function testMarkdownGlobalProcessDescription() | |
190 | { | |
191 | $md = file_get_contents('tests/plugins/resources/markdown.md'); | |
192 | $md = format_description($md); | |
193 | $html = file_get_contents('tests/plugins/resources/markdown.html'); | |
194 | ||
195 | $data = process_markdown( | |
196 | $md, | |
197 | $this->conf->get('security.markdown_escape', true), | |
198 | $this->conf->get('security.allowed_protocols') | |
199 | ); | |
200 | $this->assertEquals($html, $data); | |
201 | } | |
202 | ||
203 | /** | |
204 | * Make sure that the HTML tags are escaped. | |
205 | */ | |
206 | public function testMarkdownWithHtmlEscape() | |
207 | { | |
208 | $md = '**strong** <strong>strong</strong>'; | |
209 | $html = '<div class="markdown"><p><strong>strong</strong> <strong>strong</strong></p></div>'; | |
210 | $data = array( | |
211 | 'links' => array( | |
212 | 0 => array( | |
213 | 'description' => $md, | |
214 | ), | |
215 | ), | |
216 | ); | |
217 | $data = hook_markdown_render_linklist($data, $this->conf); | |
218 | $this->assertEquals($html, $data['links'][0]['description']); | |
219 | } | |
220 | ||
221 | /** | |
222 | * Make sure that the HTML tags aren't escaped with the setting set to false. | |
223 | */ | |
224 | public function testMarkdownWithHtmlNoEscape() | |
225 | { | |
226 | $this->conf->set('security.markdown_escape', false); | |
227 | $md = '**strong** <strong>strong</strong>'; | |
228 | $html = '<div class="markdown"><p><strong>strong</strong> <strong>strong</strong></p></div>'; | |
229 | $data = array( | |
230 | 'links' => array( | |
231 | 0 => array( | |
232 | 'description' => $md, | |
233 | ), | |
234 | ), | |
235 | ); | |
236 | $data = hook_markdown_render_linklist($data, $this->conf); | |
237 | $this->assertEquals($html, $data['links'][0]['description']); | |
238 | } | |
239 | } |