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 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   * Test classes for handling embedded media.
  19   *
  20   * @package media_swf
  21   * @copyright 2016 Marina Glancy
  22   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  /**
  28   * Test script for media embedding.
  29   *
  30   * @package media_swf
  31   * @copyright 2016 Marina Glancy
  32   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   */
  34  class media_swf_testcase extends advanced_testcase {
  35  
  36      /**
  37       * Pre-test setup. Preserves $CFG.
  38       */
  39      public function setUp(): void {
  40          global $CFG;
  41          parent::setUp();
  42  
  43          // Reset $CFG and $SERVER.
  44          $this->resetAfterTest();
  45  
  46          // We need trusttext for embedding swf.
  47          $CFG->enabletrusttext = true;
  48  
  49          // Consistent initial setup: all players disabled.
  50          \core\plugininfo\media::set_enabled_plugins('swf');
  51  
  52          // Pretend to be using Firefox browser (must support ogg for tests to work).
  53          core_useragent::instance(true, 'Mozilla/5.0 (X11; Linux x86_64; rv:46.0) Gecko/20100101 Firefox/46.0 ');
  54      }
  55  
  56  
  57      /**
  58       * Test that plugin is returned as enabled media plugin.
  59       */
  60      public function test_is_installed() {
  61          $sortorder = \core\plugininfo\media::get_enabled_plugins();
  62          $this->assertEquals(['swf' => 'swf'], $sortorder);
  63      }
  64  
  65      /**
  66       * Test embedding without media filter (for example for displaying file resorce).
  67       */
  68      public function test_embed_url() {
  69          global $CFG;
  70  
  71          $url = new moodle_url('http://example.org/1.swf');
  72  
  73          $manager = core_media_manager::instance();
  74          $embedoptions = array(
  75              core_media_manager::OPTION_TRUSTED => true,
  76              core_media_manager::OPTION_BLOCK => true,
  77          );
  78  
  79          $this->assertTrue($manager->can_embed_url($url, $embedoptions));
  80          $content = $manager->embed_url($url, 'Test & file', 0, 0, $embedoptions);
  81  
  82          $this->assertRegExp('~mediaplugin_swf~', $content);
  83          $this->assertRegExp('~</object>~', $content);
  84          $this->assertRegExp('~width="' . $CFG->media_default_width . '" height="' .
  85              $CFG->media_default_height . '"~', $content);
  86  
  87          // Repeat sending the specific size to the manager.
  88          $content = $manager->embed_url($url, 'New file', 123, 50, $embedoptions);
  89          $this->assertRegExp('~width="123" height="50"~', $content);
  90  
  91          // Not working without trust!
  92          $embedoptions = array(
  93              core_media_manager::OPTION_BLOCK => true,
  94          );
  95          $this->assertFalse($manager->can_embed_url($url, $embedoptions));
  96          $content = $manager->embed_url($url, 'Test & file', 0, 0, $embedoptions);
  97          $this->assertNotRegExp('~mediaplugin_swf~', $content);
  98      }
  99  
 100      /**
 101       * Test that mediaplugin filter replaces a link to the supported file with media tag.
 102       *
 103       * filter_mediaplugin is enabled by default.
 104       */
 105      public function test_embed_link() {
 106          global $CFG;
 107          $url = new moodle_url('http://example.org/some_filename.swf');
 108          $text = html_writer::link($url, 'Watch this one');
 109          $content = format_text($text, FORMAT_HTML, ['trusted' => true]);
 110  
 111          $this->assertRegExp('~mediaplugin_swf~', $content);
 112          $this->assertRegExp('~</object>~', $content);
 113          $this->assertRegExp('~width="' . $CFG->media_default_width . '" height="' .
 114              $CFG->media_default_height . '"~', $content);
 115  
 116          // Not working without trust!
 117          $content = format_text($text, FORMAT_HTML);
 118          $this->assertNotRegExp('~mediaplugin_swf~', $content);
 119      }
 120  
 121      /**
 122       * Test that mediaplugin filter adds player code on top of <video> tags.
 123       *
 124       * filter_mediaplugin is enabled by default.
 125       */
 126      public function test_embed_media() {
 127          global $CFG;
 128          $url = new moodle_url('http://example.org/some_filename.swf');
 129          $trackurl = new moodle_url('http://example.org/some_filename.vtt');
 130          $text = '<video controls="true"><source src="'.$url.'"/>' .
 131              '<track src="'.$trackurl.'">Unsupported text</video>';
 132          $content = format_text($text, FORMAT_HTML, ['trusted' => true]);
 133  
 134          $this->assertRegExp('~mediaplugin_swf~', $content);
 135          $this->assertRegExp('~</object>~', $content);
 136          $this->assertRegExp('~width="' . $CFG->media_default_width . '" height="' .
 137              $CFG->media_default_height . '"~', $content);
 138          // Video tag, unsupported text and tracks are removed.
 139          $this->assertNotRegExp('~</video>~', $content);
 140          $this->assertNotRegExp('~<source\b~', $content);
 141          $this->assertNotRegExp('~Unsupported text~', $content);
 142          $this->assertNotRegExp('~<track\b~i', $content);
 143  
 144          // Video with dimensions and source specified as src attribute without <source> tag.
 145          $text = '<video controls="true" width="123" height="35" src="'.$url.'">Unsupported text</video>';
 146          $content = format_text($text, FORMAT_HTML, ['trusted' => true]);
 147          $this->assertRegExp('~mediaplugin_swf~', $content);
 148          $this->assertRegExp('~</object>~', $content);
 149          $this->assertRegExp('~width="123" height="35"~', $content);
 150      }
 151  }