View source
<?php
namespace Drupal\Tests\jsonapi\Functional;
use Drupal\block_content\Entity\BlockContent;
use Drupal\block_content\Entity\BlockContentType;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Url;
use Drupal\Tests\jsonapi\Traits\CommonCollectionFilterAccessTestPatternsTrait;
class BlockContentTest extends ResourceTestBase {
use CommonCollectionFilterAccessTestPatternsTrait;
public static $modules = [
'block_content',
];
protected static $entityTypeId = 'block_content';
protected static $resourceTypeName = 'block_content--basic';
protected $entity;
protected $defaultTheme = 'stark';
protected static $patchProtectedFieldNames = [
'changed' => NULL,
];
protected function setUpAuthorization($method) {
$this
->grantPermissionsToTestedRole([
'administer blocks',
]);
}
public function createEntity() {
if (!BlockContentType::load('basic')) {
$block_content_type = BlockContentType::create([
'id' => 'basic',
'label' => 'basic',
'revision' => FALSE,
]);
$block_content_type
->save();
block_content_add_body_field($block_content_type
->id());
}
$block_content = BlockContent::create([
'info' => 'Llama',
'type' => 'basic',
'body' => [
'value' => 'The name "llama" was adopted by European settlers from native Peruvians.',
'format' => 'plain_text',
],
])
->setUnpublished();
$block_content
->save();
return $block_content;
}
protected function getExpectedDocument() {
$self_url = Url::fromUri('base:/jsonapi/block_content/basic/' . $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_content--basic',
'links' => [
'self' => [
'href' => $self_url,
],
],
'attributes' => [
'body' => [
'value' => 'The name "llama" was adopted by European settlers from native Peruvians.',
'format' => 'plain_text',
'summary' => NULL,
'processed' => "<p>The name "llama" was adopted by European settlers from native Peruvians.</p>\n",
],
'changed' => (new \DateTime())
->setTimestamp($this->entity
->getChangedTime())
->setTimezone(new \DateTimeZone('UTC'))
->format(\DateTime::RFC3339),
'info' => 'Llama',
'revision_log' => NULL,
'revision_created' => (new \DateTime())
->setTimestamp($this->entity
->getRevisionCreationTime())
->setTimezone(new \DateTimeZone('UTC'))
->format(\DateTime::RFC3339),
'revision_translation_affected' => TRUE,
'status' => FALSE,
'langcode' => 'en',
'default_langcode' => TRUE,
'drupal_internal__id' => 1,
'drupal_internal__revision_id' => 1,
'reusable' => TRUE,
],
'relationships' => [
'block_content_type' => [
'data' => [
'id' => BlockContentType::load('basic')
->uuid(),
'type' => 'block_content_type--block_content_type',
],
'links' => [
'related' => [
'href' => $self_url . '/block_content_type',
],
'self' => [
'href' => $self_url . '/relationships/block_content_type',
],
],
],
'revision_user' => [
'data' => NULL,
'links' => [
'related' => [
'href' => $self_url . '/revision_user',
],
'self' => [
'href' => $self_url . '/relationships/revision_user',
],
],
],
],
],
];
}
protected function getPostDocument() {
return [
'data' => [
'type' => 'block_content--basic',
'attributes' => [
'info' => 'Dramallama',
],
],
];
}
protected function getExpectedUnauthorizedAccessCacheability() {
return parent::getExpectedUnauthorizedAccessCacheability()
->addCacheTags([
'block_content:1',
]);
}
protected function getExpectedCacheTags(array $sparse_fieldset = NULL) {
$tags = parent::getExpectedCacheTags($sparse_fieldset);
if ($sparse_fieldset === NULL || in_array('body', $sparse_fieldset)) {
$tags = Cache::mergeTags($tags, [
'config:filter.format.plain_text',
]);
}
return $tags;
}
protected function getExpectedCacheContexts(array $sparse_fieldset = NULL) {
$contexts = parent::getExpectedCacheContexts($sparse_fieldset);
if ($sparse_fieldset === NULL || in_array('body', $sparse_fieldset)) {
$contexts = Cache::mergeContexts($contexts, [
'languages:language_interface',
'theme',
]);
}
return $contexts;
}
public function testRelated() {
$this
->markTestSkipped('Remove this in https://www.drupal.org/project/drupal/issues/2940339');
}
public function testCollectionFilterAccess() {
$this->entity
->setPublished()
->save();
$this
->doTestCollectionFilterAccessForPublishableEntities('info', NULL, 'administer blocks');
}
}