You are here

protected function BlockTest::createEntity in JSON:API 8.2

Same name and namespace in other branches
  1. 8 tests/src/Functional/BlockTest.php \Drupal\Tests\jsonapi\Functional\BlockTest::createEntity()

Creates the entity to be tested.

Return value

\Drupal\Core\Entity\EntityInterface The entity to be tested.

Overrides ResourceTestBase::createEntity

File

tests/src/Functional/BlockTest.php, line 52

Class

BlockTest
JSON:API integration test for the "Block" config entity type.

Namespace

Drupal\Tests\jsonapi\Functional

Code

protected function createEntity() {
  $block = Block::create([
    'plugin' => 'llama_block',
    'region' => 'header',
    'id' => 'llama',
    'theme' => 'classy',
  ]);

  // All blocks can be viewed by the anonymous user by default. An interesting
  // side effect of this is that any anonymous user is also able to read the
  // corresponding block config entity via REST, even if an authentication
  // provider is configured for the block config entity REST resource! In
  // other words: Block entities do not distinguish between 'view' as in
  // "render on a page" and 'view' as in "read the configuration".
  // This prevents that.
  // @todo Fix this in https://www.drupal.org/node/2820315.
  $block
    ->setVisibilityConfig('user_role', [
    'id' => 'user_role',
    'roles' => [
      'non-existing-role' => 'non-existing-role',
    ],
    'negate' => FALSE,
    'context_mapping' => [
      'user' => '@user.current_user_context:current_user',
    ],
  ]);
  $block
    ->save();
  return $block;
}