Project

General

Profile

Paste
Download (4.84 KB) Statistics
| Branch: | Revision:

root / drupal7 / modules / field / modules / number / number.test @ b0dc3a2e

1
<?php
2

    
3
/**
4
 * @file
5
 * Tests for number.module.
6
 */
7

    
8
/**
9
 * Tests for number field types.
10
 */
11
class NumberFieldTestCase extends DrupalWebTestCase {
12
  protected $field;
13
  protected $instance;
14
  protected $web_user;
15

    
16
  public static function getInfo() {
17
    return array(
18
      'name'  => 'Number field',
19
      'description'  => 'Test the creation of number fields.',
20
      'group' => 'Field types'
21
    );
22
  }
23

    
24
  function setUp() {
25
    parent::setUp('field_test');
26
    $this->web_user = $this->drupalCreateUser(array('access field_test content', 'administer field_test content', 'administer content types', 'administer fields'));
27
    $this->drupalLogin($this->web_user);
28
  }
29

    
30
  /**
31
   * Test number_decimal field.
32
   */
33
  function testNumberDecimalField() {
34
    // Create a field with settings to validate.
35
    $this->field = array(
36
      'field_name' => drupal_strtolower($this->randomName()),
37
      'type' => 'number_decimal',
38
      'settings' => array(
39
        'precision' => 8, 'scale' => 4, 'decimal_separator' => '.',
40
      )
41
    );
42
    field_create_field($this->field);
43
    $this->instance = array(
44
      'field_name' => $this->field['field_name'],
45
      'entity_type' => 'test_entity',
46
      'bundle' => 'test_bundle',
47
      'widget' => array(
48
        'type' => 'number',
49
      ),
50
      'display' => array(
51
        'default' => array(
52
          'type' => 'number_decimal',
53
        ),
54
      ),
55
    );
56
    field_create_instance($this->instance);
57

    
58
    // Display creation form.
59
    $this->drupalGet('test-entity/add/test-bundle');
60
    $langcode = LANGUAGE_NONE;
61
    $this->assertFieldByName("{$this->field['field_name']}[$langcode][0][value]", '', 'Widget is displayed');
62

    
63
    // Submit a signed decimal value within the allowed precision and scale.
64
    $value = '-1234.5678';
65
    $edit = array(
66
      "{$this->field['field_name']}[$langcode][0][value]" => $value,
67
    );
68
    $this->drupalPost(NULL, $edit, t('Save'));
69
    preg_match('|test-entity/manage/(\d+)/edit|', $this->url, $match);
70
    $id = $match[1];
71
    $this->assertRaw(t('test_entity @id has been created.', array('@id' => $id)), 'Entity was created');
72
    $this->assertRaw(round($value, 2), 'Value is displayed.');
73

    
74
    // Try to create entries with more than one decimal separator; assert fail.
75
    $wrong_entries = array(
76
      '3.14.159',
77
      '0..45469',
78
      '..4589',
79
      '6.459.52',
80
      '6.3..25',
81
    );
82

    
83
    foreach ($wrong_entries as $wrong_entry) {
84
      $this->drupalGet('test-entity/add/test-bundle');
85
      $edit = array(
86
        "{$this->field['field_name']}[$langcode][0][value]" => $wrong_entry,
87
      );
88
      $this->drupalPost(NULL, $edit, t('Save'));
89
      $this->assertText(
90
        t('There should only be one decimal separator (@separator)',
91
          array('@separator' => $this->field['settings']['decimal_separator'])),
92
        'Correctly failed to save decimal value with more than one decimal point.'
93
      );
94
    }
95

    
96
    // Try to create entries with minus sign not in the first position.
97
    $wrong_entries = array(
98
      '3-3',
99
      '4-',
100
      '1.3-',
101
      '1.2-4',
102
      '-10-10',
103
    );
104

    
105
    foreach ($wrong_entries as $wrong_entry) {
106
      $this->drupalGet('test-entity/add/test-bundle');
107
      $edit = array(
108
        "{$this->field['field_name']}[$langcode][0][value]" => $wrong_entry,
109
      );
110
      $this->drupalPost(NULL, $edit, t('Save'));
111
      $this->assertText(
112
        t('Only numbers and the decimal separator (@separator) allowed in ',
113
          array('@separator' => $this->field['settings']['decimal_separator'])),
114
        'Correctly failed to save decimal value with minus sign in the wrong position.'
115
      );
116
    }
117
  }
118

    
119
  /**
120
   * Test number_integer field.
121
   */
122
  function testNumberIntegerField() {
123
    // Display the "Add content type" form.
124
    $this->drupalGet('admin/structure/types/add');
125

    
126
    // Add a content type.
127
    $name = $this->randomName();
128
    $type = drupal_strtolower($name);
129
    $edit = array('name' => $name, 'type' => $type);
130
    $this->drupalPost(NULL, $edit, t('Save and add fields'));
131

    
132
    // Add an integer field to the newly-created type.
133
    $label = $this->randomName();
134
    $field_name = drupal_strtolower($label);
135
    $edit = array(
136
      'fields[_add_new_field][label]'=> $label,
137
      'fields[_add_new_field][field_name]' => $field_name,
138
      'fields[_add_new_field][type]' => 'number_integer',
139
      'fields[_add_new_field][widget_type]' => 'number',
140
    );
141
    $this->drupalPost(NULL, $edit, t('Save'));
142

    
143
    // Set the formatter to "number_integer" and to "unformatted", and just
144
    // check that the settings summary does not generate warnings.
145
    $this->drupalGet("admin/structure/types/manage/$type/display");
146
    $edit = array(
147
      "fields[field_$field_name][type]" => 'number_integer',
148
    );
149
    $this->drupalPost(NULL, $edit, t('Save'));
150
    $edit = array(
151
      "fields[field_$field_name][type]" => 'number_unformatted',
152
    );
153
    $this->drupalPost(NULL, $edit, t('Save'));
154
  }
155
}