function NumberFieldTest::testNumberDecimalField

Test number_decimal field.

File

drupal/core/modules/number/lib/Drupal/number/Tests/NumberFieldTest.php, line 47
Definition of Drupal\number\NumberFieldTest.

Class

NumberFieldTest
Tests for number field types.

Namespace

Drupal\number\Tests

Code

function testNumberDecimalField() {

  // Create a field with settings to validate.
  $this->field = array(
    'field_name' => drupal_strtolower($this
      ->randomName()),
    'type' => 'number_decimal',
    'settings' => array(
      'precision' => 8,
      'scale' => 4,
      'decimal_separator' => '.',
    ),
  );
  field_create_field($this->field);
  $this->instance = array(
    'field_name' => $this->field['field_name'],
    'entity_type' => 'entity_test',
    'bundle' => 'entity_test',
  );
  field_create_instance($this->instance);
  entity_get_form_display('entity_test', 'entity_test', 'default')
    ->setComponent($this->field['field_name'], array(
    'type' => 'number',
    'settings' => array(
      'placeholder' => '0.00',
    ),
  ))
    ->save();
  entity_get_display('entity_test', 'entity_test', 'default')
    ->setComponent($this->field['field_name'], array(
    'type' => 'number_decimal',
  ))
    ->save();

  // Display creation form.
  $this
    ->drupalGet('entity_test/add');
  $langcode = Language::LANGCODE_NOT_SPECIFIED;
  $this
    ->assertFieldByName("{$this->field['field_name']}[{$langcode}][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(
    'user_id' => 1,
    'name' => $this
      ->randomName(),
    "{$this->field['field_name']}[{$langcode}][0][value]" => $value,
  );
  $this
    ->drupalPost(NULL, $edit, t('Save'));
  preg_match('|entity_test/manage/(\\d+)/edit|', $this->url, $match);
  $id = $match[1];
  $this
    ->assertText(t('entity_test @id has been created.', array(
    '@id' => $id,
  )), 'Entity was created');
  $this
    ->assertRaw(round($value, 2), 'Value is displayed.');

  // 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(
      "{$this->field['field_name']}[{$langcode}][0][value]" => $wrong_entry,
    );
    $this
      ->drupalPost(NULL, $edit, t('Save'));
    $this
      ->assertRaw(t('%name must be a number.', array(
      '%name' => $this->field['field_name'],
    )), 'Correctly failed to save decimal 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(
      "{$this->field['field_name']}[{$langcode}][0][value]" => $wrong_entry,
    );
    $this
      ->drupalPost(NULL, $edit, t('Save'));
    $this
      ->assertRaw(t('%name must be a number.', array(
      '%name' => $this->field['field_name'],
    )), 'Correctly failed to save decimal value with minus sign in the wrong position.');
  }
}