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] [Versions 39 and 310]

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