diff --git a/modules/field/modules/number/number.info b/modules/field/modules/number/number.info index 8d9756fef015814d030bd22422207cc2d1dd6ee8..9bf8243f31d96a2816221ef20dc296c878afcfa0 100644 --- a/modules/field/modules/number/number.info +++ b/modules/field/modules/number/number.info @@ -6,4 +6,5 @@ version = VERSION core = 7.x dependencies[] = field files[] = number.module +files[] = number.test required = TRUE diff --git a/modules/field/modules/number/number.module b/modules/field/modules/number/number.module index 1f6ac7f89f31276fca4c8392519fa1ae3314f297..aa792ab39366c82a171184102156199779dfdaed 100644 --- a/modules/field/modules/number/number.module +++ b/modules/field/modules/number/number.module @@ -330,8 +330,9 @@ function number_field_widget_form(&$form, &$form_state, $field, $instance, $lang '#default_value' => $value, // Allow a slightly larger size that the field length to allow for some // configurations where all characters won't fit in input field. - '#size' => $field['type'] == 'number_decimal' ? $field['settings']['precision'] + 2 : 12, - '#maxlength' => $field['type'] == 'number_decimal' ? $field['settings']['precision'] : 10, + '#size' => $field['type'] == 'number_decimal' ? $field['settings']['precision'] + 4 : 12, + // Allow two extra characters for signed values and decimal separator. + '#maxlength' => $field['type'] == 'number_decimal' ? $field['settings']['precision'] + 2 : 10, // Extract the number type from the field type name for easier validation. '#number_type' => str_replace('number_', '', $field['type']), ); diff --git a/modules/field/modules/number/number.test b/modules/field/modules/number/number.test new file mode 100644 index 0000000000000000000000000000000000000000..a15b6e6aaf2247b63ad2d15233c1f0012fe2a67e --- /dev/null +++ b/modules/field/modules/number/number.test @@ -0,0 +1,75 @@ +<?php +// $Id$ + +/** + * @file + * Tests for number field types. + */ + +/** + * Tests for number field types. + */ +class NumberFieldTestCase extends DrupalWebTestCase { + protected $field; + protected $instance; + protected $web_user; + + public static function getInfo() { + return array( + 'name' => 'Number field', + 'description' => 'Test the creation of number fields.', + 'group' => 'Field types' + ); + } + + function setUp() { + parent::setUp('field_test'); + $this->web_user = $this->drupalCreateUser(array('access field_test content', 'administer field_test content')); + $this->drupalLogin($this->web_user); + } + + /** + * Test number_decimal field. + */ + 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' => 'test_entity', + 'bundle' => 'test_bundle', + 'widget' => array( + 'type' => 'number', + ), + 'display' => array( + 'default' => array( + 'type' => 'number_decimal', + ), + ), + ); + field_create_instance($this->instance); + + // Display creation form. + $this->drupalGet('test-entity/add/test-bundle'); + $langcode = LANGUAGE_NONE; + $this->assertFieldByName("{$this->field['field_name']}[$langcode][0][value]", '', t('Widget is displayed')); + + // Submit a signed decimal value within the allowed precision and scale. + $value = '-1234.5678'; + $edit = array( + "{$this->field['field_name']}[$langcode][0][value]" => $value, + ); + $this->drupalPost(NULL, $edit, t('Save')); + preg_match('|test-entity/manage/(\d+)/edit|', $this->url, $match); + $id = $match[1]; + $this->assertRaw(t('test_entity @id has been created.', array('@id' => $id)), t('Entity was created')); + $this->assertRaw(round($value, 2), t('Value is displayed.')); + } +}