function ManageFieldsTest::testAddField

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

Tests adding a field.

File

core/modules/field_ui/tests/src/Functional/ManageFieldsTest.php, line 133

Class

ManageFieldsTest
Tests the Manage Display page of a fieldable entity type.

Namespace

Drupal\Tests\field_ui\Functional

Code

public function testAddField() : void {
    $page = $this->getSession()
        ->getPage();
    $type = $this->drupalCreateContentType([
        'name' => 'Article',
        'type' => 'article',
    ]);
    // Make sure field descriptions appear, both 1 line and multiple lines.
    $this->drupalGet('/admin/structure/types/manage/' . $type->id() . '/fields/add-field');
    $edit = [
        'new_storage_type' => 'field_test_descriptions',
    ];
    $this->submitForm($edit, 'Continue');
    $this->assertSession()
        ->pageTextContains('This one-line field description is important for testing');
    $this->assertSession()
        ->pageTextContains('This multiple line description needs to use an array');
    $this->assertSession()
        ->pageTextContains('This second line contains important information');
    // Create a new field without actually saving it.
    $this->fieldUIAddNewField('admin/structure/types/manage/' . $type->id(), 'test_field', 'Test field', 'test_field', [], [], FALSE);
    // Assert that the field was not created.
    $this->assertNull(FieldStorageConfig::loadByName('node', "field_test_field"));
    $this->drupalGet('/admin/structure/types/manage/' . $type->id() . '/fields/add-field');
    $edit = [
        'new_storage_type' => 'test_field',
    ];
    $this->submitForm($edit, 'Continue');
    $edit = [
        'label' => 'Test field',
        'field_name' => 'test_field',
    ];
    $this->submitForm($edit, 'Continue');
    $this->assertSession()
        ->statusMessageNotContains('Saved');
    // Change the storage form values.
    $edit = [
        'field_storage[subform][cardinality_number]' => 5,
    ];
    $this->submitForm($edit, 'Update settings');
    $this->assertSession()
        ->statusMessageNotContains('Saved');
    // Assert that the form values persist.
    $this->assertEquals(5, $page->findField('field_storage[subform][cardinality_number]')
        ->getValue());
    // Try creating a field with the same machine name.
    $this->drupalGet('/admin/structure/types/manage/' . $type->id() . '/fields/add-field');
    $edit = [
        'new_storage_type' => 'test_field',
    ];
    $this->submitForm($edit, 'Continue');
    $edit = [
        'label' => 'Test field',
        'field_name' => 'test_field',
    ];
    $this->submitForm($edit, 'Continue');
    // Assert that the values in the field storage form are reset.
    $this->assertEquals(1, $page->findField('field_storage[subform][cardinality_number]')
        ->getValue());
    // Assert that the field is created with the new settings.
    $this->submitForm([], 'Update settings');
    $this->assertSession()
        ->statusMessageNotContains('Saved');
    $this->submitForm([], 'Save settings');
    $this->assertSession()
        ->statusMessageContains('Saved');
    $this->assertEquals(1, FieldStorageConfig::loadByName('node', 'field_test_field')->getCardinality());
}

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