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\rest\Functional\BcTimestampNormalizerUnixTestTrait;
use Drupal\Tests\jsonapi\Traits\CommonCollectionFilterAccessTestPatternsTrait;
class BlockContentTest extends ResourceTestBase {
use BcTimestampNormalizerUnixTestTrait;
use CommonCollectionFilterAccessTestPatternsTrait;
public static $modules = [
'block_content',
];
protected static $entityTypeId = 'block_content';
protected static $resourceTypeName = 'block_content--basic';
protected $entity;
protected static $patchProtectedFieldNames = [
'changed' => NULL,
];
protected static $uniqueFieldNames = [
'url',
];
protected function setUpAuthorization($method) {
$this
->grantPermissionsToTestedRole([
'administer blocks',
]);
}
public function createEntity() {
if (floatval(\Drupal::VERSION) < 8.5) {
return;
}
if (!BlockContentType::load('basic')) {
$block_content_type = BlockContentType::create([
'id' => 'basic',
'label' => 'basic',
'revision' => TRUE,
]);
$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',
],
])
->setPublished(FALSE);
$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();
$expected_document = [
'jsonapi' => [
'meta' => [
'links' => [
'self' => 'http://jsonapi.org/format/1.0/',
],
],
'version' => '1.0',
],
'links' => [
'self' => $self_url,
],
'data' => [
'id' => $this->entity
->uuid(),
'type' => 'block_content--basic',
'links' => [
'self' => $self_url,
],
'attributes' => [
'id' => 1,
'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' => $this->entity
->getChangedTime(),
'info' => 'Llama',
'revision_id' => 1,
'revision_log' => NULL,
'revision_created' => (int) $this->entity
->getRevisionCreationTime(),
'revision_translation_affected' => TRUE,
'status' => FALSE,
'langcode' => 'en',
'default_langcode' => TRUE,
'uuid' => $this->entity
->uuid(),
],
'relationships' => [
'type' => [
'data' => [
'id' => BlockContentType::load('basic')
->uuid(),
'type' => 'block_content_type--block_content_type',
],
'links' => [
'related' => $self_url . '/type',
'self' => $self_url . '/relationships/type',
],
],
'revision_user' => [
'data' => NULL,
'links' => [
'related' => $self_url . '/revision_user',
'self' => $self_url . '/relationships/revision_user',
],
],
],
],
];
if (floatval(\Drupal::VERSION) >= 8.6) {
$expected_document['data']['attributes']['reusable'] = TRUE;
}
return $expected_document;
}
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 testGetIndividual() {
if (floatval(\Drupal::VERSION) < 8.5) {
$this
->markTestSkipped('BlockContent entities were made publishable in 8.5, this is necessary for this test coverage to work.');
}
return parent::testGetIndividual();
}
public function testPostIndividual() {
if (floatval(\Drupal::VERSION) < 8.5) {
$this
->markTestSkipped('BlockContent entities were made publishable in 8.5, this is necessary for this test coverage to work.');
}
return parent::testGetIndividual();
}
public function testPatchIndividual() {
if (floatval(\Drupal::VERSION) < 8.5) {
$this
->markTestSkipped('BlockContent entities were made publishable in 8.5, this is necessary for this test coverage to work.');
}
return parent::testPatchIndividual();
}
public function testDeleteIndividual() {
if (floatval(\Drupal::VERSION) < 8.5) {
$this
->markTestSkipped('BlockContent entities were made publishable in 8.5, this is necessary for this test coverage to work.');
}
return parent::testDeleteIndividual();
}
public function testRelated() {
$this
->markTestSkipped('Remove this in https://www.drupal.org/project/jsonapi/issues/2940339');
}
public function testCollectionFilterAccess() {
$this->entity
->setPublished()
->save();
$this
->doTestCollectionFilterAccessForPublishableEntities('info', NULL, 'administer blocks');
}
}