You are here

protected function ResourceTestBase::setUp in Drupal 10

Same name in this branch
  1. 10 core/modules/jsonapi/tests/src/Functional/ResourceTestBase.php \Drupal\Tests\jsonapi\Functional\ResourceTestBase::setUp()
  2. 10 core/modules/rest/tests/src/Functional/ResourceTestBase.php \Drupal\Tests\rest\Functional\ResourceTestBase::setUp()
Same name and namespace in other branches
  1. 8 core/modules/rest/tests/src/Functional/ResourceTestBase.php \Drupal\Tests\rest\Functional\ResourceTestBase::setUp()
  2. 9 core/modules/rest/tests/src/Functional/ResourceTestBase.php \Drupal\Tests\rest\Functional\ResourceTestBase::setUp()
2 methods override ResourceTestBase::setUp()
EntityResourceTestBase::setUp in core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php
LayoutRestTestBase::setUp in core/modules/layout_builder/tests/src/Functional/Rest/LayoutRestTestBase.php

File

core/modules/rest/tests/src/Functional/ResourceTestBase.php, line 101

Class

ResourceTestBase
Subclass this for every REST resource, every format and every auth provider.

Namespace

Drupal\Tests\rest\Functional

Code

protected function setUp() : void {
  parent::setUp();
  $this->serializer = $this->container
    ->get('serializer');

  // Ensure the anonymous user role has no permissions at all.
  $user_role = Role::load(RoleInterface::ANONYMOUS_ID);
  foreach ($user_role
    ->getPermissions() as $permission) {
    $user_role
      ->revokePermission($permission);
  }
  $user_role
    ->save();
  assert([] === $user_role
    ->getPermissions(), 'The anonymous user role has no permissions at all.');
  if (static::$auth !== FALSE) {

    // Ensure the authenticated user role has no permissions at all.
    $user_role = Role::load(RoleInterface::AUTHENTICATED_ID);
    foreach ($user_role
      ->getPermissions() as $permission) {
      $user_role
        ->revokePermission($permission);
    }
    $user_role
      ->save();
    assert([] === $user_role
      ->getPermissions(), 'The authenticated user role has no permissions at all.');

    // Create an account.
    $this->account = $this
      ->createUser();
  }
  else {

    // Otherwise, also create an account, so that any test involving User
    // entities will have the same user IDs regardless of authentication.
    $this
      ->createUser();
  }
  $this->resourceConfigStorage = $this->container
    ->get('entity_type.manager')
    ->getStorage('rest_resource_config');

  // Ensure there's a clean slate: delete all REST resource config entities.
  $this->resourceConfigStorage
    ->delete($this->resourceConfigStorage
    ->loadMultiple());
  $this
    ->refreshTestStateAfterRestConfigChange();
}