MDL-49807 mod_wiki: minimum heading level should always be <h3>
[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
3d89ccb3 67 $this->assertEquals($output, $result['parsed_text'], 'Failed asserting that two strings are equal. Markup = '.$markup.", num = $num");
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>';
3d89ccb3
MG
92 $output = '<h3><a name="toc-1"></a>Code &amp; Test <a href="edit.php?pageid=&amp;section=Code+%26amp%3B+Test" '.
93 'class="wiki_edit_section">[edit]</a></h3>' . "\n";
c159e2aa 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>';
3d89ccb3
MG
105 $output = '<h3><a name="toc-1"></a>Another áéíóúç€ test <a href="edit.php?pageid=&amp;section=Another+%C'.
106 '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 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>';
3d89ccb3
MG
118 $output = '<h3><a name="toc-1"></a>Another <a href="http://moodle.org">http://moodle.org</a> test <a href="edit.php'.
119 '?pageid=&amp;section=Another+http%3A%2F%2Fmoodle.org+test" class="wiki_edit_section">[edit]</a></h3>' . "\n";
c159e2aa 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
89bb268e
EL
131 // Test toc section names being wikilinks.
132 $input = '<h1>[[Heading 1]]</h1><h2>[[Heading A]]</h2><h2>Heading D</h2>';
3d89ccb3 133 $regexpoutput = '!<h3><a name="toc-1"></a>' .
89bb268e 134 '<a class="wiki_newentry" href.*mod/wiki/create\.php\?.*title=Heading\+1.*action=new.*>Heading 1<.*' .
3d89ccb3 135 '<h4><a name="toc-2"></a>' .
89bb268e 136 '<a class="wiki_newentry" href.*mod/wiki/create\.php\?.*title=Heading\+A.*action=new.*>Heading A<.*' .
3d89ccb3 137 '<h4><a name="toc-3"></a>' .
89bb268e
EL
138 'Heading D!ms';
139 $regexptoc = '!<a href="#toc-1">Heading 1.*<a href="#toc-2">Heading A</a>.*<a href="#toc-3">Heading D</a>!ms';
140 $section = wiki_parser_proxy::get_section($input, 'html', 'Another [[wikilinked]] test');
141 $actual = wiki_parser_proxy::parse($input, 'html', array(
142 'link_callback' => '/mod/wiki/locallib.php:wiki_parser_link',
143 'link_callback_args' => array('swid' => 1)
144 ));
145 $this->assertRegExp($regexpoutput, $actual['parsed_text']);
146 $this->assertRegExp($regexptoc, $actual['toc']);
147
c159e2aa
FM
148 // Now going to test Creole markup.
149 // Note that Creole uses links to the escaped version of the section.
150
151 // Test section name using HTML entities.
152 $input = '= Code & Test =';
3d89ccb3
MG
153 $output = '<h3><a name="toc-1"></a>Code &amp; Test <a href="edit.php?pageid=&amp;section=Code+%26amp%3B+Test" '.
154 'class="wiki_edit_section">[edit]</a></h3>' . "\n";
c159e2aa 155 $toc = '<div class="wiki-toc"><p class="wiki-toc-title">Table of contents</p><p class="wiki-toc-section-1 '.
72e3cb9a 156 'wiki-toc-section">1. <a href="#toc-1">Code &amp; Test <a href="edit.php?pageid=&amp;section=Code+%26amp%3B+'.
c159e2aa
FM
157 'Test" class="wiki_edit_section">[edit]</a></a></p></div>';
158 $section = wiki_parser_proxy::get_section($input, 'creole', 'Code &amp; Test');
159 $actual = wiki_parser_proxy::parse($input, 'creole');
160 $this->assertEquals($output, $actual['parsed_text']);
161 $this->assertEquals($toc, $actual['toc']);
162 $this->assertNotEquals(false, $section);
163
164 // Test section name using non-ASCII characters.
165 $input = '= Another áéíóúç€ test =';
3d89ccb3
MG
166 $output = '<h3><a name="toc-1"></a>Another áéíóúç€ test <a href="edit.php?pageid=&amp;section=Another+%C'.
167 '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 168 $toc = '<div class="wiki-toc"><p class="wiki-toc-title">Table of contents</p><p class="wiki-toc-section-1 '.
72e3cb9a 169 'wiki-toc-section">1. <a href="#toc-1">Another áéíóúç€ test <a href="edit.php?pageid=&amp;section=Another+%C'.
c159e2aa
FM
170 '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>';
171 $section = wiki_parser_proxy::get_section($input, 'creole', 'Another áéíóúç€ test');
172 $actual = wiki_parser_proxy::parse($input, 'creole');
173 $this->assertEquals($output, $actual['parsed_text']);
174 $this->assertEquals($toc, $actual['toc']);
175 $this->assertNotEquals(false, $section);
176
177 // Test section name with a URL, creole does not support linking links in a heading.
178 $input = '= Another http://moodle.org test =';
3d89ccb3
MG
179 $output = '<h3><a name="toc-1"></a>Another http://moodle.org test <a href="edit.php'.
180 '?pageid=&amp;section=Another+http%3A%2F%2Fmoodle.org+test" class="wiki_edit_section">[edit]</a></h3>' . "\n";
c159e2aa 181 $toc = '<div class="wiki-toc"><p class="wiki-toc-title">Table of contents</p><p class="wiki-toc-section-1 '.
72e3cb9a 182 'wiki-toc-section">1. <a href="#toc-1">Another http://moodle.org test <a href="edit.php?pageid=&amp;section='.
c159e2aa
FM
183 'Another+http%3A%2F%2Fmoodle.org+test" class="wiki_edit_section">[edit]</a></a></p></div>';
184 $section = wiki_parser_proxy::get_section($input, 'creole', 'Another http://moodle.org test');
185 $actual = wiki_parser_proxy::parse($input, 'creole');
186 $this->assertEquals($output, $actual['parsed_text']);
187 $this->assertEquals($toc, $actual['toc']);
188 $this->assertNotEquals(false, $section);
189
190 // Now going to test NWiki markup.
191 // Note that Creole uses links to the escaped version of the section.
192
193 // Test section name using HTML entities.
194 $input = '= Code & Test =';
3d89ccb3
MG
195 $output = '<h3><a name="toc-1"></a>Code & Test <a href="edit.php?pageid=&amp;section=Code+%26+Test" '.
196 'class="wiki_edit_section">[edit]</a></h3>' . "\n";
c159e2aa 197 $toc = '<div class="wiki-toc"><p class="wiki-toc-title">Table of contents</p><p class="wiki-toc-section-1 '.
72e3cb9a 198 'wiki-toc-section">1. <a href="#toc-1">Code & Test <a href="edit.php?pageid=&amp;section=Code+%26+'.
c159e2aa
FM
199 'Test" class="wiki_edit_section">[edit]</a></a></p></div>';
200 $section = wiki_parser_proxy::get_section($input, 'nwiki', 'Code & Test');
201 $actual = wiki_parser_proxy::parse($input, 'nwiki');
202 $this->assertEquals($output, $actual['parsed_text']);
203 $this->assertEquals($toc, $actual['toc']);
204 $this->assertNotEquals(false, $section);
205
206 // Test section name using non-ASCII characters.
207 $input = '= Another áéíóúç€ test =';
3d89ccb3
MG
208 $output = '<h3><a name="toc-1"></a>Another áéíóúç€ test <a href="edit.php?pageid=&amp;section=Another+%C'.
209 '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 210 $toc = '<div class="wiki-toc"><p class="wiki-toc-title">Table of contents</p><p class="wiki-toc-section-1 '.
72e3cb9a 211 'wiki-toc-section">1. <a href="#toc-1">Another áéíóúç€ test <a href="edit.php?pageid=&amp;section=Another+%C'.
c159e2aa
FM
212 '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>';
213 $section = wiki_parser_proxy::get_section($input, 'nwiki', 'Another áéíóúç€ test');
214 $actual = wiki_parser_proxy::parse($input, 'nwiki');
215 $this->assertEquals($output, $actual['parsed_text']);
216 $this->assertEquals($toc, $actual['toc']);
217 $this->assertNotEquals(false, $section);
218
219 // Test section name with a URL, nwiki does not support linking links in a heading.
220 $input = '= Another http://moodle.org test =';
3d89ccb3
MG
221 $output = '<h3><a name="toc-1"></a>Another http://moodle.org test <a href="edit.php'.
222 '?pageid=&amp;section=Another+http%3A%2F%2Fmoodle.org+test" class="wiki_edit_section">[edit]</a></h3>' . "\n";
c159e2aa 223 $toc = '<div class="wiki-toc"><p class="wiki-toc-title">Table of contents</p><p class="wiki-toc-section-1 '.
72e3cb9a 224 'wiki-toc-section">1. <a href="#toc-1">Another http://moodle.org test <a href="edit.php?pageid=&amp;section='.
c159e2aa
FM
225 'Another+http%3A%2F%2Fmoodle.org+test" class="wiki_edit_section">[edit]</a></a></p></div>';
226 $section = wiki_parser_proxy::get_section($input, 'nwiki', 'Another http://moodle.org test');
227 $actual = wiki_parser_proxy::parse($input, 'nwiki');
228 $this->assertEquals($output, $actual['parsed_text']);
229 $this->assertEquals($toc, $actual['toc']);
230 $this->assertNotEquals(false, $section);
3d89ccb3
MG
231
232 // Test section names when headings start with level 3.
233 $input = '<h3>Heading test</h3><h4>Subsection</h4>';
234 $output = '<h3><a name="toc-1"></a>Heading test <a href="edit.php?pageid=&amp;section=Heading+test" '.
235 'class="wiki_edit_section">[edit]</a></h3>'. "\n" . '<h4><a name="toc-2"></a>Subsection</h4>' . "\n";
236 $toc = '<div class="wiki-toc"><p class="wiki-toc-title">Table of contents</p><p class="wiki-toc-section-1 '.
237 'wiki-toc-section">1. <a href="#toc-1">Heading test <a href="edit.php?pageid=&amp;section=Heading+'.
238 'test" class="wiki_edit_section">[edit]</a></a></p><p class="wiki-toc-section-2 wiki-toc-section">'.
239 '1.1. <a href="#toc-2">Subsection</a></p></div>';
240 $section = wiki_parser_proxy::get_section($input, 'html', 'Heading test');
241 $actual = wiki_parser_proxy::parse($input, 'html');
242 $this->assertEquals($output, $actual['parsed_text']);
243 $this->assertEquals($toc, $actual['toc']);
244 $this->assertNotEquals(false, $section);
c159e2aa
FM
245 }
246
ba0c4843 247}