Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 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 39 and 310] [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 and 403]

   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   * Provides the {@link filter_mathjaxloader_filtermath_testcase} class.
  18   *
  19   * @package     filter_mathjaxloader
  20   * @category    test
  21   * @copyright   2018 Markku Riekkinen
  22   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  defined('MOODLE_INTERNAL') || die();
  25  global $CFG;
  26  require_once($CFG->dirroot.'/filter/mathjaxloader/filter.php');
  27  /**
  28   * Unit tests for the MathJax loader filter.
  29   *
  30   * @copyright 2018 Markku Riekkinen
  31   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  32   */
  33  class filter_mathjaxloader_filtermath_testcase extends advanced_testcase {
  34  
  35      /**
  36       * Test the functionality of {@link filter_mathjaxloader::filter()}.
  37       *
  38       * @param string $inputtext The text given by the user.
  39       * @param string $expected The expected output after filtering.
  40       *
  41       * @dataProvider math_filtering_inputs
  42       */
  43      public function test_math_filtering($inputtext, $expected) {
  44          $filter = new filter_mathjaxloader(context_system::instance(), []);
  45          $this->assertEquals($expected, $filter->filter($inputtext));
  46      }
  47  
  48      /**
  49       * Data provider for {@link self::test_math_filtering()}.
  50       *
  51       * @return array of [inputtext, expectedoutput] tuples.
  52       */
  53      public function math_filtering_inputs() {
  54          return [
  55              // One inline formula.
  56              ['Some inline math \\( y = x^2 \\).',
  57              '<span class="filter_mathjaxloader_equation">Some inline math <span class="nolink">\\( y = x^2 \\)</span>.</span>'],
  58  
  59              // One inline and one display.
  60              ['Some inline math \\( y = x^2 \\) and display formula \\[ S = \\sum_{n=1}^{\\infty} 2^n \\]',
  61              '<span class="filter_mathjaxloader_equation">Some inline math <span class="nolink">\\( y = x^2 \\)</span> and '
  62                  . 'display formula <span class="nolink">\\[ S = \\sum_{n=1}^{\\infty} 2^n \\]</span></span>'],
  63  
  64              // One display and one inline.
  65              ['Display formula \\[ S = \\sum_{n=1}^{\\infty} 2^n \\] and some inline math \\( y = x^2 \\).',
  66              '<span class="filter_mathjaxloader_equation">Display formula <span class="nolink">\\[ S = \\sum_{n=1}^{\\infty} 2^n \\]</span> and '
  67                  . 'some inline math <span class="nolink">\\( y = x^2 \\)</span>.</span>'],
  68  
  69              // One inline and one display (with dollars).
  70              ['Some inline math \\( y = x^2 \\) and display formula $$ S = \\sum_{n=1}^{\\infty} 2^n $$',
  71              '<span class="filter_mathjaxloader_equation">Some inline math <span class="nolink">\\( y = x^2 \\)</span> and '
  72                  . 'display formula <span class="nolink">$$ S = \\sum_{n=1}^{\\infty} 2^n $$</span></span>'],
  73  
  74              // One display (with dollars) and one inline.
  75              ['Display formula $$ S = \\sum_{n=1}^{\\infty} 2^n $$ and some inline math \\( y = x^2 \\).',
  76              '<span class="filter_mathjaxloader_equation">Display formula <span class="nolink">$$ S = \\sum_{n=1}^{\\infty} 2^n $$</span> and '
  77                  . 'some inline math <span class="nolink">\\( y = x^2 \\)</span>.</span>'],
  78  
  79              // Inline math environment nested inside display environment (using a custom LaTex macro).
  80              ['\\[ \\newcommand{\\False}{\\mathsf{F}} \\newcommand{\\NullF}{\\fbox{\\(\\False\\)}} \\] '
  81                  . 'Text with inline formula using the custom LaTex macro \\( a = \\NullF \\).',
  82              '<span class="filter_mathjaxloader_equation"><span class="nolink">'
  83                  . '\\[ \\newcommand{\\False}{\\mathsf{F}} \\newcommand{\\NullF}{\\fbox{\\(\\False\\)}} \\]</span> '
  84                  . 'Text with inline formula using the custom LaTex macro <span class="nolink">\\( a = \\NullF \\)</span>.</span>'],
  85  
  86              // Nested environments and some more content.
  87              ['\\[ \\newcommand{\\False}{\\mathsf{F}} \\newcommand{\\NullF}{\\fbox{\\(\\False\\)}} \\] '
  88                  . 'Text with inline formula using the custom LaTex macro \\( a = \\NullF \\). Finally, a display formula '
  89                  . '$$ b = \\NullF $$',
  90              '<span class="filter_mathjaxloader_equation"><span class="nolink">'
  91                  . '\\[ \\newcommand{\\False}{\\mathsf{F}} \\newcommand{\\NullF}{\\fbox{\\(\\False\\)}} \\]</span> '
  92                  . 'Text with inline formula using the custom LaTex macro <span class="nolink">\\( a = \\NullF \\)</span>. '
  93                  . 'Finally, a display formula <span class="nolink">$$ b = \\NullF $$</span></span>'],
  94  
  95              // Broken math: the delimiters ($$) are not closed.
  96              ['Writing text and starting display math. $$ k = i^3 \\newcommand{\\False}{\\mathsf{F}} \\newcommand{\\NullF}{\\fbox{\\(\\False\\)}} '
  97                  . 'More text and inline math \\( x = \\NullF \\).',
  98              'Writing text and starting display math. $$ k = i^3 \\newcommand{\\False}{\\mathsf{F}} \\newcommand{\\NullF}{\\fbox{\\(\\False\\)}} '
  99                  . 'More text and inline math \\( x = \\NullF \\).'],
 100          ];
 101      }
 102  }