Verified Commit 311c787f authored by Lee Rowlands's avatar Lee Rowlands
Browse files

Issue #3418298 by Akhil Babu, Kanchan Bhogade: Improve the exception message...

Issue #3418298 by Akhil Babu, Kanchan Bhogade: Improve the exception message when an entity form class does not exist

(cherry picked from commit 59f38cc3)
parent cfacbed7
Loading
Loading
Loading
Loading
Loading
+5 −1
Original line number Diff line number Diff line
@@ -204,8 +204,12 @@ public function getListBuilder($entity_type_id) {
   */
  public function getFormObject($entity_type_id, $operation) {
    if (!$class = $this->getDefinition($entity_type_id, TRUE)->getFormClass($operation)) {
      $handlers = $this->getDefinition($entity_type_id, TRUE)->getHandlerClasses();
      if (!isset($handlers['form'][$operation])) {
        throw new InvalidPluginDefinitionException($entity_type_id, sprintf('The "%s" entity type did not specify a "%s" form class.', $entity_type_id, $operation));
      }
      throw new InvalidPluginDefinitionException($entity_type_id, sprintf('The "%s" form handler of the "%s" entity type specifies a non-existent class "%s".', $operation, $entity_type_id, $handlers['form'][$operation]));
    }

    $form_object = $this->classResolver->getInstanceFromDefinition($class);

+38 −4
Original line number Diff line number Diff line
@@ -246,18 +246,52 @@ public function testGetFormObject() {
    $this->assertEquals('yellow', $banana_form->color);
  }

  /**
   * Provides test data for testGetFormObjectInvalidOperation().
   *
   * @return array
   *   Test data.
   */
  public function provideFormObjectInvalidOperationData(): array {
    return [
      'missing_form_handler' => [
        'test_entity_type',
        'edit',
        '',
        'The "test_entity_type" entity type did not specify a "edit" form class.',
      ],
      'missing_form_handler_class' => [
        'test_entity_type',
        'edit',
        'Drupal\test_entity_type\Form\NonExistingClass',
        'The "edit" form handler of the "test_entity_type" entity type specifies a non-existent class "Drupal\test_entity_type\Form\NonExistingClass".',
      ],
    ];
  }

  /**
   * Tests the getFormObject() method with an invalid operation.
   *
   * @covers ::getFormObject
   *
   * @dataProvider provideFormObjectInvalidOperationData
   */
  public function testGetFormObjectInvalidOperation() {
  public function testGetFormObjectInvalidOperation(string $entity_type_id, string $operation, string $form_class, string $exception_message): void {
    $entity = $this->prophesize(EntityTypeInterface::class);
    $entity->getFormClass('edit')->willReturn('');
    $this->setUpEntityTypeDefinitions(['test_entity_type' => $entity]);
    $entity->getFormClass($operation)->willReturn(NULL);
    if (!$form_class) {
      $entity->getHandlerClasses()->willReturn([]);
    }
    else {
      $entity->getHandlerClasses()->willReturn([
        'form' => [$operation => $form_class],
      ]);
    }
    $this->setUpEntityTypeDefinitions([$entity_type_id => $entity]);

    $this->expectException(InvalidPluginDefinitionException::class);
    $this->entityTypeManager->getFormObject('test_entity_type', 'edit');
    $this->expectExceptionMessage($exception_message);
    $this->entityTypeManager->getFormObject($entity_type_id, $operation);
  }

  /**