View source
<?php
namespace Drupal\Tests\field\Functional\Number;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\field\FieldConfigInterface;
use Drupal\Tests\BrowserTestBase;
class NumberFieldTest extends BrowserTestBase {
protected static $modules = [
'node',
'entity_test',
'field_ui',
];
protected $defaultTheme = 'stark';
protected function setUp() : void {
parent::setUp();
$this
->drupalLogin($this
->drupalCreateUser([
'view test entity',
'administer entity_test content',
'administer content types',
'administer node fields',
'administer node display',
'bypass node access',
'administer entity_test fields',
]));
}
public function testNumberDecimalField() {
$field_name = mb_strtolower($this
->randomMachineName());
FieldStorageConfig::create([
'field_name' => $field_name,
'entity_type' => 'entity_test',
'type' => 'decimal',
'settings' => [
'precision' => 8,
'scale' => 4,
],
])
->save();
FieldConfig::create([
'field_name' => $field_name,
'entity_type' => 'entity_test',
'bundle' => 'entity_test',
])
->save();
$display_repository = \Drupal::service('entity_display.repository');
$display_repository
->getFormDisplay('entity_test', 'entity_test')
->setComponent($field_name, [
'type' => 'number',
'settings' => [
'placeholder' => '0.00',
],
])
->save();
$display_repository
->getViewDisplay('entity_test', 'entity_test')
->setComponent($field_name, [
'type' => 'number_decimal',
])
->save();
$this
->drupalGet('entity_test/add');
$this
->assertSession()
->fieldValueEquals("{$field_name}[0][value]", '');
$this
->assertSession()
->responseContains('placeholder="0.00"');
$value = '-1234.5678';
$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.');
$this
->assertSession()
->responseContains($value);
$wrong_entries = [
'3.14.159',
'0..45469',
'..4589',
'6.459.52',
'6.3..25',
];
foreach ($wrong_entries as $wrong_entry) {
$this
->drupalGet('entity_test/add');
$edit = [
"{$field_name}[0][value]" => $wrong_entry,
];
$this
->submitForm($edit, 'Save');
$this
->assertSession()
->pageTextContains("{$field_name} must be a number.");
}
$wrong_entries = [
'3-3',
'4-',
'1.3-',
'1.2-4',
'-10-10',
];
foreach ($wrong_entries as $wrong_entry) {
$this
->drupalGet('entity_test/add');
$edit = [
"{$field_name}[0][value]" => $wrong_entry,
];
$this
->submitForm($edit, 'Save');
$this
->assertSession()
->pageTextContains("{$field_name} must be a number.");
}
}
public function testNumberIntegerField() {
$minimum = rand(-4000, -2000);
$maximum = rand(2000, 4000);
$field_name = mb_strtolower($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();
$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();
$expected = [
'columns' => [
'value' => [
'type' => 'int',
'unsigned' => '',
'size' => 'normal',
],
],
'unique keys' => [],
'indexes' => [],
'foreign keys' => [],
];
$this
->assertEquals($expected, $storage
->getSchema());
$this
->drupalGet('entity_test/add');
$this
->assertSession()
->fieldValueEquals("{$field_name}[0][value]", '');
$this
->assertSession()
->responseContains('placeholder="4"');
$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.');
$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}.");
$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.");
$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}.");
$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.");
$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);
$this
->assertSession()
->elementNotExists('xpath', '//div[@content="' . $valid_entry . '"]');
}
$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);
$this
->assertSession()
->elementTextContains('xpath', '//div[@content="' . $integer_value . '"]', 'ThePrefix' . $integer_value);
}
public function testNumberFloatField() {
$field_name = mb_strtolower($this
->randomMachineName());
FieldStorageConfig::create([
'field_name' => $field_name,
'entity_type' => 'entity_test',
'type' => 'float',
])
->save();
FieldConfig::create([
'field_name' => $field_name,
'entity_type' => 'entity_test',
'bundle' => 'entity_test',
])
->save();
$display_repository = \Drupal::service('entity_display.repository');
$display_repository
->getFormDisplay('entity_test', 'entity_test')
->setComponent($field_name, [
'type' => 'number',
'settings' => [
'placeholder' => '0.00',
],
])
->save();
$display_repository
->getViewDisplay('entity_test', 'entity_test')
->setComponent($field_name, [
'type' => 'number_decimal',
])
->save();
$this
->drupalGet('entity_test/add');
$this
->assertSession()
->fieldValueEquals("{$field_name}[0][value]", '');
$this
->assertSession()
->responseContains('placeholder="0.00"');
$value = '-1234.5678';
$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.');
$this
->drupalGet('entity_test/' . $id);
$this
->assertSession()
->responseContains(round($value, 2));
$wrong_entries = [
'3.14.159',
'0..45469',
'..4589',
'6.459.52',
'6.3..25',
];
foreach ($wrong_entries as $wrong_entry) {
$this
->drupalGet('entity_test/add');
$edit = [
"{$field_name}[0][value]" => $wrong_entry,
];
$this
->submitForm($edit, 'Save');
$this
->assertSession()
->pageTextContains("{$field_name} must be a number.");
}
$wrong_entries = [
'3-3',
'4-',
'1.3-',
'1.2-4',
'-10-10',
];
foreach ($wrong_entries as $wrong_entry) {
$this
->drupalGet('entity_test/add');
$edit = [
"{$field_name}[0][value]" => $wrong_entry,
];
$this
->submitForm($edit, 'Save');
$this
->assertSession()
->pageTextContains("{$field_name} must be a number.");
}
}
public function testCreateNumberFloatField() {
$field_name = mb_strtolower($this
->randomMachineName());
FieldStorageConfig::create([
'field_name' => $field_name,
'entity_type' => 'entity_test',
'type' => 'float',
])
->save();
$field = FieldConfig::create([
'field_name' => $field_name,
'entity_type' => 'entity_test',
'bundle' => 'entity_test',
]);
$field
->save();
$this
->assertSetMinimumValue($field, 0.0001);
$this
->assertSetMinimumValue($field, 1);
}
public function testCreateNumberDecimalField() {
$field_name = mb_strtolower($this
->randomMachineName());
FieldStorageConfig::create([
'field_name' => $field_name,
'entity_type' => 'entity_test',
'type' => 'decimal',
])
->save();
$field = FieldConfig::create([
'field_name' => $field_name,
'entity_type' => 'entity_test',
'bundle' => 'entity_test',
]);
$field
->save();
$this
->assertSetMinimumValue($field, 0.1);
$this
->assertSetMinimumValue($field, 1);
}
public function assertSetMinimumValue(FieldConfigInterface $field, $minimum_value) : void {
$field_configuration_url = 'entity_test/structure/entity_test/fields/entity_test.entity_test.' . $field
->getName();
$edit = [
'settings[min]' => $minimum_value,
];
$this
->drupalGet($field_configuration_url);
$this
->submitForm($edit, 'Save settings');
$this
->assertSession()
->pageTextNotContains("Minimum is not a valid number.");
$this
->assertSession()
->pageTextContains("Saved {$field->getLabel()} configuration.");
$this
->drupalGet($field_configuration_url);
$this
->assertSession()
->fieldValueEquals('edit-settings-min', $minimum_value);
}
}