1 number_settings.test public NumberFieldSettingsTestCase::testNumberDecimalFieldSettings()

Test number_decimal field settings.

File

core/modules/field/modules/number/tests/number_settings.test, line 47
Tests for number.module settings.

Class

NumberFieldSettingsTestCase
Tests for number field type settings.

Code

public function testNumberDecimalFieldSettings() {
  // Create a field with settings to validate.
  $field_name = 'field_' . backdrop_strtolower($this->randomName());
  $precision = 7;
  $scale = 3;

  $this->field = array(
    'field_name' => $field_name,
    'type' => 'number_decimal',
    'settings' => array(
      'precision' => $precision,
      'scale' => $scale,
      'decimal_separator' => '.',
    ),
  );
  field_create_field($this->field);

  $this->instance = array(
    'field_name' => $field_name,
    'entity_type' => 'node',
    'bundle' => 'post',
    'widget' => array(
      'type' => 'number',
    ),
    'display' => array(
      'default' => array(
        'type' => 'number_decimal',
      ),
    ),
  );
  field_create_instance($this->instance);

  // Admin user sets various min/max values to a decimal field.
  // Maximum value is smaller than Minimum.
  $edit = array(
    'instance[settings][min]' => 30.4,
    'instance[settings][max]' => -2.222,
  );
  $this->backdropGet("admin/structure/types/manage/post/fields/$field_name");
  $this->backdropPost(NULL, $edit, t('Save settings'));
  $this->assertRaw(format_string('%max must be greater than, or equal to, %min.', array(
    '%max' => 'Maximum',
    '%min' => 'Minimum',
  )), 'Decimal field min setting greater than max setting rejected');

  // Maximum and Minimum exceed the field settings for precision and scale.
  $edit = array(
    'instance[settings][min]' => 678.3456,
    'instance[settings][max]' => 56789.234,
  );
  $this->backdropGet("admin/structure/types/manage/post/fields/$field_name");
  $this->backdropPost(NULL, $edit, t('Save settings'));
  $this->assertRaw(format_string('%min exceeds possible values for this field. Choose a number that conforms to precision %prec and scale %scale.', array(
    '%min' => $edit['instance[settings][min]'],
    '%prec' => $precision,
    '%scale' => $scale,
  )), 'Decimal field min setting outside db range rejected');
  $this->assertRaw(format_string('%max exceeds possible values for this field. Choose a number that conforms to precision %prec and scale %scale.', array(
    '%max' => $edit['instance[settings][max]'],
    '%prec' => $precision,
    '%scale' => $scale,
  )), 'Decimal field max setting outside db range rejected');

  // Proper values should pass.
  $edit = array(
    'instance[settings][min]' => 5678.901,
    'instance[settings][max]' => 9999,
  );
  $this->backdropGet("admin/structure/types/manage/post/fields/$field_name");
  $this->backdropPost(NULL, $edit, t('Save settings'));
  $this->assertRaw(format_string('Saved %field_name configuration.', array(
    '%field_name' => $field_name,
  )), 'Proper decimal field min/max settings saved');
}