You are here

public function FieldUITest::testHandlerUI in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/field/tests/src/Functional/Views/FieldUITest.php \Drupal\Tests\field\Functional\Views\FieldUITest::testHandlerUI()

Tests basic field handler settings in the UI.

File

core/modules/field/tests/src/Functional/Views/FieldUITest.php, line 59

Class

FieldUITest
Tests the UI of the field field handler.

Namespace

Drupal\Tests\field\Functional\Views

Code

public function testHandlerUI() {
  $url = "admin/structure/views/nojs/handler/test_view_fieldapi/default/field/field_name_0";
  $this
    ->drupalGet($url);

  // Tests the available formatter options.
  $options = $this
    ->assertSession()
    ->selectExists('edit-options-type')
    ->findAll('css', 'option');
  $options = array_map(function ($item) {
    return $item
      ->getValue();
  }, $options);
  $this
    ->assertEqualsCanonicalizing([
    'text_default',
    'text_trimmed',
  ], $options);
  $this
    ->submitForm([
    'options[type]' => 'text_trimmed',
  ], 'Apply');
  $this
    ->drupalGet($url);
  $this
    ->assertTrue($this
    ->assertSession()
    ->optionExists('edit-options-type', 'text_trimmed')
    ->isSelected());
  $random_number = rand(100, 400);
  $this
    ->submitForm([
    'options[settings][trim_length]' => $random_number,
  ], 'Apply');
  $this
    ->drupalGet($url);
  $this
    ->assertSession()
    ->fieldValueEquals('options[settings][trim_length]', $random_number);

  // Save the view and test whether the settings are saved.
  $this
    ->drupalGet('admin/structure/views/view/test_view_fieldapi');
  $this
    ->submitForm([], 'Save');
  $view = Views::getView('test_view_fieldapi');
  $view
    ->initHandlers();
  $this
    ->assertEquals('text_trimmed', $view->field['field_name_0']->options['type']);
  $this
    ->assertEquals($random_number, $view->field['field_name_0']->options['settings']['trim_length']);

  // Now change the formatter back to 'default' which doesn't have any
  // settings. We want to ensure that the settings are empty then.
  $edit['options[type]'] = 'text_default';
  $this
    ->drupalGet('admin/structure/views/nojs/handler/test_view_fieldapi/default/field/field_name_0');
  $this
    ->submitForm($edit, 'Apply');
  $this
    ->drupalGet('admin/structure/views/view/test_view_fieldapi');
  $this
    ->submitForm([], 'Save');
  $view = Views::getView('test_view_fieldapi');
  $view
    ->initHandlers();
  $this
    ->assertEquals('text_default', $view->field['field_name_0']->options['type']);
  $this
    ->assertEquals([], $view->field['field_name_0']->options['settings']);

  // Ensure that the view depends on the field storage.
  $dependencies = \Drupal::service('config.manager')
    ->findConfigEntityDependencies('config', [
    $this->fieldStorages[0]
      ->getConfigDependencyName(),
  ]);
  $this
    ->assertTrue(isset($dependencies['views.view.test_view_fieldapi']), 'The view is dependent on the field storage.');
}