You are here

public function BooleanItemTest::testBooleanItem in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/field/src/Tests/Boolean/BooleanItemTest.php \Drupal\field\Tests\Boolean\BooleanItemTest::testBooleanItem()

Tests using entity fields of the boolean field type.

File

core/modules/field/src/Tests/Boolean/BooleanItemTest.php, line 50
Contains \Drupal\field\Tests\Boolean\BooleanItemTest.

Class

BooleanItemTest
Tests the new entity API for the boolean field type.

Namespace

Drupal\field\Tests\Boolean

Code

public function testBooleanItem() {

  // Verify entity creation.
  $entity = entity_create('entity_test');
  $value = '1';
  $entity->field_boolean = $value;
  $entity->name->value = $this
    ->randomMachineName();
  $entity
    ->save();

  // Verify entity has been created properly.
  $id = $entity
    ->id();
  $entity = entity_load('entity_test', $id);
  $this
    ->assertTrue($entity->field_boolean instanceof FieldItemListInterface, 'Field implements interface.');
  $this
    ->assertTrue($entity->field_boolean[0] instanceof FieldItemInterface, 'Field item implements interface.');
  $this
    ->assertEqual($entity->field_boolean->value, $value);
  $this
    ->assertEqual($entity->field_boolean[0]->value, $value);

  // Verify changing the boolean value.
  $new_value = 0;
  $entity->field_boolean->value = $new_value;
  $this
    ->assertEqual($entity->field_boolean->value, $new_value);

  // Read changed entity and assert changed values.
  $entity
    ->save();
  $entity = entity_load('entity_test', $id);
  $this
    ->assertEqual($entity->field_boolean->value, $new_value);

  // Test sample item generation.
  $entity = entity_create('entity_test');
  $entity->field_boolean
    ->generateSampleItems();
  $this
    ->entityValidateAndSave($entity);
}