You are here

protected function EntityCacheTagsTestBase::setUp in Drupal 8

Same name in this branch
  1. 8 core/modules/system/src/Tests/Entity/EntityCacheTagsTestBase.php \Drupal\system\Tests\Entity\EntityCacheTagsTestBase::setUp()
  2. 8 core/modules/system/tests/src/Functional/Entity/EntityCacheTagsTestBase.php \Drupal\Tests\system\Functional\Entity\EntityCacheTagsTestBase::setUp()

Sets up a Drupal site for running functional and integration tests.

Installs Drupal with the installation profile specified in \Drupal\simpletest\WebTestBase::$profile into the prefixed database.

Afterwards, installs any additional modules specified in the static \Drupal\simpletest\WebTestBase::$modules property of each class in the class hierarchy.

After installation all caches are flushed and several configuration values are reset to the values of the parent site executing the test, since the default values may be incompatible with the environment in which tests are being executed.

Overrides PageCacheTagsTestBase::setUp

File

core/modules/system/src/Tests/Entity/EntityCacheTagsTestBase.php, line 60

Class

EntityCacheTagsTestBase
Provides helper methods for Entity cache tags tests.

Namespace

Drupal\system\Tests\Entity

Code

protected function setUp() {
  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.
  list($this->referencingEntity, $this->nonReferencingEntity, ) = $this
    ->createReferenceTestEntities($this->entity);
}