function FieldUiJSTestTrait::fieldUIAddExistingFieldJS

Same name in other branches
  1. 11.x core/modules/field_ui/tests/src/Traits/FieldUiJSTestTrait.php \Drupal\Tests\field_ui\Traits\FieldUiJSTestTrait::fieldUIAddExistingFieldJS()

Adds an existing field through the Field UI.

Parameters

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

string $existing_storage_name: The name of the existing field storage for which we want to add a new field.

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

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

1 call to FieldUiJSTestTrait::fieldUIAddExistingFieldJS()
ManageFieldsTest::testReuseExistingField in core/modules/field_ui/tests/src/FunctionalJavascript/ManageFieldsTest.php
Tests re-using an existing field and the visibility of the re-use button.

File

core/modules/field_ui/tests/src/Traits/FieldUiJSTestTrait.php, line 94

Class

FieldUiJSTestTrait
Provides common functionality for the Field UI tests that depend on JS.

Namespace

Drupal\Tests\field_ui\Traits

Code

public function fieldUIAddExistingFieldJS(string $bundle_path, string $existing_storage_name, ?string $label = NULL, array $field_edit = []) : void {
    $label = $label ?: $this->randomMachineName();
    $field_edit['edit-label'] = $label;
    // First step: navigate to the re-use field page.
    $this->drupalGet("{$bundle_path}/fields/");
    // Confirm that the local action is visible.
    $this->assertSession()
        ->linkExists('Re-use an existing field');
    $this->clickLink('Re-use an existing field');
    // Wait for the modal to open.
    $this->assertSession()
        ->waitForElementVisible('css', '#drupal-modal');
    $this->assertSession()
        ->elementExists('css', "input[value=Re-use][name={$existing_storage_name}]");
    $this->click("input[value=Re-use][name={$existing_storage_name}]");
    // Set the main content to only the content region because the label can
    // contain HTML which will be auto-escaped by Twig.
    $this->assertSession()
        ->responseContains('field-config-edit-form');
    // Check that the page does not have double escaped HTML tags.
    $this->assertSession()
        ->responseNotContains('<');
    // Second 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);
}

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