function FormTest::testFieldFormSingle

Same name in other branches
  1. 9 core/modules/field/tests/src/Functional/FormTest.php \Drupal\Tests\field\Functional\FormTest::testFieldFormSingle()
  2. 8.9.x core/modules/field/tests/src/Functional/FormTest.php \Drupal\Tests\field\Functional\FormTest::testFieldFormSingle()
  3. 10 core/modules/field/tests/src/Functional/FormTest.php \Drupal\Tests\field\Functional\FormTest::testFieldFormSingle()

File

core/modules/field/tests/src/Functional/FormTest.php, line 101

Class

FormTest
Tests field form handling.

Namespace

Drupal\Tests\field\Functional

Code

public function testFieldFormSingle() : void {
    $field_storage = $this->fieldStorageSingle;
    $field_name = $field_storage['field_name'];
    $this->field['field_name'] = $field_name;
    FieldStorageConfig::create($field_storage)->save();
    FieldConfig::create($this->field)
        ->save();
    \Drupal::service('entity_display.repository')->getFormDisplay($this->field['entity_type'], $this->field['bundle'])
        ->setComponent($field_name)
        ->save();
    // Display creation form.
    $this->drupalGet('entity_test/add');
    // Create token value expected for description.
    $token_description = Html::escape($this->config('system.site')
        ->get('name')) . '_description';
    $this->assertSession()
        ->pageTextContains($token_description);
    $this->assertSession()
        ->fieldValueEquals("{$field_name}[0][value]", '');
    // Verify that no extraneous widget is displayed.
    $this->assertSession()
        ->fieldNotExists("{$field_name}[1][value]");
    // Check that hook_field_widget_single_element_form_alter() does not believe
    // this is the default value form.
    $this->assertSession()
        ->pageTextNotContains('From hook_field_widget_single_element_form_alter(): Default form is true.');
    // Check that hook_field_widget_single_element_form_alter() does not believe
    // this is the default value form.
    $this->assertSession()
        ->pageTextNotContains('From hook_field_widget_complete_form_alter(): Default form is true.');
    // Submit with invalid value (field-level validation).
    $edit = [
        "{$field_name}[0][value]" => -1,
    ];
    $this->submitForm($edit, 'Save');
    $this->assertSession()
        ->pageTextContains("{$this->field['label']} does not accept the value -1.");
    // @todo check that the correct field is flagged for error.
    // Create an entity
    $value = mt_rand(1, 127);
    $edit = [
        "{$field_name}[0][value]" => $value,
    ];
    $this->submitForm($edit, 'Save');
    preg_match('|entity_test/manage/(\\d+)|', $this->getUrl(), $match);
    $id = $match[1];
    $this->assertSession()
        ->pageTextContains('entity_test ' . $id . ' has been created.');
    $entity = EntityTest::load($id);
    $this->assertEquals($value, $entity->{$field_name}->value, 'Field value was saved');
    // Display edit form.
    $this->drupalGet('entity_test/manage/' . $id . '/edit');
    // Check that the widget is displayed with the correct default value.
    $this->assertSession()
        ->fieldValueEquals("{$field_name}[0][value]", $value);
    // Verify that no extraneous widget is displayed.
    $this->assertSession()
        ->fieldNotExists("{$field_name}[1][value]");
    // Update the entity.
    $value = mt_rand(1, 127);
    $edit = [
        "{$field_name}[0][value]" => $value,
    ];
    $this->submitForm($edit, 'Save');
    $this->assertSession()
        ->pageTextContains('entity_test ' . $id . ' has been updated.');
    $this->container
        ->get('entity_type.manager')
        ->getStorage('entity_test')
        ->resetCache([
        $id,
    ]);
    $entity = EntityTest::load($id);
    $this->assertEquals($value, $entity->{$field_name}->value, 'Field value was updated');
    // Empty the field.
    $value = '';
    $edit = [
        "{$field_name}[0][value]" => $value,
    ];
    $this->drupalGet('entity_test/manage/' . $id . '/edit');
    $this->submitForm($edit, 'Save');
    $this->assertSession()
        ->pageTextContains('entity_test ' . $id . ' has been updated.');
    $this->container
        ->get('entity_type.manager')
        ->getStorage('entity_test')
        ->resetCache([
        $id,
    ]);
    $entity = EntityTest::load($id);
    $this->assertTrue($entity->{$field_name}
        ->isEmpty(), 'Field was emptied');
}

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