number.test

Tests for number.module.

File

drupal-7.x/modules/field/modules/number/number.test
View source
  1. <?php
  2. /**
  3. * @file
  4. * Tests for number.module.
  5. */
  6. /**
  7. * Tests for number field types.
  8. */
  9. class NumberFieldTestCase extends DrupalWebTestCase {
  10. protected $field;
  11. protected $instance;
  12. protected $web_user;
  13. public static function getInfo() {
  14. return array(
  15. 'name' => 'Number field',
  16. 'description' => 'Test the creation of number fields.',
  17. 'group' => 'Field types'
  18. );
  19. }
  20. function setUp() {
  21. parent::setUp('field_test');
  22. $this->web_user = $this->drupalCreateUser(array('access field_test content', 'administer field_test content', 'administer content types'));
  23. $this->drupalLogin($this->web_user);
  24. }
  25. /**
  26. * Test number_decimal field.
  27. */
  28. function testNumberDecimalField() {
  29. // Create a field with settings to validate.
  30. $this->field = array(
  31. 'field_name' => drupal_strtolower($this->randomName()),
  32. 'type' => 'number_decimal',
  33. 'settings' => array(
  34. 'precision' => 8, 'scale' => 4, 'decimal_separator' => '.',
  35. )
  36. );
  37. field_create_field($this->field);
  38. $this->instance = array(
  39. 'field_name' => $this->field['field_name'],
  40. 'entity_type' => 'test_entity',
  41. 'bundle' => 'test_bundle',
  42. 'widget' => array(
  43. 'type' => 'number',
  44. ),
  45. 'display' => array(
  46. 'default' => array(
  47. 'type' => 'number_decimal',
  48. ),
  49. ),
  50. );
  51. field_create_instance($this->instance);
  52. // Display creation form.
  53. $this->drupalGet('test-entity/add/test-bundle');
  54. $langcode = LANGUAGE_NONE;
  55. $this->assertFieldByName("{$this->field['field_name']}[$langcode][0][value]", '', 'Widget is displayed');
  56. // Submit a signed decimal value within the allowed precision and scale.
  57. $value = '-1234.5678';
  58. $edit = array(
  59. "{$this->field['field_name']}[$langcode][0][value]" => $value,
  60. );
  61. $this->drupalPost(NULL, $edit, t('Save'));
  62. preg_match('|test-entity/manage/(\d+)/edit|', $this->url, $match);
  63. $id = $match[1];
  64. $this->assertRaw(t('test_entity @id has been created.', array('@id' => $id)), 'Entity was created');
  65. $this->assertRaw(round($value, 2), 'Value is displayed.');
  66. // Try to create entries with more than one decimal separator; assert fail.
  67. $wrong_entries = array(
  68. '3.14.159',
  69. '0..45469',
  70. '..4589',
  71. '6.459.52',
  72. '6.3..25',
  73. );
  74. foreach ($wrong_entries as $wrong_entry) {
  75. $this->drupalGet('test-entity/add/test-bundle');
  76. $edit = array(
  77. "{$this->field['field_name']}[$langcode][0][value]" => $wrong_entry,
  78. );
  79. $this->drupalPost(NULL, $edit, t('Save'));
  80. $this->assertText(
  81. t('There should only be one decimal separator (@separator)',
  82. array('@separator' => $this->field['settings']['decimal_separator'])),
  83. 'Correctly failed to save decimal value with more than one decimal point.'
  84. );
  85. }
  86. // Try to create entries with minus sign not in the first position.
  87. $wrong_entries = array(
  88. '3-3',
  89. '4-',
  90. '1.3-',
  91. '1.2-4',
  92. '-10-10',
  93. );
  94. foreach ($wrong_entries as $wrong_entry) {
  95. $this->drupalGet('test-entity/add/test-bundle');
  96. $edit = array(
  97. "{$this->field['field_name']}[$langcode][0][value]" => $wrong_entry,
  98. );
  99. $this->drupalPost(NULL, $edit, t('Save'));
  100. $this->assertText(
  101. t('Only numbers and the decimal separator (@separator) allowed in ',
  102. array('@separator' => $this->field['settings']['decimal_separator'])),
  103. 'Correctly failed to save decimal value with minus sign in the wrong position.'
  104. );
  105. }
  106. }
  107. /**
  108. * Test number_integer field.
  109. */
  110. function testNumberIntegerField() {
  111. // Display the "Add content type" form.
  112. $this->drupalGet('admin/structure/types/add');
  113. // Add a content type.
  114. $name = $this->randomName();
  115. $type = drupal_strtolower($name);
  116. $edit = array('name' => $name, 'type' => $type);
  117. $this->drupalPost(NULL, $edit, t('Save and add fields'));
  118. // Add an integer field to the newly-created type.
  119. $label = $this->randomName();
  120. $field_name = drupal_strtolower($label);
  121. $edit = array(
  122. 'fields[_add_new_field][label]'=> $label,
  123. 'fields[_add_new_field][field_name]' => $field_name,
  124. 'fields[_add_new_field][type]' => 'number_integer',
  125. 'fields[_add_new_field][widget_type]' => 'number',
  126. );
  127. $this->drupalPost(NULL, $edit, t('Save'));
  128. // Set the formatter to "number_integer" and to "unformatted", and just
  129. // check that the settings summary does not generate warnings.
  130. $this->drupalGet("admin/structure/types/manage/$type/display");
  131. $edit = array(
  132. "fields[field_$field_name][type]" => 'number_integer',
  133. );
  134. $this->drupalPost(NULL, $edit, t('Save'));
  135. $edit = array(
  136. "fields[field_$field_name][type]" => 'number_unformatted',
  137. );
  138. $this->drupalPost(NULL, $edit, t('Save'));
  139. }
  140. }