diff --git a/core/lib/Drupal/Core/Form/FormBuilder.php b/core/lib/Drupal/Core/Form/FormBuilder.php index 17ac5a9..00e942b 100644 --- a/core/lib/Drupal/Core/Form/FormBuilder.php +++ b/core/lib/Drupal/Core/Form/FormBuilder.php @@ -651,7 +651,7 @@ public function processForm($form_id, &$form, &$form_state) { $form_state['executed'] = TRUE; // Redirect the form based on values in $form_state. - if ($redirect = $this->redirectForm($form_state)) { + if (!isset($form_state['response']) && $redirect = $this->redirectForm($form_state)) { $form_state['response'] = $redirect; } diff --git a/core/tests/Drupal/Tests/Core/Form/FormBuilderTest.php b/core/tests/Drupal/Tests/Core/Form/FormBuilderTest.php index bcfdf52..278ade3 100644 --- a/core/tests/Drupal/Tests/Core/Form/FormBuilderTest.php +++ b/core/tests/Drupal/Tests/Core/Form/FormBuilderTest.php @@ -161,6 +161,52 @@ public function formStateResponseProvider() { } /** + * Tests the handling of a redirect when $form_state['response'] exists. + */ + public function testHandleRedirectWithResponse() { + $form_id = 'test_form_id'; + $expected_form = $form_id(); + + // Set up a response that will be used. + $response = $this->getMockBuilder('Symfony\Component\HttpFoundation\Response') + ->disableOriginalConstructor() + ->getMock(); + $response->expects($this->once()) + ->method('prepare') + ->will($this->returnValue($response)); + + // Set up a redirect that will not be called. + $redirect = $this->getMockBuilder('Symfony\Component\HttpFoundation\RedirectResponse') + ->disableOriginalConstructor() + ->getMock(); + $redirect->expects($this->never()) + ->method('prepare'); + + $form_arg = $this->getMockForm($form_id, $expected_form); + $form_arg->expects($this->any()) + ->method('submitForm') + ->will($this->returnCallback(function ($form, &$form_state) use ($response, $redirect) { + // Set both the response and the redirect. + $form_state['response'] = $response; + $form_state['redirect'] = $redirect; + })); + + $form_state = array(); + $this->formBuilder->getFormId($form_arg, $form_state); + + try { + $form_state['values'] = array(); + $form_state['input']['form_id'] = $form_id; + $this->simulateFormSubmission($form_id, $form_arg, $form_state, FALSE); + $this->fail('TestFormBuilder::sendResponse() was not triggered.'); + } + catch (\Exception $e) { + $this->assertSame('exit', $e->getMessage()); + } + $this->assertSame($response, $form_state['response']); + } + + /** * Tests the redirectForm() method when a redirect is expected. * * @param array $form_state