function FieldUiTestTrait::fieldUIAddNewField

Creates a new field through the Field UI.

Parameters

string $bundle_path: Admin path of the bundle that the new field is to be attached to.

string $field_name: The field name of the new field storage.

string $label: (optional) The label of the new field. Defaults to a random string.

string $field_type: (optional) The field type of the new field storage. Defaults to 'test_field'.

array $storage_edit: (optional) $edit parameter for drupalPostForm() on the second step ('Storage settings' form).

array $field_edit: (optional) $edit parameter for drupalPostForm() on the third step ('Field settings' form).

File

core/modules/field_ui/src/Tests/FieldUiTestTrait.php, line 35

Class

FieldUiTestTrait
Provides common functionality for the Field UI test classes.

Namespace

Drupal\field_ui\Tests

Code

public function fieldUIAddNewField($bundle_path, $field_name, $label = NULL, $field_type = 'test_field', array $storage_edit = [], array $field_edit = []) {
  $label = $label ?: $this->randomString();
  $initial_edit = [
    'new_storage_type' => $field_type,
    'label' => $label,
    'field_name' => $field_name,
  ];
  // Allow the caller to set a NULL path in case they navigated to the right
  // page before calling this method.
  if ($bundle_path !== NULL) {
    $bundle_path = "{$bundle_path}/fields/add-field";
  }
  // First step: 'Add field' page.
  $this->drupalPostForm($bundle_path, $initial_edit, t('Save and continue'));
  $this->assertRaw(t('These settings apply to the %label field everywhere it is used.', [
    '%label' => $label,
  ]), 'Storage settings page was displayed.');
  // Test Breadcrumbs.
  $this->assertLink($label, 0, 'Field label is correct in the breadcrumb of the storage settings page.');
  // Second step: 'Storage settings' form.
  $this->drupalPostForm(NULL, $storage_edit, t('Save field settings'));
  $this->assertRaw(t('Updated field %label field settings.', [
    '%label' => $label,
  ]), 'Redirected to field settings page.');
  // Third step: 'Field settings' form.
  $this->drupalPostForm(NULL, $field_edit, t('Save settings'));
  $this->assertRaw(t('Saved %label configuration.', [
    '%label' => $label,
  ]), 'Redirected to "Manage fields" page.');
  // Check that the field appears in the overview form.
  $this->assertFieldByXPath('//table[@id="field-overview"]//tr/td[1]', $label, 'Field was created and appears in the overview page.');
}

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