View source
<?php
namespace Drupal\Tests\jsonapi\Kernel\Context;
use Drupal\entity_test\Entity\EntityTestBundle;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\Tests\jsonapi\Kernel\JsonapiKernelTestBase;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
class FieldResolverTest extends JsonapiKernelTestBase {
public static $modules = [
'entity_test',
'jsonapi_test_field_filter_access',
'serialization',
'field',
'text',
'user',
];
protected $sut;
protected $resourceTypeRepository;
protected function setUp() {
parent::setUp();
$this->sut = \Drupal::service('jsonapi.field_resolver');
$this
->makeBundle('bundle1');
$this
->makeBundle('bundle2');
$this
->makeBundle('bundle3');
$this
->makeField('string', 'field_test1', 'entity_test_with_bundle', [
'bundle1',
]);
$this
->makeField('string', 'field_test2', 'entity_test_with_bundle', [
'bundle1',
]);
$this
->makeField('string', 'field_test3', 'entity_test_with_bundle', [
'bundle2',
'bundle3',
]);
$settings = [
'target_type' => 'entity_test_with_bundle',
];
$this
->makeField('entity_reference', 'field_test_ref1', 'entity_test_with_bundle', [
'bundle1',
], $settings, [
'handler_settings' => [
'target_bundles' => [
'bundle2',
'bundle3',
],
],
]);
$this
->makeField('entity_reference', 'field_test_ref2', 'entity_test_with_bundle', [
'bundle1',
], $settings);
$this
->makeField('entity_reference', 'field_test_ref3', 'entity_test_with_bundle', [
'bundle2',
'bundle3',
], $settings);
$this
->makeField('text', 'field_test_text', 'entity_test_with_bundle', [
'bundle1',
'bundle2',
]);
$this->resourceTypeRepository = $this->container
->get('jsonapi.resource_type.repository');
}
public function testResolveInternalIncludePath($expect, $external_path, $entity_type_id = 'entity_test_with_bundle', $bundle = 'bundle1') {
$path_parts = explode('.', $external_path);
$resource_type = $this->resourceTypeRepository
->get($entity_type_id, $bundle);
$this
->assertEquals($expect, $this->sut
->resolveInternalIncludePath($resource_type, $path_parts));
}
public function resolveInternalIncludePathProvider() {
return [
'entity reference' => [
[
[
'field_test_ref2',
],
],
'field_test_ref2',
],
'entity reference with multi target bundles' => [
[
[
'field_test_ref1',
],
],
'field_test_ref1',
],
'entity reference then another entity reference' => [
[
[
'field_test_ref1',
'field_test_ref3',
],
],
'field_test_ref1.field_test_ref3',
],
];
}
public function testResolveInternalIncludePathError($entity_type, $bundle, $external_path, $expected_message = '') {
$path_parts = explode('.', $external_path);
$this
->setExpectedException(BadRequestHttpException::class, $expected_message);
$resource_type = $this->resourceTypeRepository
->get($entity_type, $bundle);
$this->sut
->resolveInternalIncludePath($resource_type, $path_parts);
}
public function resolveInternalIncludePathErrorProvider() {
return [
[
'entity_test_with_bundle',
'bundle1',
'host.fail!!.deep',
],
[
'entity_test_with_bundle',
'bundle2',
'field_test_ref2',
],
[
'entity_test_with_bundle',
'bundle1',
'field_test_ref3',
],
[
'entity_test_with_bundle',
'bundle1',
'field_test_ref1.field_test1',
],
[
'entity_test_with_bundle',
'bundle1',
'field_test_ref1.field_test2',
],
[
'entity_test_with_bundle',
'bundle1',
'field_test_ref1.field_test_ref1',
],
[
'entity_test_with_bundle',
'bundle1',
'field_test_ref1.field_test_ref2',
],
[
'entity_test_with_bundle',
'bundle1',
'field_test1',
'`field_test1` is not a valid relationship field name.',
],
[
'entity_test_with_bundle',
'bundle1',
'field_test_ref1.field_test3',
'`field_test_ref1.field_test3` is not a valid include path.',
],
];
}
public function testResolveInternalEntityQueryPath($expect, $external_path, $entity_type_id = 'entity_test_with_bundle', $bundle = 'bundle1') {
$this
->assertEquals($expect, $this->sut
->resolveInternalEntityQueryPath($entity_type_id, $bundle, $external_path));
}
public function resolveInternalEntityQueryPathProvider() {
return [
'config entity as base' => [
'uuid',
'uuid',
'entity_test_bundle',
'entity_test_bundle',
],
'config entity as target' => [
'type.entity:entity_test_bundle.uuid',
'type.uuid',
],
'primitive field; variation A' => [
'field_test1',
'field_test1',
],
'primitive field; variation B' => [
'field_test2',
'field_test2',
],
'entity reference then a primitive field; variation A' => [
'field_test_ref2.entity:entity_test_with_bundle.field_test1',
'field_test_ref2.field_test1',
],
'entity reference then a primitive field; variation B' => [
'field_test_ref2.entity:entity_test_with_bundle.field_test2',
'field_test_ref2.field_test2',
],
'entity reference then a complex field with no property specifier' => [
'field_test_ref2.entity:entity_test_with_bundle.field_test_text',
'field_test_ref2.field_test_text',
],
'entity reference then a complex field with property specifier `value`' => [
'field_test_ref2.entity:entity_test_with_bundle.field_test_text.value',
'field_test_ref2.field_test_text.value',
],
'entity reference then a complex field with property specifier `format`' => [
'field_test_ref2.entity:entity_test_with_bundle.field_test_text.format',
'field_test_ref2.field_test_text.format',
],
'entity reference then no delta with property specifier `target_id`' => [
'field_test_ref1.target_id',
'field_test_ref1.target_id',
],
'entity reference then delta 0 with property specifier `target_id`' => [
'field_test_ref1.0.target_id',
'field_test_ref1.0.target_id',
],
'entity reference then delta 1 with property specifier `target_id`' => [
'field_test_ref1.1.target_id',
'field_test_ref1.1.target_id',
],
'entity reference then no reference property then a complex field' => [
'field_test_ref1.entity:entity_test_with_bundle.field_test_text',
'field_test_ref1.field_test_text',
],
'entity reference then reference property then a complex field' => [
'field_test_ref1.entity.field_test_text',
'field_test_ref1.entity.field_test_text',
],
'entity reference then no reference property and a complex field with property specifier `value`' => [
'field_test_ref1.entity:entity_test_with_bundle.field_test_text.value',
'field_test_ref1.field_test_text.value',
],
'entity reference then a reference property and a complex field with property specifier `value`' => [
'field_test_ref1.entity.field_test_text.value',
'field_test_ref1.entity.field_test_text.value',
],
'entity reference then no reference property and a complex field with property specifier `format`' => [
'field_test_ref1.entity:entity_test_with_bundle.field_test_text.format',
'field_test_ref1.field_test_text.format',
],
'entity reference then a reference property and a complex field with property specifier `format`' => [
'field_test_ref1.entity.field_test_text.format',
'field_test_ref1.entity.field_test_text.format',
],
'entity reference then property specifier `entity:entity_test_with_bundle` then a complex field' => [
'field_test_ref1.entity:entity_test_with_bundle.field_test_text',
'field_test_ref1.entity:entity_test_with_bundle.field_test_text',
],
'entity reference then property specifier `entity:entity_test_with_bundle` then a complex field with property specifier `value`' => [
'field_test_ref1.entity:entity_test_with_bundle.field_test_text.value',
'field_test_ref1.entity:entity_test_with_bundle.field_test_text.value',
],
'entity reference with a delta and no reference property then a complex field and property specifier `value`' => [
'field_test_ref1.0.entity:entity_test_with_bundle.field_test_text.value',
'field_test_ref1.0.field_test_text.value',
],
'entity reference with a delta and a reference property then a complex field and property specifier `value`' => [
'field_test_ref1.0.entity.field_test_text.value',
'field_test_ref1.0.entity.field_test_text.value',
],
'entity reference with no reference property then another entity reference with no reference property a complex field with property specifier `value`' => [
'field_test_ref1.entity:entity_test_with_bundle.field_test_ref3.entity:entity_test_with_bundle.field_test_text.value',
'field_test_ref1.field_test_ref3.field_test_text.value',
],
'entity reference with a reference property then another entity reference with no reference property a complex field with property specifier `value`' => [
'field_test_ref1.entity.field_test_ref3.entity:entity_test_with_bundle.field_test_text.value',
'field_test_ref1.entity.field_test_ref3.field_test_text.value',
],
'entity reference with no reference property then another entity reference with a reference property a complex field with property specifier `value`' => [
'field_test_ref1.entity:entity_test_with_bundle.field_test_ref3.entity.field_test_text.value',
'field_test_ref1.field_test_ref3.entity.field_test_text.value',
],
'entity reference with a reference property then another entity reference with a reference property a complex field with property specifier `value`' => [
'field_test_ref1.entity.field_test_ref3.entity.field_test_text.value',
'field_test_ref1.entity.field_test_ref3.entity.field_test_text.value',
],
'entity reference with target bundles then property specifier `entity:entity_test_with_bundle` then a primitive field on multiple bundles' => [
'field_test_ref1.entity:entity_test_with_bundle.field_test3',
'field_test_ref1.entity:entity_test_with_bundle.field_test3',
],
'entity reference without target bundles then property specifier `entity:entity_test_with_bundle` then a primitive field on a single bundle' => [
'field_test_ref2.entity:entity_test_with_bundle.field_test1',
'field_test_ref2.entity:entity_test_with_bundle.field_test1',
],
'entity reference without target bundles then property specifier `entity:entity_test_with_bundle` then a primitive field on multiple bundles' => [
'field_test_ref3.entity:entity_test_with_bundle.field_test3',
'field_test_ref3.entity:entity_test_with_bundle.field_test3',
'entity_test_with_bundle',
'bundle2',
],
'entity reference without target bundles then property specifier `entity:entity_test_with_bundle` then a primitive field on a single bundle starting from a different resource type' => [
'field_test_ref3.entity:entity_test_with_bundle.field_test2',
'field_test_ref3.entity:entity_test_with_bundle.field_test2',
'entity_test_with_bundle',
'bundle3',
],
'entity reference then property specifier `entity:entity_test_with_bundle` then another entity reference before a primitive field' => [
'field_test_ref1.entity:entity_test_with_bundle.field_test_ref3.entity:entity_test_with_bundle.field_test2',
'field_test_ref1.entity:entity_test_with_bundle.field_test_ref3.field_test2',
],
];
}
public function testResolveInternalEntityQueryPathError($entity_type, $bundle, $external_path, $expected_message = '') {
$this
->setExpectedException(BadRequestHttpException::class, $expected_message);
$this->sut
->resolveInternalEntityQueryPath($entity_type, $bundle, $external_path);
}
public function resolveInternalEntityQueryPathErrorProvider() {
return [
'nested fields' => [
'entity_test_with_bundle',
'bundle1',
'none.of.these.exist',
],
'field does not exist on bundle' => [
'entity_test_with_bundle',
'bundle2',
'field_test_ref2',
],
'field does not exist on different bundle' => [
'entity_test_with_bundle',
'bundle1',
'field_test_ref3',
],
'field does not exist on targeted bundle' => [
'entity_test_with_bundle',
'bundle1',
'field_test_ref1.field_test1',
],
'different field does not exist on same targeted bundle' => [
'entity_test_with_bundle',
'bundle1',
'field_test_ref1.field_test2',
],
'entity reference field does not exist on targeted bundle' => [
'entity_test_with_bundle',
'bundle1',
'field_test_ref1.field_test_ref1',
],
'different entity reference field does not exist on same targeted bundle' => [
'entity_test_with_bundle',
'bundle1',
'field_test_ref1.field_test_ref2',
],
'message correctly identifies missing field' => [
'entity_test_with_bundle',
'bundle1',
'field_test_ref1.entity:entity_test_with_bundle.field_test1',
'Invalid nested filtering. The field `field_test1`, given in the path `field_test_ref1.entity:entity_test_with_bundle.field_test1`, does not exist.',
],
'message correctly identifies different missing field' => [
'entity_test_with_bundle',
'bundle1',
'field_test_ref1.entity:entity_test_with_bundle.field_test2',
'Invalid nested filtering. The field `field_test2`, given in the path `field_test_ref1.entity:entity_test_with_bundle.field_test2`, does not exist.',
],
'message correctly identifies missing entity reference field' => [
'entity_test_with_bundle',
'bundle2',
'field_test_ref1.entity:entity_test_with_bundle.field_test2',
'Invalid nested filtering. The field `field_test_ref1`, given in the path `field_test_ref1.entity:entity_test_with_bundle.field_test2`, does not exist.',
],
];
}
protected function makeBundle($name) {
EntityTestBundle::create([
'id' => $name,
])
->save();
}
protected function makeField($type, $name, $entity_type, array $bundles, array $storage_settings = [], array $config_settings = []) {
$storage_config = [
'field_name' => $name,
'type' => $type,
'entity_type' => $entity_type,
'settings' => $storage_settings,
];
FieldStorageConfig::create($storage_config)
->save();
foreach ($bundles as $bundle) {
FieldConfig::create([
'field_name' => $name,
'entity_type' => $entity_type,
'bundle' => $bundle,
'settings' => $config_settings,
])
->save();
}
}
}