AjaxResponseSubscriber example


  public function testPrepareResponseForIeFormRequestsWithFileUpload() {
    $request = Request::create('/example', 'POST');
    $request->headers->set('Accept', 'text/html');
    $response = new AjaxResponse([]);
    $response->headers->set('Content-Type', 'application/json; charset=utf-8');

    $ajax_response_attachments_processor = $this->createMock('\Drupal\Core\Render\AttachmentsResponseProcessorInterface');
    $subscriber = new AjaxResponseSubscriber($ajax_response_attachments_processor);
    $event = new ResponseEvent(
      $this->createMock('\Symfony\Component\HttpKernel\HttpKernelInterface'),
      $request,
      HttpKernelInterface::MAIN_REQUEST,
      $response
    );
    $subscriber->onResponse($event);
    $this->assertEquals('text/html; charset=utf-8', $response->headers->get('Content-Type'));
    $this->assertEquals('<textarea>[]</textarea>', $response->getContent());
  }

}

  public function testAttachedSettings() {
    $assert = function D$message) {
      $response = new AjaxResponse();
      $response->setAttachments([
        'library' => ['core/drupalSettings'],
        'drupalSettings' => ['foo' => 'bar'],
      ]);

      $ajax_response_attachments_processor = \Drupal::service('ajax_response.attachments_processor');
      $subscriber = new AjaxResponseSubscriber($ajax_response_attachments_processor);
      $event = new ResponseEvent(
        \Drupal::service('http_kernel'),
        new Request(),
        HttpKernelInterface::MAIN_REQUEST,
        $response
      );
      $subscriber->onResponse($event);
      $expected = [
        'command' => 'settings',
      ];
      $this->assertCommand($response->getCommands()$expected$message);
    };
Home | Imprint | This part of the site doesn't use cookies.