View source
<?php
namespace Drupal\Tests\block\Functional\Rest;
use Drupal\block\Entity\Block;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Tests\rest\Functional\EntityResource\ConfigEntityResourceTestBase;
abstract class BlockResourceTestBase extends ConfigEntityResourceTestBase {
protected static $modules = [
'block',
];
protected static $entityTypeId = 'block';
protected $entity;
protected function setUpAuthorization($method) {
switch ($method) {
case 'GET':
$this->entity
->setVisibilityConfig('user_role', [])
->save();
break;
case 'POST':
$this
->grantPermissionsToTestedRole([
'administer blocks',
]);
break;
case 'PATCH':
$this
->grantPermissionsToTestedRole([
'administer blocks',
]);
break;
}
}
protected function createEntity() {
$block = Block::create([
'plugin' => 'llama_block',
'region' => 'header',
'id' => 'llama',
'theme' => 'stark',
]);
$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 getExpectedNormalizedEntity() {
$normalization = [
'uuid' => $this->entity
->uuid(),
'id' => 'llama',
'weight' => NULL,
'langcode' => 'en',
'status' => TRUE,
'dependencies' => [
'theme' => [
'stark',
],
],
'theme' => 'stark',
'region' => 'header',
'provider' => NULL,
'plugin' => 'llama_block',
'settings' => [
'id' => 'broken',
'label' => '',
'provider' => 'core',
'label_display' => 'visible',
],
'visibility' => [],
];
return $normalization;
}
protected function getNormalizedPostEntity() {
return [];
}
protected function getExpectedCacheContexts() {
return [
'url.site',
];
}
protected function getExpectedCacheTags() {
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 (new CacheableMetadata())
->setCacheTags([
'4xx-response',
'http_response',
])
->setCacheContexts([
'user.roles',
]);
}
protected function getExpectedUnauthorizedEntityAccessCacheability($is_authenticated) {
return parent::getExpectedUnauthorizedEntityAccessCacheability($is_authenticated)
->addCacheTags([
'config:block.block.llama',
$is_authenticated ? 'user:2' : 'user:0',
]);
}
}