function RelatedResourceTypesTest::testGetRelatableResourceTypesFromFieldDefinition

Same name and namespace in other branches
  1. 9 core/modules/jsonapi/tests/src/Kernel/ResourceType/RelatedResourceTypesTest.php \Drupal\Tests\jsonapi\Kernel\ResourceType\RelatedResourceTypesTest::testGetRelatableResourceTypesFromFieldDefinition()
  2. 10 core/modules/jsonapi/tests/src/Kernel/ResourceType/RelatedResourceTypesTest.php \Drupal\Tests\jsonapi\Kernel\ResourceType\RelatedResourceTypesTest::testGetRelatableResourceTypesFromFieldDefinition()

Ensure a graceful failure when a field can references a missing bundle.

@covers \Drupal\jsonapi\ResourceType\ResourceTypeRepository::all @covers \Drupal\jsonapi\ResourceType\ResourceTypeRepository::calculateRelatableResourceTypes @covers \Drupal\jsonapi\ResourceType\ResourceTypeRepository::getRelatableResourceTypesFromFieldDefinition

@link https://www.drupal.org/project/drupal/issues/2996114

File

core/modules/jsonapi/tests/src/Kernel/ResourceType/RelatedResourceTypesTest.php, line 198

Class

RelatedResourceTypesTest
@coversDefaultClass <a href="/api/drupal/core%21modules%21jsonapi%21src%21ResourceType%21ResourceType.php/class/ResourceType/11.x" title="Value object containing all metadata for a JSON:API resource type." class="local">\Drupal\jsonapi\ResourceType\ResourceType</a> @coversClass <a href="/api/drupal/core%21modules%21jsonapi%21src%21ResourceType%21ResourceTypeRepository.php/class/ResourceTypeRepository/11.x" title="Provides a repository of all JSON:API resource types." class="local">\Drupal\jsonapi\ResourceType\ResourceTypeRepository</a> @group jsonapi

Namespace

Drupal\Tests\jsonapi\Kernel\ResourceType

Code

public function testGetRelatableResourceTypesFromFieldDefinition() : void {
    $field_config_storage = $this->container
        ->get('entity_type.manager')
        ->getStorage('field_config');
    static::assertCount(0, $this->resourceTypeRepository
        ->get('node', 'foo')
        ->getRelatableResourceTypesByField('field_relationship'));
    $this->createEntityReferenceField('node', 'foo', 'field_ref_with_missing_bundle', 'Related entity', 'node', 'default', [
        'target_bundles' => [
            'missing_bundle',
        ],
    ]);
    $fields = $field_config_storage->loadByProperties([
        'field_name' => 'field_ref_with_missing_bundle',
    ]);
    static::assertSame([
        'missing_bundle',
    ], $fields['node.foo.field_ref_with_missing_bundle']->getItemDefinition()
        ->getSetting('handler_settings')['target_bundles']);
    $a = $this->resourceTypeRepository
        ->get('node', 'foo')
        ->getRelatableResourceTypesByField('field_ref_with_missing_bundle');
    static::assertSame([
        'missing_bundle',
    ], $fields['node.foo.field_ref_with_missing_bundle']->getItemDefinition()
        ->getSetting('handler_settings')['target_bundles']);
    $arguments = [
        '@name' => 'field_ref_with_missing_bundle',
        '@target_entity_type_id' => 'node',
        '@target_bundle' => 'foo',
        '@entity_type_id' => 'node',
        '@bundle' => 'missing_bundle',
    ];
    $logged = Database::getConnection()->select('watchdog')
        ->fields('watchdog', [
        'variables',
    ])
        ->condition('type', 'jsonapi')
        ->condition('message', 'The "@name" at "@target_entity_type_id:@target_bundle" references the "@entity_type_id:@bundle" entity type that does not exist.')
        ->execute()
        ->fetchField();
    $this->assertEquals(serialize($arguments), $logged);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.