3 namespace Drupal\Tests\Core\Form\EventSubscriber;
5 use Drupal\Core\DependencyInjection\ContainerBuilder;
6 use Drupal\Core\Form\EventSubscriber\FormAjaxSubscriber;
7 use Drupal\Core\Form\Exception\BrokenPostRequestException;
8 use Drupal\Core\Form\FormAjaxException;
9 use Drupal\Core\Form\FormBuilderInterface;
10 use Drupal\Core\Form\FormState;
11 use Drupal\Core\Messenger\MessengerInterface;
12 use Drupal\Tests\UnitTestCase;
13 use Symfony\Component\HttpFoundation\Request;
14 use Symfony\Component\HttpFoundation\Response;
15 use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
16 use Symfony\Component\HttpKernel\Exception\HttpException;
17 use Symfony\Component\HttpKernel\HttpKernelInterface;
20 * @coversDefaultClass \Drupal\Core\Form\EventSubscriber\FormAjaxSubscriber
21 * @group EventSubscriber
23 class FormAjaxSubscriberTest extends UnitTestCase {
26 * @var \Drupal\Core\Form\EventSubscriber\FormAjaxSubscriber
28 protected $subscriber;
31 * @var \Drupal\Core\Form\FormAjaxResponseBuilderInterface|\PHPUnit_Framework_MockObject_MockObject
33 protected $formAjaxResponseBuilder;
36 * @var \Symfony\Component\HttpKernel\HttpKernelInterface|\PHPUnit_Framework_MockObject_MockObject
38 protected $httpKernel;
41 * The mocked string translation.
43 * @var \Drupal\Core\StringTranslation\TranslationInterface|\PHPUnit_Framework_MockObject_MockObject
45 protected $stringTranslation;
48 * The mocked messenger.
50 * @var \Drupal\Core\Messenger\MessengerInterface|\PHPUnit_Framework_MockObject_MockObject
57 protected function setUp() {
60 $this->httpKernel = $this->getMock('Symfony\Component\HttpKernel\HttpKernelInterface');
61 $this->formAjaxResponseBuilder = $this->getMock('Drupal\Core\Form\FormAjaxResponseBuilderInterface');
62 $this->stringTranslation = $this->getStringTranslationStub();
63 $this->messenger = $this->createMock(MessengerInterface::class);
64 $this->subscriber = new FormAjaxSubscriber($this->formAjaxResponseBuilder, $this->stringTranslation, $this->messenger);
68 * @covers ::onException
70 public function testOnException() {
71 $form = ['#type' => 'form', '#build_id' => 'the_build_id'];
72 $expected_form = $form + [
73 '#build_id_old' => 'the_build_id',
75 $form_state = new FormState();
76 $exception = new FormAjaxException($form, $form_state);
78 $request = new Request([], ['form_build_id' => 'the_build_id']);
80 $response = new Response('');
82 $this->formAjaxResponseBuilder->expects($this->once())
83 ->method('buildResponse')
84 ->with($request, $expected_form, $form_state, $commands)
85 ->willReturn($response);
87 $event = $this->assertResponseFromException($request, $exception, $response);
88 $this->assertTrue($event->isAllowingCustomResponseCode());
89 $this->assertSame(200, $event->getResponse()->getStatusCode());
93 * @covers ::onException
95 public function testOnExceptionNewBuildId() {
96 $form = ['#type' => 'form', '#build_id' => 'the_build_id'];
97 $expected_form = $form + [
98 '#build_id_old' => 'a_new_build_id',
100 $form_state = new FormState();
101 $exception = new FormAjaxException($form, $form_state);
103 $request = new Request([], ['form_build_id' => 'a_new_build_id']);
105 $response = new Response('');
107 $this->formAjaxResponseBuilder->expects($this->once())
108 ->method('buildResponse')
109 ->with($request, $expected_form, $form_state, $commands)
110 ->willReturn($response);
112 $event = $this->assertResponseFromException($request, $exception, $response);
113 $this->assertTrue($event->isAllowingCustomResponseCode());
114 $this->assertSame(200, $event->getResponse()->getStatusCode());
118 * @covers ::onException
120 public function testOnExceptionOtherClass() {
121 $request = new Request();
122 $exception = new \Exception();
124 $this->formAjaxResponseBuilder->expects($this->never())
125 ->method('buildResponse');
127 $this->assertResponseFromException($request, $exception, NULL);
131 * @covers ::onException
133 public function testOnExceptionResponseBuilderException() {
134 $form = ['#type' => 'form', '#build_id' => 'the_build_id'];
135 $expected_form = $form + [
136 '#build_id_old' => 'the_build_id',
138 $form_state = new FormState();
139 $exception = new FormAjaxException($form, $form_state);
140 $request = new Request([], ['form_build_id' => 'the_build_id']);
143 $expected_exception = new HttpException(500, 'The specified #ajax callback is empty or not callable.');
144 $this->formAjaxResponseBuilder->expects($this->once())
145 ->method('buildResponse')
146 ->with($request, $expected_form, $form_state, $commands)
147 ->willThrowException($expected_exception);
149 $event = $this->assertResponseFromException($request, $exception, NULL);
150 $this->assertSame($expected_exception, $event->getException());
154 * @covers ::onException
156 public function testOnExceptionBrokenPostRequest() {
157 $this->formAjaxResponseBuilder->expects($this->never())
158 ->method('buildResponse');
160 $this->messenger->expects($this->once())
161 ->method('addError');
163 $this->subscriber = $this->getMockBuilder('\Drupal\Core\Form\EventSubscriber\FormAjaxSubscriber')
164 ->setConstructorArgs([
165 $this->formAjaxResponseBuilder,
166 $this->getStringTranslationStub(),
169 ->setMethods(['formatSize'])
172 $this->subscriber->expects($this->once())
173 ->method('formatSize')
176 $rendered_output = 'the rendered output';
177 // CommandWithAttachedAssetsTrait::getRenderedContent() will call the
178 // renderer service via the container.
179 $renderer = $this->getMock('Drupal\Core\Render\RendererInterface');
180 $renderer->expects($this->once())
181 ->method('renderRoot')
183 ->willReturnCallback(function (&$elements) use ($rendered_output) {
184 $elements['#attached'] = [];
185 return $rendered_output;
187 $container = new ContainerBuilder();
188 $container->set('renderer', $renderer);
189 \Drupal::setContainer($container);
191 $exception = new BrokenPostRequestException(32 * 1e6);
192 $request = new Request([FormBuilderInterface::AJAX_FORM_REQUEST => TRUE]);
194 $event = new GetResponseForExceptionEvent($this->httpKernel, $request, HttpKernelInterface::MASTER_REQUEST, $exception);
195 $this->subscriber->onException($event);
196 $this->assertTrue($event->isAllowingCustomResponseCode());
197 $actual_response = $event->getResponse();
198 $this->assertInstanceOf('\Drupal\Core\Ajax\AjaxResponse', $actual_response);
199 $this->assertSame(200, $actual_response->getStatusCode());
200 $expected_commands[] = [
201 'command' => 'insert',
202 'method' => 'prepend',
204 'data' => $rendered_output,
207 $this->assertSame($expected_commands, $actual_response->getCommands());
211 * @covers ::onException
212 * @covers ::getFormAjaxException
214 public function testOnExceptionNestedException() {
215 $form = ['#type' => 'form', '#build_id' => 'the_build_id'];
216 $expected_form = $form + [
217 '#build_id_old' => 'the_build_id',
219 $form_state = new FormState();
220 $form_exception = new FormAjaxException($form, $form_state);
221 $exception = new \Exception('', 0, $form_exception);
223 $request = new Request([], ['form_build_id' => 'the_build_id']);
225 $response = new Response('');
227 $this->formAjaxResponseBuilder->expects($this->once())
228 ->method('buildResponse')
229 ->with($request, $expected_form, $form_state, $commands)
230 ->willReturn($response);
232 $this->assertResponseFromException($request, $exception, $response);
236 * @covers ::getFormAjaxException
238 public function testOnExceptionNestedWrongException() {
239 $nested_exception = new \Exception();
240 $exception = new \Exception('', 0, $nested_exception);
241 $request = new Request();
243 $this->formAjaxResponseBuilder->expects($this->never())
244 ->method('buildResponse');
246 $this->assertResponseFromException($request, $exception, NULL);
250 * Asserts that the expected response is derived from the given exception.
252 * @param \Symfony\Component\HttpFoundation\Request $request
254 * @param \Exception $exception
255 * The exception to pass to the event.
256 * @param \Symfony\Component\HttpFoundation\Response|null $expected_response
257 * The response expected to be set on the event.
259 * @return \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent
260 * The event used to derive the response.
262 protected function assertResponseFromException(Request $request, \Exception $exception, $expected_response) {
263 $event = new GetResponseForExceptionEvent($this->httpKernel, $request, HttpKernelInterface::MASTER_REQUEST, $exception);
264 $this->subscriber->onException($event);
266 $this->assertSame($expected_response, $event->getResponse());