Project

General

Profile

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

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

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($value, '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

    
156
  /**
157
   * Test number_float field.
158
   */
159
  function testNumberFloatField() {
160
    $this->field = array(
161
      'field_name' => drupal_strtolower($this->randomName()),
162
      'type' => 'number_float',
163
      'settings' => array(
164
        'precision' => 8, 'scale' => 4, 'decimal_separator' => '.',
165
      )
166
    );
167
    field_create_field($this->field);
168
    $this->instance = array(
169
      'field_name' => $this->field['field_name'],
170
      'entity_type' => 'test_entity',
171
      'bundle' => 'test_bundle',
172
      'widget' => array(
173
        'type' => 'number',
174
      ),
175
      'display' => array(
176
        'default' => array(
177
          'type' => 'number_float',
178
        ),
179
      ),
180
    );
181
    field_create_instance($this->instance);
182

    
183
    $langcode = LANGUAGE_NONE;
184
    $value = array(
185
      '9.' => '9',
186
      '.' => '0',
187
      '123.55' => '123.55',
188
      '.55' => '0.55',
189
      '-0.55' => '-0.55',
190
    );
191
    foreach($value as $key => $value) {
192
      $edit = array(
193
        "{$this->field['field_name']}[$langcode][0][value]" => $key,
194
      );
195
      $this->drupalPost('test-entity/add/test-bundle', $edit, t('Save'));
196
      $this->assertNoText("PDOException");
197
      $this->assertRaw($value, 'Correct value is displayed.');
198
    }
199
  }
200

    
201
}