function MediaSourceTest::createMediaTypeViaForm

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

Creates a media type via form submit.

Parameters

string $source_plugin_id: Source plugin ID.

string $field_name: Source field name.

2 calls to MediaSourceTest::createMediaTypeViaForm()
MediaSourceTest::testDifferentSourceFieldDisplays in core/modules/media/tests/src/Kernel/MediaSourceTest.php
Tests different display options for the source field.
MediaSourceTest::testHiddenSourceField in core/modules/media/tests/src/Kernel/MediaSourceTest.php
Tests hidden source field in media type.

File

core/modules/media/tests/src/Kernel/MediaSourceTest.php, line 573

Class

MediaSourceTest
Tests media source plugins related logic.

Namespace

Drupal\Tests\media\Kernel

Code

protected function createMediaTypeViaForm($source_plugin_id, $field_name) {
    
    /** @var \Drupal\media\MediaTypeInterface $type */
    $type = MediaType::create([
        'source' => $source_plugin_id,
    ]);
    $form = $this->container
        ->get('entity_type.manager')
        ->getFormObject('media_type', 'add')
        ->setEntity($type);
    $form_state = new FormState();
    $form_state->setValues([
        'label' => 'Test type',
        'id' => $source_plugin_id,
        'op' => t('Save'),
    ]);
    
    /** @var \Drupal\Core\Entity\EntityFieldManagerInterface $field_manager */
    $field_manager = \Drupal::service('entity_field.manager');
    // Source field not created yet.
    $fields = $field_manager->getFieldDefinitions('media', $source_plugin_id);
    $this->assertArrayNotHasKey($field_name, $fields);
    \Drupal::formBuilder()->submitForm($form, $form_state);
    // Source field exists now.
    $fields = $field_manager->getFieldDefinitions('media', $source_plugin_id);
    $this->assertArrayHasKey($field_name, $fields);
}

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