function MediaLibraryAddFormTest::testMediaTypeAddForm

Tests the media library add form.

File

core/modules/media_library/tests/src/Kernel/MediaLibraryAddFormTest.php, line 68

Class

MediaLibraryAddFormTest
Tests the media library add form.

Namespace

Drupal\Tests\media_library\Kernel

Code

public function testMediaTypeAddForm() {
  $entity_type_manager = \Drupal::entityTypeManager();
  $image = $entity_type_manager->getStorage('media_type')
    ->load('image');
  $remote_video = $entity_type_manager->getStorage('media_type')
    ->load('remote_video');
  $image_source_definition = $image->getSource()
    ->getPluginDefinition();
  $remote_video_source_definition = $remote_video->getSource()
    ->getPluginDefinition();
  // Assert the form class is added to the media source.
  $this->assertSame(FileUploadForm::class, $image_source_definition['forms']['media_library_add']);
  $this->assertSame(OEmbedForm::class, $remote_video_source_definition['forms']['media_library_add']);
  // Assert the media library UI does not contains the add form when the user
  // does not have access.
  $this->assertEmpty($this->buildLibraryUi('image')['content']['form']);
  $this->assertEmpty($this->buildLibraryUi('remote_video')['content']['form']);
  // Create a user that has access to create the image media type but not the
  // remote video media type.
  $this->setCurrentUser($this->createUser([
    'create image media',
  ]));
  // Assert the media library UI only contains the add form for the image
  // media type.
  $this->assertSame('managed_file', $this->buildLibraryUi('image')['content']['form']['container']['upload']['#type']);
  $this->assertEmpty($this->buildLibraryUi('remote_video')['content']['form']);
  // Create a user that has access to create both media types.
  $this->setCurrentUser($this->createUser([
    'create image media',
    'create remote_video media',
  ]));
  // Assert the media library UI only contains the add form for both media
  // types.
  $this->assertSame('managed_file', $this->buildLibraryUi('image')['content']['form']['container']['upload']['#type']);
  $this->assertSame('url', $this->buildLibraryUi('remote_video')['content']['form']['container']['url']['#type']);
}

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