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]

   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   * Skype icons filter phpunit tests
  19   *
  20   * @package    filter_emoticon
  21   * @category   test
  22   * @copyright  2013 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  namespace filter_emoticon;
  27  
  28  use filter_emoticon;
  29  
  30  defined('MOODLE_INTERNAL') || die();
  31  
  32  global $CFG;
  33  require_once($CFG->dirroot . '/filter/emoticon/filter.php'); // Include the code to test.
  34  
  35  /**
  36   * Skype icons filter testcase.
  37   */
  38  class filter_test extends \advanced_testcase {
  39  
  40      /**
  41       * Test that filter ignores nolink/pre element, and processes remaining text
  42       *
  43       * @param string $input
  44       * @param string $expectedprefix
  45       *
  46       * @dataProvider filter_emoticon_filtered_provider
  47       */
  48      public function test_filter_emoticon_filtered(string $input, string $expectedprefix): void {
  49          $this->resetAfterTest();
  50  
  51          $filteredtext = (new testable_filter_emoticon())->filter($input, [
  52              'originalformat' => FORMAT_HTML,
  53          ]);
  54  
  55          $this->assertStringStartsWith($expectedprefix, $filteredtext);
  56          $this->assertStringEndsWith($this->get_converted_content_for_emoticon('(n)'), $filteredtext);
  57      }
  58  
  59      /**
  60       * Data provider for {@see test_filter_emoticon_filtered}
  61       *
  62       * @return string[]
  63       */
  64      public function filter_emoticon_filtered_provider(): array {
  65          return [
  66              'FORMAT_HTML is filtered' => [
  67                  'input' => 'Hello(n)',
  68                  'expectedprefix' => 'Hello',
  69              ],
  70              'Nested nolink should not be processed, emoticon postfix should be' => [
  71                  'input' => '<span class="nolink"><span>(n)</span>(n)</span>(n)',
  72                  'expectedprefix' => '<span class="nolink"><span>(n)</span>(n)</span>',
  73              ],
  74              'Nested pre should not be processed, emoticon postfix should be' => [
  75                  'input' => '<pre><pre>(n)</pre>(n)</pre>(n)',
  76                  'expectedprefix' => '<pre><pre>(n)</pre>(n)</pre>',
  77              ],
  78          ];
  79      }
  80  
  81      /**
  82       * Tests the filter doesn't affect nolink classes.
  83       *
  84       * @dataProvider filter_emoticon_provider
  85       */
  86      public function test_filter_emoticon($input, $format, $expected) {
  87          $this->resetAfterTest();
  88  
  89          $filter = new testable_filter_emoticon();
  90          $this->assertEquals($expected, $filter->filter($input, [
  91                  'originalformat' => $format,
  92              ]));
  93      }
  94  
  95      /**
  96       * The data provider for filter emoticon tests, containing input that is not expected to be filtered
  97       *
  98       * @return  array
  99       */
 100      public function filter_emoticon_provider() {
 101          $grr = '(grr)';
 102          return [
 103              'FORMAT_MOODLE is not filtered' => [
 104                  'input' => $grr,
 105                  'format' => FORMAT_MOODLE,
 106                  'expected' => $grr,
 107              ],
 108              'FORMAT_MARKDOWN is not filtered' => [
 109                  'input' => $grr,
 110                  'format' => FORMAT_MARKDOWN,
 111                  'expected' => $grr,
 112              ],
 113              'FORMAT_PLAIN is not filtered' => [
 114                  'input' => $grr,
 115                  'format' => FORMAT_PLAIN,
 116                  'expected' => $grr,
 117              ],
 118              'Script tag should not be processed' => [
 119                  'input' => "<script language='javascript'>alert('{$grr}');</script>",
 120                  'format' => FORMAT_HTML,
 121                  'expected' => "<script language='javascript'>alert('{$grr}');</script>",
 122              ],
 123              'Basic nolink should not be processed' => [
 124                  'input' => '<span class="nolink">(n)</span>',
 125                  'format' => FORMAT_HTML,
 126                  'expected' => '<span class="nolink">(n)</span>',
 127              ],
 128              'Nested nolink should not be processed' => [
 129                  'input' => '<span class="nolink"><span>(n)</span>(n)</span>',
 130                  'format' => FORMAT_HTML,
 131                  'expected' => '<span class="nolink"><span>(n)</span>(n)</span>',
 132              ],
 133              'Basic pre should not be processed' => [
 134                  'input' => '<pre>(n)</pre>',
 135                  'format' => FORMAT_HTML,
 136                  'expected' => '<pre>(n)</pre>',
 137              ],
 138              'Nested pre should not be processed' => [
 139                  'input' => '<pre><pre>(n)</pre>(n)</pre>',
 140                  'format' => FORMAT_HTML,
 141                  'expected' => '<pre><pre>(n)</pre>(n)</pre>',
 142              ],
 143          ];
 144      }
 145  
 146      /**
 147       * Translate the text for a single emoticon into the rendered value.
 148       *
 149       * @param   string  $text The text to translate.
 150       * @return  string
 151       */
 152      public function get_converted_content_for_emoticon($text) {
 153          global $OUTPUT;
 154          $manager = get_emoticon_manager();
 155          $emoticons = $manager->get_emoticons();
 156          foreach ($emoticons as $emoticon) {
 157              if ($emoticon->text == $text) {
 158                  return $OUTPUT->render($manager->prepare_renderable_emoticon($emoticon));
 159              }
 160          }
 161  
 162          return $text;
 163      }
 164  
 165      /**
 166       * Tests the filter doesn't break anything if activated but invalid format passed.
 167       *
 168       */
 169      public function test_filter_invalidformat() {
 170          global $PAGE;
 171          $this->resetAfterTest();
 172  
 173          $filter = new testable_filter_emoticon();
 174          $input = '(grr)';
 175          $expected = '(grr)';
 176  
 177          $this->assertEquals($expected, $filter->filter($input, [
 178              'originalformat' => 'ILLEGALFORMAT',
 179          ]));
 180      }
 181  
 182      /**
 183       * Tests the filter doesn't break anything if activated but no emoticons available.
 184       *
 185       */
 186      public function test_filter_emptyemoticons() {
 187          global $CFG;
 188          $this->resetAfterTest();
 189          // Empty the emoticons array.
 190          $CFG->emoticons = null;
 191  
 192          $filter = new filter_emoticon(\context_system::instance(), array('originalformat' => FORMAT_HTML));
 193  
 194          $input = '(grr)';
 195          $expected = '(grr)';
 196  
 197          $this->assertEquals($expected, $filter->filter($input, [
 198              'originalformat' => FORMAT_HTML,
 199          ]));
 200      }
 201  }
 202  
 203  /**
 204   * Subclass for easier testing.
 205   */
 206  class testable_filter_emoticon extends filter_emoticon {
 207      public function __construct() {
 208          // Reset static emoticon caches.
 209          parent::$emoticontexts = array();
 210          parent::$emoticonimgs = array();
 211          // Use this context for filtering.
 212          $this->context = \context_system::instance();
 213          // Define FORMAT_HTML as only one filtering in DB.
 214          set_config('formats', implode(',', array(FORMAT_HTML)), 'filter_emoticon');
 215      }
 216  }