Merge branch 'MDL-79108-master' of https://github.com/sarjona/moodle
[moodle.git] / mod / wiki / tests / wikiparser_test.php
blob3ce41fd87a9c0b8372d8700b63f66405adc56c1a
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.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <https://www.gnu.org/licenses/>.
17 namespace mod_wiki;
19 use wiki_parser_proxy;
21 defined('MOODLE_INTERNAL') || die;
23 global $CFG;
24 require_once($CFG->dirroot . '/mod/wiki/parser/parser.php');
26 /**
27 * Unit tests for the wiki parser
29 * @package mod_wiki
30 * @category test
31 * @copyright 2009 Marc Alier, Jordi Piguillem marc.alier@upc.edu
32 * @copyright 2009 Universitat Politecnica de Catalunya http://www.upc.edu
34 * @author Jordi Piguillem
35 * @author Marc Alier
36 * @author David Jimenez
37 * @author Josep Arus
38 * @author Kenneth Riba
40 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
42 class wikiparser_test extends \basic_testcase {
44 /**
45 * URL inside the clickable text of some link should not be turned into a new link via the url_tag_rule.
47 * @dataProvider urls_inside_link_text_provider
48 * @param string $markup Markup of the Wiki page the text is part of.
49 * @param string $input The input text.
50 * @param string $output The expected output HTML as a result of the parsed input text.
52 public function test_urls_inside_link_text(string $markup, string $input, string $output) {
54 $parsingresult = wiki_parser_proxy::parse($input, $markup, [
55 'link_callback' => '/mod/wiki/locallib.php:wiki_parser_link',
56 'link_callback_args' => ['swid' => 1],
57 ]);
59 $this->assertStringContainsString($output, $parsingresult['parsed_text']);
62 /**
63 * Provides data sets for {@see self::test_urls_inside_link_text()}.
65 * @return array
67 public function urls_inside_link_text_provider() {
68 return [
69 'creole implicit link' => [
70 'markup' => 'creole',
71 'input' => 'Visit https://site.url for more information.',
72 'output' => 'Visit <a href="https://site.url">https://site.url</a> for more information.',
74 'creole explicit link' => [
75 'markup' => 'creole',
76 'input' => 'Visit [[https://site.url]] for more information.',
77 'output' => 'Visit <a href="https://site.url">https://site.url</a> for more information.',
79 'creole explicit link with text' => [
80 'markup' => 'creole',
81 'input' => 'Visit [[https://site.url|http://www.site.url]] for more information.',
82 'output' => 'Visit <a href="https://site.url">http://www.site.url</a> for more information.',
84 'nwiki implicit link' => [
85 'markup' => 'nwiki',
86 'input' => 'Visit https://site.url for more information.',
87 'output' => 'Visit <a href="https://site.url">https://site.url</a> for more information.',
89 'nwiki explicit link' => [
90 'markup' => 'nwiki',
91 'input' => 'Visit [https://site.url] for more information.',
92 'output' => 'Visit <a href="https://site.url">https://site.url</a> for more information.',
94 'nwiki explicit link with space separated text' => [
95 'markup' => 'nwiki',
96 'input' => 'Visit [https://site.url http://www.site.url] for more information.',
97 'output' => 'Visit <a href="https://site.url">http://www.site.url</a> for more information.',
99 'nwiki explicit link with pipe separated text' => [
100 'markup' => 'nwiki',
101 'input' => 'Visit [https://site.url|http://www.site.url] for more information.',
102 'output' => 'Visit <a href="https://site.url">http://www.site.url</a> for more information.',
104 'html implicit link' => [
105 'markup' => 'html',
106 'input' => 'Visit https://site.url for more information.',
107 'output' => 'Visit <a href="https://site.url">https://site.url</a> for more information.',
109 'html explicit link with text' => [
110 'markup' => 'html',
111 'input' => 'Visit <a href="https://site.url">http://www.site.url</a> for more information.',
112 'output' => 'Visit <a href="https://site.url">http://www.site.url</a> for more information.',
114 'html wiki link to non-existing page' => [
115 'markup' => 'html',
116 'input' => 'Visit [[Another page]] for more information.',
117 'output' => 'Visit <a class="wiki_newentry" ' .
118 'href="https://www.example.com/moodle/mod/wiki/create.php?swid=1&amp;title=Another+page&amp;action=new">' .
119 'Another page</a> for more information.',
121 'html wiki link inside an explicit link' => [
122 // The explicit href URL takes precedence here, the [[...]] is not turned into a wiki link.
123 'markup' => 'html',
124 'input' => 'Visit <a href="https://site.url">[[Another page]]</a> for more information.',
125 'output' => 'Visit <a href="https://site.url">[[Another page]]</a> for more information.',
130 function testCreoleMarkup() {
131 $this->assertTestFiles('creole');
134 function testNwikiMarkup() {
135 $this->assertTestFiles('nwiki');
138 function testHtmlMarkup() {
139 $this->assertTestFiles('html');
142 private function assertTestFile($num, $markup) {
143 if(!file_exists(__DIR__."/fixtures/input/$markup/$num") || !file_exists(__DIR__."/fixtures/output/$markup/$num")) {
144 return false;
146 $input = file_get_contents(__DIR__."/fixtures/input/$markup/$num");
147 $output = file_get_contents(__DIR__."/fixtures/output/$markup/$num");
149 $result = wiki_parser_proxy::parse($input, $markup, array('pretty_print' => true));
151 //removes line breaks to avoid line break encoding causing tests to fail.
152 $result['parsed_text'] = preg_replace('~[\r\n]~', '', $result['parsed_text']);
153 $output = preg_replace('~[\r\n]~', '', $output);
155 $this->assertEquals($output, $result['parsed_text'], 'Failed asserting that two strings are equal. Markup = '.$markup.", num = $num");
156 return true;
159 private function assertTestFiles($markup) {
160 $i = 1;
161 while($this->assertTestFile($i, $markup)) {
162 $i++;
167 * Check that headings with special characters work as expected with HTML.
169 * - The heading itself is well displayed,
170 * - The TOC heading is well display,
171 * - The edit link points to the right page,
172 * - The links properly works with get_section.
174 public function test_special_headings() {
176 // First testing HTML markup.
178 // Test section name using HTML entities.
179 $input = '<h1>Code &amp; Test</h1>';
180 $output = '<h3><a name="toc-1"></a>Code &amp; Test <a href="edit.php?pageid=&amp;section=Code+%26amp%3B+Test" '.
181 'class="wiki_edit_section">[edit]</a></h3>' . "\n";
182 $toc = '<div class="wiki-toc"><p class="wiki-toc-title">Table of contents</p><p class="wiki-toc-section-1 '.
183 'wiki-toc-section">1. <a href="#toc-1">Code &amp; Test <a href="edit.php?pageid=&amp;section=Code+%26amp%3B+'.
184 'Test" class="wiki_edit_section">[edit]</a></a></p></div>';
185 $section = wiki_parser_proxy::get_section($input, 'html', 'Code &amp; Test');
186 $actual = wiki_parser_proxy::parse($input, 'html');
187 $this->assertEquals($output, $actual['parsed_text']);
188 $this->assertEquals($toc, $actual['toc']);
189 $this->assertNotEquals(false, $section);
191 // Test section name using non-ASCII characters.
192 $input = '<h1>Another áéíóúç€ test</h1>';
193 $output = '<h3><a name="toc-1"></a>Another áéíóúç€ test <a href="edit.php?pageid=&amp;section=Another+%C'.
194 '3%A1%C3%A9%C3%AD%C3%B3%C3%BA%C3%A7%E2%82%AC+test" class="wiki_edit_section">[edit]</a></h3>' . "\n";
195 $toc = '<div class="wiki-toc"><p class="wiki-toc-title">Table of contents</p><p class="wiki-toc-section-1 '.
196 'wiki-toc-section">1. <a href="#toc-1">Another áéíóúç€ test <a href="edit.php?pageid=&amp;section=Another+%C'.
197 '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>';
198 $section = wiki_parser_proxy::get_section($input, 'html', 'Another áéíóúç€ test');
199 $actual = wiki_parser_proxy::parse($input, 'html');
200 $this->assertEquals($output, $actual['parsed_text']);
201 $this->assertEquals($toc, $actual['toc']);
202 $this->assertNotEquals(false, $section);
204 // Test section name with a URL.
205 $input = '<h1>Another http://moodle.org test</h1>';
206 $output = '<h3><a name="toc-1"></a>Another <a href="http://moodle.org">http://moodle.org</a> test <a href="edit.php'.
207 '?pageid=&amp;section=Another+http%3A%2F%2Fmoodle.org+test" class="wiki_edit_section">[edit]</a></h3>' . "\n";
208 $toc = '<div class="wiki-toc"><p class="wiki-toc-title">Table of contents</p><p class="wiki-toc-section-1 '.
209 'wiki-toc-section">1. <a href="#toc-1">Another http://moodle.org test <a href="edit.php?pageid=&amp;section='.
210 'Another+http%3A%2F%2Fmoodle.org+test" class="wiki_edit_section">[edit]</a></a></p></div>';
211 $section = wiki_parser_proxy::get_section($input, 'html', 'Another http://moodle.org test');
212 $actual = wiki_parser_proxy::parse($input, 'html', array(
213 'link_callback' => '/mod/wiki/locallib.php:wiki_parser_link'
215 $this->assertEquals($output, $actual['parsed_text']);
216 $this->assertEquals($toc, $actual['toc']);
217 $this->assertNotEquals(false, $section);
219 // Test toc section names being wikilinks.
220 $input = '<h1>[[Heading 1]]</h1><h2>[[Heading A]]</h2><h2>Heading D</h2>';
221 $regexpoutput = '!<h3><a name="toc-1"></a>' .
222 '<a class="wiki_newentry" href.*mod/wiki/create\.php\?.*title=Heading\+1.*action=new.*>Heading 1<.*' .
223 '<h4><a name="toc-2"></a>' .
224 '<a class="wiki_newentry" href.*mod/wiki/create\.php\?.*title=Heading\+A.*action=new.*>Heading A<.*' .
225 '<h4><a name="toc-3"></a>' .
226 'Heading D!ms';
227 $regexptoc = '!<a href="#toc-1">Heading 1.*<a href="#toc-2">Heading A</a>.*<a href="#toc-3">Heading D</a>!ms';
228 $section = wiki_parser_proxy::get_section($input, 'html', 'Another [[wikilinked]] test');
229 $actual = wiki_parser_proxy::parse($input, 'html', array(
230 'link_callback' => '/mod/wiki/locallib.php:wiki_parser_link',
231 'link_callback_args' => array('swid' => 1)
233 $this->assertMatchesRegularExpression($regexpoutput, $actual['parsed_text']);
234 $this->assertMatchesRegularExpression($regexptoc, $actual['toc']);
236 // Now going to test Creole markup.
237 // Note that Creole uses links to the escaped version of the section.
239 // Test section name using HTML entities.
240 $input = '= Code & Test =';
241 $output = '<h3><a name="toc-1"></a>Code &amp; Test <a href="edit.php?pageid=&amp;section=Code+%26amp%3B+Test" '.
242 'class="wiki_edit_section">[edit]</a></h3>' . "\n";
243 $toc = '<div class="wiki-toc"><p class="wiki-toc-title">Table of contents</p><p class="wiki-toc-section-1 '.
244 'wiki-toc-section">1. <a href="#toc-1">Code &amp; Test <a href="edit.php?pageid=&amp;section=Code+%26amp%3B+'.
245 'Test" class="wiki_edit_section">[edit]</a></a></p></div>';
246 $section = wiki_parser_proxy::get_section($input, 'creole', 'Code &amp; Test');
247 $actual = wiki_parser_proxy::parse($input, 'creole');
248 $this->assertEquals($output, $actual['parsed_text']);
249 $this->assertEquals($toc, $actual['toc']);
250 $this->assertNotEquals(false, $section);
252 // Test section name using non-ASCII characters.
253 $input = '= Another áéíóúç€ test =';
254 $output = '<h3><a name="toc-1"></a>Another áéíóúç€ test <a href="edit.php?pageid=&amp;section=Another+%C'.
255 '3%A1%C3%A9%C3%AD%C3%B3%C3%BA%C3%A7%E2%82%AC+test" class="wiki_edit_section">[edit]</a></h3>' . "\n";
256 $toc = '<div class="wiki-toc"><p class="wiki-toc-title">Table of contents</p><p class="wiki-toc-section-1 '.
257 'wiki-toc-section">1. <a href="#toc-1">Another áéíóúç€ test <a href="edit.php?pageid=&amp;section=Another+%C'.
258 '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>';
259 $section = wiki_parser_proxy::get_section($input, 'creole', 'Another áéíóúç€ test');
260 $actual = wiki_parser_proxy::parse($input, 'creole');
261 $this->assertEquals($output, $actual['parsed_text']);
262 $this->assertEquals($toc, $actual['toc']);
263 $this->assertNotEquals(false, $section);
265 // Test section name with a URL, creole does not support linking links in a heading.
266 $input = '= Another http://moodle.org test =';
267 $output = '<h3><a name="toc-1"></a>Another http://moodle.org test <a href="edit.php'.
268 '?pageid=&amp;section=Another+http%3A%2F%2Fmoodle.org+test" class="wiki_edit_section">[edit]</a></h3>' . "\n";
269 $toc = '<div class="wiki-toc"><p class="wiki-toc-title">Table of contents</p><p class="wiki-toc-section-1 '.
270 'wiki-toc-section">1. <a href="#toc-1">Another http://moodle.org test <a href="edit.php?pageid=&amp;section='.
271 'Another+http%3A%2F%2Fmoodle.org+test" class="wiki_edit_section">[edit]</a></a></p></div>';
272 $section = wiki_parser_proxy::get_section($input, 'creole', 'Another http://moodle.org test');
273 $actual = wiki_parser_proxy::parse($input, 'creole');
274 $this->assertEquals($output, $actual['parsed_text']);
275 $this->assertEquals($toc, $actual['toc']);
276 $this->assertNotEquals(false, $section);
278 // Now going to test NWiki markup.
279 // Note that Creole uses links to the escaped version of the section.
281 // Test section name using HTML entities.
282 $input = '= Code & Test =';
283 $output = '<h3><a name="toc-1"></a>Code & Test <a href="edit.php?pageid=&amp;section=Code+%26+Test" '.
284 'class="wiki_edit_section">[edit]</a></h3>' . "\n";
285 $toc = '<div class="wiki-toc"><p class="wiki-toc-title">Table of contents</p><p class="wiki-toc-section-1 '.
286 'wiki-toc-section">1. <a href="#toc-1">Code & Test <a href="edit.php?pageid=&amp;section=Code+%26+'.
287 'Test" class="wiki_edit_section">[edit]</a></a></p></div>';
288 $section = wiki_parser_proxy::get_section($input, 'nwiki', 'Code & Test');
289 $actual = wiki_parser_proxy::parse($input, 'nwiki');
290 $this->assertEquals($output, $actual['parsed_text']);
291 $this->assertEquals($toc, $actual['toc']);
292 $this->assertNotEquals(false, $section);
294 // Test section name using non-ASCII characters.
295 $input = '= Another áéíóúç€ test =';
296 $output = '<h3><a name="toc-1"></a>Another áéíóúç€ test <a href="edit.php?pageid=&amp;section=Another+%C'.
297 '3%A1%C3%A9%C3%AD%C3%B3%C3%BA%C3%A7%E2%82%AC+test" class="wiki_edit_section">[edit]</a></h3>' . "\n";
298 $toc = '<div class="wiki-toc"><p class="wiki-toc-title">Table of contents</p><p class="wiki-toc-section-1 '.
299 'wiki-toc-section">1. <a href="#toc-1">Another áéíóúç€ test <a href="edit.php?pageid=&amp;section=Another+%C'.
300 '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>';
301 $section = wiki_parser_proxy::get_section($input, 'nwiki', 'Another áéíóúç€ test');
302 $actual = wiki_parser_proxy::parse($input, 'nwiki');
303 $this->assertEquals($output, $actual['parsed_text']);
304 $this->assertEquals($toc, $actual['toc']);
305 $this->assertNotEquals(false, $section);
307 // Test section name with a URL, nwiki does not support linking links in a heading.
308 $input = '= Another http://moodle.org test =';
309 $output = '<h3><a name="toc-1"></a>Another http://moodle.org test <a href="edit.php'.
310 '?pageid=&amp;section=Another+http%3A%2F%2Fmoodle.org+test" class="wiki_edit_section">[edit]</a></h3>' . "\n";
311 $toc = '<div class="wiki-toc"><p class="wiki-toc-title">Table of contents</p><p class="wiki-toc-section-1 '.
312 'wiki-toc-section">1. <a href="#toc-1">Another http://moodle.org test <a href="edit.php?pageid=&amp;section='.
313 'Another+http%3A%2F%2Fmoodle.org+test" class="wiki_edit_section">[edit]</a></a></p></div>';
314 $section = wiki_parser_proxy::get_section($input, 'nwiki', 'Another http://moodle.org test');
315 $actual = wiki_parser_proxy::parse($input, 'nwiki');
316 $this->assertEquals($output, $actual['parsed_text']);
317 $this->assertEquals($toc, $actual['toc']);
318 $this->assertNotEquals(false, $section);
320 // Test section names when headings start with level 3.
321 $input = '<h3>Heading test</h3><h4>Subsection</h4>';
322 $output = '<h3><a name="toc-1"></a>Heading test <a href="edit.php?pageid=&amp;section=Heading+test" '.
323 'class="wiki_edit_section">[edit]</a></h3>'. "\n" . '<h4><a name="toc-2"></a>Subsection</h4>' . "\n";
324 $toc = '<div class="wiki-toc"><p class="wiki-toc-title">Table of contents</p><p class="wiki-toc-section-1 '.
325 'wiki-toc-section">1. <a href="#toc-1">Heading test <a href="edit.php?pageid=&amp;section=Heading+'.
326 'test" class="wiki_edit_section">[edit]</a></a></p><p class="wiki-toc-section-2 wiki-toc-section">'.
327 '1.1. <a href="#toc-2">Subsection</a></p></div>';
328 $section = wiki_parser_proxy::get_section($input, 'html', 'Heading test');
329 $actual = wiki_parser_proxy::parse($input, 'html');
330 $this->assertEquals($output, $actual['parsed_text']);
331 $this->assertEquals($toc, $actual['toc']);
332 $this->assertNotEquals(false, $section);