View source
<?php
namespace Drupal\Tests\jsonapi\Kernel\ResourceType;
use Drupal\Core\Cache\Cache;
use Drupal\jsonapi\ResourceType\ResourceType;
use Drupal\node\Entity\NodeType;
use Drupal\Tests\jsonapi\Kernel\JsonapiKernelTestBase;
class ResourceTypeRepositoryTest extends JsonapiKernelTestBase {
protected static $modules = [
'field',
'node',
'serialization',
'system',
'user',
'jsonapi_test_resource_type_building',
];
protected $resourceTypeRepository;
protected function setUp() : void {
parent::setUp();
$this
->installEntitySchema('node');
$this
->installEntitySchema('user');
$this
->installSchema('system', [
'sequences',
]);
$this
->installSchema('node', [
'node_access',
]);
$this
->installSchema('user', [
'users_data',
]);
NodeType::create([
'type' => 'article',
])
->save();
NodeType::create([
'type' => 'page',
])
->save();
NodeType::create([
'type' => '42',
])
->save();
$this->resourceTypeRepository = $this->container
->get('jsonapi.resource_type.repository');
}
public function testAll() {
$all = $this->resourceTypeRepository
->all();
$this
->assertNotEmpty($all);
array_walk($all, function (ResourceType $resource_type) {
$this
->assertNotEmpty($resource_type
->getDeserializationTargetClass());
$this
->assertNotEmpty($resource_type
->getEntityTypeId());
$this
->assertNotEmpty($resource_type
->getTypeName());
});
}
public function testGet($entity_type_id, $bundle, $entity_class) {
$resource_type = $this->resourceTypeRepository
->get($entity_type_id, $bundle);
$this
->assertInstanceOf(ResourceType::class, $resource_type);
$this
->assertSame($entity_class, $resource_type
->getDeserializationTargetClass());
$this
->assertSame($entity_type_id, $resource_type
->getEntityTypeId());
$this
->assertSame($bundle, $resource_type
->getBundle());
$this
->assertSame($entity_type_id . '--' . $bundle, $resource_type
->getTypeName());
}
public function getProvider() {
return [
[
'node',
'article',
'Drupal\\node\\Entity\\Node',
],
[
'node',
'42',
'Drupal\\node\\Entity\\Node',
],
[
'node_type',
'node_type',
'Drupal\\node\\Entity\\NodeType',
],
[
'menu',
'menu',
'Drupal\\system\\Entity\\Menu',
],
];
}
public function testCaching() {
$this
->assertEmpty($this->resourceTypeRepository
->get('node', 'article')
->getRelatableResourceTypesByField('field_relationship'));
$this
->createEntityReferenceField('node', 'article', 'field_relationship', 'Related entity', 'node');
$this
->assertCount(3, $this->resourceTypeRepository
->get('node', 'article')
->getRelatableResourceTypesByField('field_relationship'));
NodeType::create([
'type' => 'camelids',
])
->save();
$this
->assertCount(4, $this->resourceTypeRepository
->get('node', 'article')
->getRelatableResourceTypesByField('field_relationship'));
}
public function testMappingNameConflictCheck($field_name_list) {
$entity_type = \Drupal::entityTypeManager()
->getDefinition('node');
$bundle = 'article';
$reflection_class = new \ReflectionClass($this->resourceTypeRepository);
$reflection_method = $reflection_class
->getMethod('getFields');
$reflection_method
->setAccessible(TRUE);
$this
->expectException(\LogicException::class);
$this
->expectExceptionMessage("The generated alias '{$field_name_list[1]}' for field name '{$field_name_list[0]}' conflicts with an existing field. Please report this in the JSON:API issue queue!");
$reflection_method
->invokeArgs($this->resourceTypeRepository, [
$field_name_list,
$entity_type,
$bundle,
]);
}
public function getFieldsProvider() {
return [
[
[
'type',
'node_type',
],
],
[
[
'id',
'node_id',
],
],
];
}
public function testResourceTypeDisabling() {
$this
->assertFalse($this->resourceTypeRepository
->getByTypeName('node--article')
->isInternal());
$this
->assertFalse($this->resourceTypeRepository
->getByTypeName('node--page')
->isInternal());
$this
->assertFalse($this->resourceTypeRepository
->getByTypeName('user--user')
->isInternal());
$disabled_resource_types = [
'node--page',
'user--user',
];
\Drupal::state()
->set('jsonapi_test_resource_type_builder.disabled_resource_types', $disabled_resource_types);
Cache::invalidateTags([
'jsonapi_resource_types',
]);
$this
->assertFalse($this->resourceTypeRepository
->getByTypeName('node--article')
->isInternal());
$this
->assertTrue($this->resourceTypeRepository
->getByTypeName('node--page')
->isInternal());
$this
->assertTrue($this->resourceTypeRepository
->getByTypeName('user--user')
->isInternal());
}
public function testResourceTypeFieldAliasing() {
$this
->assertSame($this->resourceTypeRepository
->getByTypeName('node--article')
->getPublicName('uid'), 'uid');
$this
->assertSame($this->resourceTypeRepository
->getByTypeName('node--page')
->getPublicName('uid'), 'uid');
$resource_type_field_aliases = [
'node--article' => [
'uid' => 'author',
],
'node--page' => [
'uid' => 'owner',
],
];
\Drupal::state()
->set('jsonapi_test_resource_type_builder.resource_type_field_aliases', $resource_type_field_aliases);
Cache::invalidateTags([
'jsonapi_resource_types',
]);
$this
->assertSame($this->resourceTypeRepository
->getByTypeName('node--article')
->getPublicName('uid'), 'author');
$this
->assertSame($this->resourceTypeRepository
->getByTypeName('node--page')
->getPublicName('uid'), 'owner');
}
public function testResourceTypeFieldDisabling() {
$this
->assertTrue($this->resourceTypeRepository
->getByTypeName('node--article')
->isFieldEnabled('uid'));
$this
->assertTrue($this->resourceTypeRepository
->getByTypeName('node--page')
->isFieldEnabled('uid'));
$disabled_resource_type_fields = [
'node--article' => [
'uid' => TRUE,
],
'node--page' => [
'uid' => FALSE,
],
];
\Drupal::state()
->set('jsonapi_test_resource_type_builder.disabled_resource_type_fields', $disabled_resource_type_fields);
Cache::invalidateTags([
'jsonapi_resource_types',
]);
$this
->assertFalse($this->resourceTypeRepository
->getByTypeName('node--article')
->isFieldEnabled('uid'));
$this
->assertTrue($this->resourceTypeRepository
->getByTypeName('node--page')
->isFieldEnabled('uid'));
}
public function testResourceTypeRenaming() {
\Drupal::state()
->set('jsonapi_test_resource_type_builder.renamed_resource_types', [
'node--article' => 'articles',
'node--page' => 'pages',
]);
Cache::invalidateTags([
'jsonapi_resource_types',
]);
$this
->assertNull($this->resourceTypeRepository
->getByTypeName('node--article'));
$this
->assertInstanceOf(ResourceType::class, $this->resourceTypeRepository
->getByTypeName('articles'));
$this
->assertNull($this->resourceTypeRepository
->getByTypeName('node--page'));
$this
->assertInstanceOf(ResourceType::class, $this->resourceTypeRepository
->getByTypeName('pages'));
}
}