Verified Commit 5a571f6f authored by quietone's avatar quietone
Browse files

Issue #2820411 by Akhil Babu, smustgrave, larowlan, joachim, quietone: Improve...

Issue #2820411 by Akhil Babu, smustgrave, larowlan, joachim, quietone: Improve the exception message when an entity handler class does not exist
parent c2958323
Loading
Loading
Loading
Loading
Loading
+5 −1
Original line number Diff line number Diff line
@@ -253,8 +253,12 @@ public function getHandler($entity_type_id, $handler_type) {
      $definition = $this->getDefinition($entity_type_id);
      $class = $definition->getHandlerClass($handler_type);
      if (!$class) {
        $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);
    }

+36 −4
Original line number Diff line number Diff line
@@ -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);
  }

  /**