public function NumberItemTest::testNumberItem

Tests using entity fields of the number field type.

File

drupal/core/modules/number/lib/Drupal/number/Tests/NumberItemTest.php, line 56
Contains \Drupal\number\Tests\NumberItemTest.

Class

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

Namespace

Drupal\number\Tests

Code

public function testNumberItem() {

  // Verify entity creation.
  $entity = entity_create('entity_test', array());
  $integer = rand(0, 10);
  $entity->field_integer = $integer;
  $float = 3.14;
  $entity->field_float = $float;
  $decimal = '31.3';
  $entity->field_decimal = $decimal;
  $entity->name->value = $this
    ->randomName();
  $entity
    ->save();

  // Verify entity has been created properly.
  $id = $entity
    ->id();
  $entity = entity_load('entity_test', $id);
  $this
    ->assertTrue($entity->field_integer instanceof FieldInterface, 'Field implements interface.');
  $this
    ->assertTrue($entity->field_integer[0] instanceof FieldItemInterface, 'Field item implements interface.');
  $this
    ->assertEqual($entity->field_integer->value, $integer);
  $this
    ->assertEqual($entity->field_integer[0]->value, $integer);
  $this
    ->assertTrue($entity->field_float instanceof FieldInterface, 'Field implements interface.');
  $this
    ->assertTrue($entity->field_float[0] instanceof FieldItemInterface, 'Field item implements interface.');
  $this
    ->assertEqual($entity->field_float->value, $float);
  $this
    ->assertEqual($entity->field_float[0]->value, $float);
  $this
    ->assertTrue($entity->field_decimal instanceof FieldInterface, 'Field implements interface.');
  $this
    ->assertTrue($entity->field_decimal[0] instanceof FieldItemInterface, 'Field item implements interface.');
  $this
    ->assertEqual($entity->field_decimal->value, $decimal);
  $this
    ->assertEqual($entity->field_decimal[0]->value, $decimal);

  // 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
    ->assertEqual($entity->field_integer->value, $new_integer);
  $entity->field_float->value = $new_float;
  $this
    ->assertEqual($entity->field_float->value, $new_float);
  $entity->field_decimal->value = $new_decimal;
  $this
    ->assertEqual($entity->field_decimal->value, $new_decimal);

  // Read changed entity and assert changed values.
  $entity
    ->save();
  $entity = entity_load('entity_test', $id);
  $this
    ->assertEqual($entity->field_integer->value, $new_integer);
  $this
    ->assertEqual($entity->field_float->value, $new_float);
  $this
    ->assertEqual($entity->field_decimal->value, $new_decimal);
}