function MediaStandardProfileTest::audioTest

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

Tests the standard profile configuration for media type 'audio'.

1 call to MediaStandardProfileTest::audioTest()
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 105

Class

MediaStandardProfileTest
Basic tests for Media configuration in the standard profile.

Namespace

Drupal\Tests\media\FunctionalJavascript

Code

protected function audioTest() {
    $assert_session = $this->assertSession();
    $page = $this->getSession()
        ->getPage();
    $source_field_id = 'field_media_audio_file';
    // Create 2 test files.
    $test_filename = $this->randomMachineName() . '.mp3';
    $test_filepath = 'public://' . $test_filename;
    $test_filename_updated = $this->randomMachineName() . '.mp3';
    $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/audio');
    $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');
    $audio_media_id = $this->container
        ->get('entity_type.manager')
        ->getStorage('media')
        ->getQuery()
        ->accessCheck(FALSE)
        ->sort('mid', 'DESC')
        ->execute();
    $audio_media_id = reset($audio_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' => $audio_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($audio_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', 'div.media--type-audio > *', 1);
    // Assert the audio file is present inside the media element and that its
    // src attribute matches the audio file.
    $audio_element = $assert_session->elementExists('css', 'div.media--type-audio .field--name-field-media-audio-file audio > source');
    
    /** @var \Drupal\Core\File\FileUrlGeneratorInterface $file_url_generator */
    $file_url_generator = \Drupal::service('file_url_generator');
    $expected_audio_src = $file_url_generator->generate(\Drupal::token()->replace('public://[date:custom:Y]-[date:custom:m]/' . $test_filename))
        ->toString();
    $this->assertSame($expected_audio_src, $audio_element->getAttribute('src'));
    // Assert the media name is updated through the field mapping when changing
    // the source field.
    $this->drupalGet('media/' . $audio_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($audio_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', 'div.media--type-audio > *', 1);
    // Assert the audio file is present inside the media element and that its
    // src attribute matches the updated audio file.
    $audio_element = $assert_session->elementExists('css', 'div.media--type-audio .field--name-field-media-audio-file audio > source');
    $expected_audio_src = $file_url_generator->generate(\Drupal::token()->replace('public://[date:custom:Y]-[date:custom:m]/' . $test_filename_updated))
        ->toString();
    $this->assertSame($expected_audio_src, $audio_element->getAttribute('src'));
}

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