public function FormTest::testHiddenField in Drupal 8
Same name and namespace in other branches
- 9 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 572
Class
- FormTest
- Tests field form handling.
Namespace
Drupal\Tests\field\FunctionalCode
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
->assertNoField("{$field_name}[0][value]", 'The field does not appear in the form');
$this
->drupalPostForm(NULL, [], t('Save'));
preg_match('|' . $entity_type . '/manage/(\\d+)|', $this
->getUrl(), $match);
$id = $match[1];
$this
->assertText(t('entity_test_rev @id has been created.', [
'@id' => $id,
]), 'Entity was created');
$storage = $this->container
->get('entity_type.manager')
->getStorage($entity_type);
$entity = $storage
->load($id);
$this
->assertEqual($entity->{$field_name}->value, 99, '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
->assertFieldByName("{$field_name}[0][value]", 99, 'Widget is displayed with the correct default value');
// Update the entity.
$value = mt_rand(1, 127);
$edit = [
"{$field_name}[0][value]" => $value,
];
$this
->drupalPostForm(NULL, $edit, t('Save'));
$this
->assertText(t('entity_test_rev @id has been updated.', [
'@id' => $id,
]), 'Entity was updated');
$storage
->resetCache([
$id,
]);
$entity = $storage
->load($id);
$this
->assertEqual($entity->{$field_name}->value, $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
->drupalPostForm($entity_type . '/manage/' . $id . '/edit', $edit, t('Save'));
// Check that the expected value has been carried over to the new revision.
$storage
->resetCache([
$id,
]);
$entity = $storage
->load($id);
$this
->assertEqual($entity->{$field_name}->value, $value, 'New revision has the expected value for the field with the Hidden widget');
}