You are here

public function MediaLibraryDisplayModeTest::testDisplayModes in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/media_library/tests/src/Functional/MediaLibraryDisplayModeTest.php \Drupal\Tests\media_library\Functional\MediaLibraryDisplayModeTest::testDisplayModes()
  2. 9 core/modules/media_library/tests/src/Functional/MediaLibraryDisplayModeTest.php \Drupal\Tests\media_library\Functional\MediaLibraryDisplayModeTest::testDisplayModes()

Tests that the Media Library can automatically configure display modes.

File

core/modules/media_library/tests/src/Functional/MediaLibraryDisplayModeTest.php, line 56

Class

MediaLibraryDisplayModeTest
Tests that the Media Library automatically configures form/view modes.

Namespace

Drupal\Tests\media_library\Functional

Code

public function testDisplayModes() {
  $this
    ->createMediaType('file', [
    'id' => 'type_one',
  ]);
  $this
    ->createMediaType('file', [
    'id' => 'type_two',
    'field_map' => [
      'name' => File::METADATA_ATTRIBUTE_NAME,
    ],
  ]);
  $this
    ->createMediaType('image', [
    'id' => 'type_three',
  ]);
  $this
    ->createMediaType('image', [
    'id' => 'type_four',
    'field_map' => [
      'name' => Image::METADATA_ATTRIBUTE_NAME,
    ],
  ]);

  // Display modes are not automatically created when creating a media type
  // programmatically, only when installing the module or when creating a
  // media type via the UI.
  $this
    ->assertNull(EntityFormDisplay::load('media.type_one.media_library'));
  $this
    ->assertNull(EntityViewDisplay::load('media.type_one.media_library'));
  $this
    ->assertNull(EntityFormDisplay::load('media.type_two.media_library'));
  $this
    ->assertNull(EntityViewDisplay::load('media.type_two.media_library'));
  $this
    ->assertNull(EntityFormDisplay::load('media.type_three.media_library'));
  $this
    ->assertNull(EntityViewDisplay::load('media.type_three.media_library'));
  $this
    ->assertNull(EntityFormDisplay::load('media.type_four.media_library'));
  $this
    ->assertNull(EntityViewDisplay::load('media.type_four.media_library'));

  // Display modes are created on install.
  $this->container
    ->get('module_installer')
    ->install([
    'media_library',
  ]);

  // For a non-image media type without a mapped name field, the media_library
  // form mode should only contain the name field.
  $this
    ->assertFormDisplay('type_one', TRUE, FALSE);
  $this
    ->assertViewDisplay('type_one', 'medium');

  // For a non-image media type with a mapped name field, the media_library
  // form mode should not contain any fields.
  $this
    ->assertFormDisplay('type_two', FALSE, FALSE);
  $this
    ->assertViewDisplay('type_two', 'medium');

  // For an image media type without a mapped name field, the media_library
  // form mode should contain the name field and the source field.
  $this
    ->assertFormDisplay('type_three', TRUE, TRUE);
  $this
    ->assertViewDisplay('type_three', 'medium');

  // For an image media type with a mapped name field, the media_library form
  // mode should only contain the source field.
  $this
    ->assertFormDisplay('type_four', FALSE, TRUE);
  $this
    ->assertViewDisplay('type_four', 'medium');

  // Create a non-image media type without a mapped name field in the UI.
  $type_five_id = 'type_five';
  $edit = [
    'label' => $type_five_id,
    'id' => $type_five_id,
    'source' => 'file',
  ];
  $this
    ->drupalGet('admin/structure/media/add');
  $this
    ->submitForm($edit, 'Save');
  $this
    ->submitForm([], 'Save');
  $this
    ->assertSession()
    ->pageTextContains("Media Library form and view displays have been created for the {$type_five_id} media type.");
  $this
    ->assertFormDisplay($type_five_id, TRUE, FALSE);
  $this
    ->assertViewDisplay($type_five_id, 'medium');

  // Create a non-image media type with a mapped name field in the UI.
  $type_six_id = 'type_six';
  $edit = [
    'label' => $type_six_id,
    'id' => $type_six_id,
    'source' => 'file',
  ];
  $this
    ->drupalGet('admin/structure/media/add');
  $this
    ->submitForm($edit, 'Save');
  $edit = [
    'field_map[name]' => File::METADATA_ATTRIBUTE_NAME,
  ];
  $this
    ->submitForm($edit, 'Save');
  $this
    ->assertSession()
    ->pageTextContains("Media Library form and view displays have been created for the {$type_six_id} media type.");
  $this
    ->assertFormDisplay($type_six_id, FALSE, FALSE);
  $this
    ->assertViewDisplay($type_six_id, 'medium');

  // Create an image media type without a mapped name field in the UI.
  $type_seven_id = 'type_seven';
  $edit = [
    'label' => $type_seven_id,
    'id' => $type_seven_id,
    'source' => 'image',
  ];
  $this
    ->drupalGet('admin/structure/media/add');
  $this
    ->submitForm($edit, 'Save');
  $this
    ->submitForm([], 'Save');
  $this
    ->assertSession()
    ->pageTextContains("Media Library form and view displays have been created for the {$type_seven_id} media type.");
  $this
    ->assertFormDisplay($type_seven_id, TRUE, TRUE);
  $this
    ->assertViewDisplay($type_seven_id, 'medium');

  // Create an image media type with a mapped name field in the UI.
  $type_eight_id = 'type_eight';
  $edit = [
    'label' => $type_eight_id,
    'id' => $type_eight_id,
    'source' => 'image',
  ];
  $this
    ->drupalGet('admin/structure/media/add');
  $this
    ->submitForm($edit, 'Save');
  $edit = [
    'field_map[name]' => Image::METADATA_ATTRIBUTE_NAME,
  ];
  $this
    ->submitForm($edit, 'Save');
  $this
    ->assertSession()
    ->pageTextContains("Media Library form and view displays have been created for the {$type_eight_id} media type.");
  $this
    ->assertFormDisplay($type_eight_id, FALSE, TRUE);
  $this
    ->assertViewDisplay($type_eight_id, 'medium');

  // Create an oEmbed media type with a mapped name field in the UI.
  $type_id = 'pinto_bean';
  $edit = [
    'label' => $type_id,
    'id' => $type_id,
    'source' => 'oembed:video',
  ];
  $this
    ->drupalGet('admin/structure/media/add');
  $this
    ->submitForm($edit, 'Save');
  $edit = [
    'field_map[title]' => 'name',
  ];
  $this
    ->submitForm($edit, 'Save');
  $this
    ->assertSession()
    ->pageTextContains("Media Library form and view displays have been created for the {$type_id} media type.");
  $this
    ->assertFormDisplay($type_id, FALSE, FALSE);
  $this
    ->assertViewDisplay($type_id, 'medium');

  // Delete a form and view display.
  EntityFormDisplay::load('media.type_one.media_library')
    ->delete();
  EntityViewDisplay::load('media.type_one.media_library')
    ->delete();

  // Make sure the form and view display are not created when saving existing
  // media types.
  $this
    ->drupalGet('admin/structure/media/manage/type_one');
  $this
    ->submitForm([], 'Save');
  $this
    ->assertNull(EntityFormDisplay::load('media.type_one.media_library'));
  $this
    ->assertNull(EntityViewDisplay::load('media.type_one.media_library'));

  // Delete the medium image style.
  ImageStyle::load('medium')
    ->delete();

  // Create an image media type, assert the displays are created and the
  // fallback 'media_library' image style is used.
  $type_nine_id = 'type_nine';
  $edit = [
    'label' => $type_nine_id,
    'id' => $type_nine_id,
    'source' => 'image',
  ];
  $this
    ->drupalGet('admin/structure/media/add');
  $this
    ->submitForm($edit, 'Save');
  $this
    ->submitForm([], 'Save');
  $this
    ->assertSession()
    ->pageTextContains("Media Library form and view displays have been created for the {$type_nine_id} media type.");
  $this
    ->assertFormDisplay($type_nine_id, TRUE, TRUE);
  $this
    ->assertViewDisplay($type_nine_id, 'media_library');
}