- <?php
- * @file
- * Tests for number.module.
- */
-
- require_once BACKDROP_ROOT . '/core/modules/field/tests/field.test';
-
- * Tests for number field types.
- */
- class NumberFieldTestCase extends BackdropWebTestCase {
- protected $field;
- protected $instance;
- protected $web_user;
-
- function setUp() {
- parent::setUp('field_test');
- $this->web_user = $this->backdropCreateUser(array('access field_test content', 'administer field_test content', 'administer content types', 'administer fields'));
- $this->backdropLogin($this->web_user);
- }
-
-
- * Test number_decimal field.
- */
- function testNumberDecimalField() {
-
- $this->field = array(
- 'field_name' => backdrop_strtolower($this->randomName()),
- 'type' => 'number_decimal',
- '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_decimal',
- ),
- ),
- );
- field_create_instance($this->instance);
-
-
- $this->backdropGet('test-entity/add/test-bundle');
- $langcode = LANGUAGE_NONE;
- $this->assertFieldByName("{$this->field['field_name']}[$langcode][0][value]", '', 'Widget is displayed');
-
-
- $value = '-1234.5678';
- $edit = array(
- "{$this->field['field_name']}[$langcode][0][value]" => $value,
- );
- $this->backdropPost(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)), 'Entity was created');
- $this->assertRaw($value, 'Value is displayed.');
-
-
- $wrong_entries = array(
- '3.14.159',
- '0..45469',
- '..4589',
- '6.459.52',
- '6.3..25',
- );
-
- foreach ($wrong_entries as $wrong_entry) {
- $this->backdropGet('test-entity/add/test-bundle');
- $edit = array(
- "{$this->field['field_name']}[$langcode][0][value]" => $wrong_entry,
- );
- $this->backdropPost(NULL, $edit, t('Save'));
- $this->assertRaw(t('%name must be a number.', array('%name' => $this->field['field_name'])), 'Correctly failed to save decimal value with more than one decimal point.');
- }
-
-
- $wrong_entries = array(
- '3-3',
- '4-',
- '1.3-',
- '1.2-4',
- '-10-10',
- );
-
- foreach ($wrong_entries as $wrong_entry) {
- $this->backdropGet('test-entity/add/test-bundle');
- $edit = array(
- "{$this->field['field_name']}[$langcode][0][value]" => $wrong_entry,
- );
- $this->backdropPost(NULL, $edit, t('Save'));
- $this->assertRaw(t('%name must be a number.', array('%name' => $this->field['field_name'])), 'Correctly failed to save decimal value with minus sign in the wrong position.');
- }
- }
-
-
- * Test number_decimal field step validation.
- */
- public function testNumberDecimalFieldStepValidation() {
-
-
- $this->field = array(
- 'field_name' => backdrop_strtolower($this->randomName()),
- 'type' => 'number_decimal',
- 'settings' => array(
- 'precision' => 14,
- 'scale' => 10,
- '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_decimal',
- ),
- ),
- );
- field_create_instance($this->instance);
-
-
-
-
- $valid_values = array(
- 20.123456789,
- 9999.1933172003,
- 219.200005,
- 199.200001,
- );
- foreach ($valid_values as $index => $decimal) {
- $langcode = LANGUAGE_NONE;
- $this->backdropGet('test-entity/add/test-bundle');
- $edit = array(
- "{$this->field['field_name']}[$langcode][0][value]" => $decimal,
- );
- $this->backdropPost(NULL, $edit, t('Save'));
- $this->assertNoRaw(format_string('%name is not a valid number.', array('%name' => $this->field['field_name'])));
- }
- }
-
-
- * Test number_integer field.
- */
- function testNumberIntegerField() {
-
- $this->backdropGet('admin/structure/types/add');
-
-
- $name = $this->randomName();
- $type = backdrop_strtolower($name);
- $edit = array('name' => $name, 'type' => $type);
- $this->backdropPost(NULL, $edit, t('Save and add fields'));
-
-
- $label = $this->randomName();
- $field_name = backdrop_strtolower($label);
- $edit = array(
- 'fields[_add_new_field][label]'=> $label,
- 'fields[_add_new_field][field_name]' => $field_name,
- 'fields[_add_new_field][type]' => 'number_integer',
- 'fields[_add_new_field][widget_type]' => 'number',
- );
- $this->backdropPost(NULL, $edit, t('Save'));
-
-
-
- $this->backdropGet("admin/structure/types/manage/$type/display/default");
- $edit = array(
- "fields[field_$field_name][type]" => 'number_integer',
- );
- $this->backdropPost(NULL, $edit, t('Save'));
- $edit = array(
- "fields[field_$field_name][type]" => 'number_unformatted',
- );
- $this->backdropPost(NULL, $edit, t('Save'));
- }
-
-
- * Test number_float field.
- */
- function testNumberFloatField() {
- $this->field = array(
- 'field_name' => backdrop_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);
-
- $langcode = LANGUAGE_NONE;
- $value = array(
- '9.' => '9',
- '.' => '0',
- '123.55' => '123.55',
- '.55' => '0.55',
- '-0.55' => '-0.55',
- );
- foreach($value as $key => $value) {
- $edit = array(
- "{$this->field['field_name']}[$langcode][0][value]" => $key,
- );
- $this->backdropPost('test-entity/add/test-bundle', $edit, t('Save'));
- $this->assertNoText("PDOException");
- $this->assertRaw($value, 'Correct value is displayed.');
- }
- }
-
-
- * Test empty value submission.
- */
- function testNumberFieldEmpty() {
-
- $this->backdropGet('admin/structure/types/add');
- $name = $this->randomName();
- $bundle = backdrop_strtolower($name);
- $edit = array('name' => $name, 'type' => $bundle);
- $this->backdropPost(NULL, $edit, t('Save and add fields'));
-
-
- $label = $this->randomName();
- $field_name = backdrop_strtolower($label);
- $edit = array(
- 'fields[_add_new_field][label]' => $label,
- 'fields[_add_new_field][field_name]' => $field_name,
- 'fields[_add_new_field][type]' => 'number_decimal',
- 'fields[_add_new_field][widget_type]' => 'number',
- );
- $this->backdropPost(NULL, $edit, t('Save'));
-
-
- $path = "admin/structure/types/manage/$bundle/fields/field_{$field_name}";
- $this->backdropGet($path);
- $langcode = LANGUAGE_NONE;
- $edit = array(
- "field_{$field_name}[$langcode][0][value]" => '',
- );
- $this->backdropPost(NULL, $edit, t('Save settings'));
-
- $config = config("field.instance.node.{$bundle}.field_{$field_name}");
- $this->assertEqual($config->get('default_value'), NULL, 'Default value of number field is NULL');
- $node_user = $this->backdropCreateUser(array('bypass node access'));
- $this->backdropLogin($node_user);
-
- $edit['title'] = $this->randomName();
- $this->backdropGet("node/add/{$bundle}");
- $this->backdropPost(NULL, $edit, t('Save'));
- $this->assertResponse(200, 'Post to create new node with empty number value returned 200 OK');
- }
-
- }