You are here

public function NumberFieldTest::testNumberFloatField in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/field/tests/src/Functional/Number/NumberFieldTest.php \Drupal\Tests\field\Functional\Number\NumberFieldTest::testNumberFloatField()

Tests float field.

File

core/modules/field/tests/src/Functional/Number/NumberFieldTest.php, line 287

Class

NumberFieldTest
Tests the creation of numeric fields.

Namespace

Drupal\Tests\field\Functional\Number

Code

public function testNumberFloatField() {

  // Create a field with settings to validate.
  $field_name = mb_strtolower($this
    ->randomMachineName());
  FieldStorageConfig::create([
    'field_name' => $field_name,
    'entity_type' => 'entity_test',
    'type' => 'float',
  ])
    ->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' => 'number',
    'settings' => [
      'placeholder' => '0.00',
    ],
  ])
    ->save();
  $display_repository
    ->getViewDisplay('entity_test', 'entity_test')
    ->setComponent($field_name, [
    'type' => 'number_decimal',
  ])
    ->save();

  // Display creation form.
  $this
    ->drupalGet('entity_test/add');
  $this
    ->assertSession()
    ->fieldValueEquals("{$field_name}[0][value]", '');
  $this
    ->assertSession()
    ->responseContains('placeholder="0.00"');

  // Submit a signed decimal value within the allowed precision and scale.
  $value = '-1234.5678';
  $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.');

  // Ensure that the 'number_decimal' formatter displays the number with the
  // expected rounding.
  $this
    ->drupalGet('entity_test/' . $id);
  $this
    ->assertSession()
    ->responseContains(round($value, 2));

  // Try to create entries with more than one decimal separator; assert fail.
  $wrong_entries = [
    '3.14.159',
    '0..45469',
    '..4589',
    '6.459.52',
    '6.3..25',
  ];
  foreach ($wrong_entries as $wrong_entry) {
    $this
      ->drupalGet('entity_test/add');
    $edit = [
      "{$field_name}[0][value]" => $wrong_entry,
    ];
    $this
      ->submitForm($edit, 'Save');
    $this
      ->assertSession()
      ->pageTextContains("{$field_name} must be a number.");
  }

  // Try to create entries with minus sign not in the first position.
  $wrong_entries = [
    '3-3',
    '4-',
    '1.3-',
    '1.2-4',
    '-10-10',
  ];
  foreach ($wrong_entries as $wrong_entry) {
    $this
      ->drupalGet('entity_test/add');
    $edit = [
      "{$field_name}[0][value]" => $wrong_entry,
    ];
    $this
      ->submitForm($edit, 'Save');
    $this
      ->assertSession()
      ->pageTextContains("{$field_name} must be a number.");
  }
}