Commit 662bd393 authored by webchick's avatar webchick

Issue #2120847 by tim.plunkett: Add test coverage for FormBuilder::sendResponse().

parent 482f4eb0
......@@ -274,6 +274,7 @@ public function buildForm($form_id, &$form_state) {
// If the form returns some kind of response, deliver it.
if ($response instanceof Response) {
$this->sendResponse($response);
exit;
}
// If this was a successful submission of a single-step form or the last step
......@@ -572,6 +573,7 @@ public function retrieveForm($form_id, &$form_state) {
// If the form returns some kind of response, deliver it.
if ($form instanceof Response) {
$this->sendResponse($form);
exit;
}
$form['#form_id'] = $form_id;
......@@ -1661,7 +1663,6 @@ protected function sendResponse(Response $response) {
->prepare($this->request)
->send();
$this->httpKernel->terminate($this->request, $response);
exit;
}
/**
......
......@@ -14,6 +14,7 @@
use Drupal\Tests\UnitTestCase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
/**
* Tests the form builder.
......@@ -101,8 +102,11 @@ public function setUp() {
$this->csrfToken = $this->getMockBuilder('Drupal\Core\Access\CsrfTokenGenerator')
->disableOriginalConstructor()
->getMock();
$http_kernel = $this->getMockBuilder('Drupal\Core\HttpKernel')
->disableOriginalConstructor()
->getMock();
$this->formBuilder = new TestFormBuilder($this->moduleHandler, $key_value_expirable_factory, $event_dispatcher, $this->urlGenerator, $translation_manager, $this->csrfToken);
$this->formBuilder = new TestFormBuilder($this->moduleHandler, $key_value_expirable_factory, $event_dispatcher, $this->urlGenerator, $translation_manager, $this->csrfToken, $http_kernel);
$this->formBuilder->setRequest(new Request());
$this->account = $this->getMock('Drupal\Core\Session\AccountInterface');
......@@ -475,6 +479,32 @@ public function testGetCache() {
$this->formBuilder->buildForm($form_id, $form_state);
}
/**
* Tests the sendResponse() method.
*
* @expectedException \Exception
*/
public function testSendResponse() {
$form_id = 'test_form_id';
$expected_form = $this->getMockBuilder('Symfony\Component\HttpFoundation\Response')
->disableOriginalConstructor()
->getMock();
$expected_form->expects($this->once())
->method('prepare')
->will($this->returnValue($expected_form));
$form_arg = $this->getMock('Drupal\Core\Form\FormInterface');
$form_arg->expects($this->any())
->method('buildForm')
->will($this->returnValue($expected_form));
// Do an initial build of the form and track the build ID.
$form_state = array();
$form_state['build_info']['callback_object'] = $form_arg;
$form_state['build_info']['args'] = array();
$this->formBuilder->buildForm($form_id, $form_state);
}
/**
* Asserts that the expected form structure is found in a form for a given key.
*
......@@ -499,6 +529,15 @@ protected function assertFormElement(array $expected_form, array $actual_form, $
*/
class TestFormBuilder extends FormBuilder {
/**
* {@inheritdoc}
*/
protected function sendResponse(Response $response) {
parent::sendResponse($response);
// Throw an exception instead of exiting.
throw new \Exception('exit');
}
/**
* @param \Drupal\Core\Session\AccountInterface $account
*/
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment