public function FractionFieldTest::testFractionWidgetDecimalEditNull in Fraction 8
Same name and namespace in other branches
- 2.x tests/src/Functional/FractionFieldTest.php \Drupal\Tests\fraction\Functional\FractionFieldTest::testFractionWidgetDecimalEditNull()
Tests that editing an empty fraction doesn't show a 0.
See also
https://www.drupal.org/project/fraction/issues/3096236
File
- tests/
src/ Functional/ FractionFieldTest.php, line 337
Class
- FractionFieldTest
- Tests the creation of fraction fields.
Namespace
Drupal\Tests\fraction\FunctionalCode
public function testFractionWidgetDecimalEditNull() {
// Create a field with settings to validate.
$field_name = mb_strtolower($this
->randomMachineName());
FieldStorageConfig::create([
'field_name' => $field_name,
'entity_type' => 'entity_test',
'type' => 'fraction',
])
->save();
FieldConfig::create([
'field_name' => $field_name,
'entity_type' => 'entity_test',
'bundle' => 'entity_test',
])
->save();
/** @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface $display_repository */
$display_repository = \Drupal::service('entity_display.repository');
$display_repository
->getFormDisplay('entity_test', 'entity_test')
->setComponent($field_name, [
'type' => 'fraction_decimal',
])
->save();
$display_repository
->getViewDisplay('entity_test', 'entity_test')
->setComponent($field_name, [
'type' => 'fraction_decimal',
])
->save();
// Display creation form.
$this
->drupalGet('entity_test/add');
$this
->assertFieldByName("{$field_name}[0][decimal]", '', 'Widget is displayed');
// Submit decimal value.
$value = '1.234';
$edit = [
"{$field_name}[0][decimal]" => $value,
];
$this
->drupalPostForm(NULL, $edit, $this
->t('Save'));
preg_match('|entity_test/manage/(\\d+)|', $this
->getUrl(), $match);
$id = $match[1];
$this
->assertText($this
->t('entity_test @id has been created.', [
'@id' => $id,
]), 'Entity was created');
$this
->assertRaw($value, 'Value is displayed.');
// Empty the field.
$edit = [
"{$field_name}[0][decimal]" => NULL,
];
$this
->drupalPostForm("entity_test/manage/{$id}/edit", $edit, $this
->t('Save'));
$this
->assertNoRaw($value, 'Value is removed.');
// The field should have no value (not 0, just empty).
$this
->drupalGet("entity_test/manage/{$id}/edit");
$elements = $this
->xpath($this
->constructFieldXpath('name', "{$field_name}[0][decimal]"));
$element = reset($elements);
$this
->assertIdentical($element
->getValue(), '', 'Field is empty');
}