[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/mod/wiki/tests/ -> wikiparser_test.php (source)

   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      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  
  67          $this->assertEquals($output, $result['parsed_text'], 'Failed asserting that two strings are equal. Markup = '.$markup.", num = $num");
  68          return true;
  69      }
  70  
  71      private function assertTestFiles($markup) {
  72          $i = 1;
  73          while($this->assertTestFile($i, $markup)) {
  74              $i++;
  75          }
  76      }
  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>';
  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";
  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 &amp; Test <a href="edit.php?pageid=&amp;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 &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>';
 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";
 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=&amp;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);
 115  
 116          // Test section name with a URL.
 117          $input = '<h1>Another http://moodle.org test</h1>';
 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";
 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=&amp;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'
 126          ));
 127          $this->assertEquals($output, $actual['parsed_text']);
 128          $this->assertEquals($toc, $actual['toc']);
 129          $this->assertNotEquals(false, $section);
 130  
 131          // Test toc section names being wikilinks.
 132          $input = '<h1>[[Heading 1]]</h1><h2>[[Heading A]]</h2><h2>Heading D</h2>';
 133          $regexpoutput = '!<h3><a name="toc-1"></a>' .
 134              '<a class="wiki_newentry" href.*mod/wiki/create\.php\?.*title=Heading\+1.*action=new.*>Heading 1<.*' .
 135              '<h4><a name="toc-2"></a>' .
 136              '<a class="wiki_newentry" href.*mod/wiki/create\.php\?.*title=Heading\+A.*action=new.*>Heading A<.*' .
 137              '<h4><a name="toc-3"></a>' .
 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  
 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 =';
 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";
 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 &amp; Test <a href="edit.php?pageid=&amp;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 &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 =';
 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";
 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=&amp;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);
 176  
 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 = '<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";
 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=&amp;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);
 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 =';
 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";
 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=&amp;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);
 205  
 206          // Test section name using non-ASCII characters.
 207          $input = '= Another áéíóúç€ test =';
 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";
 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=&amp;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);
 218  
 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 = '<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";
 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=&amp;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);
 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);
 245      }
 246  
 247  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1