function FieldUITest::testHandlerUI

Same name in other branches
  1. 9 core/modules/field/tests/src/Functional/Views/FieldUITest.php \Drupal\Tests\field\Functional\Views\FieldUITest::testHandlerUI()
  2. 10 core/modules/field/tests/src/Functional/Views/FieldUITest.php \Drupal\Tests\field\Functional\Views\FieldUITest::testHandlerUI()
  3. 11.x 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.
    $result = $this->xpath('//select[@id=:id]/option', [
        ':id' => 'edit-options-type',
    ]);
    $options = array_map(function ($item) {
        return $item->getAttribute('value');
    }, $result);
    // @todo Replace this sort by assertArray once it's in.
    sort($options, SORT_STRING);
    $this->assertEqual($options, [
        'text_default',
        'text_trimmed',
    ], 'The text formatters for a simple text field appear as expected.');
    $this->drupalPostForm(NULL, [
        'options[type]' => 'text_trimmed',
    ], t('Apply'));
    $this->drupalGet($url);
    $this->assertOptionSelected('edit-options-type', 'text_trimmed');
    $random_number = rand(100, 400);
    $this->drupalPostForm(NULL, [
        'options[settings][trim_length]' => $random_number,
    ], t('Apply'));
    $this->drupalGet($url);
    $this->assertFieldByName('options[settings][trim_length]', $random_number, 'The formatter setting got saved.');
    // Save the view and test whether the settings are saved.
    $this->drupalPostForm('admin/structure/views/view/test_view_fieldapi', [], t('Save'));
    $view = Views::getView('test_view_fieldapi');
    $view->initHandlers();
    $this->assertEqual($view->field['field_name_0']->options['type'], 'text_trimmed');
    $this->assertEqual($view->field['field_name_0']->options['settings']['trim_length'], $random_number);
    // 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->drupalPostForm('admin/structure/views/nojs/handler/test_view_fieldapi/default/field/field_name_0', $edit, t('Apply'));
    $this->drupalPostForm('admin/structure/views/view/test_view_fieldapi', [], t('Save'));
    $view = Views::getView('test_view_fieldapi');
    $view->initHandlers();
    $this->assertEqual($view->field['field_name_0']->options['type'], 'text_default');
    $this->assertEqual($view->field['field_name_0']->options['settings'], []);
    // Ensure that the view depends on the field storage.
    $dependencies = \Drupal::service('config.manager')->findConfigEntityDependents('config', [
        $this->fieldStorages[0]
            ->getConfigDependencyName(),
    ]);
    $this->assertTrue(isset($dependencies['views.view.test_view_fieldapi']), 'The view is dependent on the field storage.');
}

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