function BlockTest::createEntity

Same name and namespace in other branches
  1. 9 core/modules/jsonapi/tests/src/Functional/BlockTest.php \Drupal\Tests\jsonapi\Functional\BlockTest::createEntity()
  2. 8.9.x core/modules/jsonapi/tests/src/Functional/BlockTest.php \Drupal\Tests\jsonapi\Functional\BlockTest::createEntity()
  3. 10 core/modules/jsonapi/tests/src/Functional/BlockTest.php \Drupal\Tests\jsonapi\Functional\BlockTest::createEntity()

Overrides ResourceTestBase::createEntity

File

core/modules/jsonapi/tests/src/Functional/BlockTest.php, line 60

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' => '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.