function ResourceTestBase::resaveEntity

Same name in other branches
  1. 11.x core/modules/jsonapi/tests/src/Functional/ResourceTestBase.php \Drupal\Tests\jsonapi\Functional\ResourceTestBase::resaveEntity()
3 calls to ResourceTestBase::resaveEntity()
FileTest::testIndividual in core/modules/jsonapi/tests/src/Functional/FileTest.php
Tests POST/PATCH/DELETE for an individual resource.
ResourceTestBase::setUpFields in core/modules/jsonapi/tests/src/Functional/ResourceTestBase.php
Sets up additional fields for testing.
ResourceTestBase::testIndividual in core/modules/jsonapi/tests/src/Functional/ResourceTestBase.php
Tests POST/PATCH/DELETE for an individual resource.

File

core/modules/jsonapi/tests/src/Functional/ResourceTestBase.php, line 330

Class

ResourceTestBase
Subclass this for every JSON:API resource type.

Namespace

Drupal\Tests\jsonapi\Functional

Code

protected function resaveEntity(EntityInterface $entity, AccountInterface $account) : EntityInterface {
    // Reload entity so that it has the new field.
    $reloaded_entity = $this->entityLoadUnchanged($entity->id());
    // Some entity types are not stored, hence they cannot be reloaded.
    if ($reloaded_entity !== NULL) {
        $entity = $reloaded_entity;
        // Set a default value on the fields.
        $entity->set('field_rest_test', [
            'value' => 'All the faith he had had had had no effect on the outcome of his life.',
        ]);
        $entity->set('field_jsonapi_test_entity_ref', [
            'user' => $account->id(),
        ]);
        $entity->set('field_rest_test_multivalue', [
            [
                'value' => 'One',
            ],
            [
                'value' => 'Two',
            ],
        ]);
        $entity->save();
    }
    return $entity;
}

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