You are here

protected function EntityFieldTest::doTestSave in Drupal 9

Same name and namespace in other branches
  1. 8 core/tests/Drupal/KernelTests/Core/Entity/EntityFieldTest.php \Drupal\KernelTests\Core\Entity\EntityFieldTest::doTestSave()

Executes the save tests for the given entity type.

Parameters

string $entity_type: The entity type to run the tests with.

1 call to EntityFieldTest::doTestSave()
EntityFieldTest::testSave in core/tests/Drupal/KernelTests/Core/Entity/EntityFieldTest.php
Tries to save and load an entity again.

File

core/tests/Drupal/KernelTests/Core/Entity/EntityFieldTest.php, line 380

Class

EntityFieldTest
Tests the Entity Field API.

Namespace

Drupal\KernelTests\Core\Entity

Code

protected function doTestSave($entity_type) {
  $langcode_key = $this->entityTypeManager
    ->getDefinition($entity_type)
    ->getKey('langcode');
  $entity = $this
    ->createTestEntity($entity_type);
  $entity
    ->save();
  $this
    ->assertTrue((bool) $entity
    ->id(), new FormattableMarkup('%entity_type: Entity has received an id.', [
    '%entity_type' => $entity_type,
  ]));
  $entity = $this->container
    ->get('entity_type.manager')
    ->getStorage($entity_type)
    ->load($entity
    ->id());
  $this
    ->assertTrue((bool) $entity
    ->id(), new FormattableMarkup('%entity_type: Entity loaded.', [
    '%entity_type' => $entity_type,
  ]));

  // Access the name field.
  $this
    ->assertEquals(1, $entity->id->value, new FormattableMarkup('%entity_type: ID value can be read.', [
    '%entity_type' => $entity_type,
  ]));
  $this
    ->assertIsString($entity->uuid->value);
  $this
    ->assertEquals('en', $entity->{$langcode_key}->value, new FormattableMarkup('%entity_type: Language code can be read.', [
    '%entity_type' => $entity_type,
  ]));
  $this
    ->assertEquals(\Drupal::languageManager()
    ->getLanguage('en'), $entity->{$langcode_key}->language, new FormattableMarkup('%entity_type: Language object can be read.', [
    '%entity_type' => $entity_type,
  ]));
  $this
    ->assertEquals($this->entityUser
    ->id(), $entity->user_id->target_id, new FormattableMarkup('%entity_type: User id can be read.', [
    '%entity_type' => $entity_type,
  ]));
  $this
    ->assertEquals($this->entityUser
    ->getAccountName(), $entity->user_id->entity->name->value, new FormattableMarkup('%entity_type: User name can be read.', [
    '%entity_type' => $entity_type,
  ]));
  $this
    ->assertEquals($this->entityFieldText, $entity->field_test_text->value, new FormattableMarkup('%entity_type: Text field can be read.', [
    '%entity_type' => $entity_type,
  ]));
}