function BlockResourceTestBase::createEntity

Same name in other branches
  1. 9 core/modules/block/tests/src/Functional/Rest/BlockResourceTestBase.php \Drupal\Tests\block\Functional\Rest\BlockResourceTestBase::createEntity()
  2. 8.9.x core/modules/block/tests/src/Functional/Rest/BlockResourceTestBase.php \Drupal\Tests\block\Functional\Rest\BlockResourceTestBase::createEntity()
  3. 10 core/modules/block/tests/src/Functional/Rest/BlockResourceTestBase.php \Drupal\Tests\block\Functional\Rest\BlockResourceTestBase::createEntity()

Overrides EntityResourceTestBase::createEntity

File

core/modules/block/tests/src/Functional/Rest/BlockResourceTestBase.php, line 50

Class

BlockResourceTestBase

Namespace

Drupal\Tests\block\Functional\Rest

Code

protected function createEntity() {
    $block = Block::create([
        'plugin' => 'llama_block',
        'region' => 'header',
        'id' => 'llama',
        'theme' => 'stark',
    ]);
    // 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;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.