function EntityResourceTestBase::setUp

Same name in other branches
  1. 9 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

4 calls to EntityResourceTestBase::setUp()
EntityTestDateonlyTest::setUp in core/modules/datetime/tests/src/Functional/EntityResource/EntityTest/EntityTestDateonlyTest.php
EntityTestDateRangeTest::setUp in core/modules/datetime_range/tests/src/Functional/EntityResource/EntityTest/EntityTestDateRangeTest.php
EntityTestDatetimeTest::setUp in core/modules/datetime/tests/src/Functional/EntityResource/EntityTest/EntityTestDatetimeTest.php
MediaResourceTestBase::setUp in core/modules/media/tests/src/Functional/Rest/MediaResourceTestBase.php
4 methods override EntityResourceTestBase::setUp()
EntityTestDateonlyTest::setUp in core/modules/datetime/tests/src/Functional/EntityResource/EntityTest/EntityTestDateonlyTest.php
EntityTestDateRangeTest::setUp in core/modules/datetime_range/tests/src/Functional/EntityResource/EntityTest/EntityTestDateRangeTest.php
EntityTestDatetimeTest::setUp in core/modules/datetime/tests/src/Functional/EntityResource/EntityTest/EntityTestDatetimeTest.php
MediaResourceTestBase::setUp in core/modules/media/tests/src/Functional/Rest/MediaResourceTestBase.php

File

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

Class

EntityResourceTestBase
Even though there is the generic EntityResource, it's necessary for every entity type to have its own test, because they each have different fields, validation constraints, et cetera. It's not because the generic case works, that every case…

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.