You are here

public function EdgeEntityDisplaySettingsFormTest::testForm in Apigee Edge 8

Tests the config form.

File

tests/src/Kernel/Form/EdgeEntityDisplaySettingsFormTest.php, line 53

Class

EdgeEntityDisplaySettingsFormTest
Tests for EdgeEntityDisplaySettingsForm.

Namespace

Drupal\Tests\apigee_edge\Kernel\Form

Code

public function testForm() {
  $builder = $this->container
    ->get('form_builder');
  $form = $builder
    ->getForm(EdgeEntityDisplaySettingsForm::class, static::ENTITY_TYPE);
  $display_type_options = $form['display_settings']['display_type']['#options'];
  static::assertCount(3, $display_type_options);
  static::assertSame('Default', (string) $display_type_options['default']);
  static::assertSame('Display mode', (string) $display_type_options['view_mode']);
  $view_mode_options = $form['display_settings']['display_mode_container']['view_mode']['#options'];
  static::assertCount(1, $view_mode_options);
  static::assertSame('Default', (string) $view_mode_options['default']);

  // Add view mode.
  EntityViewMode::create([
    'id' => static::ENTITY_TYPE . '.foo',
    'targetEntityType' => static::ENTITY_TYPE,
    'label' => 'Foo',
    'status' => TRUE,
  ])
    ->save();

  // Check if new view mode appears on form.
  $builder = $this->container
    ->get('form_builder');
  $form = $builder
    ->getForm(EdgeEntityDisplaySettingsForm::class, static::ENTITY_TYPE);
  $view_mode_options = $form['display_settings']['display_mode_container']['view_mode']['#options'];
  static::assertCount(2, $view_mode_options);
  static::assertSame('Default', (string) $view_mode_options['default']);
  static::assertSame('Foo', (string) $view_mode_options['foo']);

  // Submit form and test config.
  $form_state = new FormState();
  $form_state
    ->setValue('display_type', 'view_mode');
  $form_state
    ->setValue('view_mode', 'foo');
  $this->container
    ->get('form_builder')
    ->submitForm(EdgeEntityDisplaySettingsForm::class, $form_state, static::ENTITY_TYPE);
  $config = $this
    ->config('apigee_edge.display_settings.' . static::ENTITY_TYPE);
  static::assertSame('view_mode', $config
    ->get('display_type'));
  static::assertSame('foo', $config
    ->get('view_mode'));
}