function NumberFieldTest::testNumberIntegerField
Same name in other branches
- 9 core/modules/field/tests/src/Functional/Number/NumberFieldTest.php \Drupal\Tests\field\Functional\Number\NumberFieldTest::testNumberIntegerField()
- 8.9.x core/modules/field/tests/src/Functional/Number/NumberFieldTest.php \Drupal\Tests\field\Functional\Number\NumberFieldTest::testNumberIntegerField()
- 11.x core/modules/field/tests/src/Functional/Number/NumberFieldTest.php \Drupal\Tests\field\Functional\Number\NumberFieldTest::testNumberIntegerField()
Tests integer field.
File
-
core/
modules/ field/ tests/ src/ Functional/ Number/ NumberFieldTest.php, line 136
Class
- NumberFieldTest
- Tests the creation of numeric fields.
Namespace
Drupal\Tests\field\Functional\NumberCode
public function testNumberIntegerField() : void {
$minimum = rand(-4000, -2000);
$maximum = rand(2000, 4000);
// Create a field with settings to validate.
$field_name = $this->randomMachineName();
$storage = FieldStorageConfig::create([
'field_name' => $field_name,
'entity_type' => 'entity_test',
'type' => 'integer',
]);
$storage->save();
FieldConfig::create([
'field_name' => $field_name,
'entity_type' => 'entity_test',
'bundle' => 'entity_test',
'settings' => [
'min' => $minimum,
'max' => $maximum,
'prefix' => 'ThePrefix',
],
])->save();
/** @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface $display_repository */
$display_repository = \Drupal::service('entity_display.repository');
$display_repository->getFormDisplay('entity_test', 'entity_test')
->setComponent($field_name, [
'type' => 'number',
'settings' => [
'placeholder' => '4',
],
])
->save();
$display_repository->getViewDisplay('entity_test', 'entity_test')
->setComponent($field_name, [
'type' => 'number_integer',
'settings' => [
'prefix_suffix' => FALSE,
],
])
->save();
// Check the storage schema.
$expected = [
'columns' => [
'value' => [
'type' => 'int',
'unsigned' => '',
'size' => 'normal',
],
],
'unique keys' => [],
'indexes' => [],
'foreign keys' => [],
];
$this->assertEquals($expected, $storage->getSchema());
// Display creation form.
$this->drupalGet('entity_test/add');
$this->assertSession()
->fieldValueEquals("{$field_name}[0][value]", '');
$this->assertSession()
->responseContains('placeholder="4"');
// Submit a valid integer
$value = rand($minimum, $maximum);
$edit = [
"{$field_name}[0][value]" => $value,
];
$this->submitForm($edit, 'Save');
preg_match('|entity_test/manage/(\\d+)|', $this->getUrl(), $match);
$id = $match[1];
$this->assertSession()
->pageTextContains('entity_test ' . $id . ' has been created.');
// Try to set a value below the minimum value
$this->drupalGet('entity_test/add');
$edit = [
"{$field_name}[0][value]" => $minimum - 1,
];
$this->submitForm($edit, 'Save');
$this->assertSession()
->pageTextContains("{$field_name} must be higher than or equal to {$minimum}.");
// Try to set a decimal value
$this->drupalGet('entity_test/add');
$edit = [
"{$field_name}[0][value]" => 1.5,
];
$this->submitForm($edit, 'Save');
$this->assertSession()
->pageTextContains("{$field_name} is not a valid number.");
// Try to set a value above the maximum value
$this->drupalGet('entity_test/add');
$edit = [
"{$field_name}[0][value]" => $maximum + 1,
];
$this->submitForm($edit, 'Save');
$this->assertSession()
->pageTextContains("{$field_name} must be lower than or equal to {$maximum}.");
// Try to set a wrong integer value.
$this->drupalGet('entity_test/add');
$edit = [
"{$field_name}[0][value]" => '20-40',
];
$this->submitForm($edit, 'Save');
$this->assertSession()
->pageTextContains("{$field_name} must be a number.");
// Test with valid entries.
$valid_entries = [
'-1234',
'0',
'1234',
];
foreach ($valid_entries as $valid_entry) {
$this->drupalGet('entity_test/add');
$edit = [
"{$field_name}[0][value]" => $valid_entry,
];
$this->submitForm($edit, 'Save');
preg_match('|entity_test/manage/(\\d+)|', $this->getUrl(), $match);
$id = $match[1];
$this->assertSession()
->pageTextContains('entity_test ' . $id . ' has been created.');
$this->assertSession()
->responseContains($valid_entry);
// Verify that the "content" attribute is not present since the Prefix is
// not being displayed.
$this->assertSession()
->elementNotExists('xpath', '//div[@content="' . $valid_entry . '"]');
}
// Test for the content attribute when a Prefix is displayed. Presumably this also tests for the attribute when a Suffix is displayed.
$display_repository->getViewDisplay('entity_test', 'entity_test')
->setComponent($field_name, [
'type' => 'number_integer',
'settings' => [
'prefix_suffix' => TRUE,
],
])
->save();
$integer_value = '123';
$this->drupalGet('entity_test/add');
$edit = [
"{$field_name}[0][value]" => $integer_value,
];
$this->submitForm($edit, 'Save');
preg_match('|entity_test/manage/(\\d+)|', $this->getUrl(), $match);
$id = $match[1];
$this->assertSession()
->pageTextContains('entity_test ' . $id . ' has been created.');
$this->drupalGet('entity_test/' . $id);
// Verify that the "content" attribute has been set to the value of the
// field, and the prefix is being displayed.
$this->assertSession()
->elementTextContains('xpath', '//div[@content="' . $integer_value . '"]', 'ThePrefix' . $integer_value);
$field_configuration_url = 'entity_test/structure/entity_test/fields/entity_test.entity_test.' . $field_name;
$this->drupalGet($field_configuration_url);
// Tests Number validation messages.
$edit = [
'settings[min]' => 10,
'settings[max]' => 8,
];
$this->submitForm($edit, 'Save settings');
$this->assertSession()
->pageTextContains("The minimum value must be less than or equal to {$edit['settings[max]']}.");
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.