function NumberItemTest::testNumberItem

Same name in other branches
  1. 8.9.x core/modules/field/tests/src/Kernel/Number/NumberItemTest.php \Drupal\Tests\field\Kernel\Number\NumberItemTest::testNumberItem()
  2. 10 core/modules/field/tests/src/Kernel/Number/NumberItemTest.php \Drupal\Tests\field\Kernel\Number\NumberItemTest::testNumberItem()
  3. 11.x 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 50

Class

NumberItemTest
Tests the new entity API for the number field type.

Namespace

Drupal\Tests\field\Kernel\Number

Code

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();
    // Make sure that field settings are respected by the generation.
    $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();
    // Confirm that the generated sample values are within range.
    $this->entityValidateAndSave($entity);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.