PageRenderTime 45ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/mod/wiki/tests/wikiparser_test.php

https://github.com/pauln/moodle
PHP | 247 lines | 163 code | 29 blank | 55 comment | 3 complexity | cad1cf08cbbd23976c67cc0368d12319 MD5 | raw file
  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. * Unit tests for the wiki parser
  18. *
  19. * @package mod_wiki
  20. * @category phpunit
  21. * @copyright 2009 Marc Alier, Jordi Piguillem marc.alier@upc.edu
  22. * @copyright 2009 Universitat Politecnica de Catalunya http://www.upc.edu
  23. *
  24. * @author Jordi Piguillem
  25. * @author Marc Alier
  26. * @author David Jimenez
  27. * @author Josep Arus
  28. * @author Kenneth Riba
  29. *
  30. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  31. */
  32. defined('MOODLE_INTERNAL') || die;
  33. global $CFG;
  34. require_once($CFG->dirroot . '/mod/wiki/parser/parser.php');
  35. class mod_wiki_wikiparser_test extends basic_testcase {
  36. function testCreoleMarkup() {
  37. $this->assertTestFiles('creole');
  38. }
  39. function testNwikiMarkup() {
  40. $this->assertTestFiles('nwiki');
  41. }
  42. function testHtmlMarkup() {
  43. $this->assertTestFiles('html');
  44. }
  45. private function assertTestFile($num, $markup) {
  46. if(!file_exists(__DIR__."/fixtures/input/$markup/$num") || !file_exists(__DIR__."/fixtures/output/$markup/$num")) {
  47. return false;
  48. }
  49. $input = file_get_contents(__DIR__."/fixtures/input/$markup/$num");
  50. $output = file_get_contents(__DIR__."/fixtures/output/$markup/$num");
  51. $result = wiki_parser_proxy::parse($input, $markup, array('pretty_print' => true));
  52. //removes line breaks to avoid line break encoding causing tests to fail.
  53. $result['parsed_text'] = preg_replace('~[\r\n]~', '', $result['parsed_text']);
  54. $output = preg_replace('~[\r\n]~', '', $output);
  55. $this->assertEquals($output, $result['parsed_text'], 'Failed asserting that two strings are equal. Markup = '.$markup.", num = $num");
  56. return true;
  57. }
  58. private function assertTestFiles($markup) {
  59. $i = 1;
  60. while($this->assertTestFile($i, $markup)) {
  61. $i++;
  62. }
  63. }
  64. /**
  65. * Check that headings with special characters work as expected with HTML.
  66. *
  67. * - The heading itself is well displayed,
  68. * - The TOC heading is well display,
  69. * - The edit link points to the right page,
  70. * - The links properly works with get_section.
  71. */
  72. public function test_special_headings() {
  73. // First testing HTML markup.
  74. // Test section name using HTML entities.
  75. $input = '<h1>Code &amp; Test</h1>';
  76. $output = '<h3><a name="toc-1"></a>Code &amp; Test <a href="edit.php?pageid=&amp;section=Code+%26amp%3B+Test" '.
  77. 'class="wiki_edit_section">[edit]</a></h3>' . "\n";
  78. $toc = '<div class="wiki-toc"><p class="wiki-toc-title">Table of contents</p><p class="wiki-toc-section-1 '.
  79. 'wiki-toc-section">1. <a href="#toc-1">Code &amp; Test <a href="edit.php?pageid=&amp;section=Code+%26amp%3B+'.
  80. 'Test" class="wiki_edit_section">[edit]</a></a></p></div>';
  81. $section = wiki_parser_proxy::get_section($input, 'html', 'Code &amp; Test');
  82. $actual = wiki_parser_proxy::parse($input, 'html');
  83. $this->assertEquals($output, $actual['parsed_text']);
  84. $this->assertEquals($toc, $actual['toc']);
  85. $this->assertNotEquals(false, $section);
  86. // Test section name using non-ASCII characters.
  87. $input = '<h1>Another áéíóúç€ test</h1>';
  88. $output = '<h3><a name="toc-1"></a>Another áéíóúç€ test <a href="edit.php?pageid=&amp;section=Another+%C'.
  89. '3%A1%C3%A9%C3%AD%C3%B3%C3%BA%C3%A7%E2%82%AC+test" class="wiki_edit_section">[edit]</a></h3>' . "\n";
  90. $toc = '<div class="wiki-toc"><p class="wiki-toc-title">Table of contents</p><p class="wiki-toc-section-1 '.
  91. 'wiki-toc-section">1. <a href="#toc-1">Another áéíóúç€ test <a href="edit.php?pageid=&amp;section=Another+%C'.
  92. '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>';
  93. $section = wiki_parser_proxy::get_section($input, 'html', 'Another áéíóúç€ test');
  94. $actual = wiki_parser_proxy::parse($input, 'html');
  95. $this->assertEquals($output, $actual['parsed_text']);
  96. $this->assertEquals($toc, $actual['toc']);
  97. $this->assertNotEquals(false, $section);
  98. // Test section name with a URL.
  99. $input = '<h1>Another http://moodle.org test</h1>';
  100. $output = '<h3><a name="toc-1"></a>Another <a href="http://moodle.org">http://moodle.org</a> test <a href="edit.php'.
  101. '?pageid=&amp;section=Another+http%3A%2F%2Fmoodle.org+test" class="wiki_edit_section">[edit]</a></h3>' . "\n";
  102. $toc = '<div class="wiki-toc"><p class="wiki-toc-title">Table of contents</p><p class="wiki-toc-section-1 '.
  103. 'wiki-toc-section">1. <a href="#toc-1">Another http://moodle.org test <a href="edit.php?pageid=&amp;section='.
  104. 'Another+http%3A%2F%2Fmoodle.org+test" class="wiki_edit_section">[edit]</a></a></p></div>';
  105. $section = wiki_parser_proxy::get_section($input, 'html', 'Another http://moodle.org test');
  106. $actual = wiki_parser_proxy::parse($input, 'html', array(
  107. 'link_callback' => '/mod/wiki/locallib.php:wiki_parser_link'
  108. ));
  109. $this->assertEquals($output, $actual['parsed_text']);
  110. $this->assertEquals($toc, $actual['toc']);
  111. $this->assertNotEquals(false, $section);
  112. // Test toc section names being wikilinks.
  113. $input = '<h1>[[Heading 1]]</h1><h2>[[Heading A]]</h2><h2>Heading D</h2>';
  114. $regexpoutput = '!<h3><a name="toc-1"></a>' .
  115. '<a class="wiki_newentry" href.*mod/wiki/create\.php\?.*title=Heading\+1.*action=new.*>Heading 1<.*' .
  116. '<h4><a name="toc-2"></a>' .
  117. '<a class="wiki_newentry" href.*mod/wiki/create\.php\?.*title=Heading\+A.*action=new.*>Heading A<.*' .
  118. '<h4><a name="toc-3"></a>' .
  119. 'Heading D!ms';
  120. $regexptoc = '!<a href="#toc-1">Heading 1.*<a href="#toc-2">Heading A</a>.*<a href="#toc-3">Heading D</a>!ms';
  121. $section = wiki_parser_proxy::get_section($input, 'html', 'Another [[wikilinked]] test');
  122. $actual = wiki_parser_proxy::parse($input, 'html', array(
  123. 'link_callback' => '/mod/wiki/locallib.php:wiki_parser_link',
  124. 'link_callback_args' => array('swid' => 1)
  125. ));
  126. $this->assertRegExp($regexpoutput, $actual['parsed_text']);
  127. $this->assertRegExp($regexptoc, $actual['toc']);
  128. // Now going to test Creole markup.
  129. // Note that Creole uses links to the escaped version of the section.
  130. // Test section name using HTML entities.
  131. $input = '= Code & Test =';
  132. $output = '<h3><a name="toc-1"></a>Code &amp; Test <a href="edit.php?pageid=&amp;section=Code+%26amp%3B+Test" '.
  133. 'class="wiki_edit_section">[edit]</a></h3>' . "\n";
  134. $toc = '<div class="wiki-toc"><p class="wiki-toc-title">Table of contents</p><p class="wiki-toc-section-1 '.
  135. 'wiki-toc-section">1. <a href="#toc-1">Code &amp; Test <a href="edit.php?pageid=&amp;section=Code+%26amp%3B+'.
  136. 'Test" class="wiki_edit_section">[edit]</a></a></p></div>';
  137. $section = wiki_parser_proxy::get_section($input, 'creole', 'Code &amp; Test');
  138. $actual = wiki_parser_proxy::parse($input, 'creole');
  139. $this->assertEquals($output, $actual['parsed_text']);
  140. $this->assertEquals($toc, $actual['toc']);
  141. $this->assertNotEquals(false, $section);
  142. // Test section name using non-ASCII characters.
  143. $input = '= Another áéíóúç€ test =';
  144. $output = '<h3><a name="toc-1"></a>Another áéíóúç€ test <a href="edit.php?pageid=&amp;section=Another+%C'.
  145. '3%A1%C3%A9%C3%AD%C3%B3%C3%BA%C3%A7%E2%82%AC+test" class="wiki_edit_section">[edit]</a></h3>' . "\n";
  146. $toc = '<div class="wiki-toc"><p class="wiki-toc-title">Table of contents</p><p class="wiki-toc-section-1 '.
  147. 'wiki-toc-section">1. <a href="#toc-1">Another áéíóúç€ test <a href="edit.php?pageid=&amp;section=Another+%C'.
  148. '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>';
  149. $section = wiki_parser_proxy::get_section($input, 'creole', 'Another áéíóúç€ test');
  150. $actual = wiki_parser_proxy::parse($input, 'creole');
  151. $this->assertEquals($output, $actual['parsed_text']);
  152. $this->assertEquals($toc, $actual['toc']);
  153. $this->assertNotEquals(false, $section);
  154. // Test section name with a URL, creole does not support linking links in a heading.
  155. $input = '= Another http://moodle.org test =';
  156. $output = '<h3><a name="toc-1"></a>Another http://moodle.org test <a href="edit.php'.
  157. '?pageid=&amp;section=Another+http%3A%2F%2Fmoodle.org+test" class="wiki_edit_section">[edit]</a></h3>' . "\n";
  158. $toc = '<div class="wiki-toc"><p class="wiki-toc-title">Table of contents</p><p class="wiki-toc-section-1 '.
  159. 'wiki-toc-section">1. <a href="#toc-1">Another http://moodle.org test <a href="edit.php?pageid=&amp;section='.
  160. 'Another+http%3A%2F%2Fmoodle.org+test" class="wiki_edit_section">[edit]</a></a></p></div>';
  161. $section = wiki_parser_proxy::get_section($input, 'creole', 'Another http://moodle.org test');
  162. $actual = wiki_parser_proxy::parse($input, 'creole');
  163. $this->assertEquals($output, $actual['parsed_text']);
  164. $this->assertEquals($toc, $actual['toc']);
  165. $this->assertNotEquals(false, $section);
  166. // Now going to test NWiki markup.
  167. // Note that Creole uses links to the escaped version of the section.
  168. // Test section name using HTML entities.
  169. $input = '= Code & Test =';
  170. $output = '<h3><a name="toc-1"></a>Code & Test <a href="edit.php?pageid=&amp;section=Code+%26+Test" '.
  171. 'class="wiki_edit_section">[edit]</a></h3>' . "\n";
  172. $toc = '<div class="wiki-toc"><p class="wiki-toc-title">Table of contents</p><p class="wiki-toc-section-1 '.
  173. 'wiki-toc-section">1. <a href="#toc-1">Code & Test <a href="edit.php?pageid=&amp;section=Code+%26+'.
  174. 'Test" class="wiki_edit_section">[edit]</a></a></p></div>';
  175. $section = wiki_parser_proxy::get_section($input, 'nwiki', 'Code & Test');
  176. $actual = wiki_parser_proxy::parse($input, 'nwiki');
  177. $this->assertEquals($output, $actual['parsed_text']);
  178. $this->assertEquals($toc, $actual['toc']);
  179. $this->assertNotEquals(false, $section);
  180. // Test section name using non-ASCII characters.
  181. $input = '= Another áéíóúç€ test =';
  182. $output = '<h3><a name="toc-1"></a>Another áéíóúç€ test <a href="edit.php?pageid=&amp;section=Another+%C'.
  183. '3%A1%C3%A9%C3%AD%C3%B3%C3%BA%C3%A7%E2%82%AC+test" class="wiki_edit_section">[edit]</a></h3>' . "\n";
  184. $toc = '<div class="wiki-toc"><p class="wiki-toc-title">Table of contents</p><p class="wiki-toc-section-1 '.
  185. 'wiki-toc-section">1. <a href="#toc-1">Another áéíóúç€ test <a href="edit.php?pageid=&amp;section=Another+%C'.
  186. '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>';
  187. $section = wiki_parser_proxy::get_section($input, 'nwiki', 'Another áéíóúç€ test');
  188. $actual = wiki_parser_proxy::parse($input, 'nwiki');
  189. $this->assertEquals($output, $actual['parsed_text']);
  190. $this->assertEquals($toc, $actual['toc']);
  191. $this->assertNotEquals(false, $section);
  192. // Test section name with a URL, nwiki does not support linking links in a heading.
  193. $input = '= Another http://moodle.org test =';
  194. $output = '<h3><a name="toc-1"></a>Another http://moodle.org test <a href="edit.php'.
  195. '?pageid=&amp;section=Another+http%3A%2F%2Fmoodle.org+test" class="wiki_edit_section">[edit]</a></h3>' . "\n";
  196. $toc = '<div class="wiki-toc"><p class="wiki-toc-title">Table of contents</p><p class="wiki-toc-section-1 '.
  197. 'wiki-toc-section">1. <a href="#toc-1">Another http://moodle.org test <a href="edit.php?pageid=&amp;section='.
  198. 'Another+http%3A%2F%2Fmoodle.org+test" class="wiki_edit_section">[edit]</a></a></p></div>';
  199. $section = wiki_parser_proxy::get_section($input, 'nwiki', 'Another http://moodle.org test');
  200. $actual = wiki_parser_proxy::parse($input, 'nwiki');
  201. $this->assertEquals($output, $actual['parsed_text']);
  202. $this->assertEquals($toc, $actual['toc']);
  203. $this->assertNotEquals(false, $section);
  204. // Test section names when headings start with level 3.
  205. $input = '<h3>Heading test</h3><h4>Subsection</h4>';
  206. $output = '<h3><a name="toc-1"></a>Heading test <a href="edit.php?pageid=&amp;section=Heading+test" '.
  207. 'class="wiki_edit_section">[edit]</a></h3>'. "\n" . '<h4><a name="toc-2"></a>Subsection</h4>' . "\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">Heading test <a href="edit.php?pageid=&amp;section=Heading+'.
  210. 'test" class="wiki_edit_section">[edit]</a></a></p><p class="wiki-toc-section-2 wiki-toc-section">'.
  211. '1.1. <a href="#toc-2">Subsection</a></p></div>';
  212. $section = wiki_parser_proxy::get_section($input, 'html', 'Heading test');
  213. $actual = wiki_parser_proxy::parse($input, 'html');
  214. $this->assertEquals($output, $actual['parsed_text']);
  215. $this->assertEquals($toc, $actual['toc']);
  216. $this->assertNotEquals(false, $section);
  217. }
  218. }