View source
<?php
namespace Drupal\Tests\jsonapi\Functional;
use Drupal\block\Entity\Block;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Url;
class BlockTest extends ResourceTestBase {
protected static $modules = [
'block',
];
protected static $entityTypeId = 'block';
protected static $resourceTypeName = 'block--block';
protected $entity;
protected $defaultTheme = 'classy';
protected function setUpAuthorization($method) {
switch ($method) {
case 'GET':
$this->entity
->setVisibilityConfig('user_role', [])
->save();
break;
}
}
protected function createEntity() {
$block = Block::create([
'plugin' => 'llama_block',
'region' => 'header',
'id' => 'llama',
'theme' => 'classy',
]);
$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;
}
protected function getExpectedDocument() {
$self_url = Url::fromUri('base:/jsonapi/block/block/' . $this->entity
->uuid())
->setAbsolute()
->toString(TRUE)
->getGeneratedUrl();
return [
'jsonapi' => [
'meta' => [
'links' => [
'self' => [
'href' => 'http://jsonapi.org/format/1.0/',
],
],
],
'version' => '1.0',
],
'links' => [
'self' => [
'href' => $self_url,
],
],
'data' => [
'id' => $this->entity
->uuid(),
'type' => 'block--block',
'links' => [
'self' => [
'href' => $self_url,
],
],
'attributes' => [
'weight' => NULL,
'langcode' => 'en',
'status' => TRUE,
'dependencies' => [
'theme' => [
'classy',
],
],
'theme' => 'classy',
'region' => 'header',
'provider' => NULL,
'plugin' => 'llama_block',
'settings' => [
'id' => 'broken',
'label' => '',
'provider' => 'core',
'label_display' => 'visible',
],
'visibility' => [],
'drupal_internal__id' => 'llama',
],
],
];
}
protected function getPostDocument() {
}
protected function getExpectedCacheContexts(array $sparse_fieldset = NULL) {
return array_values(array_diff(parent::getExpectedCacheContexts(), [
'user.permissions',
]));
}
protected function getExpectedCacheTags(array $sparse_fieldset = NULL) {
return array_values(array_diff(parent::getExpectedCacheTags(), [
'config:user.role.anonymous',
]));
}
protected function getExpectedUnauthorizedAccessMessage($method) {
switch ($method) {
case 'GET':
return "The block visibility condition 'user_role' denied access.";
default:
return parent::getExpectedUnauthorizedAccessMessage($method);
}
}
protected function getExpectedUnauthorizedAccessCacheability() {
return parent::getExpectedUnauthorizedAccessCacheability()
->setCacheTags([
'4xx-response',
'config:block.block.llama',
'http_response',
'user:2',
])
->setCacheContexts([
'url.site',
'user.roles',
]);
}
protected static function getExpectedCollectionCacheability(AccountInterface $account, array $collection, array $sparse_fieldset = NULL, $filtered = FALSE) {
return parent::getExpectedCollectionCacheability($account, $collection, $sparse_fieldset, $filtered)
->addCacheTags([
'user:2',
])
->addCacheContexts([
'user.roles',
]);
}
}