Skip to content
Snippets Groups Projects
Verified Commit 59f38cc3 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
parent 5a0ad9b6
No related branches found
No related tags found
No related merge requests found
......@@ -207,7 +207,11 @@ public function getListBuilder($entity_type_id) {
*/
public function getFormObject($entity_type_id, $operation) {
if (!$class = $this->getDefinition($entity_type_id, TRUE)->getFormClass($operation)) {
throw new InvalidPluginDefinitionException($entity_type_id, sprintf('The "%s" entity type did not specify a "%s" form class.', $entity_type_id, $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);
......
......@@ -248,18 +248,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);
}
/**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment