Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402] [Versions 402 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 test for the filter_mediaplugin
  19   *
  20   * @package    filter_mediaplugin
  21   * @category   phpunit
  22   * @copyright  2011 Rossiani Wijaya <rwijaya@moodle.com>
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  namespace filter_mediaplugin;
  27  
  28  use filter_mediaplugin;
  29  
  30  defined('MOODLE_INTERNAL') || die();
  31  
  32  global $CFG;
  33  require_once($CFG->dirroot . '/filter/mediaplugin/filter.php'); // Include the code to test
  34  
  35  
  36  class filter_test extends \advanced_testcase {
  37  
  38      function test_filter_mediaplugin_link() {
  39          $this->resetAfterTest(true);
  40  
  41          // We need to enable the media plugins.
  42          \core\plugininfo\media::set_enabled_plugins('vimeo,youtube,videojs,html5video,html5audio');
  43  
  44          $filterplugin = new filter_mediaplugin(null, array());
  45  
  46          $longurl = '<a href="http://moodle/.mp4">my test file</a>';
  47          $longhref = '';
  48  
  49          do {
  50              $longhref .= 'a';
  51          } while(strlen($longhref) + strlen($longurl) < 4095);
  52  
  53          $longurl = '<a href="http://moodle/' . $longhref . '.mp4">my test file</a>';
  54  
  55          $validtexts = array (
  56              '<a href="http://moodle.org/testfile/test.mp3">test mp3</a>',
  57              '<a href="http://moodle.org/testfile/test.ogg">test ogg</a>',
  58              '<a id="movie player" class="center" href="http://moodle.org/testfile/test.mp4">test mp4</a>',
  59              '<a href="http://moodle.org/testfile/test.webm">test</a>',
  60              '<a href="http://www.youtube.com/watch?v=JghQgA2HMX8" class="href=css">test file</a>',
  61              '<a href="http://www.youtube-nocookie.com/watch?v=JghQgA2HMX8" class="href=css">test file</a>',
  62              '<a href="http://youtu.be/JghQgA2HMX8" class="href=css">test file</a>',
  63              '<a href="http://y2u.be/JghQgA2HMX8" class="href=css">test file</a>',
  64              '<a class="youtube" href="http://www.youtube.com/watch?v=JghQgA2HMX8">test file</a>',
  65              '<a class="hrefcss" href="http://www.youtube.com/watch?v=JghQgA2HMX8">test file</a>',
  66              '<a  class="content"     href="http://moodle.org/testfile/test.ogg">test ogg</a>',
  67              '<a     id="audio"      href="http://moodle.org/testfile/test.mp3">test mp3</a>',
  68              '<a  href="http://moodle.org/testfile/test.mp3">test mp3</a>',
  69              '<a     href="http://moodle.org/testfile/test.mp3">test mp3</a>',
  70              '<a     href="http://www.youtube.com/watch?v=JghQgA2HMX8?d=200x200">youtube\'s</a>',
  71              '<a
  72                              href="http://moodle.org/testfile/test.mp3">
  73                              test mp3</a>',
  74              '<a                         class="content"
  75  
  76  
  77                              href="http://moodle.org/testfile/test.wav">test wav
  78                                      </a>',
  79              '<a             href="http://www.youtube.com/watch?v=JghQgA2HMX8?d=200x200"     >youtube\'s</a>',
  80              // Test a long URL under 4096 characters.
  81              $longurl
  82          );
  83  
  84          //test for valid link
  85          foreach ($validtexts as $text) {
  86              $msg = "Testing text: ". $text;
  87              $filter = $filterplugin->filter($text);
  88              $this->assertNotEquals($text, $filter, $msg);
  89          }
  90  
  91          $insertpoint = strrpos($longurl, 'http://');
  92          $longurl = substr_replace($longurl, 'http://pushover4096chars', $insertpoint, 0);
  93  
  94          $originalurl = '<p>Some text.</p><pre style="color: rgb(0, 0, 0); line-height: normal;">' .
  95              '<a href="https://www.youtube.com/watch?v=uUhWl9Lm3OM">Valid link</a></pre><pre style="color: rgb(0, 0, 0); line-height: normal;">';
  96          $paddedurl = str_pad($originalurl, 6000, 'z');
  97          $validpaddedurl = '<p>Some text.</p><pre style="color: rgb(0, 0, 0); line-height: normal;"><span class="mediaplugin mediaplugin_youtube">
  98  <iframe title="Valid link" width="640" height="360"
  99    src="https://www.youtube.com/embed/uUhWl9Lm3OM?rel=0&amp;wmode=transparent" frameborder="0" allowfullscreen="1"></iframe>
 100  </span></pre><pre style="color: rgb(0, 0, 0); line-height: normal;">';
 101          $validpaddedurl = str_pad($validpaddedurl, 6000 + (strlen($validpaddedurl) - strlen($originalurl)), 'z');
 102  
 103          $invalidtexts = array(
 104              '<a class="_blanktarget">href="http://moodle.org/testfile/test.mp3"</a>',
 105              '<a>test test</a>',
 106              '<a >test test</a>',
 107              '<a     >test test</a>',
 108              '<a >test test</a>',
 109              '<ahref="http://moodle.org/testfile/test.mp3">sample</a>',
 110              '<a href="" test></a>',
 111              '<a href="http://www.moodle.com/path/to?#param=29">test</a>',
 112              '<a href="http://moodle.org/testfile/test.mp3">test mp3',
 113              '<a href="http://moodle.org/testfile/test.mp3"test</a>',
 114              '<a href="http://moodle.org/testfile/">test</a>',
 115              '<href="http://moodle.org/testfile/test.avi">test</a>',
 116              '<abbr href="http://moodle.org/testfile/test.mp3">test mp3</abbr>',
 117              '<ahref="http://moodle.org/testfile/test.mp3">test mp3</a>',
 118              '<aclass="content" href="http://moodle.org/testfile/test.mp3">test mp3</a>',
 119              // Test a long URL over 4096 characters.
 120              $longurl
 121          );
 122  
 123          //test for invalid link
 124          foreach ($invalidtexts as $text) {
 125              $msg = "Testing text: ". $text;
 126              $filter = $filterplugin->filter($text);
 127              $this->assertEquals($text, $filter, $msg);
 128          }
 129  
 130          // Valid mediaurl followed by a longurl.
 131          $precededlongurl = '<a href="http://moodle.org/testfile/test.mp3">test.mp3</a>'. $longurl;
 132          $filter = $filterplugin->filter($precededlongurl);
 133          $this->assertEquals(1, substr_count($filter, '</audio>'));
 134          $this->assertStringContainsString($longurl, $filter);
 135  
 136          // Testing for cases where: to be filtered content has 6+ text afterwards.
 137          $filter = $filterplugin->filter($paddedurl);
 138          $this->assertEquals($validpaddedurl, $filter, $msg);
 139      }
 140  }