function BreadcrumbPreprocessTest::testEntityWithBundleKeyButNoBundleEntity

Tests an entity type whose bundle key does not resolve to a bundle entity.

The entity_test type declares a bundle key but no bundle entity type, so its bundle field is a plain string and reading ::entity off it yields NULL. Calling ::label() on that is a fatal error, which took down every edit form of every such entity type, menu links among them.

File

core/themes/default_admin/tests/src/Kernel/BreadcrumbPreprocessTest.php, line 62

Class

BreadcrumbPreprocessTest
Tests breadcrumb preprocessing for entities.

Namespace

Drupal\Tests\default_admin\Kernel

Code

public function testEntityWithBundleKeyButNoBundleEntity() : void {
  $entity = EntityTest::create([
    'name' => 'A test entity',
  ]);
  $entity->save();
  $entity_type = $entity->getEntityType();
  $this->assertNotEmpty($entity_type->getKey('bundle'));
  $this->assertNull($entity_type->getBundleEntityType());
  $this->assertNull($entity->get($entity_type->getKey('bundle'))->entity);
  $variables = $this->preprocessBreadcrumb($entity, 'entity.entity_test.edit_form');
  // With no bundle entity to name it, the entity type's own singular label
  // has to stand in.
  $this->assertSame('Edit Entity Test Bundle', (string) end($variables['breadcrumb'])['text']);
}

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