Verified Commit babbb202 authored by Dave Long's avatar Dave Long
Browse files

fix: #3560093 $target_url in ViewAjaxController::ajaxView can be NULL

By: berdir
(cherry picked from commit c32f2c70)
parent b62d87be
Loading
Loading
Loading
Loading
Loading
+4 −2
Original line number Diff line number Diff line
@@ -142,7 +142,7 @@ public function ajaxView(Request $request) {

        $used_query_parameters = $request_clone->query->all();
        $query = UrlHelper::buildQuery($used_query_parameters);
        if ($query != '') {
        if ($query != '' && $target_url) {
          $origin_destination .= '?' . $query;
          $target_url->setOption('query', $used_query_parameters);
        }
@@ -168,7 +168,9 @@ public function ajaxView(Request $request) {
        }
        $preview = $view->preview($display_id, $args);
        $request->attributes->remove('ajax_page_state');
        if ($target_url) {
          $response->addCommand(new SetBrowserUrl($target_url->toString()));
        }
        $response->addCommand(new ReplaceCommand(".js-view-dom-id-$dom_id", $preview));
        $response->addCommand(new PrependCommand(".js-view-dom-id-$dom_id", ['#type' => 'status_messages']));
        $request->query->set('ajax_page_state', $existing_page_state);
+44 −2
Original line number Diff line number Diff line
@@ -115,8 +115,13 @@ protected function setUp(): void {
       * Use a callback because container is not initialized yet
       * so we can't generate a URL object right now.
       */
      ->willReturnCallback(function () {
      ->willReturnCallback(function ($path) {
        if ($path === '/invalid-test-page') {
          return FALSE;
        }
        else {
          return Url::fromUserInput('/foo');
        }
      });
    $unroutedUrlAssembler = $this->createMock(UnroutedUrlAssemblerInterface::class);
    $unroutedUrlAssembler->expects($this->any())
@@ -251,6 +256,43 @@ public function testAjaxView(): void {
    ], $response->getAttachments());
  }

  /**
   * Tests a valid view without arguments pagers etc.
   */
  public function testInvalidPath(): void {
    $request = new Request();
    $request->query->set('view_name', 'test_view');
    $request->query->set('view_display_id', 'page_1');
    $request->query->set('view_path', '/invalid-test-page');
    $request->query->set('_wrapper_format', 'ajax');
    $request->query->set('ajax_page_state', 'drupal.settings[]');
    $request->query->set('type', 'article');

    $executable = $this->setupValidMocks();

    $this->redirectDestination->expects($this->atLeastOnce())
      ->method('set')
      ->with('/invalid-test-page');
    $this->currentPath->expects($this->once())
      ->method('setPath')
      ->with('/invalid-test-page', $request);

    $response = $this->viewAjaxController->ajaxView($request);
    $this->assertTrue($response instanceof ViewAjaxResponse);

    $this->assertSame($response->getView(), $executable);

    $this->assertViewResultCommand($response, 0);

    // Test that the ajax controller for Views contains the
    // Drupal Settings.
    $this->assertEquals([
      'drupalSettings' => [
        'testSetting' => ['Setting'],
      ],
    ], $response->getAttachments());
  }

  /**
   * Tests a valid view with a view_path with no slash.
   */