You are here

function ManageFieldsTest::testHiddenFields in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/field_ui/src/Tests/ManageFieldsTest.php \Drupal\field_ui\Tests\ManageFieldsTest::testHiddenFields()

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

File

core/modules/field_ui/src/Tests/ManageFieldsTest.php, line 545
Contains \Drupal\field_ui\Tests\ManageFieldsTest.

Class

ManageFieldsTest
Tests the Field UI "Manage fields" screen.

Namespace

Drupal\field_ui\Tests

Code

function testHiddenFields() {

  // 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
    ->assertFalse($this
    ->xpath('//select[@id="edit-new-storage-type"]//option[@value="hidden_test_field"]'), "The 'add new field' select respects field types 'no_ui' property.");
  $this
    ->assertTrue($this
    ->xpath('//select[@id="edit-new-storage-type"]//option[@value="shape"]'), "The 'add new field' select shows a valid option.");

  // Create a field storage and a field programmatically.
  $field_name = 'hidden_test_field';
  FieldStorageConfig::create(array(
    'field_name' => $field_name,
    'entity_type' => 'node',
    'type' => $field_name,
  ))
    ->save();
  $field = array(
    'field_name' => $field_name,
    'bundle' => $this->contentType,
    'entity_type' => 'node',
    'label' => t('Hidden field'),
  );
  FieldConfig::create($field)
    ->save();
  entity_get_form_display('node', $this->contentType, 'default')
    ->setComponent($field_name)
    ->save();
  $this
    ->assertTrue(FieldConfig::load('node.' . $this->contentType . '.' . $field_name), format_string('A field of the field storage %field was created programmatically.', array(
    '%field' => $field_name,
  )));

  // Check that the newly added field appears on the 'Manage Fields'
  // screen.
  $this
    ->drupalGet('admin/structure/types/manage/' . $this->contentType . '/fields');
  $this
    ->assertFieldByXPath('//table[@id="field-overview"]//tr[@id="hidden-test-field"]//td[1]', $field['label'], 'Field was created and appears in the overview page.');

  // 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/add-field');
  $this
    ->assertFalse($this
    ->xpath('//select[@id="edit-existing-storage-name"]//option[@value=:field_name]', array(
    ':field_name' => $field_name,
  )), "The 're-use existing field' select respects field types 'no_ui' property.");
  $this
    ->assertTrue($this
    ->xpath('//select[@id="edit-existing-storage-name"]//option[@value=:field_name]', array(
    ':field_name' => 'field_tags',
  )), "The 're-use existing field' select shows a valid option.");

  // Check that non-configurable fields are not available.
  $field_types = \Drupal::service('plugin.manager.field.field_type')
    ->getDefinitions();
  foreach ($field_types as $field_type => $definition) {
    if (empty($definition['no_ui'])) {
      $this
        ->assertTrue($this
        ->xpath('//select[@id="edit-new-storage-type"]//option[@value=:field_type]', array(
        ':field_type' => $field_type,
      )), SafeMarkup::format('Configurable field type @field_type is available.', array(
        '@field_type' => $field_type,
      )));
    }
    else {
      $this
        ->assertFalse($this
        ->xpath('//select[@id="edit-new-storage-type"]//option[@value=:field_type]', array(
        ':field_type' => $field_type,
      )), SafeMarkup::format('Non-configurable field type @field_type is not available.', array(
        '@field_type' => $field_type,
      )));
    }
  }
}