function ManageFieldsFunctionalTest::testFieldNameValidation

Same name and namespace in other branches
  1. 10 core/modules/field_ui/tests/src/Functional/ManageFieldsFunctionalTest.php \Drupal\Tests\field_ui\Functional\ManageFieldsFunctionalTest::testFieldNameValidation()

Tests validation of duplicate and disallowed field names.

File

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

Class

ManageFieldsFunctionalTest
Tests the Field UI "Manage fields" screen.

Namespace

Drupal\Tests\field_ui\Functional

Code

public function testFieldNameValidation() : void {
  // field_tags already exists, so we're expecting an error when trying to
  // create a new field with the same name.
  $url = 'admin/structure/types/manage/' . $this->contentType . '/fields/add-field';
  $this->drupalGet($url);
  $this->clickLink('Boolean');
  $this->submitForm([], 'Continue');
  $edit = [
    'label' => $this->randomMachineName(),
    'field_name' => 'tags',
  ];
  $this->submitForm($edit, 'Continue');
  $this->assertSession()
    ->pageTextContains('The machine-readable name is already in use. It must be unique.');
  // Reset the field prefix so we can test properly.
  $this->config('field_ui.settings')
    ->set('field_prefix', '')
    ->save();
  $label = 'Disallowed field';
  $edit = [
    'label' => $label,
  ];
  // Try with an entity key.
  $edit['field_name'] = 'title';
  $bundle_path = 'admin/structure/types/manage/' . $this->contentType;
  $this->drupalGet("{$bundle_path}/fields/add-field");
  $this->clickLink('Test field');
  $this->submitForm([], 'Continue');
  $this->submitForm($edit, 'Continue');
  $this->assertSession()
    ->pageTextContains('The machine-readable name is already in use. It must be unique.');
  // Try with a base field.
  $edit['field_name'] = 'sticky';
  $bundle_path = 'admin/structure/types/manage/' . $this->contentType;
  $this->drupalGet("{$bundle_path}/fields/add-field");
  $this->clickLink('Test field');
  $this->submitForm([], 'Continue');
  $this->submitForm($edit, 'Continue');
  $this->assertSession()
    ->pageTextContains('The machine-readable name is already in use. It must be unique.');
  $this->assertSession()
    ->addressEquals($url . '/test_field/false');
}

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