function FieldUiTestTrait::fieldUIAddNewField

Same name in this branch
  1. 8.9.x core/modules/field_ui/src/Tests/FieldUiTestTrait.php \Drupal\field_ui\Tests\FieldUiTestTrait::fieldUIAddNewField()
Same name in other branches
  1. 9 core/modules/field_ui/tests/src/Traits/FieldUiTestTrait.php \Drupal\Tests\field_ui\Traits\FieldUiTestTrait::fieldUIAddNewField()
  2. 10 core/modules/field_ui/tests/src/Traits/FieldUiTestTrait.php \Drupal\Tests\field_ui\Traits\FieldUiTestTrait::fieldUIAddNewField()
  3. 11.x 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 drupalPostForm() on the second step ('Storage settings' form).

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

13 calls to FieldUiTestTrait::fieldUIAddNewField()
CommentNonNodeTest::testCommentFunctionality in core/modules/comment/tests/src/Functional/CommentNonNodeTest.php
Tests anonymous comment functionality.
ContactSitewideTest::testSiteWideContact in core/modules/contact/tests/src/Functional/ContactSitewideTest.php
Tests configuration options and the site-wide contact form.
EntityReferenceAdminTest::createEntityReferenceField in core/modules/field/tests/src/Functional/EntityReference/EntityReferenceAdminTest.php
Creates a new Entity Reference fields with a given target type.
FieldUIDeleteTest::testDeleteField in core/modules/field_ui/tests/src/Functional/FieldUIDeleteTest.php
Tests that deletion removes field storages and fields as expected.
FileFieldWidgetTest::testPrivateFileComment in core/modules/file/tests/src/Functional/FileFieldWidgetTest.php
Tests that download restrictions on private files work on comments.

... See full list

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.
    $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->assertSession()
        ->linkExists($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.