You are here

public function FieldNoteItemTest::testFieldNoteItem in Examples for Developers 8

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

Test entity fields of the field_permission_example_fieldnote field type.

File

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_fieldnote = $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_fieldnote instanceof FieldItemListInterface, 'Field implements interface.');
  $this
    ->assertTrue($entity->field_fieldnote[0] instanceof FieldItemInterface, 'Field item implements interface.');
  $this
    ->assertEqual($entity->field_fieldnote->value, $value);
  $this
    ->assertEqual($entity->field_fieldnote[0]->value, $value);

  // Verify changing the field's value.
  $new_value = $this
    ->randomMachineName();
  $entity->field_fieldnote->value = $new_value;
  $this
    ->assertEqual($entity->field_fieldnote->value, $new_value);

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

  // Test sample item generation.
  $entity = $type_manager
    ->getStorage('entity_test')
    ->create([]);
  $entity->field_fieldnote
    ->generateSampleItems();
  $this
    ->entityValidateAndSave($entity);
}