function BooleanItemTest::testBooleanItem

Same name in other branches
  1. 9 core/modules/field/tests/src/Kernel/Boolean/BooleanItemTest.php \Drupal\Tests\field\Kernel\Boolean\BooleanItemTest::testBooleanItem()
  2. 10 core/modules/field/tests/src/Kernel/Boolean/BooleanItemTest.php \Drupal\Tests\field\Kernel\Boolean\BooleanItemTest::testBooleanItem()
  3. 11.x core/modules/field/tests/src/Kernel/Boolean/BooleanItemTest.php \Drupal\Tests\field\Kernel\Boolean\BooleanItemTest::testBooleanItem()

Tests using entity fields of the boolean field type.

File

core/modules/field/tests/src/Kernel/Boolean/BooleanItemTest.php, line 49

Class

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

Namespace

Drupal\Tests\field\Kernel\Boolean

Code

public function testBooleanItem() {
    // Verify entity creation.
    $entity = EntityTest::create();
    $value = '1';
    $entity->field_boolean = $value;
    $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_boolean);
    $this->assertInstanceOf(FieldItemInterface::class, $entity->field_boolean[0]);
    $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 = EntityTest::load($id);
    $this->assertEqual($entity->field_boolean->value, $new_value);
    // Test sample item generation.
    $entity = EntityTest::create();
    $entity->field_boolean
        ->generateSampleItems();
    $this->entityValidateAndSave($entity);
}

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