View source
<?php
namespace Drupal\Tests\field\Kernel\Number;
use Drupal\Core\Field\FieldItemInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\field\Entity\FieldConfig;
use Drupal\Tests\field\Kernel\FieldKernelTestBase;
use Drupal\field\Entity\FieldStorageConfig;
class NumberItemTest extends FieldKernelTestBase {
protected static $modules = [];
protected function setUp() : void {
parent::setUp();
foreach ([
'integer',
'float',
'decimal',
] as $type) {
FieldStorageConfig::create([
'entity_type' => 'entity_test',
'field_name' => 'field_' . $type,
'type' => $type,
])
->save();
FieldConfig::create([
'entity_type' => 'entity_test',
'field_name' => 'field_' . $type,
'bundle' => 'entity_test',
])
->save();
}
}
public function testNumberItem() {
$entity = EntityTest::create();
$integer = rand(0, 10);
$entity->field_integer = $integer;
$float = 3.14;
$entity->field_float = $float;
$entity->field_decimal = '20-40';
$violations = $entity
->validate();
$this
->assertCount(1, $violations, 'Wrong decimal value causes validation error');
$decimal = '31.3';
$entity->field_decimal = $decimal;
$entity->name->value = $this
->randomMachineName();
$entity
->save();
$id = $entity
->id();
$entity = EntityTest::load($id);
$this
->assertInstanceOf(FieldItemListInterface::class, $entity->field_integer);
$this
->assertInstanceOf(FieldItemInterface::class, $entity->field_integer[0]);
$this
->assertEquals($integer, $entity->field_integer->value);
$this
->assertEquals($integer, $entity->field_integer[0]->value);
$this
->assertInstanceOf(FieldItemListInterface::class, $entity->field_float);
$this
->assertInstanceOf(FieldItemInterface::class, $entity->field_float[0]);
$this
->assertEquals($float, $entity->field_float->value);
$this
->assertEquals($float, $entity->field_float[0]->value);
$this
->assertInstanceOf(FieldItemListInterface::class, $entity->field_decimal);
$this
->assertInstanceOf(FieldItemInterface::class, $entity->field_decimal[0]);
$this
->assertEquals((double) $decimal, $entity->field_decimal->value);
$this
->assertEquals((double) $decimal, $entity->field_decimal[0]->value);
$new_integer = rand(11, 20);
$new_float = rand(1001, 2000) / 100;
$new_decimal = '18.2';
$entity->field_integer->value = $new_integer;
$this
->assertEquals($new_integer, $entity->field_integer->value);
$entity->field_float->value = $new_float;
$this
->assertEquals($new_float, $entity->field_float->value);
$entity->field_decimal->value = $new_decimal;
$this
->assertEquals((double) $new_decimal, $entity->field_decimal->value);
$entity
->save();
$entity = EntityTest::load($id);
$this
->assertEquals($new_integer, $entity->field_integer->value);
$this
->assertEquals($new_float, $entity->field_float->value);
$this
->assertEquals((double) $new_decimal, $entity->field_decimal->value);
$entity = EntityTest::create();
$entity->field_decimal
->getFieldDefinition()
->setSetting('min', 99)
->setSetting('max', 100);
$entity->field_float
->getFieldDefinition()
->setSetting('min', 99)
->setSetting('max', 100);
$entity->field_integer
->getFieldDefinition()
->setSetting('min', 99)
->setSetting('max', 100);
$entity->field_decimal
->generateSampleItems();
$entity->field_integer
->generateSampleItems();
$entity->field_float
->generateSampleItems();
$this
->entityValidateAndSave($entity);
}
public function testConstraints($type, $min, $max, $value, $expect_constraints, $expected_constraint_message = '') {
$field = FieldConfig::loadByName('entity_test', 'entity_test', 'field_' . $type);
$field
->setSetting('min', $min);
$field
->setSetting('max', $max);
$field
->save();
$entity = EntityTest::create();
$entity->{'field_' . $type} = $value;
$violations = $entity
->validate();
$this
->assertEquals($expect_constraints, $violations
->count() > 0);
if ($expect_constraints) {
$this
->assertEquals($expected_constraint_message, $violations
->get(0)
->getMessage());
}
}
public function dataNumberFieldSettingsProvider() {
(yield [
'integer',
NULL,
NULL,
-100,
FALSE,
]);
(yield [
'integer',
0,
NULL,
-100,
TRUE,
'<em class="placeholder">field_integer</em>: the value may be no less than <em class="placeholder">0</em>.',
]);
(yield [
'integer',
10,
NULL,
100,
FALSE,
]);
(yield [
'integer',
10,
NULL,
5,
TRUE,
'<em class="placeholder">field_integer</em>: the value may be no less than <em class="placeholder">10</em>.',
]);
(yield [
'integer',
10,
20,
25,
TRUE,
'<em class="placeholder">field_integer</em>: the value may be no greater than <em class="placeholder">20</em>.',
]);
(yield [
'integer',
10,
20,
15,
FALSE,
]);
(yield [
'float',
NULL,
NULL,
-100,
FALSE,
]);
(yield [
'float',
0.003,
NULL,
0.0029,
TRUE,
'<em class="placeholder">field_float</em>: the value may be no less than <em class="placeholder">0.003</em>.',
]);
(yield [
'float',
10.05,
NULL,
13.4,
FALSE,
]);
(yield [
'float',
10,
NULL,
9.999000000000001,
TRUE,
'<em class="placeholder">field_float</em>: the value may be no less than <em class="placeholder">10</em>.',
]);
(yield [
'float',
1,
2,
2.5,
TRUE,
'<em class="placeholder">field_float</em>: the value may be no greater than <em class="placeholder">2</em>.',
]);
(yield [
'float',
1,
2,
1.5,
FALSE,
]);
(yield [
'decimal',
NULL,
NULL,
-100,
FALSE,
]);
(yield [
'decimal',
0.001,
NULL,
-0.05,
TRUE,
'<em class="placeholder">field_decimal</em>: the value may be no less than <em class="placeholder">0.001</em>.',
]);
(yield [
'decimal',
10.05,
NULL,
13.4,
FALSE,
]);
(yield [
'decimal',
10,
NULL,
9.999000000000001,
TRUE,
'<em class="placeholder">field_decimal</em>: the value may be no less than <em class="placeholder">10</em>.',
]);
(yield [
'decimal',
1,
2,
2.5,
TRUE,
'<em class="placeholder">field_decimal</em>: the value may be no greater than <em class="placeholder">2</em>.',
]);
(yield [
'decimal',
1,
2,
1.5,
FALSE,
]);
}
}