Chris@0: drupalLogin($this->drupalCreateUser([ Chris@0: 'view test entity', Chris@0: 'administer entity_test content', Chris@0: 'administer content types', Chris@0: 'administer node fields', Chris@0: 'administer node display', Chris@0: 'bypass node access', Chris@0: 'administer entity_test fields', Chris@0: ])); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Test decimal field. Chris@0: */ Chris@0: public function testNumberDecimalField() { Chris@0: // Create a field with settings to validate. Chris@0: $field_name = Unicode::strtolower($this->randomMachineName()); Chris@0: FieldStorageConfig::create([ Chris@0: 'field_name' => $field_name, Chris@0: 'entity_type' => 'entity_test', Chris@0: 'type' => 'decimal', Chris@0: 'settings' => ['precision' => 8, 'scale' => 4], Chris@0: ])->save(); Chris@0: FieldConfig::create([ Chris@0: 'field_name' => $field_name, Chris@0: 'entity_type' => 'entity_test', Chris@0: 'bundle' => 'entity_test', Chris@0: ])->save(); Chris@0: Chris@0: entity_get_form_display('entity_test', 'entity_test', 'default') Chris@0: ->setComponent($field_name, [ Chris@0: 'type' => 'number', Chris@0: 'settings' => [ Chris@0: 'placeholder' => '0.00' Chris@0: ], Chris@0: ]) Chris@0: ->save(); Chris@0: entity_get_display('entity_test', 'entity_test', 'default') Chris@0: ->setComponent($field_name, [ Chris@0: 'type' => 'number_decimal', Chris@0: ]) Chris@0: ->save(); Chris@0: Chris@0: // Display creation form. Chris@0: $this->drupalGet('entity_test/add'); Chris@0: $this->assertFieldByName("{$field_name}[0][value]", '', 'Widget is displayed'); Chris@0: $this->assertRaw('placeholder="0.00"'); Chris@0: Chris@0: // Submit a signed decimal value within the allowed precision and scale. Chris@0: $value = '-1234.5678'; Chris@0: $edit = [ Chris@0: "{$field_name}[0][value]" => $value, Chris@0: ]; Chris@0: $this->drupalPostForm(NULL, $edit, t('Save')); Chris@0: preg_match('|entity_test/manage/(\d+)|', $this->url, $match); Chris@0: $id = $match[1]; Chris@0: $this->assertText(t('entity_test @id has been created.', ['@id' => $id]), 'Entity was created'); Chris@0: $this->assertRaw($value, 'Value is displayed.'); Chris@0: Chris@0: // Try to create entries with more than one decimal separator; assert fail. Chris@0: $wrong_entries = [ Chris@0: '3.14.159', Chris@0: '0..45469', Chris@0: '..4589', Chris@0: '6.459.52', Chris@0: '6.3..25', Chris@0: ]; Chris@0: Chris@0: foreach ($wrong_entries as $wrong_entry) { Chris@0: $this->drupalGet('entity_test/add'); Chris@0: $edit = [ Chris@0: "{$field_name}[0][value]" => $wrong_entry, Chris@0: ]; Chris@0: $this->drupalPostForm(NULL, $edit, t('Save')); Chris@0: $this->assertRaw(t('%name must be a number.', ['%name' => $field_name]), 'Correctly failed to save decimal value with more than one decimal point.'); Chris@0: } Chris@0: Chris@0: // Try to create entries with minus sign not in the first position. Chris@0: $wrong_entries = [ Chris@0: '3-3', Chris@0: '4-', Chris@0: '1.3-', Chris@0: '1.2-4', Chris@0: '-10-10', Chris@0: ]; Chris@0: Chris@0: foreach ($wrong_entries as $wrong_entry) { Chris@0: $this->drupalGet('entity_test/add'); Chris@0: $edit = [ Chris@0: "{$field_name}[0][value]" => $wrong_entry, Chris@0: ]; Chris@0: $this->drupalPostForm(NULL, $edit, t('Save')); Chris@0: $this->assertRaw(t('%name must be a number.', ['%name' => $field_name]), 'Correctly failed to save decimal value with minus sign in the wrong position.'); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Test integer field. Chris@0: */ Chris@0: public function testNumberIntegerField() { Chris@0: $minimum = rand(-4000, -2000); Chris@0: $maximum = rand(2000, 4000); Chris@0: Chris@0: // Create a field with settings to validate. Chris@0: $field_name = Unicode::strtolower($this->randomMachineName()); Chris@0: $storage = FieldStorageConfig::create([ Chris@0: 'field_name' => $field_name, Chris@0: 'entity_type' => 'entity_test', Chris@0: 'type' => 'integer', Chris@0: ]); Chris@0: $storage->save(); Chris@0: Chris@0: FieldConfig::create([ Chris@0: 'field_name' => $field_name, Chris@0: 'entity_type' => 'entity_test', Chris@0: 'bundle' => 'entity_test', Chris@0: 'settings' => [ Chris@0: 'min' => $minimum, Chris@0: 'max' => $maximum, Chris@0: 'prefix' => 'ThePrefix', Chris@0: ], Chris@0: ])->save(); Chris@0: Chris@0: entity_get_form_display('entity_test', 'entity_test', 'default') Chris@0: ->setComponent($field_name, [ Chris@0: 'type' => 'number', Chris@0: 'settings' => [ Chris@0: 'placeholder' => '4' Chris@0: ], Chris@0: ]) Chris@0: ->save(); Chris@0: entity_get_display('entity_test', 'entity_test', 'default') Chris@0: ->setComponent($field_name, [ Chris@0: 'type' => 'number_integer', Chris@0: 'settings' => [ Chris@0: 'prefix_suffix' => FALSE, Chris@0: ], Chris@0: ]) Chris@0: ->save(); Chris@0: Chris@0: // Check the storage schema. Chris@0: $expected = [ Chris@0: 'columns' => [ Chris@0: 'value' => [ Chris@0: 'type' => 'int', Chris@0: 'unsigned' => '', Chris@0: 'size' => 'normal' Chris@0: ], Chris@0: ], Chris@0: 'unique keys' => [], Chris@0: 'indexes' => [], Chris@0: 'foreign keys' => [] Chris@0: ]; Chris@0: $this->assertEqual($storage->getSchema(), $expected); Chris@0: Chris@0: // Display creation form. Chris@0: $this->drupalGet('entity_test/add'); Chris@0: $this->assertFieldByName("{$field_name}[0][value]", '', 'Widget is displayed'); Chris@0: $this->assertRaw('placeholder="4"'); Chris@0: Chris@0: // Submit a valid integer Chris@0: $value = rand($minimum, $maximum); Chris@0: $edit = [ Chris@0: "{$field_name}[0][value]" => $value, Chris@0: ]; Chris@0: $this->drupalPostForm(NULL, $edit, t('Save')); Chris@0: preg_match('|entity_test/manage/(\d+)|', $this->url, $match); Chris@0: $id = $match[1]; Chris@0: $this->assertText(t('entity_test @id has been created.', ['@id' => $id]), 'Entity was created'); Chris@0: Chris@0: // Try to set a value below the minimum value Chris@0: $this->drupalGet('entity_test/add'); Chris@0: $edit = [ Chris@0: "{$field_name}[0][value]" => $minimum - 1, Chris@0: ]; Chris@0: $this->drupalPostForm(NULL, $edit, t('Save')); Chris@0: $this->assertRaw(t('%name must be higher than or equal to %minimum.', ['%name' => $field_name, '%minimum' => $minimum]), 'Correctly failed to save integer value less than minimum allowed value.'); Chris@0: Chris@0: // Try to set a decimal value Chris@0: $this->drupalGet('entity_test/add'); Chris@0: $edit = [ Chris@0: "{$field_name}[0][value]" => 1.5, Chris@0: ]; Chris@0: $this->drupalPostForm(NULL, $edit, t('Save')); Chris@0: $this->assertRaw(t('%name is not a valid number.', ['%name' => $field_name]), 'Correctly failed to save decimal value to integer field.'); Chris@0: Chris@0: // Try to set a value above the maximum value Chris@0: $this->drupalGet('entity_test/add'); Chris@0: $edit = [ Chris@0: "{$field_name}[0][value]" => $maximum + 1, Chris@0: ]; Chris@0: $this->drupalPostForm(NULL, $edit, t('Save')); Chris@0: $this->assertRaw(t('%name must be lower than or equal to %maximum.', ['%name' => $field_name, '%maximum' => $maximum]), 'Correctly failed to save integer value greater than maximum allowed value.'); Chris@0: Chris@0: // Try to set a wrong integer value. Chris@0: $this->drupalGet('entity_test/add'); Chris@0: $edit = [ Chris@0: "{$field_name}[0][value]" => '20-40', Chris@0: ]; Chris@0: $this->drupalPostForm(NULL, $edit, t('Save')); Chris@0: $this->assertRaw(t('%name must be a number.', ['%name' => $field_name]), 'Correctly failed to save wrong integer value.'); Chris@0: Chris@0: // Test with valid entries. Chris@0: $valid_entries = [ Chris@0: '-1234', Chris@0: '0', Chris@0: '1234', Chris@0: ]; Chris@0: Chris@0: foreach ($valid_entries as $valid_entry) { Chris@0: $this->drupalGet('entity_test/add'); Chris@0: $edit = [ Chris@0: "{$field_name}[0][value]" => $valid_entry, Chris@0: ]; Chris@0: $this->drupalPostForm(NULL, $edit, t('Save')); Chris@0: preg_match('|entity_test/manage/(\d+)|', $this->url, $match); Chris@0: $id = $match[1]; Chris@0: $this->assertText(t('entity_test @id has been created.', ['@id' => $id]), 'Entity was created'); Chris@0: $this->assertRaw($valid_entry, 'Value is displayed.'); Chris@0: $this->assertNoFieldByXpath('//div[@content="' . $valid_entry . '"]', NULL, 'The "content" attribute is not present since the Prefix is not being displayed'); Chris@0: } Chris@0: Chris@0: // Test for the content attribute when a Prefix is displayed. Presumably this also tests for the attribute when a Suffix is displayed. Chris@0: entity_get_display('entity_test', 'entity_test', 'default') Chris@0: ->setComponent($field_name, [ Chris@0: 'type' => 'number_integer', Chris@0: 'settings' => [ Chris@0: 'prefix_suffix' => TRUE, Chris@0: ], Chris@0: ]) Chris@0: ->save(); Chris@0: $integer_value = '123'; Chris@0: $this->drupalGet('entity_test/add'); Chris@0: $edit = [ Chris@0: "{$field_name}[0][value]" => $integer_value, Chris@0: ]; Chris@0: $this->drupalPostForm(NULL, $edit, t('Save')); Chris@0: preg_match('|entity_test/manage/(\d+)|', $this->url, $match); Chris@0: $id = $match[1]; Chris@0: $this->assertText(t('entity_test @id has been created.', ['@id' => $id]), 'Entity was created'); Chris@0: $this->drupalGet('entity_test/' . $id); Chris@0: $this->assertFieldByXPath('//div[@content="' . $integer_value . '"]', 'ThePrefix' . $integer_value, 'The "content" attribute has been set to the value of the field, and the prefix is being displayed.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Test float field. Chris@0: */ Chris@0: public function testNumberFloatField() { Chris@0: // Create a field with settings to validate. Chris@0: $field_name = Unicode::strtolower($this->randomMachineName()); Chris@0: FieldStorageConfig::create([ Chris@0: 'field_name' => $field_name, Chris@0: 'entity_type' => 'entity_test', Chris@0: 'type' => 'float', Chris@0: ])->save(); Chris@0: Chris@0: FieldConfig::create([ Chris@0: 'field_name' => $field_name, Chris@0: 'entity_type' => 'entity_test', Chris@0: 'bundle' => 'entity_test', Chris@0: ])->save(); Chris@0: Chris@0: entity_get_form_display('entity_test', 'entity_test', 'default') Chris@0: ->setComponent($field_name, [ Chris@0: 'type' => 'number', Chris@0: 'settings' => [ Chris@0: 'placeholder' => '0.00' Chris@0: ], Chris@0: ]) Chris@0: ->save(); Chris@0: Chris@0: entity_get_display('entity_test', 'entity_test', 'default') Chris@0: ->setComponent($field_name, [ Chris@0: 'type' => 'number_decimal', Chris@0: ]) Chris@0: ->save(); Chris@0: Chris@0: // Display creation form. Chris@0: $this->drupalGet('entity_test/add'); Chris@0: $this->assertFieldByName("{$field_name}[0][value]", '', 'Widget is displayed'); Chris@0: $this->assertRaw('placeholder="0.00"'); Chris@0: Chris@0: // Submit a signed decimal value within the allowed precision and scale. Chris@0: $value = '-1234.5678'; Chris@0: $edit = [ Chris@0: "{$field_name}[0][value]" => $value, Chris@0: ]; Chris@0: $this->drupalPostForm(NULL, $edit, t('Save')); Chris@0: preg_match('|entity_test/manage/(\d+)|', $this->url, $match); Chris@0: $id = $match[1]; Chris@0: $this->assertText(t('entity_test @id has been created.', ['@id' => $id]), 'Entity was created'); Chris@0: Chris@0: // Ensure that the 'number_decimal' formatter displays the number with the Chris@0: // expected rounding. Chris@0: $this->drupalGet('entity_test/' . $id); Chris@0: $this->assertRaw(round($value, 2)); Chris@0: Chris@0: // Try to create entries with more than one decimal separator; assert fail. Chris@0: $wrong_entries = [ Chris@0: '3.14.159', Chris@0: '0..45469', Chris@0: '..4589', Chris@0: '6.459.52', Chris@0: '6.3..25', Chris@0: ]; Chris@0: Chris@0: foreach ($wrong_entries as $wrong_entry) { Chris@0: $this->drupalGet('entity_test/add'); Chris@0: $edit = [ Chris@0: "{$field_name}[0][value]" => $wrong_entry, Chris@0: ]; Chris@0: $this->drupalPostForm(NULL, $edit, t('Save')); Chris@0: $this->assertRaw(t('%name must be a number.', ['%name' => $field_name]), 'Correctly failed to save float value with more than one decimal point.'); Chris@0: } Chris@0: Chris@0: // Try to create entries with minus sign not in the first position. Chris@0: $wrong_entries = [ Chris@0: '3-3', Chris@0: '4-', Chris@0: '1.3-', Chris@0: '1.2-4', Chris@0: '-10-10', Chris@0: ]; Chris@0: Chris@0: foreach ($wrong_entries as $wrong_entry) { Chris@0: $this->drupalGet('entity_test/add'); Chris@0: $edit = [ Chris@0: "{$field_name}[0][value]" => $wrong_entry, Chris@0: ]; Chris@0: $this->drupalPostForm(NULL, $edit, t('Save')); Chris@0: $this->assertRaw(t('%name must be a number.', ['%name' => $field_name]), 'Correctly failed to save float value with minus sign in the wrong position.'); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Test default formatter behavior Chris@0: */ Chris@0: public function testNumberFormatter() { Chris@0: $type = Unicode::strtolower($this->randomMachineName()); Chris@0: $float_field = Unicode::strtolower($this->randomMachineName()); Chris@0: $integer_field = Unicode::strtolower($this->randomMachineName()); Chris@0: $thousand_separators = ['', '.', ',', ' ', chr(8201), "'"]; Chris@0: $decimal_separators = ['.', ',']; Chris@0: $prefix = $this->randomMachineName(); Chris@0: $suffix = $this->randomMachineName(); Chris@0: $random_float = rand(0, pow(10, 6)); Chris@0: $random_integer = rand(0, pow(10, 6)); Chris@0: Chris@0: // Create a content type containing float and integer fields. Chris@0: $this->drupalCreateContentType(['type' => $type]); Chris@0: Chris@0: FieldStorageConfig::create([ Chris@0: 'field_name' => $float_field, Chris@0: 'entity_type' => 'node', Chris@0: 'type' => 'float', Chris@0: ])->save(); Chris@0: Chris@0: FieldStorageConfig::create([ Chris@0: 'field_name' => $integer_field, Chris@0: 'entity_type' => 'node', Chris@0: 'type' => 'integer', Chris@0: ])->save(); Chris@0: Chris@0: FieldConfig::create([ Chris@0: 'field_name' => $float_field, Chris@0: 'entity_type' => 'node', Chris@0: 'bundle' => $type, Chris@0: 'settings' => [ Chris@0: 'prefix' => $prefix, Chris@0: 'suffix' => $suffix Chris@0: ], Chris@0: ])->save(); Chris@0: Chris@0: FieldConfig::create([ Chris@0: 'field_name' => $integer_field, Chris@0: 'entity_type' => 'node', Chris@0: 'bundle' => $type, Chris@0: 'settings' => [ Chris@0: 'prefix' => $prefix, Chris@0: 'suffix' => $suffix Chris@0: ], Chris@0: ])->save(); Chris@0: Chris@0: entity_get_form_display('node', $type, 'default') Chris@0: ->setComponent($float_field, [ Chris@0: 'type' => 'number', Chris@0: 'settings' => [ Chris@0: 'placeholder' => '0.00' Chris@0: ], Chris@0: ]) Chris@0: ->setComponent($integer_field, [ Chris@0: 'type' => 'number', Chris@0: 'settings' => [ Chris@0: 'placeholder' => '0.00' Chris@0: ], Chris@0: ]) Chris@0: ->save(); Chris@0: Chris@0: entity_get_display('node', $type, 'default') Chris@0: ->setComponent($float_field, [ Chris@0: 'type' => 'number_decimal', Chris@0: ]) Chris@0: ->setComponent($integer_field, [ Chris@0: 'type' => 'number_unformatted', Chris@0: ]) Chris@0: ->save(); Chris@0: Chris@0: // Create a node to test formatters. Chris@0: $node = Node::create([ Chris@0: 'type' => $type, Chris@0: 'title' => $this->randomMachineName(), Chris@0: $float_field => ['value' => $random_float], Chris@0: $integer_field => ['value' => $random_integer], Chris@0: ]); Chris@0: $node->save(); Chris@0: Chris@0: // Go to manage display page. Chris@0: $this->drupalGet("admin/structure/types/manage/$type/display"); Chris@0: Chris@0: // Configure number_decimal formatter for the 'float' field type. Chris@0: $thousand_separator = $thousand_separators[array_rand($thousand_separators)]; Chris@0: $decimal_separator = $decimal_separators[array_rand($decimal_separators)]; Chris@0: $scale = rand(0, 10); Chris@0: Chris@0: $this->drupalPostAjaxForm(NULL, [], "${float_field}_settings_edit"); Chris@0: $edit = [ Chris@0: "fields[${float_field}][settings_edit_form][settings][prefix_suffix]" => TRUE, Chris@0: "fields[${float_field}][settings_edit_form][settings][scale]" => $scale, Chris@0: "fields[${float_field}][settings_edit_form][settings][decimal_separator]" => $decimal_separator, Chris@0: "fields[${float_field}][settings_edit_form][settings][thousand_separator]" => $thousand_separator, Chris@0: ]; Chris@0: $this->drupalPostAjaxForm(NULL, $edit, "${float_field}_plugin_settings_update"); Chris@0: $this->drupalPostForm(NULL, [], t('Save')); Chris@0: Chris@0: // Check number_decimal and number_unformatted formatters behavior. Chris@0: $this->drupalGet('node/' . $node->id()); Chris@0: $float_formatted = number_format($random_float, $scale, $decimal_separator, $thousand_separator); Chris@0: $this->assertRaw("$prefix$float_formatted$suffix", 'Prefix and suffix added'); Chris@0: $this->assertRaw((string) $random_integer); Chris@0: Chris@0: // Configure the number_decimal formatter. Chris@0: entity_get_display('node', $type, 'default') Chris@0: ->setComponent($integer_field, [ Chris@0: 'type' => 'number_integer', Chris@0: ]) Chris@0: ->save(); Chris@0: $this->drupalGet("admin/structure/types/manage/$type/display"); Chris@0: Chris@0: $thousand_separator = $thousand_separators[array_rand($thousand_separators)]; Chris@0: Chris@0: $this->drupalPostAjaxForm(NULL, [], "${integer_field}_settings_edit"); Chris@0: $edit = [ Chris@0: "fields[${integer_field}][settings_edit_form][settings][prefix_suffix]" => FALSE, Chris@0: "fields[${integer_field}][settings_edit_form][settings][thousand_separator]" => $thousand_separator, Chris@0: ]; Chris@0: $this->drupalPostAjaxForm(NULL, $edit, "${integer_field}_plugin_settings_update"); Chris@0: $this->drupalPostForm(NULL, [], t('Save')); Chris@0: Chris@0: // Check number_integer formatter behavior. Chris@0: $this->drupalGet('node/' . $node->id()); Chris@0: Chris@0: $integer_formatted = number_format($random_integer, 0, '', $thousand_separator); Chris@0: $this->assertRaw($integer_formatted, 'Random integer formatted'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests setting the minimum value of a float field through the interface. Chris@0: */ Chris@0: public function testCreateNumberFloatField() { Chris@0: // Create a float field. Chris@0: $field_name = Unicode::strtolower($this->randomMachineName()); Chris@0: FieldStorageConfig::create([ Chris@0: 'field_name' => $field_name, Chris@0: 'entity_type' => 'entity_test', Chris@0: 'type' => 'float', Chris@0: ])->save(); Chris@0: Chris@0: $field = FieldConfig::create([ Chris@0: 'field_name' => $field_name, Chris@0: 'entity_type' => 'entity_test', Chris@0: 'bundle' => 'entity_test', Chris@0: ]); Chris@0: $field->save(); Chris@0: Chris@0: // Set the minimum value to a float value. Chris@0: $this->assertSetMinimumValue($field, 0.0001); Chris@0: // Set the minimum value to an integer value. Chris@0: $this->assertSetMinimumValue($field, 1); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests setting the minimum value of a decimal field through the interface. Chris@0: */ Chris@0: public function testCreateNumberDecimalField() { Chris@0: // Create a decimal field. Chris@0: $field_name = Unicode::strtolower($this->randomMachineName()); Chris@0: FieldStorageConfig::create([ Chris@0: 'field_name' => $field_name, Chris@0: 'entity_type' => 'entity_test', Chris@0: 'type' => 'decimal', Chris@0: ])->save(); Chris@0: Chris@0: $field = FieldConfig::create([ Chris@0: 'field_name' => $field_name, Chris@0: 'entity_type' => 'entity_test', Chris@0: 'bundle' => 'entity_test', Chris@0: ]); Chris@0: $field->save(); Chris@0: Chris@0: // Set the minimum value to a decimal value. Chris@0: $this->assertSetMinimumValue($field, 0.1); Chris@0: // Set the minimum value to an integer value. Chris@0: $this->assertSetMinimumValue($field, 1); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Helper function to set the minimum value of a field. Chris@0: */ Chris@0: public function assertSetMinimumValue($field, $minimum_value) { Chris@0: $field_configuration_url = 'entity_test/structure/entity_test/fields/entity_test.entity_test.' . $field->getName(); Chris@0: Chris@0: // Set the minimum value. Chris@0: $edit = [ Chris@0: 'settings[min]' => $minimum_value, Chris@0: ]; Chris@0: $this->drupalPostForm($field_configuration_url, $edit, t('Save settings')); Chris@0: // Check if an error message is shown. Chris@0: $this->assertNoRaw(t('%name is not a valid number.', ['%name' => t('Minimum')]), 'Saved ' . gettype($minimum_value) . ' value as minimal value on a ' . $field->getType() . ' field'); Chris@0: // Check if a success message is shown. Chris@0: $this->assertRaw(t('Saved %label configuration.', ['%label' => $field->getLabel()])); Chris@0: // Check if the minimum value was actually set. Chris@0: $this->drupalGet($field_configuration_url); Chris@0: $this->assertFieldById('edit-settings-min', $minimum_value, 'Minimal ' . gettype($minimum_value) . ' value was set on a ' . $field->getType() . ' field.'); Chris@0: } Chris@0: Chris@0: }