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. 11.x 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 \Drupal\jsonapi\ResourceType\ResourceType[[api-linebreak]] @coversClass \Drupal\jsonapi\ResourceType\ResourceTypeRepository[[api-linebreak]] @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.