From 078d326b293a36be1e9f4e447604ad1301b9b7d1 Mon Sep 17 00:00:00 2001
From: Dave Ingram <dave@ingraminnovation.com>
Date: Fri, 11 Mar 2011 12:11:31 -0500
Subject: [PATCH] Issue #558362 by Dave.Ingram: Added test coverage for Integer and Float number fields.

---
 modules/field/modules/number/number.test |   96 +++++++++++++++++++++++++++++-
 1 files changed, 95 insertions(+), 1 deletions(-)

diff --git a/modules/field/modules/number/number.test b/modules/field/modules/number/number.test
index ec100f1..49c8665 100644
--- a/modules/field/modules/number/number.test
+++ b/modules/field/modules/number/number.test
@@ -26,6 +26,56 @@ class NumberFieldTestCase extends DrupalWebTestCase {
     $this->web_user = $this->drupalCreateUser(array('access field_test content', 'administer field_test content'));
     $this->drupalLogin($this->web_user);
   }
+  
+  /**
+   * Test number_integer field.
+   */
+  function testNumberIntegerField() {
+    // Create a field with settings to validate.
+    $this->field = array(
+      'field_name' => drupal_strtolower($this->randomName()),
+      'type' => 'number_integer',
+    );
+    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_integer',
+        ),
+      ),
+    );
+    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 valid integer.
+    $value = '123456';
+    $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.'));
+    
+    // Now submit an invalid integer and make sure we get an error message
+    $value = '1234.1';
+    $edit = array(
+      "{$this->field['field_name']}[$langcode][0][value]" => $value,
+    );
+    $this->drupalPost(NULL, $edit, t('Save'));
+    $this->assertRaw(t('Only numbers are allowed'), t('Validation Error appears as expected'));
+  }
 
   /**
    * Test number_decimal field.
@@ -71,4 +121,48 @@ class NumberFieldTestCase extends DrupalWebTestCase {
     $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.'));
   }
-}
+
+  /**
+   * Test number_float field.
+   */
+  function testNumberFloatField() {
+    // Create a field with settings to validate.
+    $this->field = array(
+      'field_name' => drupal_strtolower($this->randomName()),
+      'type' => 'number_float',
+      '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_float',
+        ),
+      ),
+    );
+    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.'));
+  }}
-- 
1.7.4.1

