function ManageFieldsFunctionalTest::testHiddenFields

Same name in other branches
  1. 9 core/modules/field_ui/tests/src/Functional/ManageFieldsFunctionalTest.php \Drupal\Tests\field_ui\Functional\ManageFieldsFunctionalTest::testHiddenFields()
  2. 8.9.x core/modules/field_ui/tests/src/Functional/ManageFieldsFunctionalTest.php \Drupal\Tests\field_ui\Functional\ManageFieldsFunctionalTest::testHiddenFields()
  3. 11.x core/modules/field_ui/tests/src/Functional/ManageFieldsFunctionalTest.php \Drupal\Tests\field_ui\Functional\ManageFieldsFunctionalTest::testHiddenFields()

Tests that Field UI respects the 'no_ui' flag in the field type definition.

File

core/modules/field_ui/tests/src/Functional/ManageFieldsFunctionalTest.php, line 149

Class

ManageFieldsFunctionalTest
Tests the Field UI "Manage fields" screen.

Namespace

Drupal\Tests\field_ui\Functional

Code

public function testHiddenFields() : void {
    // Check that the field type is not available in the 'add new field' row.
    $this->drupalGet('admin/structure/types/manage/' . $this->contentType . '/fields/add-field');
    $this->assertSession()
        ->elementNotExists('css', "[name='new_storage_type'][value='hidden_test_field']");
    $this->assertSession()
        ->elementExists('css', "[name='new_storage_type'][value='shape']");
    // Create a field storage and a field programmatically.
    $field_name = 'hidden_test_field';
    FieldStorageConfig::create([
        'field_name' => $field_name,
        'entity_type' => 'node',
        'type' => $field_name,
    ])->save();
    $field = [
        'field_name' => $field_name,
        'bundle' => $this->contentType,
        'entity_type' => 'node',
        'label' => 'Hidden field',
    ];
    FieldConfig::create($field)->save();
    \Drupal::service('entity_display.repository')->getFormDisplay('node', $this->contentType)
        ->setComponent($field_name)
        ->save();
    $this->assertInstanceOf(FieldConfig::class, FieldConfig::load('node.' . $this->contentType . '.' . $field_name));
    // Check that the newly added field appears on the 'Manage Fields'
    // screen.
    $this->drupalGet('admin/structure/types/manage/' . $this->contentType . '/fields');
    $this->assertSession()
        ->elementTextContains('xpath', '//table[@id="field-overview"]//tr[@id="hidden-test-field"]//td[1]', $field['label']);
    // Check that the field does not appear in the 're-use existing field' row
    // on other bundles.
    $this->drupalGet('admin/structure/types/manage/page/fields/reuse');
    $this->assertSession()
        ->elementNotExists('css', ".js-reuse-table [data-field-id='{$field_name}']");
    $this->assertSession()
        ->elementExists('css', '.js-reuse-table [data-field-id="field_tags"]');
    // Check that non-configurable fields are not available.
    $field_types = \Drupal::service('plugin.manager.field.field_type')->getDefinitions();
    $this->drupalGet('admin/structure/types/manage/page/fields/add-field');
    foreach ($field_types as $field_type => $definition) {
        if (empty($definition['no_ui'])) {
            try {
                $this->assertSession()
                    ->elementExists('css', "[name='new_storage_type'][value='{$field_type}']");
            } catch (ElementNotFoundException) {
                if ($group = $this->getFieldFromGroup($field_type)) {
                    $this->assertSession()
                        ->elementExists('css', "[name='new_storage_type'][value='{$group}']");
                    $this->submitForm([
                        'new_storage_type' => $group,
                    ], 'Continue');
                    $this->assertSession()
                        ->elementExists('css', "[name='group_field_options_wrapper'][value='{$field_type}']");
                    $this->submitForm([], 'Back');
                }
            }
        }
        else {
            $this->assertSession()
                ->elementNotExists('css', "[name='new_storage_type'][value='{$field_type}']");
        }
    }
}

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