Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403] [Versions 39 and 310]

   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
  22   * @copyright 2009 Marc Alier, Jordi Piguillem marc.alier@upc.edu
  23   * @copyright 2009 Universitat Politecnica de Catalunya http://www.upc.edu
  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  
  34  defined('MOODLE_INTERNAL') || die;
  35  
  36  global $CFG;
  37  require_once($CFG->dirroot . '/mod/wiki/parser/parser.php');
  38  
  39  
  40  class mod_wiki_wikiparser_test extends basic_testcase {
  41  
  42      /**
  43       * URL inside the clickable text of some link should not be turned into a new link via the url_tag_rule.
  44       *
  45       * @dataProvider urls_inside_link_text_provider
  46       * @param string $markup Markup of the Wiki page the text is part of.
  47       * @param string $input The input text.
  48       * @param string $output The expected output HTML as a result of the parsed input text.
  49       */
  50      public function test_urls_inside_link_text(string $markup, string $input, string $output) {
  51  
  52          $parsingresult = wiki_parser_proxy::parse($input, $markup, [
  53              'link_callback' => '/mod/wiki/locallib.php:wiki_parser_link',
  54              'link_callback_args' => ['swid' => 1],
  55          ]);
  56  
  57          $this->assertStringContainsString($output, $parsingresult['parsed_text']);
  58      }
  59  
  60      /**
  61       * Provides data sets for {@see self::test_urls_inside_link_text()}.
  62       *
  63       * @return array
  64       */
  65      public function urls_inside_link_text_provider() {
  66          return [
  67              'creole implicit link' => [
  68                  'markup' => 'creole',
  69                  'input' => 'Visit https://site.url for more information.',
  70                  'output' => 'Visit <a href="https://site.url">https://site.url</a> for more information.',
  71              ],
  72              'creole explicit link' => [
  73                  'markup' => 'creole',
  74                  'input' => 'Visit [[https://site.url]] for more information.',
  75                  'output' => 'Visit <a href="https://site.url">https://site.url</a> for more information.',
  76              ],
  77              'creole explicit link with text' => [
  78                  'markup' => 'creole',
  79                  'input' => 'Visit [[https://site.url|http://www.site.url]] for more information.',
  80                  'output' => 'Visit <a href="https://site.url">http://www.site.url</a> for more information.',
  81              ],
  82              'nwiki implicit link' => [
  83                  'markup' => 'nwiki',
  84                  'input' => 'Visit https://site.url for more information.',
  85                  'output' => 'Visit <a href="https://site.url">https://site.url</a> for more information.',
  86              ],
  87              'nwiki explicit link' => [
  88                  'markup' => 'nwiki',
  89                  'input' => 'Visit [https://site.url] for more information.',
  90                  'output' => 'Visit <a href="https://site.url">https://site.url</a> for more information.',
  91              ],
  92              'nwiki explicit link with space separated text' => [
  93                  'markup' => 'nwiki',
  94                  'input' => 'Visit [https://site.url http://www.site.url] for more information.',
  95                  'output' => 'Visit <a href="https://site.url">http://www.site.url</a> for more information.',
  96              ],
  97              'nwiki explicit link with pipe separated text' => [
  98                  'markup' => 'nwiki',
  99                  'input' => 'Visit [https://site.url|http://www.site.url] for more information.',
 100                  'output' => 'Visit <a href="https://site.url">http://www.site.url</a> for more information.',
 101              ],
 102              'html implicit link' => [
 103                  'markup' => 'html',
 104                  'input' => 'Visit https://site.url for more information.',
 105                  'output' => 'Visit <a href="https://site.url">https://site.url</a> for more information.',
 106              ],
 107              'html explicit link with text' => [
 108                  'markup' => 'html',
 109                  'input' => 'Visit <a href="https://site.url">http://www.site.url</a> for more information.',
 110                  'output' => 'Visit <a href="https://site.url">http://www.site.url</a> for more information.',
 111              ],
 112              'html wiki link to non-existing page' => [
 113                  'markup' => 'html',
 114                  'input' => 'Visit [[Another page]] for more information.',
 115                  'output' => 'Visit <a class="wiki_newentry" ' .
 116                      'href="https://www.example.com/moodle/mod/wiki/create.php?swid=1&amp;title=Another+page&amp;action=new">' .
 117                      'Another page</a> for more information.',
 118              ],
 119              'html wiki link inside an explicit link' => [
 120                  // The explicit href URL takes precedence here, the [[...]] is not turned into a wiki link.
 121                  'markup' => 'html',
 122                  'input' => 'Visit <a href="https://site.url">[[Another page]]</a> for more information.',
 123                  'output' => 'Visit <a href="https://site.url">[[Another page]]</a> for more information.',
 124              ],
 125          ];
 126      }
 127  
 128      function testCreoleMarkup() {
 129          $this->assertTestFiles('creole');
 130      }
 131  
 132      function testNwikiMarkup() {
 133          $this->assertTestFiles('nwiki');
 134      }
 135  
 136      function testHtmlMarkup() {
 137          $this->assertTestFiles('html');
 138      }
 139  
 140      private function assertTestFile($num, $markup) {
 141          if(!file_exists(__DIR__."/fixtures/input/$markup/$num") || !file_exists(__DIR__."/fixtures/output/$markup/$num")) {
 142              return false;
 143          }
 144          $input = file_get_contents(__DIR__."/fixtures/input/$markup/$num");
 145          $output = file_get_contents(__DIR__."/fixtures/output/$markup/$num");
 146  
 147          $result = wiki_parser_proxy::parse($input, $markup, array('pretty_print' => true));
 148  
 149          //removes line breaks to avoid line break encoding causing tests to fail.
 150          $result['parsed_text'] = preg_replace('~[\r\n]~', '', $result['parsed_text']);
 151          $output                = preg_replace('~[\r\n]~', '', $output);
 152  
 153          $this->assertEquals($output, $result['parsed_text'], 'Failed asserting that two strings are equal. Markup = '.$markup.", num = $num");
 154          return true;
 155      }
 156  
 157      private function assertTestFiles($markup) {
 158          $i = 1;
 159          while($this->assertTestFile($i, $markup)) {
 160              $i++;
 161          }
 162      }
 163  
 164      /**
 165       * Check that headings with special characters work as expected with HTML.
 166       *
 167       * - The heading itself is well displayed,
 168       * - The TOC heading is well display,
 169       * - The edit link points to the right page,
 170       * - The links properly works with get_section.
 171       */
 172      public function test_special_headings() {
 173  
 174          // First testing HTML markup.
 175  
 176          // Test section name using HTML entities.
 177          $input = '<h1>Code &amp; Test</h1>';
 178          $output = '<h3><a name="toc-1"></a>Code &amp; Test <a href="edit.php?pageid=&amp;section=Code+%26amp%3B+Test" '.
 179              'class="wiki_edit_section">[edit]</a></h3>' . "\n";
 180          $toc = '<div class="wiki-toc"><p class="wiki-toc-title">Table of contents</p><p class="wiki-toc-section-1 '.
 181              'wiki-toc-section">1. <a href="#toc-1">Code &amp; Test <a href="edit.php?pageid=&amp;section=Code+%26amp%3B+'.
 182              'Test" class="wiki_edit_section">[edit]</a></a></p></div>';
 183          $section = wiki_parser_proxy::get_section($input, 'html', 'Code &amp; Test');
 184          $actual = wiki_parser_proxy::parse($input, 'html');
 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 = '<h1>Another áéíóúç€ test</h1>';
 191          $output = '<h3><a name="toc-1"></a>Another áéíóúç€ test <a href="edit.php?pageid=&amp;section=Another+%C'.
 192              '3%A1%C3%A9%C3%AD%C3%B3%C3%BA%C3%A7%E2%82%AC+test" class="wiki_edit_section">[edit]</a></h3>' . "\n";
 193          $toc = '<div class="wiki-toc"><p class="wiki-toc-title">Table of contents</p><p class="wiki-toc-section-1 '.
 194              'wiki-toc-section">1. <a href="#toc-1">Another áéíóúç€ test <a href="edit.php?pageid=&amp;section=Another+%C'.
 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, 'html', 'Another áéíóúç€ test');
 197          $actual = wiki_parser_proxy::parse($input, 'html');
 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.
 203          $input = '<h1>Another http://moodle.org test</h1>';
 204          $output = '<h3><a name="toc-1"></a>Another <a href="http://moodle.org">http://moodle.org</a> test <a href="edit.php'.
 205              '?pageid=&amp;section=Another+http%3A%2F%2Fmoodle.org+test" class="wiki_edit_section">[edit]</a></h3>' . "\n";
 206          $toc = '<div class="wiki-toc"><p class="wiki-toc-title">Table of contents</p><p class="wiki-toc-section-1 '.
 207              'wiki-toc-section">1. <a href="#toc-1">Another http://moodle.org test <a href="edit.php?pageid=&amp;section='.
 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, 'html', 'Another http://moodle.org test');
 210          $actual = wiki_parser_proxy::parse($input, 'html', array(
 211              'link_callback' => '/mod/wiki/locallib.php:wiki_parser_link'
 212          ));
 213          $this->assertEquals($output, $actual['parsed_text']);
 214          $this->assertEquals($toc, $actual['toc']);
 215          $this->assertNotEquals(false, $section);
 216  
 217          // Test toc section names being wikilinks.
 218          $input = '<h1>[[Heading 1]]</h1><h2>[[Heading A]]</h2><h2>Heading D</h2>';
 219          $regexpoutput = '!<h3><a name="toc-1"></a>' .
 220              '<a class="wiki_newentry" href.*mod/wiki/create\.php\?.*title=Heading\+1.*action=new.*>Heading 1<.*' .
 221              '<h4><a name="toc-2"></a>' .
 222              '<a class="wiki_newentry" href.*mod/wiki/create\.php\?.*title=Heading\+A.*action=new.*>Heading A<.*' .
 223              '<h4><a name="toc-3"></a>' .
 224              'Heading D!ms';
 225          $regexptoc = '!<a href="#toc-1">Heading 1.*<a href="#toc-2">Heading A</a>.*<a href="#toc-3">Heading D</a>!ms';
 226          $section = wiki_parser_proxy::get_section($input, 'html', 'Another [[wikilinked]] test');
 227          $actual = wiki_parser_proxy::parse($input, 'html', array(
 228              'link_callback' => '/mod/wiki/locallib.php:wiki_parser_link',
 229              'link_callback_args' => array('swid' => 1)
 230          ));
 231          $this->assertRegExp($regexpoutput, $actual['parsed_text']);
 232          $this->assertRegExp($regexptoc, $actual['toc']);
 233  
 234          // Now going to test Creole markup.
 235          // Note that Creole uses links to the escaped version of the section.
 236  
 237          // Test section name using HTML entities.
 238          $input = '= Code & Test =';
 239          $output = '<h3><a name="toc-1"></a>Code &amp; Test <a href="edit.php?pageid=&amp;section=Code+%26amp%3B+Test" '.
 240              'class="wiki_edit_section">[edit]</a></h3>' . "\n";
 241          $toc = '<div class="wiki-toc"><p class="wiki-toc-title">Table of contents</p><p class="wiki-toc-section-1 '.
 242              'wiki-toc-section">1. <a href="#toc-1">Code &amp; Test <a href="edit.php?pageid=&amp;section=Code+%26amp%3B+'.
 243              'Test" class="wiki_edit_section">[edit]</a></a></p></div>';
 244          $section = wiki_parser_proxy::get_section($input, 'creole', 'Code &amp; Test');
 245          $actual = wiki_parser_proxy::parse($input, 'creole');
 246          $this->assertEquals($output, $actual['parsed_text']);
 247          $this->assertEquals($toc, $actual['toc']);
 248          $this->assertNotEquals(false, $section);
 249  
 250          // Test section name using non-ASCII characters.
 251          $input = '= Another áéíóúç€ test =';
 252          $output = '<h3><a name="toc-1"></a>Another áéíóúç€ test <a href="edit.php?pageid=&amp;section=Another+%C'.
 253              '3%A1%C3%A9%C3%AD%C3%B3%C3%BA%C3%A7%E2%82%AC+test" class="wiki_edit_section">[edit]</a></h3>' . "\n";
 254          $toc = '<div class="wiki-toc"><p class="wiki-toc-title">Table of contents</p><p class="wiki-toc-section-1 '.
 255              'wiki-toc-section">1. <a href="#toc-1">Another áéíóúç€ test <a href="edit.php?pageid=&amp;section=Another+%C'.
 256              '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>';
 257          $section = wiki_parser_proxy::get_section($input, 'creole', 'Another áéíóúç€ test');
 258          $actual = wiki_parser_proxy::parse($input, 'creole');
 259          $this->assertEquals($output, $actual['parsed_text']);
 260          $this->assertEquals($toc, $actual['toc']);
 261          $this->assertNotEquals(false, $section);
 262  
 263          // Test section name with a URL, creole does not support linking links in a heading.
 264          $input = '= Another http://moodle.org test =';
 265          $output = '<h3><a name="toc-1"></a>Another http://moodle.org test <a href="edit.php'.
 266              '?pageid=&amp;section=Another+http%3A%2F%2Fmoodle.org+test" class="wiki_edit_section">[edit]</a></h3>' . "\n";
 267          $toc = '<div class="wiki-toc"><p class="wiki-toc-title">Table of contents</p><p class="wiki-toc-section-1 '.
 268              'wiki-toc-section">1. <a href="#toc-1">Another http://moodle.org test <a href="edit.php?pageid=&amp;section='.
 269              'Another+http%3A%2F%2Fmoodle.org+test" class="wiki_edit_section">[edit]</a></a></p></div>';
 270          $section = wiki_parser_proxy::get_section($input, 'creole', 'Another http://moodle.org test');
 271          $actual = wiki_parser_proxy::parse($input, 'creole');
 272          $this->assertEquals($output, $actual['parsed_text']);
 273          $this->assertEquals($toc, $actual['toc']);
 274          $this->assertNotEquals(false, $section);
 275  
 276          // Now going to test NWiki markup.
 277          // Note that Creole uses links to the escaped version of the section.
 278  
 279          // Test section name using HTML entities.
 280          $input = '= Code & Test =';
 281          $output = '<h3><a name="toc-1"></a>Code & Test <a href="edit.php?pageid=&amp;section=Code+%26+Test" '.
 282              'class="wiki_edit_section">[edit]</a></h3>' . "\n";
 283          $toc = '<div class="wiki-toc"><p class="wiki-toc-title">Table of contents</p><p class="wiki-toc-section-1 '.
 284              'wiki-toc-section">1. <a href="#toc-1">Code & Test <a href="edit.php?pageid=&amp;section=Code+%26+'.
 285              'Test" class="wiki_edit_section">[edit]</a></a></p></div>';
 286          $section = wiki_parser_proxy::get_section($input, 'nwiki', 'Code & Test');
 287          $actual = wiki_parser_proxy::parse($input, 'nwiki');
 288          $this->assertEquals($output, $actual['parsed_text']);
 289          $this->assertEquals($toc, $actual['toc']);
 290          $this->assertNotEquals(false, $section);
 291  
 292          // Test section name using non-ASCII characters.
 293          $input = '= Another áéíóúç€ test =';
 294          $output = '<h3><a name="toc-1"></a>Another áéíóúç€ test <a href="edit.php?pageid=&amp;section=Another+%C'.
 295              '3%A1%C3%A9%C3%AD%C3%B3%C3%BA%C3%A7%E2%82%AC+test" class="wiki_edit_section">[edit]</a></h3>' . "\n";
 296          $toc = '<div class="wiki-toc"><p class="wiki-toc-title">Table of contents</p><p class="wiki-toc-section-1 '.
 297              'wiki-toc-section">1. <a href="#toc-1">Another áéíóúç€ test <a href="edit.php?pageid=&amp;section=Another+%C'.
 298              '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>';
 299          $section = wiki_parser_proxy::get_section($input, 'nwiki', 'Another áéíóúç€ test');
 300          $actual = wiki_parser_proxy::parse($input, 'nwiki');
 301          $this->assertEquals($output, $actual['parsed_text']);
 302          $this->assertEquals($toc, $actual['toc']);
 303          $this->assertNotEquals(false, $section);
 304  
 305          // Test section name with a URL, nwiki does not support linking links in a heading.
 306          $input = '= Another http://moodle.org test =';
 307          $output = '<h3><a name="toc-1"></a>Another http://moodle.org test <a href="edit.php'.
 308              '?pageid=&amp;section=Another+http%3A%2F%2Fmoodle.org+test" class="wiki_edit_section">[edit]</a></h3>' . "\n";
 309          $toc = '<div class="wiki-toc"><p class="wiki-toc-title">Table of contents</p><p class="wiki-toc-section-1 '.
 310              'wiki-toc-section">1. <a href="#toc-1">Another http://moodle.org test <a href="edit.php?pageid=&amp;section='.
 311              'Another+http%3A%2F%2Fmoodle.org+test" class="wiki_edit_section">[edit]</a></a></p></div>';
 312          $section = wiki_parser_proxy::get_section($input, 'nwiki', 'Another http://moodle.org test');
 313          $actual = wiki_parser_proxy::parse($input, 'nwiki');
 314          $this->assertEquals($output, $actual['parsed_text']);
 315          $this->assertEquals($toc, $actual['toc']);
 316          $this->assertNotEquals(false, $section);
 317  
 318          // Test section names when headings start with level 3.
 319          $input = '<h3>Heading test</h3><h4>Subsection</h4>';
 320          $output = '<h3><a name="toc-1"></a>Heading test <a href="edit.php?pageid=&amp;section=Heading+test" '.
 321              'class="wiki_edit_section">[edit]</a></h3>'. "\n" . '<h4><a name="toc-2"></a>Subsection</h4>' . "\n";
 322          $toc = '<div class="wiki-toc"><p class="wiki-toc-title">Table of contents</p><p class="wiki-toc-section-1 '.
 323              'wiki-toc-section">1. <a href="#toc-1">Heading test <a href="edit.php?pageid=&amp;section=Heading+'.
 324              'test" class="wiki_edit_section">[edit]</a></a></p><p class="wiki-toc-section-2 wiki-toc-section">'.
 325              '1.1. <a href="#toc-2">Subsection</a></p></div>';
 326          $section = wiki_parser_proxy::get_section($input, 'html', 'Heading test');
 327          $actual = wiki_parser_proxy::parse($input, 'html');
 328          $this->assertEquals($output, $actual['parsed_text']);
 329          $this->assertEquals($toc, $actual['toc']);
 330          $this->assertNotEquals(false, $section);
 331      }
 332  
 333  }