MDL-68738 wiki: Prevent URLs inside links from turning into another link
[moodle.git] / mod / wiki / tests / wikiparser_test.php
CommitLineData
ba0c4843
PS
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * Unit tests for the wiki parser
19 *
20 * @package mod_wiki
21 * @category phpunit
7db38cd5
SH
22 * @copyright 2009 Marc Alier, Jordi Piguillem marc.alier@upc.edu
23 * @copyright 2009 Universitat Politecnica de Catalunya http://www.upc.edu
ba0c4843
PS
24 *
25 * @author Jordi Piguillem
26 * @author Marc Alier
27 * @author David Jimenez
28 * @author Josep Arus
29 * @author Kenneth Riba
30 *
31 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
32 */
33
34defined('MOODLE_INTERNAL') || die;
35
36global $CFG;
37require_once($CFG->dirroot . '/mod/wiki/parser/parser.php');
38
39
40class mod_wiki_wikiparser_test extends basic_testcase {
41
59641dd5
DM
42 /**
43 * URL inside the clickable text of some link should not be turned into a new link via the url_tag_rule.
44 *
45 * @dataProvider urls_inside_link_text_provider
46 * @param string $markup Markup of the Wiki page the text is part of.
47 * @param string $input The input text.
48 * @param string $output The expected output HTML as a result of the parsed input text.
49 */
50 public function test_urls_inside_link_text(string $markup, string $input, string $output) {
51
52 $parsingresult = wiki_parser_proxy::parse($input, $markup, [
53 'link_callback' => '/mod/wiki/locallib.php:wiki_parser_link',
54 'link_callback_args' => ['swid' => 1],
55 ]);
56
57 $this->assertContains($output, $parsingresult['parsed_text']);
58 }
59
60 /**
61 * Provides data sets for {@see self::test_urls_inside_link_text()}.
62 *
63 * @return array
64 */
65 public function urls_inside_link_text_provider() {
66 return [
67 'creole implicit link' => [
68 'markup' => 'creole',
69 'input' => 'Visit https://site.url for more information.',
70 'output' => 'Visit <a href="https://site.url">https://site.url</a> for more information.',
71 ],
72 'creole explicit link' => [
73 'markup' => 'creole',
74 'input' => 'Visit [[https://site.url]] for more information.',
75 'output' => 'Visit <a href="https://site.url">https://site.url</a> for more information.',
76 ],
77 'creole explicit link with text' => [
78 'markup' => 'creole',
79 'input' => 'Visit [[https://site.url|http://www.site.url]] for more information.',
80 'output' => 'Visit <a href="https://site.url">http://www.site.url</a> for more information.',
81 ],
82 'nwiki implicit link' => [
83 'markup' => 'nwiki',
84 'input' => 'Visit https://site.url for more information.',
85 'output' => 'Visit <a href="https://site.url">https://site.url</a> for more information.',
86 ],
87 'nwiki explicit link' => [
88 'markup' => 'nwiki',
89 'input' => 'Visit [https://site.url] for more information.',
90 'output' => 'Visit <a href="https://site.url">https://site.url</a> for more information.',
91 ],
92 'nwiki explicit link with space separated text' => [
93 'markup' => 'nwiki',
94 'input' => 'Visit [https://site.url http://www.site.url] for more information.',
95 'output' => 'Visit <a href="https://site.url">http://www.site.url</a> for more information.',
96 ],
97 'nwiki explicit link with pipe separated text' => [
98 'markup' => 'nwiki',
99 'input' => 'Visit [https://site.url|http://www.site.url] for more information.',
100 'output' => 'Visit <a href="https://site.url">http://www.site.url</a> for more information.',
101 ],
102 'html implicit link' => [
103 'markup' => 'html',
104 'input' => 'Visit https://site.url for more information.',
105 'output' => 'Visit <a href="https://site.url">https://site.url</a> for more information.',
106 ],
107 'html explicit link with text' => [
108 'markup' => 'html',
109 'input' => 'Visit <a href="https://site.url">http://www.site.url</a> for more information.',
110 'output' => 'Visit <a href="https://site.url">http://www.site.url</a> for more information.',
111 ],
112 'html wiki link to non-existing page' => [
113 'markup' => 'html',
114 'input' => 'Visit [[Another page]] for more information.',
115 'output' => 'Visit <a class="wiki_newentry" ' .
116 'href="https://www.example.com/moodle/mod/wiki/create.php?swid=1&amp;title=Another+page&amp;action=new">' .
117 'Another page</a> for more information.',
118 ],
119 'html wiki link inside an explicit link' => [
120 // The explicit href URL takes precedence here, the [[...]] is not turned into a wiki link.
121 'markup' => 'html',
122 'input' => 'Visit <a href="https://site.url">[[Another page]]</a> for more information.',
123 'output' => 'Visit <a href="https://site.url">[[Another page]]</a> for more information.',
124 ],
125 ];
126 }
127
ba0c4843
PS
128 function testCreoleMarkup() {
129 $this->assertTestFiles('creole');
130 }
131
132 function testNwikiMarkup() {
133 $this->assertTestFiles('nwiki');
134 }
135
136 function testHtmlMarkup() {
137 $this->assertTestFiles('html');
138 }
139
140 private function assertTestFile($num, $markup) {
141 if(!file_exists(__DIR__."/fixtures/input/$markup/$num") || !file_exists(__DIR__."/fixtures/output/$markup/$num")) {
142 return false;
143 }
144 $input = file_get_contents(__DIR__."/fixtures/input/$markup/$num");
145 $output = file_get_contents(__DIR__."/fixtures/output/$markup/$num");
146
147 $result = wiki_parser_proxy::parse($input, $markup, array('pretty_print' => true));
148
149 //removes line breaks to avoid line break encoding causing tests to fail.
150 $result['parsed_text'] = preg_replace('~[\r\n]~', '', $result['parsed_text']);
151 $output = preg_replace('~[\r\n]~', '', $output);
152
3d89ccb3 153 $this->assertEquals($output, $result['parsed_text'], 'Failed asserting that two strings are equal. Markup = '.$markup.", num = $num");
ba0c4843
PS
154 return true;
155 }
156
157 private function assertTestFiles($markup) {
158 $i = 1;
159 while($this->assertTestFile($i, $markup)) {
160 $i++;
161 }
162 }
c159e2aa
FM
163
164 /**
165 * Check that headings with special characters work as expected with HTML.
166 *
167 * - The heading itself is well displayed,
168 * - The TOC heading is well display,
169 * - The edit link points to the right page,
170 * - The links properly works with get_section.
171 */
172 public function test_special_headings() {
173
174 // First testing HTML markup.
175
176 // Test section name using HTML entities.
177 $input = '<h1>Code &amp; Test</h1>';
3d89ccb3
MG
178 $output = '<h3><a name="toc-1"></a>Code &amp; Test <a href="edit.php?pageid=&amp;section=Code+%26amp%3B+Test" '.
179 'class="wiki_edit_section">[edit]</a></h3>' . "\n";
c159e2aa 180 $toc = '<div class="wiki-toc"><p class="wiki-toc-title">Table of contents</p><p class="wiki-toc-section-1 '.
72e3cb9a 181 'wiki-toc-section">1. <a href="#toc-1">Code &amp; Test <a href="edit.php?pageid=&amp;section=Code+%26amp%3B+'.
c159e2aa
FM
182 'Test" class="wiki_edit_section">[edit]</a></a></p></div>';
183 $section = wiki_parser_proxy::get_section($input, 'html', 'Code &amp; Test');
184 $actual = wiki_parser_proxy::parse($input, 'html');
185 $this->assertEquals($output, $actual['parsed_text']);
186 $this->assertEquals($toc, $actual['toc']);
187 $this->assertNotEquals(false, $section);
188
189 // Test section name using non-ASCII characters.
190 $input = '<h1>Another áéíóúç€ test</h1>';
3d89ccb3
MG
191 $output = '<h3><a name="toc-1"></a>Another áéíóúç€ test <a href="edit.php?pageid=&amp;section=Another+%C'.
192 '3%A1%C3%A9%C3%AD%C3%B3%C3%BA%C3%A7%E2%82%AC+test" class="wiki_edit_section">[edit]</a></h3>' . "\n";
c159e2aa 193 $toc = '<div class="wiki-toc"><p class="wiki-toc-title">Table of contents</p><p class="wiki-toc-section-1 '.
72e3cb9a 194 'wiki-toc-section">1. <a href="#toc-1">Another áéíóúç€ test <a href="edit.php?pageid=&amp;section=Another+%C'.
c159e2aa
FM
195 '3%A1%C3%A9%C3%AD%C3%B3%C3%BA%C3%A7%E2%82%AC+test" class="wiki_edit_section">[edit]</a></a></p></div>';
196 $section = wiki_parser_proxy::get_section($input, 'html', 'Another áéíóúç€ test');
197 $actual = wiki_parser_proxy::parse($input, 'html');
198 $this->assertEquals($output, $actual['parsed_text']);
199 $this->assertEquals($toc, $actual['toc']);
200 $this->assertNotEquals(false, $section);
201
202 // Test section name with a URL.
203 $input = '<h1>Another http://moodle.org test</h1>';
3d89ccb3
MG
204 $output = '<h3><a name="toc-1"></a>Another <a href="http://moodle.org">http://moodle.org</a> test <a href="edit.php'.
205 '?pageid=&amp;section=Another+http%3A%2F%2Fmoodle.org+test" class="wiki_edit_section">[edit]</a></h3>' . "\n";
c159e2aa 206 $toc = '<div class="wiki-toc"><p class="wiki-toc-title">Table of contents</p><p class="wiki-toc-section-1 '.
72e3cb9a 207 'wiki-toc-section">1. <a href="#toc-1">Another http://moodle.org test <a href="edit.php?pageid=&amp;section='.
c159e2aa
FM
208 'Another+http%3A%2F%2Fmoodle.org+test" class="wiki_edit_section">[edit]</a></a></p></div>';
209 $section = wiki_parser_proxy::get_section($input, 'html', 'Another http://moodle.org test');
210 $actual = wiki_parser_proxy::parse($input, 'html', array(
211 'link_callback' => '/mod/wiki/locallib.php:wiki_parser_link'
212 ));
213 $this->assertEquals($output, $actual['parsed_text']);
214 $this->assertEquals($toc, $actual['toc']);
215 $this->assertNotEquals(false, $section);
216
89bb268e
EL
217 // Test toc section names being wikilinks.
218 $input = '<h1>[[Heading 1]]</h1><h2>[[Heading A]]</h2><h2>Heading D</h2>';
3d89ccb3 219 $regexpoutput = '!<h3><a name="toc-1"></a>' .
89bb268e 220 '<a class="wiki_newentry" href.*mod/wiki/create\.php\?.*title=Heading\+1.*action=new.*>Heading 1<.*' .
3d89ccb3 221 '<h4><a name="toc-2"></a>' .
89bb268e 222 '<a class="wiki_newentry" href.*mod/wiki/create\.php\?.*title=Heading\+A.*action=new.*>Heading A<.*' .
3d89ccb3 223 '<h4><a name="toc-3"></a>' .
89bb268e
EL
224 'Heading D!ms';
225 $regexptoc = '!<a href="#toc-1">Heading 1.*<a href="#toc-2">Heading A</a>.*<a href="#toc-3">Heading D</a>!ms';
226 $section = wiki_parser_proxy::get_section($input, 'html', 'Another [[wikilinked]] test');
227 $actual = wiki_parser_proxy::parse($input, 'html', array(
228 'link_callback' => '/mod/wiki/locallib.php:wiki_parser_link',
229 'link_callback_args' => array('swid' => 1)
230 ));
231 $this->assertRegExp($regexpoutput, $actual['parsed_text']);
232 $this->assertRegExp($regexptoc, $actual['toc']);
233
c159e2aa
FM
234 // Now going to test Creole markup.
235 // Note that Creole uses links to the escaped version of the section.
236
237 // Test section name using HTML entities.
238 $input = '= Code & Test =';
3d89ccb3
MG
239 $output = '<h3><a name="toc-1"></a>Code &amp; Test <a href="edit.php?pageid=&amp;section=Code+%26amp%3B+Test" '.
240 'class="wiki_edit_section">[edit]</a></h3>' . "\n";
c159e2aa 241 $toc = '<div class="wiki-toc"><p class="wiki-toc-title">Table of contents</p><p class="wiki-toc-section-1 '.
72e3cb9a 242 'wiki-toc-section">1. <a href="#toc-1">Code &amp; Test <a href="edit.php?pageid=&amp;section=Code+%26amp%3B+'.
c159e2aa
FM
243 'Test" class="wiki_edit_section">[edit]</a></a></p></div>';
244 $section = wiki_parser_proxy::get_section($input, 'creole', 'Code &amp; Test');
245 $actual = wiki_parser_proxy::parse($input, 'creole');
246 $this->assertEquals($output, $actual['parsed_text']);
247 $this->assertEquals($toc, $actual['toc']);
248 $this->assertNotEquals(false, $section);
249
250 // Test section name using non-ASCII characters.
251 $input = '= Another áéíóúç€ test =';
3d89ccb3
MG
252 $output = '<h3><a name="toc-1"></a>Another áéíóúç€ test <a href="edit.php?pageid=&amp;section=Another+%C'.
253 '3%A1%C3%A9%C3%AD%C3%B3%C3%BA%C3%A7%E2%82%AC+test" class="wiki_edit_section">[edit]</a></h3>' . "\n";
c159e2aa 254 $toc = '<div class="wiki-toc"><p class="wiki-toc-title">Table of contents</p><p class="wiki-toc-section-1 '.
72e3cb9a 255 'wiki-toc-section">1. <a href="#toc-1">Another áéíóúç€ test <a href="edit.php?pageid=&amp;section=Another+%C'.
c159e2aa
FM
256 '3%A1%C3%A9%C3%AD%C3%B3%C3%BA%C3%A7%E2%82%AC+test" class="wiki_edit_section">[edit]</a></a></p></div>';
257 $section = wiki_parser_proxy::get_section($input, 'creole', 'Another áéíóúç€ test');
258 $actual = wiki_parser_proxy::parse($input, 'creole');
259 $this->assertEquals($output, $actual['parsed_text']);
260 $this->assertEquals($toc, $actual['toc']);
261 $this->assertNotEquals(false, $section);
262
263 // Test section name with a URL, creole does not support linking links in a heading.
264 $input = '= Another http://moodle.org test =';
3d89ccb3
MG
265 $output = '<h3><a name="toc-1"></a>Another http://moodle.org test <a href="edit.php'.
266 '?pageid=&amp;section=Another+http%3A%2F%2Fmoodle.org+test" class="wiki_edit_section">[edit]</a></h3>' . "\n";
c159e2aa 267 $toc = '<div class="wiki-toc"><p class="wiki-toc-title">Table of contents</p><p class="wiki-toc-section-1 '.
72e3cb9a 268 'wiki-toc-section">1. <a href="#toc-1">Another http://moodle.org test <a href="edit.php?pageid=&amp;section='.
c159e2aa
FM
269 'Another+http%3A%2F%2Fmoodle.org+test" class="wiki_edit_section">[edit]</a></a></p></div>';
270 $section = wiki_parser_proxy::get_section($input, 'creole', 'Another http://moodle.org test');
271 $actual = wiki_parser_proxy::parse($input, 'creole');
272 $this->assertEquals($output, $actual['parsed_text']);
273 $this->assertEquals($toc, $actual['toc']);
274 $this->assertNotEquals(false, $section);
275
276 // Now going to test NWiki markup.
277 // Note that Creole uses links to the escaped version of the section.
278
279 // Test section name using HTML entities.
280 $input = '= Code & Test =';
3d89ccb3
MG
281 $output = '<h3><a name="toc-1"></a>Code & Test <a href="edit.php?pageid=&amp;section=Code+%26+Test" '.
282 'class="wiki_edit_section">[edit]</a></h3>' . "\n";
c159e2aa 283 $toc = '<div class="wiki-toc"><p class="wiki-toc-title">Table of contents</p><p class="wiki-toc-section-1 '.
72e3cb9a 284 'wiki-toc-section">1. <a href="#toc-1">Code & Test <a href="edit.php?pageid=&amp;section=Code+%26+'.
c159e2aa
FM
285 'Test" class="wiki_edit_section">[edit]</a></a></p></div>';
286 $section = wiki_parser_proxy::get_section($input, 'nwiki', 'Code & Test');
287 $actual = wiki_parser_proxy::parse($input, 'nwiki');
288 $this->assertEquals($output, $actual['parsed_text']);
289 $this->assertEquals($toc, $actual['toc']);
290 $this->assertNotEquals(false, $section);
291
292 // Test section name using non-ASCII characters.
293 $input = '= Another áéíóúç€ test =';
3d89ccb3
MG
294 $output = '<h3><a name="toc-1"></a>Another áéíóúç€ test <a href="edit.php?pageid=&amp;section=Another+%C'.
295 '3%A1%C3%A9%C3%AD%C3%B3%C3%BA%C3%A7%E2%82%AC+test" class="wiki_edit_section">[edit]</a></h3>' . "\n";
c159e2aa 296 $toc = '<div class="wiki-toc"><p class="wiki-toc-title">Table of contents</p><p class="wiki-toc-section-1 '.
72e3cb9a 297 'wiki-toc-section">1. <a href="#toc-1">Another áéíóúç€ test <a href="edit.php?pageid=&amp;section=Another+%C'.
c159e2aa
FM
298 '3%A1%C3%A9%C3%AD%C3%B3%C3%BA%C3%A7%E2%82%AC+test" class="wiki_edit_section">[edit]</a></a></p></div>';
299 $section = wiki_parser_proxy::get_section($input, 'nwiki', 'Another áéíóúç€ test');
300 $actual = wiki_parser_proxy::parse($input, 'nwiki');
301 $this->assertEquals($output, $actual['parsed_text']);
302 $this->assertEquals($toc, $actual['toc']);
303 $this->assertNotEquals(false, $section);
304
305 // Test section name with a URL, nwiki does not support linking links in a heading.
306 $input = '= Another http://moodle.org test =';
3d89ccb3
MG
307 $output = '<h3><a name="toc-1"></a>Another http://moodle.org test <a href="edit.php'.
308 '?pageid=&amp;section=Another+http%3A%2F%2Fmoodle.org+test" class="wiki_edit_section">[edit]</a></h3>' . "\n";
c159e2aa 309 $toc = '<div class="wiki-toc"><p class="wiki-toc-title">Table of contents</p><p class="wiki-toc-section-1 '.
72e3cb9a 310 'wiki-toc-section">1. <a href="#toc-1">Another http://moodle.org test <a href="edit.php?pageid=&amp;section='.
c159e2aa
FM
311 'Another+http%3A%2F%2Fmoodle.org+test" class="wiki_edit_section">[edit]</a></a></p></div>';
312 $section = wiki_parser_proxy::get_section($input, 'nwiki', 'Another http://moodle.org test');
313 $actual = wiki_parser_proxy::parse($input, 'nwiki');
314 $this->assertEquals($output, $actual['parsed_text']);
315 $this->assertEquals($toc, $actual['toc']);
316 $this->assertNotEquals(false, $section);
3d89ccb3
MG
317
318 // Test section names when headings start with level 3.
319 $input = '<h3>Heading test</h3><h4>Subsection</h4>';
320 $output = '<h3><a name="toc-1"></a>Heading test <a href="edit.php?pageid=&amp;section=Heading+test" '.
321 'class="wiki_edit_section">[edit]</a></h3>'. "\n" . '<h4><a name="toc-2"></a>Subsection</h4>' . "\n";
322 $toc = '<div class="wiki-toc"><p class="wiki-toc-title">Table of contents</p><p class="wiki-toc-section-1 '.
323 'wiki-toc-section">1. <a href="#toc-1">Heading test <a href="edit.php?pageid=&amp;section=Heading+'.
324 'test" class="wiki_edit_section">[edit]</a></a></p><p class="wiki-toc-section-2 wiki-toc-section">'.
325 '1.1. <a href="#toc-2">Subsection</a></p></div>';
326 $section = wiki_parser_proxy::get_section($input, 'html', 'Heading test');
327 $actual = wiki_parser_proxy::parse($input, 'html');
328 $this->assertEquals($output, $actual['parsed_text']);
329 $this->assertEquals($toc, $actual['toc']);
330 $this->assertNotEquals(false, $section);
c159e2aa
FM
331 }
332
ba0c4843 333}