function EntityResourceTestBase::setUp

Same name in other branches
  1. 8.9.x core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php \Drupal\Tests\rest\Functional\EntityResource\EntityResourceTestBase::setUp()
  2. 10 core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php \Drupal\Tests\rest\Functional\EntityResource\EntityResourceTestBase::setUp()
  3. 11.x core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php \Drupal\Tests\rest\Functional\EntityResource\EntityResourceTestBase::setUp()

Overrides ResourceTestBase::setUp

2 calls to EntityResourceTestBase::setUp()
ConfigEntityResourceTestBase::setUp in core/modules/rest/tests/src/Functional/EntityResource/ConfigEntityResourceTestBase.php
MediaResourceTestBase::setUp in core/modules/media/tests/src/Functional/Rest/MediaResourceTestBase.php
2 methods override EntityResourceTestBase::setUp()
ConfigEntityResourceTestBase::setUp in core/modules/rest/tests/src/Functional/EntityResource/ConfigEntityResourceTestBase.php
MediaResourceTestBase::setUp in core/modules/media/tests/src/Functional/Rest/MediaResourceTestBase.php

File

core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php, line 182

Class

EntityResourceTestBase
Defines a base class for testing all entity resources.

Namespace

Drupal\Tests\rest\Functional\EntityResource

Code

public function setUp() {
    parent::setUp();
    // Calculate REST Resource config entity ID.
    static::$resourceConfigId = 'entity.' . static::$entityTypeId;
    $this->entityStorage = $this->container
        ->get('entity_type.manager')
        ->getStorage(static::$entityTypeId);
    // Create an entity.
    $this->entity = $this->createEntity();
    if ($this->entity instanceof FieldableEntityInterface) {
        // Add access-protected field.
        FieldStorageConfig::create([
            'entity_type' => static::$entityTypeId,
            'field_name' => 'field_rest_test',
            'type' => 'text',
        ])->setCardinality(1)
            ->save();
        FieldConfig::create([
            'entity_type' => static::$entityTypeId,
            'field_name' => 'field_rest_test',
            'bundle' => $this->entity
                ->bundle(),
        ])
            ->setLabel('Test field')
            ->setTranslatable(FALSE)
            ->save();
        // Add multi-value field.
        FieldStorageConfig::create([
            'entity_type' => static::$entityTypeId,
            'field_name' => 'field_rest_test_multivalue',
            'type' => 'string',
        ])->setCardinality(3)
            ->save();
        FieldConfig::create([
            'entity_type' => static::$entityTypeId,
            'field_name' => 'field_rest_test_multivalue',
            'bundle' => $this->entity
                ->bundle(),
        ])
            ->setLabel('Test field: multi-value')
            ->setTranslatable(FALSE)
            ->save();
        // Reload entity so that it has the new field.
        $reloaded_entity = $this->entityStorage
            ->loadUnchanged($this->entity
            ->id());
        // Some entity types are not stored, hence they cannot be reloaded.
        if ($reloaded_entity !== NULL) {
            $this->entity = $reloaded_entity;
            // Set a default value on the fields.
            $this->entity
                ->set('field_rest_test', [
                'value' => 'All the faith they had had had had no effect on the outcome of their life.',
            ]);
            $this->entity
                ->set('field_rest_test_multivalue', [
                [
                    'value' => 'One',
                ],
                [
                    'value' => 'Two',
                ],
            ]);
            $this->entity
                ->set('rest_test_validation', [
                'value' => 'allowed value',
            ]);
            $this->entity
                ->save();
        }
    }
}

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