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