You are here

public function FormTest::testHiddenField in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/field/tests/src/Functional/FormTest.php \Drupal\Tests\field\Functional\FormTest::testHiddenField()
  2. 10 core/modules/field/tests/src/Functional/FormTest.php \Drupal\Tests\field\Functional\FormTest::testHiddenField()

Tests hiding a field in a form.

File

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

Class

FormTest
Tests field form handling.

Namespace

Drupal\Tests\field\Functional

Code

public function testHiddenField() {
  $entity_type = 'entity_test_rev';
  $field_storage = $this->fieldStorageSingle;
  $field_storage['entity_type'] = $entity_type;
  $field_name = $field_storage['field_name'];
  $this->field['field_name'] = $field_name;
  $this->field['default_value'] = [
    0 => [
      'value' => 99,
    ],
  ];
  $this->field['entity_type'] = $entity_type;
  $this->field['bundle'] = $entity_type;
  FieldStorageConfig::create($field_storage)
    ->save();
  $this->field = FieldConfig::create($this->field);
  $this->field
    ->save();

  // We explicitly do not assign a widget in a form display, so the field
  // stays hidden in forms.
  // Display the entity creation form.
  $this
    ->drupalGet($entity_type . '/add');

  // Create an entity and test that the default value is assigned correctly to
  // the field that uses the hidden widget.
  $this
    ->assertSession()
    ->fieldNotExists("{$field_name}[0][value]");
  $this
    ->submitForm([], 'Save');
  preg_match('|' . $entity_type . '/manage/(\\d+)|', $this
    ->getUrl(), $match);
  $id = $match[1];
  $this
    ->assertSession()
    ->pageTextContains('entity_test_rev ' . $id . ' has been created.');
  $storage = $this->container
    ->get('entity_type.manager')
    ->getStorage($entity_type);
  $entity = $storage
    ->load($id);
  $this
    ->assertEquals(99, $entity->{$field_name}->value, 'Default value was saved');

  // Update the field to remove the default value, and switch to the default
  // widget.
  $this->field
    ->setDefaultValue([]);
  $this->field
    ->save();
  \Drupal::service('entity_display.repository')
    ->getFormDisplay($entity_type, $this->field
    ->getTargetBundle())
    ->setComponent($this->field
    ->getName(), [
    'type' => 'test_field_widget',
  ])
    ->save();

  // Display edit form.
  $this
    ->drupalGet($entity_type . '/manage/' . $id . '/edit');
  $this
    ->assertSession()
    ->fieldValueEquals("{$field_name}[0][value]", 99);

  // Update the entity.
  $value = mt_rand(1, 127);
  $edit = [
    "{$field_name}[0][value]" => $value,
  ];
  $this
    ->submitForm($edit, 'Save');
  $this
    ->assertSession()
    ->pageTextContains('entity_test_rev ' . $id . ' has been updated.');
  $storage
    ->resetCache([
    $id,
  ]);
  $entity = $storage
    ->load($id);
  $this
    ->assertEquals($value, $entity->{$field_name}->value, 'Field value was updated');

  // Set the field back to hidden.
  \Drupal::service('entity_display.repository')
    ->getFormDisplay($entity_type, $this->field
    ->getTargetBundle())
    ->removeComponent($this->field
    ->getName())
    ->save();

  // Create a new revision.
  $edit = [
    'revision' => TRUE,
  ];
  $this
    ->drupalGet($entity_type . '/manage/' . $id . '/edit');
  $this
    ->submitForm($edit, 'Save');

  // Check that the expected value has been carried over to the new revision.
  $storage
    ->resetCache([
    $id,
  ]);
  $entity = $storage
    ->load($id);
  $this
    ->assertEquals($value, $entity->{$field_name}->value, 'New revision has the expected value for the field with the Hidden widget');
}