function FieldNoteItemTest::testFieldNoteItem

Same name in other branches
  1. 3.x modules/field_permission_example/tests/src/Kernel/FieldNoteItemTest.php \Drupal\Tests\field_permission_example\Kernel\FieldNoteItemTest::testFieldNoteItem()
  2. 8.x-1.x field_permission_example/tests/src/Kernel/FieldNoteItemTest.php \Drupal\Tests\field_permission_example\Kernel\FieldNoteItemTest::testFieldNoteItem()

Test entity fields of the field_permission_example_field_note field type.

File

modules/field_permission_example/tests/src/Kernel/FieldNoteItemTest.php, line 126

Class

FieldNoteItemTest
Tests our sticky-note field type.

Namespace

Drupal\Tests\field_permission_example\Kernel

Code

public function testFieldNoteItem() {
    // Verify entity creation.
    $type_manager = $this->container
        ->get('entity_type.manager');
    $entity = $type_manager->getStorage('entity_test')
        ->create([]);
    $value = 'This is an epic entity';
    $entity->field_field_note = $value;
    $entity->name->value = $this->randomMachineName();
    $entity->save();
    // Verify entity has been created properly.
    $id = $entity->id();
    $entity = $type_manager->getStorage('entity_test')
        ->load($id);
    $this->assertTrue($entity->field_field_note instanceof FieldItemListInterface, 'Field implements interface.');
    $this->assertTrue($entity->field_field_note[0] instanceof FieldItemInterface, 'Field item implements interface.');
    $this->assertEquals($entity->field_field_note->value, $value);
    $this->assertEquals($entity->field_field_note[0]->value, $value);
    // Verify changing the field's value.
    $new_value = $this->randomMachineName();
    $entity->field_field_note->value = $new_value;
    $this->assertEquals($entity->field_field_note->value, $new_value);
    // Read changed entity and assert changed values.
    $entity->save();
    $entity = $type_manager->getStorage('entity_test')
        ->load($id);
    $this->assertEquals($entity->field_field_note->value, $new_value);
    // Test sample item generation.
    $entity = $type_manager->getStorage('entity_test')
        ->create([]);
    $entity->field_field_note
        ->generateSampleItems();
    $this->entityValidateAndSave($entity);
}