function MediaUiFunctionalTest::testMediaReferenceWidget

Same name in other branches
  1. 9 core/modules/media/tests/src/Functional/MediaUiFunctionalTest.php \Drupal\Tests\media\Functional\MediaUiFunctionalTest::testMediaReferenceWidget()

Tests the default autocomplete widgets for media reference fields.

@dataProvider providerTestMediaReferenceWidget

Parameters

int $cardinality: The field cardinality.

bool[] $media_type_create_access: An array of booleans indicating whether to grant the test user create access for each media type. A media type is created automatically for each; for example, an array [TRUE, FALSE] would create two media types, one that allows the user to create media and a second that does not.

bool $list_access: Whether to grant the test user access to list media.

See also

media_field_widget_entity_reference_autocomplete_form_alter()

media_field_widget_multiple_entity_reference_autocomplete_form_alter()

File

core/modules/media/tests/src/Functional/MediaUiFunctionalTest.php, line 256

Class

MediaUiFunctionalTest
Ensures that media UI works correctly.

Namespace

Drupal\Tests\media\Functional

Code

public function testMediaReferenceWidget($cardinality, array $media_type_create_access, $list_access, $widget_id = 'entity_reference_autocomplete') {
    $assert_session = $this->assertSession();
    // Create two content types.
    $non_media_content_type = $this->createContentType();
    $content_type = $this->createContentType();
    // Create some media types.
    $media_types = [];
    $permissions = [];
    $create_media_types = [];
    foreach ($media_type_create_access as $id => $access) {
        if ($access) {
            $create_media_types[] = "media_type_{$id}";
            $permissions[] = "create media_type_{$id} media";
        }
        $this->createMediaType('test', [
            'id' => "media_type_{$id}",
            'label' => "media_type_{$id}",
        ]);
        $media_types["media_type_{$id}"] = "media_type_{$id}";
    }
    // Create a user that can create content of the type, with other
    // permissions as given by the data provider.
    $permissions[] = "create {$content_type->id()} content";
    if ($list_access) {
        $permissions[] = "access media overview";
    }
    $test_user = $this->drupalCreateUser($permissions);
    // Create a non-media entity reference.
    $non_media_storage = FieldStorageConfig::create([
        'field_name' => 'field_not_a_media_field',
        'entity_type' => 'node',
        'type' => 'entity_reference',
        'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
        'settings' => [
            'target_type' => 'node',
        ],
    ]);
    $non_media_storage->save();
    $non_media_field = FieldConfig::create([
        'label' => 'No media here!',
        'field_storage' => $non_media_storage,
        'entity_type' => 'node',
        'bundle' => $non_media_content_type->id(),
        'settings' => [
            'handler' => 'default',
            'handler_settings' => [
                'target_bundles' => [
                    $non_media_content_type->id() => $non_media_content_type->id(),
                ],
            ],
        ],
    ]);
    $non_media_field->save();
    \Drupal::entityTypeManager()->getStorage('entity_form_display')
        ->load('node.' . $non_media_content_type->id() . '.default')
        ->setComponent('field_not_a_media_field', [
        'type' => $widget_id,
    ])
        ->save();
    // Create a media field through the user interface to ensure that the
    // help text handling does not break the default value entry on the field
    // settings form.
    // Using drupalPostForm() to avoid dealing with JavaScript on the previous
    // page in the field creation.
    $edit = [
        'new_storage_type' => 'field_ui:entity_reference:media',
        'label' => "Media (cardinality {$cardinality})",
        'field_name' => 'media_reference',
    ];
    $this->drupalPostForm("admin/structure/types/manage/{$content_type->id()}/fields/add-field", $edit, 'Save and continue');
    $edit = [];
    foreach ($media_types as $type) {
        $edit["settings[handler_settings][target_bundles][{$type}]"] = TRUE;
    }
    $this->drupalPostForm("admin/structure/types/manage/{$content_type->id()}/fields/node.{$content_type->id()}.field_media_reference", $edit, "Save settings");
    \Drupal::entityTypeManager()->getStorage('entity_form_display')
        ->load('node.' . $content_type->id() . '.default')
        ->setComponent('field_media_reference', [
        'type' => $widget_id,
    ])
        ->save();
    // Some of the expected texts.
    $create_help = 'Create your media on the media add page (opens a new window), then add it by name to the field below.';
    $list_text = 'See the media list (opens a new window) to help locate media.';
    $use_help = 'Type part of the media name.';
    $create_header = "Create new media";
    $use_header = "Use existing media";
    // First check that none of the help texts are on the non-media content.
    $this->drupalGet("/node/add/{$non_media_content_type->id()}");
    $this->assertNoHelpTexts([
        $create_header,
        $create_help,
        $use_header,
        $use_help,
        $list_text,
        'Allowed media types:',
    ]);
    // Now, check that the widget displays the expected help text under the
    // given conditions for the test user.
    $this->drupalLogin($test_user);
    $this->drupalGet("/node/add/{$content_type->id()}");
    // Specific expected help texts for the media field.
    $create_header = "Create new media";
    $use_header = "Use existing media";
    $type_list = 'Allowed media types: ' . implode(", ", array_keys($media_types));
    $fieldset_selector = '#edit-field-media-reference-wrapper fieldset';
    $fieldset = $assert_session->elementExists('css', $fieldset_selector);
    $this->assertSame("Media (cardinality {$cardinality})", $assert_session->elementExists('css', 'legend', $fieldset)
        ->getText());
    // Assert text that should be displayed regardless of other access.
    $this->assertHelpTexts([
        $use_header,
        $use_help,
        $type_list,
    ], $fieldset_selector);
    // The entire section for creating new media should only be displayed if
    // the user can create at least one media of the type.
    if ($create_media_types) {
        if (count($create_media_types) === 1) {
            $url = Url::fromRoute('entity.media.add_form')->setRouteParameter('media_type', $create_media_types[0]);
        }
        else {
            $url = Url::fromRoute('entity.media.add_page');
        }
        $this->assertHelpTexts([
            $create_header,
            $create_help,
        ], $fieldset_selector);
        $this->assertHelpLink($fieldset, 'media add page', [
            'target' => '_blank',
            'href' => $url->toString(),
        ]);
    }
    else {
        $this->assertNoHelpTexts([
            $create_header,
            $create_help,
        ]);
        $this->assertNoHelpLink($fieldset, 'media add page');
    }
    if ($list_access) {
        $this->assertHelpTexts([
            $list_text,
        ], $fieldset_selector);
        $this->assertHelpLink($fieldset, 'media list', [
            'target' => '_blank',
            'href' => Url::fromRoute('entity.media.collection')->toString(),
        ]);
    }
    else {
        $this->assertNoHelpTexts([
            $list_text,
        ]);
        $this->assertNoHelpLink($fieldset, 'media list');
    }
}

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