function MediaStandardProfileTest::videoTest

Same name in other branches
  1. 9 core/modules/media/tests/src/FunctionalJavascript/MediaStandardProfileTest.php \Drupal\Tests\media\FunctionalJavascript\MediaStandardProfileTest::videoTest()
  2. 10 core/modules/media/tests/src/FunctionalJavascript/MediaStandardProfileTest.php \Drupal\Tests\media\FunctionalJavascript\MediaStandardProfileTest::videoTest()
  3. 11.x core/modules/media/tests/src/FunctionalJavascript/MediaStandardProfileTest.php \Drupal\Tests\media\FunctionalJavascript\MediaStandardProfileTest::videoTest()

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

1 call to MediaStandardProfileTest::videoTest()
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 465

Class

MediaStandardProfileTest
Basic tests for Media configuration in the standard profile.

Namespace

Drupal\Tests\media\FunctionalJavascript

Code

protected function videoTest() {
    $assert_session = $this->assertSession();
    $page = $this->getSession()
        ->getPage();
    $source_field_id = 'field_media_video_file';
    // Create 2 test files.
    $test_filename = $this->randomMachineName() . '.mp4';
    $test_filepath = 'public://' . $test_filename;
    $test_filename_updated = $this->randomMachineName() . '.mp4';
    $test_filepath_updated = 'public://' . $test_filename_updated;
    file_put_contents($test_filepath, str_repeat('t', 10));
    file_put_contents($test_filepath_updated, str_repeat('u', 10));
    // Check if the name field is properly hidden on the media form.
    $this->drupalGet('media/add/video');
    $assert_session->fieldNotExists('name');
    // Check if the source field is available.
    $assert_session->fieldExists("files[{$source_field_id}_0]");
    // Create a media item.
    $page->attachFileToField("files[{$source_field_id}_0]", \Drupal::service('file_system')->realpath($test_filepath));
    $result = $assert_session->waitForButton('Remove');
    $this->assertNotEmpty($result);
    $page->pressButton('Save');
    $video_media_id = $this->container
        ->get('entity_type.manager')
        ->getStorage('media')
        ->getQuery()
        ->sort('mid', 'DESC')
        ->execute();
    $video_media_id = reset($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' => $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($video_media_id);
    $this->assertSame($test_filename, $media->label());
    // Here we expect to see only the linked filename. Assert only one element
    // in the content region.
    $assert_session->elementsCount('css', 'article.media--type-video > *', 1);
    // Assert the video element is present inside the media element and that its
    // src attribute matches the video file.
    $video_element = $assert_session->elementExists('css', 'article.media--type-video .field--name-field-media-video-file video > source');
    $expected_video_src = file_url_transform_relative(file_create_url(\Drupal::token()->replace('public://[date:custom:Y]-[date:custom:m]/' . $test_filename)));
    $this->assertSame($expected_video_src, $video_element->getAttribute('src'));
    // Assert the media name is updated through the field mapping when changing
    // the source field.
    $this->drupalGet('media/' . $video_media_id . '/edit');
    $page->pressButton('Remove');
    $result = $assert_session->waitForField("files[{$source_field_id}_0]");
    $this->assertNotEmpty($result);
    $page->attachFileToField("files[{$source_field_id}_0]", \Drupal::service('file_system')->realpath($test_filepath_updated));
    $result = $assert_session->waitForButton('Remove');
    $this->assertNotEmpty($result);
    $page->pressButton('Save');
    $this->drupalGet('/node/' . $node->id());
    // Check if the default media name is updated as expected.
    $media = \Drupal::entityTypeManager()->getStorage('media')
        ->loadUnchanged($video_media_id);
    $this->assertSame($test_filename_updated, $media->label());
    // Again we expect to see only the linked filename. Assert only one element
    // in the content region.
    $assert_session->elementsCount('css', 'article.media--type-video > *', 1);
    // Assert the video element is present inside the media element and that its
    // src attribute matches the updated video file.
    $video_element = $assert_session->elementExists('css', 'article.media--type-video .field--name-field-media-video-file video > source');
    $expected_video_src = file_url_transform_relative(file_create_url(\Drupal::token()->replace('public://[date:custom:Y]-[date:custom:m]/' . $test_filename_updated)));
    $this->assertSame($expected_video_src, $video_element->getAttribute('src'));
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.