Merge branch 'MDL-37739-malformed-wiki-toc-links' of https://github.com/lucaboesch...
[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
42 function testCreoleMarkup() {
43 $this->assertTestFiles('creole');
44 }
45
46 function testNwikiMarkup() {
47 $this->assertTestFiles('nwiki');
48 }
49
50 function testHtmlMarkup() {
51 $this->assertTestFiles('html');
52 }
53
54 private function assertTestFile($num, $markup) {
55 if(!file_exists(__DIR__."/fixtures/input/$markup/$num") || !file_exists(__DIR__."/fixtures/output/$markup/$num")) {
56 return false;
57 }
58 $input = file_get_contents(__DIR__."/fixtures/input/$markup/$num");
59 $output = file_get_contents(__DIR__."/fixtures/output/$markup/$num");
60
61 $result = wiki_parser_proxy::parse($input, $markup, array('pretty_print' => true));
62
63 //removes line breaks to avoid line break encoding causing tests to fail.
64 $result['parsed_text'] = preg_replace('~[\r\n]~', '', $result['parsed_text']);
65 $output = preg_replace('~[\r\n]~', '', $output);
66
c159e2aa 67 $this->assertEquals($output, $result['parsed_text']);
ba0c4843
PS
68 return true;
69 }
70
71 private function assertTestFiles($markup) {
72 $i = 1;
73 while($this->assertTestFile($i, $markup)) {
74 $i++;
75 }
76 }
c159e2aa
FM
77
78 /**
79 * Check that headings with special characters work as expected with HTML.
80 *
81 * - The heading itself is well displayed,
82 * - The TOC heading is well display,
83 * - The edit link points to the right page,
84 * - The links properly works with get_section.
85 */
86 public function test_special_headings() {
87
88 // First testing HTML markup.
89
90 // Test section name using HTML entities.
91 $input = '<h1>Code &amp; Test</h1>';
72e3cb9a 92 $output = '<h1><a name="toc-1"></a>Code &amp; Test <a href="edit.php?pageid=&amp;section=Code+%26amp%3B+Test" '.
c159e2aa
FM
93 'class="wiki_edit_section">[edit]</a></h1>' . "\n";
94 $toc = '<div class="wiki-toc"><p class="wiki-toc-title">Table of contents</p><p class="wiki-toc-section-1 '.
72e3cb9a 95 'wiki-toc-section">1. <a href="#toc-1">Code &amp; Test <a href="edit.php?pageid=&amp;section=Code+%26amp%3B+'.
c159e2aa
FM
96 'Test" class="wiki_edit_section">[edit]</a></a></p></div>';
97 $section = wiki_parser_proxy::get_section($input, 'html', 'Code &amp; Test');
98 $actual = wiki_parser_proxy::parse($input, 'html');
99 $this->assertEquals($output, $actual['parsed_text']);
100 $this->assertEquals($toc, $actual['toc']);
101 $this->assertNotEquals(false, $section);
102
103 // Test section name using non-ASCII characters.
104 $input = '<h1>Another áéíóúç€ test</h1>';
72e3cb9a 105 $output = '<h1><a name="toc-1"></a>Another áéíóúç€ test <a href="edit.php?pageid=&amp;section=Another+%C'.
c159e2aa
FM
106 '3%A1%C3%A9%C3%AD%C3%B3%C3%BA%C3%A7%E2%82%AC+test" class="wiki_edit_section">[edit]</a></h1>' . "\n";
107 $toc = '<div class="wiki-toc"><p class="wiki-toc-title">Table of contents</p><p class="wiki-toc-section-1 '.
72e3cb9a 108 'wiki-toc-section">1. <a href="#toc-1">Another áéíóúç€ test <a href="edit.php?pageid=&amp;section=Another+%C'.
c159e2aa
FM
109 '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>';
110 $section = wiki_parser_proxy::get_section($input, 'html', 'Another áéíóúç€ test');
111 $actual = wiki_parser_proxy::parse($input, 'html');
112 $this->assertEquals($output, $actual['parsed_text']);
113 $this->assertEquals($toc, $actual['toc']);
114 $this->assertNotEquals(false, $section);
115
116 // Test section name with a URL.
117 $input = '<h1>Another http://moodle.org test</h1>';
72e3cb9a 118 $output = '<h1><a name="toc-1"></a>Another <a href="http://moodle.org">http://moodle.org</a> test <a href="edit.php'.
c159e2aa
FM
119 '?pageid=&amp;section=Another+http%3A%2F%2Fmoodle.org+test" class="wiki_edit_section">[edit]</a></h1>' . "\n";
120 $toc = '<div class="wiki-toc"><p class="wiki-toc-title">Table of contents</p><p class="wiki-toc-section-1 '.
72e3cb9a 121 'wiki-toc-section">1. <a href="#toc-1">Another http://moodle.org test <a href="edit.php?pageid=&amp;section='.
c159e2aa
FM
122 'Another+http%3A%2F%2Fmoodle.org+test" class="wiki_edit_section">[edit]</a></a></p></div>';
123 $section = wiki_parser_proxy::get_section($input, 'html', 'Another http://moodle.org test');
124 $actual = wiki_parser_proxy::parse($input, 'html', array(
125 'link_callback' => '/mod/wiki/locallib.php:wiki_parser_link'
126 ));
127 $this->assertEquals($output, $actual['parsed_text']);
128 $this->assertEquals($toc, $actual['toc']);
129 $this->assertNotEquals(false, $section);
130
131 // Now going to test Creole markup.
132 // Note that Creole uses links to the escaped version of the section.
133
134 // Test section name using HTML entities.
135 $input = '= Code & Test =';
72e3cb9a 136 $output = '<h1><a name="toc-1"></a>Code &amp; Test <a href="edit.php?pageid=&amp;section=Code+%26amp%3B+Test" '.
c159e2aa
FM
137 'class="wiki_edit_section">[edit]</a></h1>' . "\n";
138 $toc = '<div class="wiki-toc"><p class="wiki-toc-title">Table of contents</p><p class="wiki-toc-section-1 '.
72e3cb9a 139 'wiki-toc-section">1. <a href="#toc-1">Code &amp; Test <a href="edit.php?pageid=&amp;section=Code+%26amp%3B+'.
c159e2aa
FM
140 'Test" class="wiki_edit_section">[edit]</a></a></p></div>';
141 $section = wiki_parser_proxy::get_section($input, 'creole', 'Code &amp; Test');
142 $actual = wiki_parser_proxy::parse($input, 'creole');
143 $this->assertEquals($output, $actual['parsed_text']);
144 $this->assertEquals($toc, $actual['toc']);
145 $this->assertNotEquals(false, $section);
146
147 // Test section name using non-ASCII characters.
148 $input = '= Another áéíóúç€ test =';
72e3cb9a 149 $output = '<h1><a name="toc-1"></a>Another áéíóúç€ test <a href="edit.php?pageid=&amp;section=Another+%C'.
c159e2aa
FM
150 '3%A1%C3%A9%C3%AD%C3%B3%C3%BA%C3%A7%E2%82%AC+test" class="wiki_edit_section">[edit]</a></h1>' . "\n";
151 $toc = '<div class="wiki-toc"><p class="wiki-toc-title">Table of contents</p><p class="wiki-toc-section-1 '.
72e3cb9a 152 'wiki-toc-section">1. <a href="#toc-1">Another áéíóúç€ test <a href="edit.php?pageid=&amp;section=Another+%C'.
c159e2aa
FM
153 '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>';
154 $section = wiki_parser_proxy::get_section($input, 'creole', 'Another áéíóúç€ test');
155 $actual = wiki_parser_proxy::parse($input, 'creole');
156 $this->assertEquals($output, $actual['parsed_text']);
157 $this->assertEquals($toc, $actual['toc']);
158 $this->assertNotEquals(false, $section);
159
160 // Test section name with a URL, creole does not support linking links in a heading.
161 $input = '= Another http://moodle.org test =';
72e3cb9a 162 $output = '<h1><a name="toc-1"></a>Another http://moodle.org test <a href="edit.php'.
c159e2aa
FM
163 '?pageid=&amp;section=Another+http%3A%2F%2Fmoodle.org+test" class="wiki_edit_section">[edit]</a></h1>' . "\n";
164 $toc = '<div class="wiki-toc"><p class="wiki-toc-title">Table of contents</p><p class="wiki-toc-section-1 '.
72e3cb9a 165 'wiki-toc-section">1. <a href="#toc-1">Another http://moodle.org test <a href="edit.php?pageid=&amp;section='.
c159e2aa
FM
166 'Another+http%3A%2F%2Fmoodle.org+test" class="wiki_edit_section">[edit]</a></a></p></div>';
167 $section = wiki_parser_proxy::get_section($input, 'creole', 'Another http://moodle.org test');
168 $actual = wiki_parser_proxy::parse($input, 'creole');
169 $this->assertEquals($output, $actual['parsed_text']);
170 $this->assertEquals($toc, $actual['toc']);
171 $this->assertNotEquals(false, $section);
172
173 // Now going to test NWiki markup.
174 // Note that Creole uses links to the escaped version of the section.
175
176 // Test section name using HTML entities.
177 $input = '= Code & Test =';
72e3cb9a 178 $output = '<h1><a name="toc-1"></a>Code & Test <a href="edit.php?pageid=&amp;section=Code+%26+Test" '.
c159e2aa
FM
179 'class="wiki_edit_section">[edit]</a></h1>' . "\n";
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 & Test <a href="edit.php?pageid=&amp;section=Code+%26+'.
c159e2aa
FM
182 'Test" class="wiki_edit_section">[edit]</a></a></p></div>';
183 $section = wiki_parser_proxy::get_section($input, 'nwiki', 'Code & Test');
184 $actual = wiki_parser_proxy::parse($input, 'nwiki');
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 = '= Another áéíóúç€ test =';
72e3cb9a 191 $output = '<h1><a name="toc-1"></a>Another áéíóúç€ test <a href="edit.php?pageid=&amp;section=Another+%C'.
c159e2aa
FM
192 '3%A1%C3%A9%C3%AD%C3%B3%C3%BA%C3%A7%E2%82%AC+test" class="wiki_edit_section">[edit]</a></h1>' . "\n";
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, 'nwiki', 'Another áéíóúç€ test');
197 $actual = wiki_parser_proxy::parse($input, 'nwiki');
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, nwiki does not support linking links in a heading.
203 $input = '= Another http://moodle.org test =';
72e3cb9a 204 $output = '<h1><a name="toc-1"></a>Another http://moodle.org test <a href="edit.php'.
c159e2aa
FM
205 '?pageid=&amp;section=Another+http%3A%2F%2Fmoodle.org+test" class="wiki_edit_section">[edit]</a></h1>' . "\n";
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, 'nwiki', 'Another http://moodle.org test');
210 $actual = wiki_parser_proxy::parse($input, 'nwiki');
211 $this->assertEquals($output, $actual['parsed_text']);
212 $this->assertEquals($toc, $actual['toc']);
213 $this->assertNotEquals(false, $section);
214 }
215
ba0c4843 216}