You are here

function NumberFieldTest::testNumberFloatField in Zircon Profile 8.0

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

Test float field.

File

core/modules/field/src/Tests/Number/NumberFieldTest.php, line 276
Contains \Drupal\field\Tests\Number\NumberFieldTest.

Class

NumberFieldTest
Tests the creation of numeric fields.

Namespace

Drupal\field\Tests\Number

Code

function testNumberFloatField() {

  // Create a field with settings to validate.
  $field_name = Unicode::strtolower($this
    ->randomMachineName());
  entity_create('field_storage_config', array(
    'field_name' => $field_name,
    'entity_type' => 'entity_test',
    'type' => 'float',
  ))
    ->save();
  entity_create('field_config', array(
    'field_name' => $field_name,
    'entity_type' => 'entity_test',
    'bundle' => 'entity_test',
  ))
    ->save();
  entity_get_form_display('entity_test', 'entity_test', 'default')
    ->setComponent($field_name, array(
    'type' => 'number',
    'settings' => array(
      'placeholder' => '0.00',
    ),
  ))
    ->save();
  entity_get_display('entity_test', 'entity_test', 'default')
    ->setComponent($field_name, array(
    'type' => 'number_decimal',
  ))
    ->save();

  // Display creation form.
  $this
    ->drupalGet('entity_test/add');
  $this
    ->assertFieldByName("{$field_name}[0][value]", '', 'Widget is displayed');
  $this
    ->assertRaw('placeholder="0.00"');

  // Submit a signed decimal value within the allowed precision and scale.
  $value = '-1234.5678';
  $edit = array(
    "{$field_name}[0][value]" => $value,
  );
  $this
    ->drupalPostForm(NULL, $edit, t('Save'));
  preg_match('|entity_test/manage/(\\d+)|', $this->url, $match);
  $id = $match[1];
  $this
    ->assertText(t('entity_test @id has been created.', array(
    '@id' => $id,
  )), 'Entity was created');

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

  // Try to create entries with more than one decimal separator; assert fail.
  $wrong_entries = array(
    '3.14.159',
    '0..45469',
    '..4589',
    '6.459.52',
    '6.3..25',
  );
  foreach ($wrong_entries as $wrong_entry) {
    $this
      ->drupalGet('entity_test/add');
    $edit = array(
      "{$field_name}[0][value]" => $wrong_entry,
    );
    $this
      ->drupalPostForm(NULL, $edit, t('Save'));
    $this
      ->assertRaw(t('%name must be a number.', array(
      '%name' => $field_name,
    )), 'Correctly failed to save float value with more than one decimal point.');
  }

  // Try to create entries with minus sign not in the first position.
  $wrong_entries = array(
    '3-3',
    '4-',
    '1.3-',
    '1.2-4',
    '-10-10',
  );
  foreach ($wrong_entries as $wrong_entry) {
    $this
      ->drupalGet('entity_test/add');
    $edit = array(
      "{$field_name}[0][value]" => $wrong_entry,
    );
    $this
      ->drupalPostForm(NULL, $edit, t('Save'));
    $this
      ->assertRaw(t('%name must be a number.', array(
      '%name' => $field_name,
    )), 'Correctly failed to save float value with minus sign in the wrong position.');
  }
}