You are here

public function MediaSourceTest::testSourceConfigurationSubmit in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/media/tests/src/Kernel/MediaSourceTest.php \Drupal\Tests\media\Kernel\MediaSourceTest::testSourceConfigurationSubmit()
  2. 9 core/modules/media/tests/src/Kernel/MediaSourceTest.php \Drupal\Tests\media\Kernel\MediaSourceTest::testSourceConfigurationSubmit()

Tests configuration form submit handler on the base media source plugin.

File

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

Class

MediaSourceTest
Tests media source plugins related logic.

Namespace

Drupal\Tests\media\Kernel

Code

public function testSourceConfigurationSubmit() {

  /** @var \Drupal\media\MediaSourceManager $manager */
  $manager = $this->container
    ->get('plugin.manager.media.source');
  $form = [];
  $form_state = new FormState();
  $form_state
    ->setValues([
    'test_config_value' => 'Somewhere over the rainbow.',
  ]);

  /** @var \Drupal\media\MediaSourceInterface $source */
  $source = $manager
    ->createInstance('test', []);
  $source
    ->submitConfigurationForm($form, $form_state);
  $expected = [
    'source_field' => 'field_media_test_1',
    'test_config_value' => 'Somewhere over the rainbow.',
  ];
  $this
    ->assertSame($expected, $source
    ->getConfiguration(), 'Submitted values were saved correctly.');

  // Try to save a NULL value.
  $form_state
    ->setValue('test_config_value', NULL);
  $source
    ->submitConfigurationForm($form, $form_state);
  $expected['test_config_value'] = NULL;
  $this
    ->assertSame($expected, $source
    ->getConfiguration(), 'Submitted values were saved correctly.');

  // Make sure that the config keys are determined correctly even if the
  // existing value is NULL.
  $form_state
    ->setValue('test_config_value', 'Somewhere over the rainbow.');
  $source
    ->submitConfigurationForm($form, $form_state);
  $expected['test_config_value'] = 'Somewhere over the rainbow.';
  $this
    ->assertSame($expected, $source
    ->getConfiguration(), 'Submitted values were saved correctly.');

  // Make sure that a non-relevant value will be skipped.
  $form_state
    ->setValue('not_relevant', 'Should not be saved in the plugin.');
  $source
    ->submitConfigurationForm($form, $form_state);
  $this
    ->assertSame($expected, $source
    ->getConfiguration(), 'Submitted values were saved correctly.');
}