You are here

public function FieldUiTestTrait::fieldUIAddNewField in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/field_ui/tests/src/Traits/FieldUiTestTrait.php \Drupal\Tests\field_ui\Traits\FieldUiTestTrait::fieldUIAddNewField()
  2. 9 core/modules/field_ui/tests/src/Traits/FieldUiTestTrait.php \Drupal\Tests\field_ui\Traits\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 submitForm() on the second step ('Storage settings' form).

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

1 call to FieldUiTestTrait::fieldUIAddNewField()
EntityReferenceAdminTest::createEntityReferenceField in core/modules/field/tests/src/Functional/EntityReference/EntityReferenceAdminTest.php
Creates a new Entity Reference fields with a given target type.

File

core/modules/field_ui/tests/src/Traits/FieldUiTestTrait.php, line 29

Class

FieldUiTestTrait
Provides common functionality for the Field UI test classes.

Namespace

Drupal\Tests\field_ui\Traits

Code

public function fieldUIAddNewField($bundle_path, $field_name, $label = NULL, $field_type = 'test_field', array $storage_edit = [], array $field_edit = []) {

  // Generate a label containing only letters and numbers to prevent random
  // test failure.
  // See https://www.drupal.org/project/drupal/issues/3030902
  $label = $label ?: $this
    ->randomMachineName();
  $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.
  if ($bundle_path !== NULL) {
    $this
      ->drupalGet($bundle_path);
  }
  $this
    ->submitForm($initial_edit, 'Save and continue');
  $this
    ->assertSession()
    ->pageTextContains("These settings apply to the {$label} field everywhere it is used.");

  // Test Breadcrumbs.
  $this
    ->assertSession()
    ->linkExists($label, 0, 'Field label is correct in the breadcrumb of the storage settings page.');

  // Second step: 'Storage settings' form.
  $this
    ->submitForm($storage_edit, 'Save field settings');
  $this
    ->assertSession()
    ->pageTextContains("Updated field {$label} field settings.");

  // Third step: 'Field settings' form.
  $this
    ->submitForm($field_edit, 'Save settings');
  $this
    ->assertSession()
    ->pageTextContains("Saved {$label} configuration.");

  // Check that the field appears in the overview form.
  $xpath = $this
    ->assertSession()
    ->buildXPathQuery("//table[@id=\"field-overview\"]//tr/td[1 and text() = :label]", [
    ':label' => $label,
  ]);
  $this
    ->assertSession()
    ->elementExists('xpath', $xpath);
}