Chris@18: entity->setVisibilityConfig('user_role', [])->save(); Chris@18: break; Chris@18: } Chris@18: } Chris@18: Chris@18: /** Chris@18: * {@inheritdoc} Chris@18: */ Chris@18: protected function createEntity() { Chris@18: $block = Block::create([ Chris@18: 'plugin' => 'llama_block', Chris@18: 'region' => 'header', Chris@18: 'id' => 'llama', Chris@18: 'theme' => 'classy', Chris@18: ]); Chris@18: // All blocks can be viewed by the anonymous user by default. An interesting Chris@18: // side effect of this is that any anonymous user is also able to read the Chris@18: // corresponding block config entity via REST, even if an authentication Chris@18: // provider is configured for the block config entity REST resource! In Chris@18: // other words: Block entities do not distinguish between 'view' as in Chris@18: // "render on a page" and 'view' as in "read the configuration". Chris@18: // This prevents that. Chris@18: // @todo Fix this in https://www.drupal.org/node/2820315. Chris@18: $block->setVisibilityConfig('user_role', [ Chris@18: 'id' => 'user_role', Chris@18: 'roles' => ['non-existing-role' => 'non-existing-role'], Chris@18: 'negate' => FALSE, Chris@18: 'context_mapping' => [ Chris@18: 'user' => '@user.current_user_context:current_user', Chris@18: ], Chris@18: ]); Chris@18: $block->save(); Chris@18: Chris@18: return $block; Chris@18: } Chris@18: Chris@18: /** Chris@18: * {@inheritdoc} Chris@18: */ Chris@18: protected function getExpectedDocument() { Chris@18: $self_url = Url::fromUri('base:/jsonapi/block/block/' . $this->entity->uuid())->setAbsolute()->toString(TRUE)->getGeneratedUrl(); Chris@18: return [ Chris@18: 'jsonapi' => [ Chris@18: 'meta' => [ Chris@18: 'links' => [ Chris@18: 'self' => ['href' => 'http://jsonapi.org/format/1.0/'], Chris@18: ], Chris@18: ], Chris@18: 'version' => '1.0', Chris@18: ], Chris@18: 'links' => [ Chris@18: 'self' => ['href' => $self_url], Chris@18: ], Chris@18: 'data' => [ Chris@18: 'id' => $this->entity->uuid(), Chris@18: 'type' => 'block--block', Chris@18: 'links' => [ Chris@18: 'self' => ['href' => $self_url], Chris@18: ], Chris@18: 'attributes' => [ Chris@18: 'weight' => NULL, Chris@18: 'langcode' => 'en', Chris@18: 'status' => TRUE, Chris@18: 'dependencies' => [ Chris@18: 'theme' => [ Chris@18: 'classy', Chris@18: ], Chris@18: ], Chris@18: 'theme' => 'classy', Chris@18: 'region' => 'header', Chris@18: 'provider' => NULL, Chris@18: 'plugin' => 'llama_block', Chris@18: 'settings' => [ Chris@18: 'id' => 'broken', Chris@18: 'label' => '', Chris@18: 'provider' => 'core', Chris@18: 'label_display' => 'visible', Chris@18: ], Chris@18: 'visibility' => [], Chris@18: 'drupal_internal__id' => 'llama', Chris@18: ], Chris@18: ], Chris@18: ]; Chris@18: } Chris@18: Chris@18: /** Chris@18: * {@inheritdoc} Chris@18: */ Chris@18: protected function getPostDocument() { Chris@18: // @todo Update once https://www.drupal.org/node/2300677 is fixed. Chris@18: } Chris@18: Chris@18: /** Chris@18: * {@inheritdoc} Chris@18: */ Chris@18: protected function getExpectedCacheContexts(array $sparse_fieldset = NULL) { Chris@18: // @see ::createEntity() Chris@18: return array_values(array_diff(parent::getExpectedCacheContexts(), ['user.permissions'])); Chris@18: } Chris@18: Chris@18: /** Chris@18: * {@inheritdoc} Chris@18: */ Chris@18: protected function getExpectedCacheTags(array $sparse_fieldset = NULL) { Chris@18: // Because the 'user.permissions' cache context is missing, the cache tag Chris@18: // for the anonymous user role is never added automatically. Chris@18: return array_values(array_diff(parent::getExpectedCacheTags(), ['config:user.role.anonymous'])); Chris@18: } Chris@18: Chris@18: /** Chris@18: * {@inheritdoc} Chris@18: */ Chris@18: protected function getExpectedUnauthorizedAccessMessage($method) { Chris@18: switch ($method) { Chris@18: case 'GET': Chris@18: return "The block visibility condition 'user_role' denied access."; Chris@18: Chris@18: default: Chris@18: return parent::getExpectedUnauthorizedAccessMessage($method); Chris@18: } Chris@18: } Chris@18: Chris@18: /** Chris@18: * {@inheritdoc} Chris@18: */ Chris@18: protected function getExpectedUnauthorizedAccessCacheability() { Chris@18: // @see \Drupal\block\BlockAccessControlHandler::checkAccess() Chris@18: return parent::getExpectedUnauthorizedAccessCacheability() Chris@18: ->setCacheTags([ Chris@18: '4xx-response', Chris@18: 'config:block.block.llama', Chris@18: 'http_response', Chris@18: 'user:2', Chris@18: ]) Chris@18: ->setCacheContexts(['url.site', 'user.roles']); Chris@18: } Chris@18: Chris@18: /** Chris@18: * {@inheritdoc} Chris@18: */ Chris@18: protected static function getExpectedCollectionCacheability(AccountInterface $account, array $collection, array $sparse_fieldset = NULL, $filtered = FALSE) { Chris@18: return parent::getExpectedCollectionCacheability($account, $collection, $sparse_fieldset, $filtered) Chris@18: ->addCacheTags(['user:2']) Chris@18: ->addCacheContexts(['user.roles']); Chris@18: } Chris@18: Chris@18: }