You are here

protected function EntityResourceTestBase::setUp in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php \Drupal\Tests\rest\Functional\EntityResource\EntityResourceTestBase::setUp()
  2. 9 core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php \Drupal\Tests\rest\Functional\EntityResource\EntityResourceTestBase::setUp()

Overrides ResourceTestBase::setUp

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 178

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

protected function setUp() : void {
  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();
    }
  }
}