function EntityFieldTest::doTestSave

Same name in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Entity/EntityFieldTest.php \Drupal\KernelTests\Core\Entity\EntityFieldTest::doTestSave()
  2. 8.9.x core/tests/Drupal/KernelTests/Core/Entity/EntityFieldTest.php \Drupal\KernelTests\Core\Entity\EntityFieldTest::doTestSave()
  3. 11.x 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 416

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(), "{$entity_type}: Entity has received an id.");
    $entity = $this->container
        ->get('entity_type.manager')
        ->getStorage($entity_type)
        ->load($entity->id());
    $this->assertTrue((bool) $entity->id(), "{$entity_type}: Entity loaded.");
    // Access the name field.
    $this->assertEquals(1, $entity->id->value, "{$entity_type}: ID value can be read.");
    $this->assertIsString($entity->uuid->value);
    $this->assertEquals('en', $entity->{$langcode_key}->value, "{$entity_type}: Language code can be read.");
    $this->assertEquals(\Drupal::languageManager()->getLanguage('en'), $entity->{$langcode_key}->language, "{$entity_type}: Language object can be read.");
    $this->assertEquals($this->entityUser
        ->id(), $entity->user_id->target_id, "{$entity_type}: User id can be read.");
    $this->assertEquals($this->entityUser
        ->getAccountName(), $entity->user_id->entity->name->value, "{$entity_type}: User name can be read.");
    $this->assertEquals($this->entityFieldText, $entity->field_test_text->value, "{$entity_type}: Text field can be read.");
}

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