Verified Commit e882b59c authored by Andrei Mateescu's avatar Andrei Mateescu
Browse files

fix: #3614993 AjaxRenderer::renderResponse() should handle a render array with no attachments

By: macsim
By: smustgrave
parent cc28583d
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -49,7 +49,7 @@ public function renderResponse(array $main_content, Request $request, RouteMatch
    $response = new AjaxResponse();

    $html = $this->renderer->renderRoot($main_content);
    $response->setAttachments($main_content['#attached']);
    $response->setAttachments($main_content['#attached'] ?? []);

    // The selector for the insert command is NULL as the new content will
    // replace the element making the Ajax call. The default 'replaceWith'
+34 −3
Original line number Diff line number Diff line
@@ -41,9 +41,8 @@ protected function setUp(): void {
    parent::setUp();

    $element_info_manager = $this->createStub(ElementInfoManagerInterface::class);
    $renderer = $this->createMock(RendererInterface::class);
    $renderer->expects($this->atLeastOnce())
      ->method('renderRoot')
    $renderer = $this->createStub(RendererInterface::class);
    $renderer->method('renderRoot')
      ->willReturnCallback(function (array &$elements, $is_root_call = FALSE) {
        $elements += ['#attached' => []];
        if (isset($elements['#markup'])) {
@@ -60,6 +59,38 @@ protected function setUp(): void {
    $this->ajaxRenderer = new AjaxRenderer($element_info_manager, $renderer);
  }

  /**
   * Tests renderResponse() when the render array has no #attached key.
   */
  public function testRenderWithoutAttached(): void {
    $element_info_manager = $this->createStub(ElementInfoManagerInterface::class);
    $renderer = $this->createStub(RendererInterface::class);
    $renderer->method('renderRoot')
      ->willReturnCallback(function (array &$elements, $is_root_call = FALSE) {
        if (isset($elements['#markup'])) {
          return $elements['#markup'];
        }
        elseif (isset($elements['#type'])) {
          return $elements['#type'];
        }
        else {
          return 'Markup';
        }
      });

    $ajax_renderer = new AjaxRenderer($element_info_manager, $renderer);
    $main_content = ['#markup' => 'example content'];
    $request = new Request();
    $route_match = $this->createStub(RouteMatchInterface::class);

    $result = $ajax_renderer->renderResponse($main_content, $request, $route_match);

    $this->assertInstanceOf('Drupal\Core\Ajax\AjaxResponse', $result);
    $commands = $result->getCommands();
    $this->assertEquals('insert', $commands[0]['command']);
    $this->assertEquals('example content', $commands[0]['data']);
  }

  /**
   * Tests the content method.
   *