You are here

public function ContactTest::testEntity in Examples for Developers 8

Same name and namespace in other branches
  1. 3.x modules/content_entity_example/tests/src/Kernel/ContactTest.php \Drupal\Tests\content_entity_example\Kernel\ContactTest::testEntity()

Basic CRUD operations on a Contact entity.

File

content_entity_example/tests/src/Kernel/ContactTest.php, line 31

Class

ContactTest
Test basic CRUD operations for our Contact entity type.

Namespace

Drupal\Tests\content_entity_example\Kernel

Code

public function testEntity() {
  $this
    ->installEntitySchema('content_entity_example_contact');
  $entity = Contact::create([
    'name' => 'Name',
    'first_name' => 'Firstname',
    'user_id' => 0,
    'role' => 'user',
  ]);
  $this
    ->assertNotNull($entity);
  $this
    ->assertEquals(SAVED_NEW, $entity
    ->save());
  $this
    ->assertEquals(SAVED_UPDATED, $entity
    ->set('role', 'administrator')
    ->save());
  $entity_id = $entity
    ->id();
  $this
    ->assertNotEmpty($entity_id);
  $entity
    ->delete();
  $this
    ->assertNull(Contact::load($entity_id));
}