diff --git a/core/lib/Drupal/Core/Entity/EntityTypeManager.php b/core/lib/Drupal/Core/Entity/EntityTypeManager.php index 3ddea3eb0c6aba55ab27a27b565c2def18bbaadf..7e78c08752e07ce0aaf4ab53219cb8027217ffe3 100644 --- a/core/lib/Drupal/Core/Entity/EntityTypeManager.php +++ b/core/lib/Drupal/Core/Entity/EntityTypeManager.php @@ -253,7 +253,11 @@ public function getHandler($entity_type_id, $handler_type) { $definition = $this->getDefinition($entity_type_id); $class = $definition->getHandlerClass($handler_type); if (!$class) { - throw new InvalidPluginDefinitionException($entity_type_id, sprintf('The "%s" entity type did not specify a %s handler.', $entity_type_id, $handler_type)); + $handlers = $definition->getHandlerClasses(); + if (!isset($handlers[$handler_type])) { + throw new InvalidPluginDefinitionException($entity_type_id, sprintf('The "%s" entity type did not specify a %s handler.', $entity_type_id, $handler_type)); + } + throw new InvalidPluginDefinitionException($entity_type_id, sprintf('The %s handler of the "%s" entity type specifies a non-existent class "%s".', $handler_type, $entity_type_id, $handlers[$handler_type])); } $this->handlers[$handler_type][$entity_type_id] = $this->createHandlerInstance($class, $definition); } diff --git a/core/tests/Drupal/Tests/Core/Entity/EntityTypeManagerTest.php b/core/tests/Drupal/Tests/Core/Entity/EntityTypeManagerTest.php index ba0031958a3b6d0ae5c11d0276f6fa0683cdb028..5ab49683c7e9d3e0531b5bdd2333fb9e9c2390ee 100644 --- a/core/tests/Drupal/Tests/Core/Entity/EntityTypeManagerTest.php +++ b/core/tests/Drupal/Tests/Core/Entity/EntityTypeManagerTest.php @@ -280,17 +280,49 @@ public function testGetHandler() { $this->assertInstanceOf(TranslationInterface::class, $apple_controller->stringTranslation); } + /** + * Provides test data for testGetHandlerMissingHandler(). + * + * @return array + * Test data. + */ + public function provideMissingHandlerData() : array { + return [ + 'missing_handler' => [ + 'test_entity_type', + 'storage', + '', + 'The "test_entity_type" entity type did not specify a storage handler.', + ], + 'missing_handler_class' => [ + 'test_entity_type', + 'storage', + 'Non_Existing_Class', + 'The storage handler of the "test_entity_type" entity type specifies a non-existent class "Non_Existing_Class".', + ], + ]; + } + /** * Tests the getHandler() method when no controller is defined. * * @covers ::getHandler + * + * @dataProvider provideMissingHandlerData */ - public function testGetHandlerMissingHandler() { + public function testGetHandlerMissingHandler(string $entity_type, string $handler_name, string $handler_class, $exception_message) : void { $entity = $this->prophesize(EntityTypeInterface::class); - $entity->getHandlerClass('storage')->willReturn(''); - $this->setUpEntityTypeDefinitions(['test_entity_type' => $entity]); + $entity->getHandlerClass($handler_name)->willReturn(NULL); + if (!$handler_class) { + $entity->getHandlerClasses()->willReturn([]); + } + else { + $entity->getHandlerClasses()->willReturn([$handler_name => $handler_class]); + } + $this->setUpEntityTypeDefinitions([$entity_type => $entity]); $this->expectException(InvalidPluginDefinitionException::class); - $this->entityTypeManager->getHandler('test_entity_type', 'storage'); + $this->expectExceptionMessage($exception_message); + $this->entityTypeManager->getHandler($entity_type, $handler_name); } /**