You are here

protected function EntityCacheTagsTestBase::setUp in Drupal 10

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

Overrides PageCacheTagsTestBase::setUp

File

core/modules/system/tests/src/Functional/Entity/EntityCacheTagsTestBase.php, line 53

Class

EntityCacheTagsTestBase
Provides helper methods for Entity cache tags tests.

Namespace

Drupal\Tests\system\Functional\Entity

Code

protected function setUp() : void {
  parent::setUp();

  // Give anonymous users permission to view test entities, so that we can
  // verify the cache tags of cached versions of test entity pages.
  $user_role = Role::load(RoleInterface::ANONYMOUS_ID);
  $user_role
    ->grantPermission('view test entity');
  $user_role
    ->save();

  // Create an entity.
  $this->entity = $this
    ->createEntity();

  // If this is an entity with field UI enabled, then add a configurable
  // field. We will use this configurable field in later tests to ensure that
  // field configuration invalidate render cache entries.
  if ($this->entity
    ->getEntityType()
    ->get('field_ui_base_route')) {

    // Add field, so we can modify the field storage and field entities to
    // verify that changes to those indeed clear cache tags.
    FieldStorageConfig::create([
      'field_name' => 'configurable_field',
      'entity_type' => $this->entity
        ->getEntityTypeId(),
      'type' => 'test_field',
      'settings' => [],
    ])
      ->save();
    FieldConfig::create([
      'entity_type' => $this->entity
        ->getEntityTypeId(),
      'bundle' => $this->entity
        ->bundle(),
      'field_name' => 'configurable_field',
      'label' => 'Configurable field',
      'settings' => [],
    ])
      ->save();

    // Reload the entity now that a new field has been added to it.
    $storage = $this->container
      ->get('entity_type.manager')
      ->getStorage($this->entity
      ->getEntityTypeId());
    $storage
      ->resetCache();
    $this->entity = $storage
      ->load($this->entity
      ->id());
  }

  // Create a referencing and a non-referencing entity.
  [
    $this->referencingEntity,
    $this->nonReferencingEntity,
  ] = $this
    ->createReferenceTestEntities($this->entity);
}