public function NumberItemTest::testNumberItem in Drupal 9
Same name and namespace in other branches
- 8 core/modules/field/tests/src/Kernel/Number/NumberItemTest.php \Drupal\Tests\field\Kernel\Number\NumberItemTest::testNumberItem()
- 10 core/modules/field/tests/src/Kernel/Number/NumberItemTest.php \Drupal\Tests\field\Kernel\Number\NumberItemTest::testNumberItem()
Tests using entity fields of the number field type.
File
- core/
modules/ field/ tests/ src/ Kernel/ Number/ NumberItemTest.php, line 47
Class
- NumberItemTest
- Tests the new entity API for the number field type.
Namespace
Drupal\Tests\field\Kernel\NumberCode
public function testNumberItem() {
// Verify entity creation.
$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();
// Verify entity has been created properly.
$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);
// Verify changing the number 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);
// Read changed entity and assert changed values.
$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);
// Test sample item generation.
$entity = EntityTest::create();
$entity->field_integer
->generateSampleItems();
$entity->field_float
->generateSampleItems();
$entity->field_decimal
->generateSampleItems();
$this
->entityValidateAndSave($entity);
}