Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 39 and 401] [Versions 401 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  namespace core;
  18  
  19  use filter_manager;
  20  
  21  /**
  22   * Unit tests for the {@link filter_manager} class.
  23   *
  24   * @package   core
  25   * @category  test
  26   * @copyright 2015 The Open University
  27   * @license   http://www.gnu.org/copyleft/gpl.html GNU Public License
  28   */
  29  class filter_manager_test extends \advanced_testcase {
  30  
  31      /**
  32       * Helper method to apply filters to some text and return the result.
  33       * @param string $text the text to filter.
  34       * @param array $skipfilters any filters not to apply, even if they are configured.
  35       * @return string the filtered text.
  36       */
  37      protected function filter_text($text, $skipfilters) {
  38          global $PAGE;
  39          $filtermanager = filter_manager::instance();
  40          $filtermanager->setup_page_for_filters($PAGE, $PAGE->context);
  41          $filteroptions = array(
  42                  'originalformat' => FORMAT_HTML,
  43                  'noclean' => false,
  44          );
  45          return $filtermanager->filter_text($text, $PAGE->context, $filteroptions, $skipfilters);
  46      }
  47  
  48      public function test_filter_normal() {
  49          $this->resetAfterTest();
  50          filter_set_global_state('emoticon', TEXTFILTER_ON);
  51          $this->assertMatchesRegularExpression(
  52              '~^<p><img class="icon emoticon" alt="smile" title="smile" ' .
  53                  'src="https://www.example.com/moodle/theme/image.php/_s/boost/core/1/s/smiley" /></p>$~',
  54              $this->filter_text('<p>:-)</p>', array()));
  55      }
  56  
  57      public function test_one_filter_disabled() {
  58          $this->resetAfterTest();
  59          filter_set_global_state('emoticon', TEXTFILTER_ON);
  60          $this->assertEquals('<p>:-)</p>',
  61                  $this->filter_text('<p>:-)</p>', array('emoticon')));
  62      }
  63  
  64      public function test_disabling_other_filter_does_not_break_it() {
  65          $this->resetAfterTest();
  66          filter_set_global_state('emoticon', TEXTFILTER_ON);
  67          $this->assertMatchesRegularExpression('~^<p><img class="icon emoticon" alt="smile" ' .
  68                  'title="smile" src="https://www.example.com/moodle/theme/image.php/_s/boost/core/1/s/smiley" /></p>$~',
  69              $this->filter_text('<p>:-)</p>', array('urltolink')));
  70      }
  71  
  72      public function test_one_filter_of_two_disabled() {
  73          $this->resetAfterTest();
  74          filter_set_global_state('emoticon', TEXTFILTER_ON);
  75          filter_set_global_state('urltolink', TEXTFILTER_ON);
  76          $this->assertMatchesRegularExpression('~^<p><img class="icon emoticon" alt="smile" title="smile" ' .
  77                  'src="https://www.example.com/moodle/theme/image.php/_s/boost/core/1/s/smiley" /> http://google.com/</p>$~',
  78              $this->filter_text('<p>:-) http://google.com/</p>', array('glossary', 'urltolink')));
  79      }
  80  }