Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 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 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 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   * 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  defined('MOODLE_INTERNAL') || die();
  27  
  28  global $CFG;
  29  require_once($CFG->dirroot . '/filter/emoticon/filter.php'); // Include the code to test.
  30  
  31  /**
  32   * Skype icons filter testcase.
  33   */
  34  class filter_emoticon_testcase extends advanced_testcase {
  35  
  36      /**
  37       * Tests the filter doesn't affect nolink classes.
  38       *
  39       * @dataProvider filter_emoticon_provider
  40       */
  41      public function test_filter_emoticon($input, $format, $expected) {
  42          $this->resetAfterTest();
  43  
  44          $filter = new testable_filter_emoticon();
  45          $this->assertEquals($expected, $filter->filter($input, [
  46                  'originalformat' => $format,
  47              ]));
  48      }
  49  
  50      /**
  51       * The data provider for filter emoticon tests.
  52       *
  53       * @return  array
  54       */
  55      public function filter_emoticon_provider() {
  56          $grr = '(grr)';
  57          return [
  58              'FORMAT_MOODLE is not filtered' => [
  59                  'input' => $grr,
  60                  'format' => FORMAT_MOODLE,
  61                  'expected' => $grr,
  62              ],
  63              'FORMAT_MARKDOWN is not filtered' => [
  64                  'input' => $grr,
  65                  'format' => FORMAT_MARKDOWN,
  66                  'expected' => $grr,
  67              ],
  68              'FORMAT_PLAIN is not filtered' => [
  69                  'input' => $grr,
  70                  'format' => FORMAT_PLAIN,
  71                  'expected' => $grr,
  72              ],
  73              'FORMAT_HTML is filtered' => [
  74                  'input' => $grr,
  75                  'format' => FORMAT_HTML,
  76                  'expected' => $this->get_converted_content_for_emoticon($grr),
  77              ],
  78              'Script tag should not be processed' => [
  79                  'input' => "<script language='javascript'>alert('{$grr}');</script>",
  80                  'format' => FORMAT_HTML,
  81                  'expected' => "<script language='javascript'>alert('{$grr}');</script>",
  82              ],
  83              'Basic nolink should not be processed' => [
  84                  'input' => '<span class="nolink">(n)</span>',
  85                  'format' => FORMAT_HTML,
  86                  'expected' => '<span class="nolink">(n)</span>',
  87              ],
  88              'Nested nolink should not be processed' => [
  89                  'input' => '<span class="nolink"><span>(n)</span>(n)</span>',
  90                  'format' => FORMAT_HTML,
  91                  'expected' => '<span class="nolink"><span>(n)</span>(n)</span>',
  92              ],
  93              'Nested nolink should not be processed but following emoticon' => [
  94                  'input' => '<span class="nolink"><span>(n)</span>(n)</span>(n)',
  95                  'format' => FORMAT_HTML,
  96                  'expected' => '<span class="nolink"><span>(n)</span>(n)</span>' . $this->get_converted_content_for_emoticon('(n)'),
  97              ],
  98              'Basic pre should not be processed' => [
  99                  'input' => '<pre>(n)</pre>',
 100                  'format' => FORMAT_HTML,
 101                  'expected' => '<pre>(n)</pre>',
 102              ],
 103              'Nested pre should not be processed' => [
 104                  'input' => '<pre><pre>(n)</pre>(n)</pre>',
 105                  'format' => FORMAT_HTML,
 106                  'expected' => '<pre><pre>(n)</pre>(n)</pre>',
 107              ],
 108              'Nested pre should not be processed but following emoticon' => [
 109                  'input' => '<pre><pre>(n)</pre>(n)</pre>(n)',
 110                  'format' => FORMAT_HTML,
 111                  'expected' => '<pre><pre>(n)</pre>(n)</pre>' . $this->get_converted_content_for_emoticon('(n)'),
 112              ],
 113          ];
 114      }
 115  
 116      /**
 117       * Translate the text for a single emoticon into the rendered value.
 118       *
 119       * @param   string  $text The text to translate.
 120       * @return  string
 121       */
 122      public function get_converted_content_for_emoticon($text) {
 123          global $OUTPUT;
 124          $manager = get_emoticon_manager();
 125          $emoticons = $manager->get_emoticons();
 126          foreach ($emoticons as $emoticon) {
 127              if ($emoticon->text == $text) {
 128                  return $OUTPUT->render($manager->prepare_renderable_emoticon($emoticon));
 129              }
 130          }
 131  
 132          return $text;
 133      }
 134  
 135      /**
 136       * Tests the filter doesn't break anything if activated but invalid format passed.
 137       *
 138       */
 139      public function test_filter_invalidformat() {
 140          global $PAGE;
 141          $this->resetAfterTest();
 142  
 143          $filter = new testable_filter_emoticon();
 144          $input = '(grr)';
 145          $expected = '(grr)';
 146  
 147          $this->assertEquals($expected, $filter->filter($input, [
 148              'originalformat' => 'ILLEGALFORMAT',
 149          ]));
 150      }
 151  
 152      /**
 153       * Tests the filter doesn't break anything if activated but no emoticons available.
 154       *
 155       */
 156      public function test_filter_emptyemoticons() {
 157          global $CFG;
 158          $this->resetAfterTest();
 159          // Empty the emoticons array.
 160          $CFG->emoticons = null;
 161  
 162          $filter = new filter_emoticon(context_system::instance(), array('originalformat' => FORMAT_HTML));
 163  
 164          $input = '(grr)';
 165          $expected = '(grr)';
 166  
 167          $this->assertEquals($expected, $filter->filter($input, [
 168              'originalformat' => FORMAT_HTML,
 169          ]));
 170      }
 171  }
 172  
 173  /**
 174   * Subclass for easier testing.
 175   */
 176  class testable_filter_emoticon extends filter_emoticon {
 177      public function __construct() {
 178          // Reset static emoticon caches.
 179          parent::$emoticontexts = array();
 180          parent::$emoticonimgs = array();
 181          // Use this context for filtering.
 182          $this->context = context_system::instance();
 183          // Define FORMAT_HTML as only one filtering in DB.
 184          set_config('formats', implode(',', array(FORMAT_HTML)), 'filter_emoticon');
 185      }
 186  }