You are here

protected function MediaStandardProfileTest::remoteVideoTest in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/media/tests/src/FunctionalJavascript/MediaStandardProfileTest.php \Drupal\Tests\media\FunctionalJavascript\MediaStandardProfileTest::remoteVideoTest()

Test the standard profile configuration for media type 'remote_video'.

1 call to MediaStandardProfileTest::remoteVideoTest()
MediaStandardProfileTest::testMediaSources in core/modules/media/tests/src/FunctionalJavascript/MediaStandardProfileTest.php
Tests all media sources in one method.

File

core/modules/media/tests/src/FunctionalJavascript/MediaStandardProfileTest.php, line 371

Class

MediaStandardProfileTest
Basic tests for Media configuration in the standard profile.

Namespace

Drupal\Tests\media\FunctionalJavascript

Code

protected function remoteVideoTest() {
  $assert_session = $this
    ->assertSession();
  $page = $this
    ->getSession()
    ->getPage();
  $source_field_id = 'field_media_oembed_video';

  // Set video fixtures.
  $video_title = 'Drupal Rap Video - Schipulcon09';
  $video_url = 'https://vimeo.com/7073899';
  ResourceController::setResourceUrl($video_url, $this
    ->getFixturesDirectory() . '/video_vimeo.json');
  $video_title_updated = "Everyday I'm Drupalin' Drupal Rap (Rick Ross - Hustlin)";
  $video_url_updated = 'https://www.youtube.com/watch?v=PWjcqE3QKBg';
  ResourceController::setResourceUrl($video_url_updated, $this
    ->getFixturesDirectory() . '/video_youtube.json');

  // Check if the name field is properly hidden on the media form.
  $this
    ->drupalGet('media/add/remote_video');
  $assert_session
    ->fieldNotExists('name');

  // Check if the source field is available.
  $assert_session
    ->fieldExists("{$source_field_id}[0][value]");

  // Create a media item.
  $page
    ->fillField("{$source_field_id}[0][value]", $video_url);
  $page
    ->pressButton('Save');
  $remote_video_media_id = $this->container
    ->get('entity_type.manager')
    ->getStorage('media')
    ->getQuery()
    ->sort('mid', 'DESC')
    ->execute();
  $remote_video_media_id = reset($remote_video_media_id);

  // Reference the created media using an entity_reference field and make sure
  // the output is what we expect.
  $node = Node::create([
    'title' => 'Host node',
    'type' => 'article',
    'field_related_media' => [
      'target_id' => $remote_video_media_id,
    ],
  ]);
  $node
    ->save();
  $this
    ->drupalGet('/node/' . $node
    ->id());

  // Check if the default media name is generated as expected.
  $media = \Drupal::entityTypeManager()
    ->getStorage('media')
    ->loadUnchanged($remote_video_media_id);
  $this
    ->assertSame($video_title, $media
    ->label());

  // Here we expect to see only the video iframe. Assert only one element in
  // the content region.
  $assert_session
    ->elementsCount('css', 'article.media--type-remote-video > *', 1);

  // Assert the iframe is present in the media element and its src attribute
  // matches the URL and query parameters.
  $iframe_url = $assert_session
    ->elementExists('css', 'article.media--type-remote-video .field--name-field-media-oembed-video iframe')
    ->getAttribute('src');
  $iframe_url = parse_url($iframe_url);
  $this
    ->assertStringEndsWith('/media/oembed', $iframe_url['path']);
  $this
    ->assertNotEmpty($iframe_url['query']);
  $query = [];
  parse_str($iframe_url['query'], $query);
  $this
    ->assertSame($video_url, $query['url']);
  $this
    ->assertNotEmpty($query['hash']);

  // Assert the media name is updated through the field mapping when changing
  // the source field.
  $this
    ->drupalGet('media/' . $remote_video_media_id . '/edit');
  $page
    ->fillField("{$source_field_id}[0][value]", $video_url_updated);
  $page
    ->pressButton('Save');
  $this
    ->drupalGet('/node/' . $node
    ->id());

  // Check if the default media name is updated as expected.
  $media = \Drupal::entityTypeManager()
    ->getStorage('media')
    ->loadUnchanged($remote_video_media_id);
  $this
    ->assertSame($video_title_updated, $media
    ->label());

  // Again we expect to see only the video iframe. Assert only one element in
  // the content region.
  $assert_session
    ->elementsCount('css', 'article.media--type-remote-video > *', 1);

  // Assert the iframe is present in the media element and its src attribute
  // matches the updated URL and query parameters.
  $iframe_url = $assert_session
    ->elementExists('css', 'article.media--type-remote-video .field--name-field-media-oembed-video iframe')
    ->getAttribute('src');
  $iframe_url = parse_url($iframe_url);
  $this
    ->assertStringEndsWith('/media/oembed', $iframe_url['path']);
  $this
    ->assertNotEmpty($iframe_url['query']);
  $query = [];
  parse_str($iframe_url['query'], $query);
  $this
    ->assertSame($video_url_updated, $query['url']);
  $this
    ->assertNotEmpty($query['hash']);
}