From 311fe849a1d82844f2b2d3c64249beda63e55a09 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Claes=20Gyllensv=C3=A4rd?= <letharion@gmail.com>
Date: Fri, 24 Aug 2012 13:37:09 +0200
Subject: [PATCH] Issue #558362 by mtift, letharion, dabl: Add test coverage
 for Field API Number

---
 .../lib/Drupal/number/Tests/NumberFieldTest.php    |  107 +++++++++++++++++++-
 1 file changed, 103 insertions(+), 4 deletions(-)

diff --git a/core/modules/field/modules/number/lib/Drupal/number/Tests/NumberFieldTest.php b/core/modules/field/modules/number/lib/Drupal/number/Tests/NumberFieldTest.php
index 64aabe3..399a780 100644
--- a/core/modules/field/modules/number/lib/Drupal/number/Tests/NumberFieldTest.php
+++ b/core/modules/field/modules/number/lib/Drupal/number/Tests/NumberFieldTest.php
@@ -41,6 +41,57 @@ class NumberFieldTest extends WebTestBase {
   }
 
   /**
+   * 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;
+    $field_name = $this->field['field_name'];
+    $this->assertFieldByName("{$field_name}[$langcode][0][value]", '', t('Widget is displayed'));
+
+    // Submit a valid integer.
+    $value = '123456';
+    $edit = array(
+      "{$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(
+      "{$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.
    */
   function testNumberDecimalField() {
@@ -49,7 +100,9 @@ class NumberFieldTest extends WebTestBase {
       'field_name' => drupal_strtolower($this->randomName()),
       'type' => 'number_decimal',
       'settings' => array(
-        'precision' => 8, 'scale' => 4, 'decimal_separator' => '.',
+        'precision' => 8,
+        'scale' => 4,
+        'decimal_separator' => '.',
       )
     );
     field_create_field($this->field);
@@ -71,12 +124,13 @@ class NumberFieldTest extends WebTestBase {
     // Display creation form.
     $this->drupalGet('test-entity/add/test_bundle');
     $langcode = LANGUAGE_NOT_SPECIFIED;
-    $this->assertFieldByName("{$this->field['field_name']}[$langcode][0][value]", '', t('Widget is displayed'));
+    $field_name = $this->field['field_name'];
+    $this->assertFieldByName("{$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,
+      "{$field_name}[$langcode][0][value]" => $value,
     );
     $this->drupalPost(NULL, $edit, t('Save'));
     preg_match('|test-entity/manage/(\d+)/edit|', $this->url, $match);
@@ -157,4 +211,49 @@ class NumberFieldTest extends WebTestBase {
     );
     $this->drupalPost(NULL, $edit, t('Save'));
   }
-}
+
+  /**
+   * 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;
+    $field_name = $this->field['field_name'];
+    $this->assertFieldByName("{$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(
+      "{$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.9.5

