function ManageFieldsTest::testAddField

Same name in this branch
  1. 10 core/modules/field_ui/tests/src/Functional/ManageFieldsTest.php \Drupal\Tests\field_ui\Functional\ManageFieldsTest::testAddField()
Same name in other branches
  1. 11.x core/modules/field_ui/tests/src/FunctionalJavascript/ManageFieldsTest.php \Drupal\Tests\field_ui\FunctionalJavascript\ManageFieldsTest::testAddField()
  2. 11.x core/modules/field_ui/tests/src/Functional/ManageFieldsTest.php \Drupal\Tests\field_ui\Functional\ManageFieldsTest::testAddField()

Tests field add.

File

core/modules/field_ui/tests/src/FunctionalJavascript/ManageFieldsTest.php, line 178

Class

ManageFieldsTest
Tests the Field UI "Manage Fields" screens.

Namespace

Drupal\Tests\field_ui\FunctionalJavascript

Code

public function testAddField() : void {
    $page = $this->getSession()
        ->getPage();
    $assert_session = $this->assertSession();
    $this->drupalGet('admin/structure/types/manage/article/fields/add-field');
    // Test validation.
    $page->pressButton('Continue');
    $assert_session->pageTextContains('You need to select a field type.');
    $assert_session->pageTextNotContains('Choose an option below');
    $this->assertNotEmpty($number_field = $page->find('xpath', '//*[text() = "Number"]')
        ->getParent());
    $number_field->click();
    $this->assertTrue($assert_session->elementExists('css', '[name="new_storage_type"][value="number"]')
        ->isSelected());
    $page->pressButton('Continue');
    $assert_session->pageTextContains('Choose an option below');
    $field_name = 'test_field_1';
    $page->fillField('label', $field_name);
    $page->pressButton('Continue');
    $assert_session->pageTextContains('You need to choose an option.');
    $assert_session->elementNotExists('css', '[name="new_storage_type"].error');
    $assert_session->elementExists('css', '[name="group_field_options_wrapper"].error');
    $page->pressButton('Back');
    // Try adding a field using a grouped field type.
    $this->assertNotEmpty($email_field = $page->find('xpath', '//*[text() = "Email"]')
        ->getParent());
    $email_field->click();
    $this->assertTrue($assert_session->elementExists('css', '[name="new_storage_type"][value="email"]')
        ->isSelected());
    $page->pressButton('Continue');
    $assert_session->pageTextNotContains('Choose an option below');
    $page->pressButton('Back');
    $this->assertNotEmpty($text = $page->find('xpath', '//*[text() = "Plain text"]')
        ->getParent());
    $text->click();
    $this->assertTrue($assert_session->elementExists('css', '[name="new_storage_type"][value="plain_text"]')
        ->isSelected());
    $page->pressButton('Continue');
    $assert_session->pageTextContains('Choose an option below');
    $page->fillField('label', $field_name);
    $this->assertNotEmpty($text_plain = $page->find('xpath', '//*[text() = "Text (plain)"]')
        ->getParent());
    $text_plain->click();
    $this->assertTrue($assert_session->elementExists('css', '[name="group_field_options_wrapper"][value="string"]')
        ->isSelected());
    $page->pressButton('Continue');
    $this->assertMatchesRegularExpression('/.*article\\/add-field\\/node\\/field_test_field_1.*/', $this->getUrl());
    // Ensure the default value is reloaded when the field storage settings
    // are changed.
    $default_input_1_name = 'default_value_input[field_test_field_1][0][value]';
    $default_input_1 = $assert_session->fieldExists($default_input_1_name);
    $this->assertFalse($default_input_1->isVisible());
    $default_value = $assert_session->fieldExists('set_default_value');
    $default_value->check();
    $assert_session->waitForElementVisible('xpath', $default_value->getXpath());
    $default_input_1->setValue('There can be only one!');
    $default_input_2_name = 'default_value_input[field_test_field_1][1][value]';
    $assert_session->fieldNotExists($default_input_2_name);
    $cardinality = $assert_session->fieldExists('field_storage[subform][cardinality_number]');
    $cardinality->setValue(2);
    $default_input_2 = $assert_session->waitForField($default_input_2_name);
    // Ensure the default value for first input is retained.
    $assert_session->fieldValueEquals($default_input_1_name, 'There can be only one!');
    $page->findField($default_input_2_name)
        ->setValue('But maybe also two?');
    $cardinality->setValue('1');
    $assert_session->assertWaitOnAjaxRequest();
    $assert_session->waitForElementRemoved('xpath', $default_input_2->getXpath());
    // Ensure the first input retains its value.
    $assert_session->fieldValueEquals($default_input_1_name, 'There can be only one!');
    $cardinality->setValue(2);
    $assert_session->waitForField($default_input_2_name);
    // Ensure when the second input is added again it does not retain its value.
    $assert_session->fieldValueEquals($default_input_2_name, '');
    // Ensure changing the max length input will also reload the form.
    $max_length_input = $assert_session->fieldExists('field_storage[subform][settings][max_length]');
    $this->assertSame('255', $max_length_input->getValue());
    $this->assertSame('255', $default_input_1->getAttribute('maxlength'));
    $max_length_input->setValue('5');
    $page->waitFor(5, function () use ($default_input_1) {
        return $default_input_1->getAttribute('maxlength') === '5';
    });
    $this->assertSame('5', $default_input_1->getAttribute('maxlength'));
    // Set a default value that is under the new limit.
    $default_input_1->setValue('Five!');
    $page->pressButton('Save settings');
    $assert_session->pageTextContains('Saved ' . $field_name . ' configuration.');
    $this->assertNotNull($field_storage = FieldStorageConfig::loadByName('node', "field_{$field_name}"));
    $this->assertEquals('string', $field_storage->getType());
    // Try adding a field using a non-grouped field type.
    $this->drupalGet('admin/structure/types/manage/article/fields/add-field');
    $this->assertNotEmpty($number_field = $page->find('xpath', '//*[text() = "Number"]')
        ->getParent());
    $number_field->click();
    $this->assertTrue($assert_session->elementExists('css', '[name="new_storage_type"][value="number"]')
        ->isSelected());
    $page->pressButton('Continue');
    $assert_session->pageTextContains('Choose an option below');
    $this->assertNotEmpty($number_integer = $page->find('xpath', '//*[text() = "Number (integer)"]')
        ->getParent());
    $number_integer->click();
    $this->assertTrue($assert_session->elementExists('css', '[name="group_field_options_wrapper"][value="integer"]')
        ->isSelected());
    $page->pressButton('Back');
    $this->assertNotEmpty($test_field = $page->find('xpath', '//*[text() = "Test field"]')
        ->getParent());
    $test_field->click();
    $this->assertTrue($assert_session->elementExists('css', '[name="new_storage_type"][value="test_field"]')
        ->isSelected());
    $page->pressButton('Continue');
    $field_name = 'test_field_2';
    $page->fillField('label', $field_name);
    $assert_session->pageTextNotContains('Choose an option below');
    $page->pressButton('Continue');
    $this->assertMatchesRegularExpression('/.*article\\/add-field\\/node\\/field_test_field_2.*/', $this->getUrl());
    $page->pressButton('Save settings');
    $assert_session->pageTextContains('Saved ' . $field_name . ' configuration.');
    $this->assertNotNull($field_storage = FieldStorageConfig::loadByName('node', "field_{$field_name}"));
    $this->assertEquals('test_field', $field_storage->getType());
}

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