Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 310 and 311] [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403] [Versions 39 and 311]

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