[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/lib/tests/ -> medialib_test.php (source)

   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 (audio/video).
  19   *
  20   * @package core_media
  21   * @category phpunit
  22   * @copyright 2012 The Open University
  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->libdir . '/medialib.php');
  30  
  31  /**
  32   * Test script for media embedding.
  33   */
  34  class core_medialib_testcase extends advanced_testcase {
  35  
  36      /** @var array Files covered by test */
  37      public static $includecoverage = array('lib/medialib.php', 'lib/outputrenderers.php');
  38  
  39      /**
  40       * Pre-test setup. Preserves $CFG.
  41       */
  42      public function setUp() {
  43          global $CFG;
  44          parent::setUp();
  45  
  46          // Reset $CFG and $SERVER.
  47          $this->resetAfterTest();
  48  
  49          // Consistent initial setup: all players disabled.
  50          $CFG->core_media_enable_html5video = false;
  51          $CFG->core_media_enable_html5audio = false;
  52          $CFG->core_media_enable_mp3 = false;
  53          $CFG->core_media_enable_flv = false;
  54          $CFG->core_media_enable_wmp = false;
  55          $CFG->core_media_enable_qt = false;
  56          $CFG->core_media_enable_rm = false;
  57          $CFG->core_media_enable_youtube = false;
  58          $CFG->core_media_enable_vimeo = false;
  59          $CFG->core_media_enable_swf = false;
  60  
  61          $_SERVER = array('HTTP_USER_AGENT' => '');
  62          $this->pretend_to_be_safari();
  63      }
  64  
  65      /**
  66       * Sets user agent to Safari.
  67       */
  68      private function pretend_to_be_safari() {
  69          // Pretend to be using Safari browser (must support mp4 for tests to work).
  70          core_useragent::instance(true, 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_8; de-at) ' .
  71                  'AppleWebKit/533.21.1 (KHTML, like Gecko) Version/5.0.5 Safari/533.21.1');
  72      }
  73  
  74      /**
  75       * Sets user agent to Firefox.
  76       */
  77      private function pretend_to_be_firefox() {
  78          // Pretend to be using Firefox browser (must support ogg for tests to work).
  79          core_useragent::instance(true, 'Mozilla/5.0 (Windows NT 6.1; rv:8.0) Gecko/20100101 Firefox/8.0');
  80      }
  81  
  82      /**
  83       * Test for the core_media_player is_enabled.
  84       */
  85      public function test_is_enabled() {
  86          global $CFG;
  87  
  88          // Test enabled: unset, 0, 1.
  89          $test = new core_media_player_test;
  90          $this->assertFalse($test->is_enabled());
  91          $CFG->core_media_enable_test = 0;
  92          $this->assertFalse($test->is_enabled());
  93          $CFG->core_media_enable_test = 1;
  94          $this->assertTrue($test->is_enabled());
  95      }
  96  
  97      /**
  98       * Test for core_media::get_filename.
  99       */
 100      public function test_get_filename() {
 101          $this->assertSame('frog.mp4', core_media::get_filename(new moodle_url(
 102                  '/pluginfile.php/312/mod_page/content/7/frog.mp4')));
 103          // This should work even though slasharguments is true, because we want
 104          // it to support 'legacy' links if somebody toggles the option later.
 105          $this->assertSame('frog.mp4', core_media::get_filename(new moodle_url(
 106                  '/pluginfile.php?file=/312/mod_page/content/7/frog.mp4')));
 107      }
 108  
 109      /**
 110       * Test for core_media::get_extension.
 111       */
 112      public function test_get_extension() {
 113          $this->assertSame('mp4', core_media::get_extension(new moodle_url(
 114                  '/pluginfile.php/312/mod_page/content/7/frog.mp4')));
 115          $this->assertSame('', core_media::get_extension(new moodle_url(
 116                  '/pluginfile.php/312/mod_page/content/7/frog')));
 117          $this->assertSame('mp4', core_media::get_extension(new moodle_url(
 118                  '/pluginfile.php?file=/312/mod_page/content/7/frog.mp4')));
 119          $this->assertSame('', core_media::get_extension(new moodle_url(
 120                  '/pluginfile.php?file=/312/mod_page/content/7/frog')));
 121      }
 122  
 123      /**
 124       * Test for the core_media_player list_supported_urls.
 125       */
 126      public function test_list_supported_urls() {
 127          global $CFG;
 128          $test = new core_media_player_test;
 129  
 130          // Some example URLs.
 131          $supported1 = new moodle_url('http://example.org/1.test');
 132          $supported2 = new moodle_url('http://example.org/2.TST');
 133          $unsupported = new moodle_url('http://example.org/2.jpg');
 134  
 135          // No URLs => none.
 136          $result = $test->list_supported_urls(array());
 137          $this->assertEquals(array(), $result);
 138  
 139          // One supported URL => same.
 140          $result = $test->list_supported_urls(array($supported1));
 141          $this->assertEquals(array($supported1), $result);
 142  
 143          // Two supported URLS => same.
 144          $result = $test->list_supported_urls(array($supported1, $supported2));
 145          $this->assertEquals(array($supported1, $supported2), $result);
 146  
 147          // One unsupported => none.
 148          $result = $test->list_supported_urls(array($unsupported));
 149          $this->assertEquals(array(), $result);
 150  
 151          // Two supported and one unsupported => same.
 152          $result = $test->list_supported_urls(array($supported2, $unsupported, $supported1));
 153          $this->assertEquals(array($supported2, $supported1), $result);
 154      }
 155  
 156      /**
 157       * Test for core_media_renderer get_players
 158       */
 159      public function test_get_players() {
 160          global $CFG, $PAGE;
 161  
 162          // All players are initially disabled (except link, which you can't).
 163          $renderer = new core_media_renderer_test($PAGE, '');
 164          $this->assertSame('link', $renderer->get_players_test());
 165  
 166          // A couple enabled, check the order.
 167          $CFG->core_media_enable_html5audio = true;
 168          $CFG->core_media_enable_mp3 = true;
 169          $renderer = new core_media_renderer_test($PAGE, '');
 170          $this->assertSame('mp3, html5audio, link', $renderer->get_players_test());
 171  
 172          // Test QT and HTML5 media order.
 173          $CFG->core_media_enable_mp3 = false;
 174          $CFG->core_media_enable_html5video = true;
 175          $CFG->core_media_enable_qt = true;
 176          $renderer = new core_media_renderer_test($PAGE, '');
 177          $this->assertSame('html5video, html5audio, qt, link', $renderer->get_players_test());
 178      }
 179  
 180      /**
 181       * Test for core_media_renderer can_embed_url
 182       */
 183      public function test_can_embed_url() {
 184          global $CFG, $PAGE;
 185  
 186          // All players are initially disabled, so mp4 cannot be rendered.
 187          $url = new moodle_url('http://example.org/test.mp4');
 188          $renderer = new core_media_renderer_test($PAGE, '');
 189          $this->assertFalse($renderer->can_embed_url($url));
 190  
 191          // Enable QT player.
 192          $CFG->core_media_enable_qt = true;
 193          $renderer = new core_media_renderer_test($PAGE, '');
 194          $this->assertTrue($renderer->can_embed_url($url));
 195  
 196          // QT + html5.
 197          $CFG->core_media_enable_html5video = true;
 198          $renderer = new core_media_renderer_test($PAGE, '');
 199          $this->assertTrue($renderer->can_embed_url($url));
 200  
 201          // Only html5.
 202          $CFG->core_media_enable_qt = false;
 203          $renderer = new core_media_renderer_test($PAGE, '');
 204          $this->assertTrue($renderer->can_embed_url($url));
 205  
 206          // Only WMP.
 207          $CFG->core_media_enable_html5video = false;
 208          $CFG->core_media_enable_wmp = true;
 209          $renderer = new core_media_renderer_test($PAGE, '');
 210          $this->assertFalse($renderer->can_embed_url($url));
 211      }
 212  
 213      /**
 214       * Test for core_media_renderer embed_url.
 215       * Checks multiple format/fallback support.
 216       */
 217      public function test_embed_url_fallbacks() {
 218          global $CFG, $PAGE;
 219  
 220          // Key strings in the embed code that identify with the media formats being tested.
 221          $qt = 'qtplugin.cab';
 222          $html5video = '</video>';
 223          $html5audio = '</audio>';
 224          $link = 'mediafallbacklink';
 225          $mp3 = 'mediaplugin_mp3';
 226  
 227          $url = new moodle_url('http://example.org/test.mp4');
 228  
 229          // All plugins disabled, NOLINK option.
 230          $renderer = new core_media_renderer_test($PAGE, '');
 231          $t = $renderer->embed_url($url, 0, 0, '',
 232                  array(core_media::OPTION_NO_LINK => true));
 233          // Completely empty.
 234          $this->assertSame('', $t);
 235  
 236          // All plugins disabled but not NOLINK.
 237          $renderer = new core_media_renderer_test($PAGE, '');
 238          $t = $renderer->embed_url($url);
 239          $this->assertContains($link, $t);
 240  
 241          // Enable media players that can play the same media formats. (ie. qt & html5video for mp4 files, etc.)
 242          $CFG->core_media_enable_html5video = true;
 243          $CFG->core_media_enable_html5audio = true;
 244          $CFG->core_media_enable_mp3 = true;
 245          $CFG->core_media_enable_qt = true;
 246  
 247          // Test media formats that can be played by 2 or more players.
 248          $mediaformats = array('mp3', 'm4a', 'mp4', 'm4v');
 249  
 250          foreach ($mediaformats as $format) {
 251              $url = new moodle_url('http://example.org/test.' . $format);
 252              $renderer = new core_media_renderer_test($PAGE, '');
 253              $textwithlink = $renderer->embed_url($url);
 254              $textwithoutlink = $renderer->embed_url($url, 0, 0, '', array(core_media::OPTION_NO_LINK => true));
 255  
 256              switch ($format) {
 257                  case 'mp3':
 258                      $this->assertContains($mp3, $textwithlink);
 259                      $this->assertContains($html5audio, $textwithlink);
 260                      $this->assertContains($link, $textwithlink);
 261  
 262                      $this->assertContains($mp3, $textwithoutlink);
 263                      $this->assertContains($html5audio, $textwithoutlink);
 264                      $this->assertNotContains($link, $textwithoutlink);
 265                      break;
 266  
 267                  case 'm4a':
 268                      $this->assertContains($qt, $textwithlink);
 269                      $this->assertContains($html5audio, $textwithlink);
 270                      $this->assertContains($link, $textwithlink);
 271  
 272                      $this->assertContains($qt, $textwithoutlink);
 273                      $this->assertContains($html5audio, $textwithoutlink);
 274                      $this->assertNotContains($link, $textwithoutlink);
 275                      break;
 276  
 277                  case 'mp4':
 278                  case 'm4v':
 279                      $this->assertContains($qt, $textwithlink);
 280                      $this->assertContains($html5video, $textwithlink);
 281                      $this->assertContains($link, $textwithlink);
 282  
 283                      $this->assertContains($qt, $textwithoutlink);
 284                      $this->assertContains($html5video, $textwithoutlink);
 285                      $this->assertNotContains($link, $textwithoutlink);
 286                      break;
 287  
 288                  default:
 289                      break;
 290              }
 291          }
 292      }
 293  
 294      /**
 295       * Test for core_media_renderer embed_url.
 296       * Check SWF works including the special option required to enable it
 297       */
 298      public function test_embed_url_swf() {
 299          global $CFG, $PAGE;
 300          $CFG->core_media_enable_swf = true;
 301          $renderer = new core_media_renderer_test($PAGE, '');
 302  
 303          // Without any options...
 304          $url = new moodle_url('http://example.org/test.swf');
 305          $t = $renderer->embed_url($url);
 306          $this->assertNotContains('</object>', $t);
 307  
 308          // ...and with the 'no it's safe, I checked it' option.
 309          $url = new moodle_url('http://example.org/test.swf');
 310          $t = $renderer->embed_url($url, '', 0, 0, array(core_media::OPTION_TRUSTED => true));
 311          $this->assertContains('</object>', $t);
 312      }
 313  
 314      /**
 315       * Test for core_media_renderer embed_url.
 316       * Exercises all the basic formats not covered elsewhere.
 317       */
 318      public function test_embed_url_other_formats() {
 319          global $CFG, $PAGE;
 320  
 321          // Enable all players and get renderer.
 322          $CFG->core_media_enable_html5audio = true;
 323          $CFG->core_media_enable_mp3 = true;
 324          $CFG->core_media_enable_flv = true;
 325          $CFG->core_media_enable_wmp = true;
 326          $CFG->core_media_enable_rm = true;
 327          $CFG->core_media_enable_youtube = true;
 328          $CFG->core_media_enable_vimeo = true;
 329          $renderer = new core_media_renderer_test($PAGE, '');
 330  
 331          // Check each format one at a time. This is a basic check to be sure
 332          // the HTML is included for files of the right type, not a test that
 333          // the HTML itself is correct.
 334  
 335          // Format: mp3.
 336          $url = new moodle_url('http://example.org/test.mp3');
 337          $t = $renderer->embed_url($url);
 338          $this->assertContains('core_media_mp3_', $t);
 339  
 340          // Format: flv.
 341          $url = new moodle_url('http://example.org/test.flv');
 342          $t = $renderer->embed_url($url);
 343          $this->assertContains('core_media_flv_', $t);
 344  
 345          // Format: wmp.
 346          $url = new moodle_url('http://example.org/test.avi');
 347          $t = $renderer->embed_url($url);
 348          $this->assertContains('6BF52A52-394A-11d3-B153-00C04F79FAA6', $t);
 349  
 350          // Format: rm.
 351          $url = new moodle_url('http://example.org/test.rm');
 352          $t = $renderer->embed_url($url);
 353          $this->assertContains('CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA', $t);
 354  
 355          // Format: youtube.
 356          $url = new moodle_url('http://www.youtube.com/watch?v=vyrwMmsufJc');
 357          $t = $renderer->embed_url($url);
 358          $this->assertContains('</iframe>', $t);
 359          $url = new moodle_url('http://www.youtube.com/v/vyrwMmsufJc');
 360          $t = $renderer->embed_url($url);
 361          $this->assertContains('</iframe>', $t);
 362  
 363          // Format: youtube video within playlist.
 364          $url = new moodle_url('https://www.youtube.com/watch?v=dv2f_xfmbD8&index=4&list=PLxcO_MFWQBDcyn9xpbmx601YSDlDcTcr0');
 365          $t = $renderer->embed_url($url);
 366          $this->assertContains('</iframe>', $t);
 367          $this->assertContains('list=PLxcO_MFWQBDcyn9xpbmx601YSDlDcTcr0', $t);
 368  
 369          // Format: youtube video with start time.
 370          $url = new moodle_url('https://www.youtube.com/watch?v=JNJMF1l3udM&t=1h11s');
 371          $t = $renderer->embed_url($url);
 372          $this->assertContains('</iframe>', $t);
 373          $this->assertContains('start=3611', $t);
 374  
 375          // Format: youtube video within playlist with start time.
 376          $url = new moodle_url('https://www.youtube.com/watch?v=dv2f_xfmbD8&index=4&list=PLxcO_MFWQBDcyn9xpbmx601YSDlDcTcr0&t=1m5s');
 377          $t = $renderer->embed_url($url);
 378          $this->assertContains('</iframe>', $t);
 379          $this->assertContains('list=PLxcO_MFWQBDcyn9xpbmx601YSDlDcTcr0', $t);
 380          $this->assertContains('start=65', $t);
 381  
 382          // Format: youtube video with invalid parameter values (injection attempts).
 383          $url = new moodle_url('https://www.youtube.com/watch?v=dv2f_xfmbD8&index=4&list=PLxcO_">');
 384          $t = $renderer->embed_url($url);
 385          $this->assertContains('</iframe>', $t);
 386          $this->assertNotContains('list=PLxcO_', $t); // We shouldn't get a list param as input was invalid.
 387          $url = new moodle_url('https://www.youtube.com/watch?v=JNJMF1l3udM&t=">');
 388          $t = $renderer->embed_url($url);
 389          $this->assertContains('</iframe>', $t);
 390          $this->assertNotContains('start=', $t); // We shouldn't get a start param as input was invalid.
 391  
 392          // Format: youtube playlist.
 393          $url = new moodle_url('http://www.youtube.com/view_play_list?p=PL6E18E2927047B662');
 394          $t = $renderer->embed_url($url);
 395          $this->assertContains('</iframe>', $t);
 396          $url = new moodle_url('http://www.youtube.com/playlist?list=PL6E18E2927047B662');
 397          $t = $renderer->embed_url($url);
 398          $this->assertContains('</iframe>', $t);
 399          $url = new moodle_url('http://www.youtube.com/p/PL6E18E2927047B662');
 400          $t = $renderer->embed_url($url);
 401          $this->assertContains('</iframe>', $t);
 402  
 403          // Format: vimeo.
 404          $url = new moodle_url('http://vimeo.com/1176321');
 405          $t = $renderer->embed_url($url);
 406          $this->assertContains('</iframe>', $t);
 407  
 408          // Format: html5audio.
 409          $this->pretend_to_be_firefox();
 410          $url = new moodle_url('http://example.org/test.ogg');
 411          $t = $renderer->embed_url($url);
 412          $this->assertContains('</audio>', $t);
 413      }
 414  
 415      /**
 416       * Same as test_embed_url MP3 test, but for slash arguments.
 417       */
 418      public function test_slash_arguments() {
 419          global $CFG, $PAGE;
 420  
 421          // Again we do not turn slasharguments actually on, because it has to
 422          // work regardless of the setting of that variable in case of handling
 423          // links created using previous setting.
 424  
 425          // Enable MP3 and get renderer.
 426          $CFG->core_media_enable_mp3 = true;
 427          $renderer = new core_media_renderer_test($PAGE, '');
 428  
 429          // Format: mp3.
 430          $url = new moodle_url('http://example.org/pluginfile.php?file=x/y/z/test.mp3');
 431          $t = $renderer->embed_url($url);
 432          $this->assertContains('core_media_mp3_', $t);
 433      }
 434  
 435      /**
 436       * Test for core_media_renderer embed_url.
 437       * Checks the EMBED_OR_BLANK option.
 438       */
 439      public function test_embed_or_blank() {
 440          global $CFG, $PAGE;
 441          $CFG->core_media_enable_html5audio = true;
 442          $this->pretend_to_be_firefox();
 443  
 444          $renderer = new core_media_renderer_test($PAGE, '');
 445  
 446          $options = array(core_media::OPTION_FALLBACK_TO_BLANK => true);
 447  
 448          // Embed that does match something should still include the link too.
 449          $url = new moodle_url('http://example.org/test.ogg');
 450          $t = $renderer->embed_url($url, '', 0, 0, $options);
 451          $this->assertContains('</audio>', $t);
 452          $this->assertContains('mediafallbacklink', $t);
 453  
 454          // Embed that doesn't match something should be totally blank.
 455          $url = new moodle_url('http://example.org/test.mp4');
 456          $t = $renderer->embed_url($url, '', 0, 0, $options);
 457          $this->assertSame('', $t);
 458      }
 459  
 460      /**
 461       * Test for core_media_renderer embed_url.
 462       * Checks that size is passed through correctly to player objects and tests
 463       * size support in html5video output.
 464       */
 465      public function test_embed_url_size() {
 466          global $CFG, $PAGE;
 467  
 468          // Technically this could break in every format and they handle size
 469          // in several different ways, but I'm too lazy to test it in every
 470          // format, so let's just pick one to check the values get passed
 471          // through.
 472          $CFG->core_media_enable_html5video = true;
 473          $renderer = new core_media_renderer_test($PAGE, '');
 474          $url = new moodle_url('http://example.org/test.mp4');
 475  
 476          // HTML5 default size - specifies core width and does not specify height.
 477          $t = $renderer->embed_url($url);
 478          $this->assertContains('width="' . CORE_MEDIA_VIDEO_WIDTH . '"', $t);
 479          $this->assertNotContains('height', $t);
 480  
 481          // HTML5 specified size - specifies both.
 482          $t = $renderer->embed_url($url, '', '666', '101');
 483          $this->assertContains('width="666"', $t);
 484          $this->assertContains('height="101"', $t);
 485  
 486          // HTML5 size specified in url, overrides call.
 487          $url = new moodle_url('http://example.org/test.mp4?d=123x456');
 488          $t = $renderer->embed_url($url, '', '666', '101');
 489          $this->assertContains('width="123"', $t);
 490          $this->assertContains('height="456"', $t);
 491      }
 492  
 493      /**
 494       * Test for core_media_renderer embed_url.
 495       * Checks that name is passed through correctly to player objects and tests
 496       * name support in html5video output.
 497       */
 498      public function test_embed_url_name() {
 499          global $CFG, $PAGE;
 500  
 501          // As for size this could break in every format but I'm only testing
 502          // html5video.
 503          $CFG->core_media_enable_html5video = true;
 504          $renderer = new core_media_renderer_test($PAGE, '');
 505          $url = new moodle_url('http://example.org/test.mp4');
 506  
 507          // HTML5 default name - use filename.
 508          $t = $renderer->embed_url($url);
 509          $this->assertContains('title="test.mp4"', $t);
 510  
 511          // HTML5 specified name - check escaping.
 512          $t = $renderer->embed_url($url, 'frog & toad');
 513          $this->assertContains('title="frog &amp; toad"', $t);
 514      }
 515  
 516      /**
 517       * Test for core_media_renderer split_alternatives.
 518       */
 519      public function test_split_alternatives() {
 520          // Single URL - identical moodle_url.
 521          $mp4 = 'http://example.org/test.mp4';
 522          $result = core_media::split_alternatives($mp4, $w, $h);
 523          $this->assertEquals($mp4, $result[0]->out(false));
 524  
 525          // Width and height weren't specified.
 526          $this->assertEquals(0, $w);
 527          $this->assertEquals(0, $h);
 528  
 529          // Two URLs - identical moodle_urls.
 530          $webm = 'http://example.org/test.webm';
 531          $result = core_media::split_alternatives("$mp4#$webm", $w, $h);
 532          $this->assertEquals($mp4, $result[0]->out(false));
 533          $this->assertEquals($webm, $result[1]->out(false));
 534  
 535          // Two URLs plus dimensions.
 536          $size = 'd=400x280';
 537          $result = core_media::split_alternatives("$mp4#$webm#$size", $w, $h);
 538          $this->assertEquals($mp4, $result[0]->out(false));
 539          $this->assertEquals($webm, $result[1]->out(false));
 540          $this->assertEquals(400, $w);
 541          $this->assertEquals(280, $h);
 542  
 543          // Two URLs plus legacy dimensions (use last one).
 544          $result = core_media::split_alternatives("$mp4?d=1x1#$webm?$size", $w, $h);
 545          $this->assertEquals($mp4, $result[0]->out(false));
 546          $this->assertEquals($webm, $result[1]->out(false));
 547          $this->assertEquals(400, $w);
 548          $this->assertEquals(280, $h);
 549      }
 550  
 551      /**
 552       * Test for core_media_renderer embed_alternatives (with multiple urls)
 553       */
 554      public function test_embed_alternatives() {
 555          global $PAGE, $CFG;
 556  
 557          // Most aspects of this are same as single player so let's just try
 558          // a single typical / complicated scenario.
 559  
 560          // MP3, WebM and FLV.
 561          $urls = array(
 562              new moodle_url('http://example.org/test.mp4'),
 563              new moodle_url('http://example.org/test.webm'),
 564              new moodle_url('http://example.org/test.flv'),
 565          );
 566  
 567          // Enable html5 and flv.
 568          $CFG->core_media_enable_html5video = true;
 569          $CFG->core_media_enable_flv = true;
 570          $renderer = new core_media_renderer_test($PAGE, '');
 571  
 572          // Result should contain HTML5 with two sources + FLV.
 573          $t = $renderer->embed_alternatives($urls);
 574  
 575          // HTML5 sources - mp4, not flv or webm (not supported in Safari).
 576          $this->assertContains('<source src="http://example.org/test.mp4"', $t);
 577          $this->assertNotContains('<source src="http://example.org/test.webm"', $t);
 578          $this->assertNotContains('<source src="http://example.org/test.flv"', $t);
 579  
 580          // FLV is before the video tag (indicating html5 is used as fallback to flv
 581          // and not vice versa).
 582          $this->assertTrue((bool)preg_match('~core_media_flv_.*<video~s', $t));
 583  
 584          // Do same test with firefox and check we get the webm and not mp4.
 585          $this->pretend_to_be_firefox();
 586          $t = $renderer->embed_alternatives($urls);
 587  
 588          // HTML5 sources - webm, not not flv or mp4 (not supported in Firefox).
 589          $this->assertNotContains('<source src="http://example.org/test.mp4"', $t);
 590          $this->assertContains('<source src="http://example.org/test.webm"', $t);
 591          $this->assertNotContains('<source src="http://example.org/test.flv"', $t);
 592      }
 593  
 594      /**
 595       * Converts moodle_url array into a single comma-separated string for
 596       * easier testing.
 597       *
 598       * @param array $urls Array of moodle_urls
 599       * @return string String containing those URLs, comma-separated
 600       */
 601      public static function string_urls($urls) {
 602          $out = array();
 603          foreach ($urls as $url) {
 604              $out[] = $url->out(false);
 605          }
 606          return implode(',', $out);
 607      }
 608  
 609      /**
 610       * Converts associative array into a semicolon-separated string for easier
 611       * testing.
 612       *
 613       * @param array $options Associative array
 614       * @return string String of form 'a=b;c=d'
 615       */
 616      public static function string_options($options) {
 617          $out = '';
 618          foreach ($options as $key => $value) {
 619              if ($out) {
 620                  $out .= ';';
 621              }
 622              $out .= "$key=$value";
 623          }
 624          return $out;
 625      }
 626  }
 627  
 628  /**
 629   * Media player stub for testing purposes.
 630   */
 631  class core_media_player_test extends core_media_player {
 632      /** @var array Array of supported extensions */
 633      public $ext;
 634      /** @var int Player rank */
 635      public $rank;
 636      /** @var int Arbitrary number */
 637      public $num;
 638  
 639      /**
 640       * @param int $num Number (used in output)
 641       * @param int $rank Player rank
 642       * @param array $ext Array of supported extensions
 643       */
 644      public function __construct($num = 1, $rank = 13, $ext = array('tst', 'test')) {
 645          $this->ext = $ext;
 646          $this->rank = $rank;
 647          $this->num = $num;
 648      }
 649  
 650      public function embed($urls, $name, $width, $height, $options) {
 651          return $this->num . ':' . medialib_test::string_urls($urls) .
 652                  ",$name,$width,$height,<!--FALLBACK-->," . medialib_test::string_options($options);
 653      }
 654  
 655      public function get_supported_extensions() {
 656          return $this->ext;
 657      }
 658  
 659      public function get_rank() {
 660          return $this->rank;
 661      }
 662  }
 663  
 664  /**
 665   * Media renderer override for testing purposes.
 666   */
 667  class core_media_renderer_test extends core_media_renderer {
 668      /**
 669       * Access list of players as string, shortening it by getting rid of
 670       * repeated text.
 671       * @return string Comma-separated list of players
 672       */
 673      public function get_players_test() {
 674          $players = $this->get_players();
 675          $out = '';
 676          foreach ($players as $player) {
 677              if ($out) {
 678                  $out .= ', ';
 679              }
 680              $out .= str_replace('core_media_player_', '', get_class($player));
 681          }
 682          return $out;
 683      }
 684  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1