View source
<?php
namespace Drupal\Tests\jsonapi\Functional;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Url;
use Drupal\language\Entity\ContentLanguageSettings;
use Drupal\node\Entity\NodeType;
class ContentLanguageSettingsTest extends ResourceTestBase {
public static $modules = [
'language',
'node',
];
protected static $entityTypeId = 'language_content_settings';
protected static $resourceTypeName = 'language_content_settings--language_content_settings';
protected $entity;
protected function setUpAuthorization($method) {
$this
->grantPermissionsToTestedRole([
'administer languages',
]);
}
protected function createEntity() {
$camelids = NodeType::create([
'name' => 'Camelids',
'type' => 'camelids',
]);
$camelids
->save();
$entity = ContentLanguageSettings::create([
'target_entity_type_id' => 'node',
'target_bundle' => 'camelids',
]);
$entity
->setDefaultLangcode('site_default')
->save();
return $entity;
}
protected function getExpectedDocument() {
$self_url = Url::fromUri('base:/jsonapi/language_content_settings/language_content_settings/' . $this->entity
->uuid())
->setAbsolute()
->toString(TRUE)
->getGeneratedUrl();
return [
'jsonapi' => [
'meta' => [
'links' => [
'self' => 'http://jsonapi.org/format/1.0/',
],
],
'version' => '1.0',
],
'links' => [
'self' => $self_url,
],
'data' => [
'id' => $this->entity
->uuid(),
'type' => 'language_content_settings--language_content_settings',
'links' => [
'self' => $self_url,
],
'attributes' => [
'default_langcode' => 'site_default',
'dependencies' => [
'config' => [
'node.type.camelids',
],
],
'id' => 'node.camelids',
'langcode' => 'en',
'language_alterable' => FALSE,
'status' => TRUE,
'target_bundle' => 'camelids',
'target_entity_type_id' => 'node',
'uuid' => $this->entity
->uuid(),
],
],
];
}
protected function getPostDocument() {
}
protected function getExpectedCacheContexts(array $sparse_fieldset = NULL) {
return Cache::mergeContexts(parent::getExpectedCacheContexts(), [
'languages:language_interface',
]);
}
protected function createAnotherEntity($key) {
NodeType::create([
'name' => 'Llamaids',
'type' => 'llamaids',
])
->save();
$entity = ContentLanguageSettings::create([
'target_entity_type_id' => 'node',
'target_bundle' => 'llamaids',
]);
$entity
->setDefaultLangcode('site_default');
$entity
->save();
return $entity;
}
protected static function getExpectedCollectionCacheability(array $collection, array $sparse_fieldset = NULL, AccountInterface $account, $filtered = FALSE) {
$cacheability = parent::getExpectedCollectionCacheability($collection, $sparse_fieldset, $account, $filtered);
if (static::entityAccess(reset($collection), 'view', $account)
->isAllowed()) {
$cacheability
->addCacheContexts([
'languages:language_interface',
]);
}
return $cacheability;
}
}