class EntityResolverTest
Tests that entities references can be resolved.
@group hal @group legacy
Hierarchy
- class \Drupal\KernelTests\KernelTestBase extends \Drupal\Core\DependencyInjection\ServiceProviderInterface uses \Drupal\KernelTests\AssertLegacyTrait, \Drupal\KernelTests\AssertContentTrait, \Drupal\Tests\RandomGeneratorTrait, \Drupal\Tests\ConfigTestTrait, \Drupal\Tests\ExtensionListTestTrait, \Drupal\Tests\TestRequirementsTrait, \Drupal\Tests\Traits\PhpUnitWarnings, \Drupal\Tests\PhpUnitCompatibilityTrait, \Symfony\Bridge\PhpUnit\ExpectDeprecationTrait implements \PHPUnit\Framework\TestCase
- class \Drupal\Tests\serialization\Kernel\NormalizerTestBase implements \Drupal\KernelTests\KernelTestBase
- class \Drupal\Tests\hal\Kernel\EntityResolverTest implements \Drupal\Tests\serialization\Kernel\NormalizerTestBase
- class \Drupal\Tests\serialization\Kernel\NormalizerTestBase implements \Drupal\KernelTests\KernelTestBase
Expanded class hierarchy of EntityResolverTest
File
-
core/
modules/ hal/ tests/ src/ Kernel/ EntityResolverTest.php, line 17
Namespace
Drupal\Tests\hal\KernelView source
class EntityResolverTest extends NormalizerTestBase {
/**
* Modules to enable.
*
* @var array
*/
protected static $modules = [
'hal',
'rest',
];
/**
* The format being tested.
*
* @var string
*/
protected $format = 'hal_json';
/**
* {@inheritdoc}
*/
protected function setUp() : void {
parent::setUp();
// Create the test field storage.
FieldStorageConfig::create([
'entity_type' => 'entity_test_mulrev',
'field_name' => 'field_test_entity_reference',
'type' => 'entity_reference',
'settings' => [
'target_type' => 'entity_test_mulrev',
],
])->save();
// Create the test field.
FieldConfig::create([
'entity_type' => 'entity_test_mulrev',
'field_name' => 'field_test_entity_reference',
'bundle' => 'entity_test_mulrev',
])->save();
}
/**
* Tests that fields referencing UUIDs can be denormalized.
*/
public function testUuidEntityResolver() {
// Create an entity to get the UUID from.
$entity = EntityTestMulRev::create([
'type' => 'entity_test_mulrev',
]);
$entity->set('name', 'foobar');
$entity->set('field_test_entity_reference', [
[
'target_id' => 1,
],
]);
$entity->save();
$field_uri = Url::fromUri('base:rest/relation/entity_test_mulrev/entity_test_mulrev/field_test_entity_reference', [
'absolute' => TRUE,
])->toString();
$data = [
'_links' => [
'type' => [
'href' => Url::fromUri('base:rest/type/entity_test_mulrev/entity_test_mulrev', [
'absolute' => TRUE,
])->toString(),
],
$field_uri => [
[
'href' => $entity->toUrl()
->toString(),
],
],
],
'_embedded' => [
$field_uri => [
[
'_links' => [
'self' => $entity->toUrl()
->toString(),
],
'uuid' => [
[
'value' => $entity->uuid(),
],
],
],
],
],
];
$denormalized = $this->container
->get('serializer')
->denormalize($data, 'Drupal\\entity_test\\Entity\\EntityTestMulRev', $this->format);
$field_value = $denormalized->get('field_test_entity_reference')
->getValue();
$this->assertEquals(1, $field_value[0]['target_id'], 'Entity reference resolved using UUID.');
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.