diff --git a/core/lib/Drupal/Core/Form/FormBuilder.php b/core/lib/Drupal/Core/Form/FormBuilder.php index 55126a73a96883d5b656b65e431b74170667af0b..cec98161251c518939a327f297479c890823d3b7 100644 --- a/core/lib/Drupal/Core/Form/FormBuilder.php +++ b/core/lib/Drupal/Core/Form/FormBuilder.php @@ -193,8 +193,11 @@ public function getFormId($form_arg, FormStateInterface &$form_state) { $form_arg = $this->classResolver->getInstanceFromDefinition($form_arg); } - if (!is_object($form_arg) || !($form_arg instanceof FormInterface)) { - throw new \InvalidArgumentException("The form argument $form_arg is not a valid form."); + if (!is_object($form_arg)) { + throw new \InvalidArgumentException(("The form class $form_arg could not be found or loaded.")); + } + elseif (!($form_arg instanceof FormInterface)) { + throw new \InvalidArgumentException('The form argument ' . $form_arg::class . ' must be an instance of \Drupal\Core\Form\FormInterface.'); } // Add the $form_arg as the callback object and determine the form ID. diff --git a/core/tests/Drupal/Tests/Core/Form/FormBuilderTest.php b/core/tests/Drupal/Tests/Core/Form/FormBuilderTest.php index ebdf4b54932cd2ed2c3fc7d835c89b91c9b3f0c0..ea6ad1899446b176eca625e37adb0fff2d98c7f0 100644 --- a/core/tests/Drupal/Tests/Core/Form/FormBuilderTest.php +++ b/core/tests/Drupal/Tests/Core/Form/FormBuilderTest.php @@ -53,12 +53,25 @@ protected function setUp(): void { /** * Tests the getFormId() method with a string based form ID. + * + * @covers ::getFormId */ public function testGetFormIdWithString() { $form_arg = 'foo'; $form_state = new FormState(); $this->expectException(\InvalidArgumentException::class); - $this->expectExceptionMessage('The form argument foo is not a valid form.'); + $this->expectExceptionMessage('The form class foo could not be found or loaded.'); + $this->formBuilder->getFormId($form_arg, $form_state); + } + + /** + * @covers ::getFormId + */ + public function testGetFormIdWithNonFormClass() { + $form_arg = __CLASS__; + $form_state = new FormState(); + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage("The form argument $form_arg must be an instance of \Drupal\Core\Form\FormInterface."); $this->formBuilder->getFormId($form_arg, $form_state); } @@ -217,7 +230,7 @@ public function testHandleRedirectWithResponse() { public function testGetFormWithString() { $form_id = 'test_form_id'; $this->expectException(\InvalidArgumentException::class); - $this->expectExceptionMessage('The form argument test_form_id is not a valid form.'); + $this->expectExceptionMessage('The form class test_form_id could not be found or loaded.'); $this->formBuilder->getForm($form_id); } @@ -256,7 +269,7 @@ public function testGetFormWithClassString() { public function testBuildFormWithString() { $form_id = 'test_form_id'; $this->expectException(\InvalidArgumentException::class); - $this->expectExceptionMessage('The form argument test_form_id is not a valid form.'); + $this->expectExceptionMessage('The form class test_form_id could not be found or loaded.'); $this->formBuilder->getForm($form_id); }