2 // This file is part of Moodle - http://moodle.org/
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.
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 <http://www.gnu.org/licenses/>.
18 * Unit tests for the wiki parser
22 * @copyright 2009 Marc Alier, Jordi Piguillem marc.alier@upc.edu
23 * @copyright 2009 Universitat Politecnica de Catalunya http://www.upc.edu
25 * @author Jordi Piguillem
27 * @author David Jimenez
29 * @author Kenneth Riba
31 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34 defined('MOODLE_INTERNAL') || die;
37 require_once($CFG->dirroot . '/mod/wiki/parser/parser.php');
40 class mod_wiki_wikiparser_test extends basic_testcase {
42 function testCreoleMarkup() {
43 $this->assertTestFiles('creole');
46 function testNwikiMarkup() {
47 $this->assertTestFiles('nwiki');
50 function testHtmlMarkup() {
51 $this->assertTestFiles('html');
54 private function assertTestFile($num, $markup) {
55 if(!file_exists(__DIR__."/fixtures/input/$markup/$num") || !file_exists(__DIR__."/fixtures/output/$markup/$num")) {
58 $input = file_get_contents(__DIR__."/fixtures/input/$markup/$num");
59 $output = file_get_contents(__DIR__."/fixtures/output/$markup/$num");
61 $result = wiki_parser_proxy::parse($input, $markup, array('pretty_print' => true));
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);
67 $this->assertEquals($output, $result['parsed_text']);
71 private function assertTestFiles($markup) {
73 while($this->assertTestFile($i, $markup)) {
79 * Check that headings with special characters work as expected with HTML.
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.
86 public function test_special_headings() {
88 // First testing HTML markup.
90 // Test section name using HTML entities.
91 $input = '<h1>Code & Test</h1>';
92 $output = '<h1><a name="toc-1"></a>Code & Test <a href="edit.php?pageid=&section=Code+%26amp%3B+Test" '.
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 '.
95 'wiki-toc-section">1. <a href="#toc-1">Code & Test <a href="edit.php?pageid=&section=Code+%26amp%3B+'.
96 'Test" class="wiki_edit_section">[edit]</a></a></p></div>';
97 $section = wiki_parser_proxy::get_section($input, 'html', 'Code & 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);
103 // Test section name using non-ASCII characters.
104 $input = '<h1>Another áéíóúç€ test</h1>';
105 $output = '<h1><a name="toc-1"></a>Another áéíóúç€ test <a href="edit.php?pageid=&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></h1>' . "\n";
107 $toc = '<div class="wiki-toc"><p class="wiki-toc-title">Table of contents</p><p class="wiki-toc-section-1 '.
108 'wiki-toc-section">1. <a href="#toc-1">Another áéíóúç€ test <a href="edit.php?pageid=&section=Another+%C'.
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);
116 // Test section name with a URL.
117 $input = '<h1>Another http://moodle.org test</h1>';
118 $output = '<h1><a name="toc-1"></a>Another <a href="http://moodle.org">http://moodle.org</a> test <a href="edit.php'.
119 '?pageid=&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 '.
121 'wiki-toc-section">1. <a href="#toc-1">Another http://moodle.org test <a href="edit.php?pageid=&section='.
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'
127 $this->assertEquals($output, $actual['parsed_text']);
128 $this->assertEquals($toc, $actual['toc']);
129 $this->assertNotEquals(false, $section);
131 // Test toc section names being wikilinks.
132 $input = '<h1>[[Heading 1]]</h1><h2>[[Heading A]]</h2><h2>Heading D</h2>';
133 $regexpoutput = '!<h1><a name="toc-1"></a>' .
134 '<a class="wiki_newentry" href.*mod/wiki/create\.php\?.*title=Heading\+1.*action=new.*>Heading 1<.*' .
135 '<h2><a name="toc-2"></a>' .
136 '<a class="wiki_newentry" href.*mod/wiki/create\.php\?.*title=Heading\+A.*action=new.*>Heading A<.*' .
137 '<h2><a name="toc-3"></a>' .
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)
145 $this->assertRegExp($regexpoutput, $actual['parsed_text']);
146 $this->assertRegExp($regexptoc, $actual['toc']);
148 // Now going to test Creole markup.
149 // Note that Creole uses links to the escaped version of the section.
151 // Test section name using HTML entities.
152 $input = '= Code & Test =';
153 $output = '<h1><a name="toc-1"></a>Code & Test <a href="edit.php?pageid=&section=Code+%26amp%3B+Test" '.
154 'class="wiki_edit_section">[edit]</a></h1>' . "\n";
155 $toc = '<div class="wiki-toc"><p class="wiki-toc-title">Table of contents</p><p class="wiki-toc-section-1 '.
156 'wiki-toc-section">1. <a href="#toc-1">Code & Test <a href="edit.php?pageid=&section=Code+%26amp%3B+'.
157 'Test" class="wiki_edit_section">[edit]</a></a></p></div>';
158 $section = wiki_parser_proxy::get_section($input, 'creole', 'Code & 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);
164 // Test section name using non-ASCII characters.
165 $input = '= Another áéíóúç€ test =';
166 $output = '<h1><a name="toc-1"></a>Another áéíóúç€ test <a href="edit.php?pageid=&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></h1>' . "\n";
168 $toc = '<div class="wiki-toc"><p class="wiki-toc-title">Table of contents</p><p class="wiki-toc-section-1 '.
169 'wiki-toc-section">1. <a href="#toc-1">Another áéíóúç€ test <a href="edit.php?pageid=&section=Another+%C'.
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);
177 // Test section name with a URL, creole does not support linking links in a heading.
178 $input = '= Another http://moodle.org test =';
179 $output = '<h1><a name="toc-1"></a>Another http://moodle.org test <a href="edit.php'.
180 '?pageid=&section=Another+http%3A%2F%2Fmoodle.org+test" class="wiki_edit_section">[edit]</a></h1>' . "\n";
181 $toc = '<div class="wiki-toc"><p class="wiki-toc-title">Table of contents</p><p class="wiki-toc-section-1 '.
182 'wiki-toc-section">1. <a href="#toc-1">Another http://moodle.org test <a href="edit.php?pageid=&section='.
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);
190 // Now going to test NWiki markup.
191 // Note that Creole uses links to the escaped version of the section.
193 // Test section name using HTML entities.
194 $input = '= Code & Test =';
195 $output = '<h1><a name="toc-1"></a>Code & Test <a href="edit.php?pageid=&section=Code+%26+Test" '.
196 'class="wiki_edit_section">[edit]</a></h1>' . "\n";
197 $toc = '<div class="wiki-toc"><p class="wiki-toc-title">Table of contents</p><p class="wiki-toc-section-1 '.
198 'wiki-toc-section">1. <a href="#toc-1">Code & Test <a href="edit.php?pageid=&section=Code+%26+'.
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);
206 // Test section name using non-ASCII characters.
207 $input = '= Another áéíóúç€ test =';
208 $output = '<h1><a name="toc-1"></a>Another áéíóúç€ test <a href="edit.php?pageid=&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></h1>' . "\n";
210 $toc = '<div class="wiki-toc"><p class="wiki-toc-title">Table of contents</p><p class="wiki-toc-section-1 '.
211 'wiki-toc-section">1. <a href="#toc-1">Another áéíóúç€ test <a href="edit.php?pageid=&section=Another+%C'.
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);
219 // Test section name with a URL, nwiki does not support linking links in a heading.
220 $input = '= Another http://moodle.org test =';
221 $output = '<h1><a name="toc-1"></a>Another http://moodle.org test <a href="edit.php'.
222 '?pageid=&section=Another+http%3A%2F%2Fmoodle.org+test" class="wiki_edit_section">[edit]</a></h1>' . "\n";
223 $toc = '<div class="wiki-toc"><p class="wiki-toc-title">Table of contents</p><p class="wiki-toc-section-1 '.
224 'wiki-toc-section">1. <a href="#toc-1">Another http://moodle.org test <a href="edit.php?pageid=&section='.
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);