Verified Commit 4584557f authored by Alex Pott's avatar Alex Pott
Browse files

fix: #3273334 Unable to unselect a multi select exposed filter in ajax view

By: pasqualle
By: immaculatexavier
By: yogeshmpawar
By: smustgrave
By: dcam
By: acbramley
By: longwave
(cherry picked from commit 48f2609f)
parent d4804ac2
Loading
Loading
Loading
Loading
Loading
+14 −2
Changes for core/modules/views/js/ajax_view.js: 14 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -85,13 +85,25 @@
      // Remove the question mark and Drupal path component if any.
      queryString = queryString.slice(1);

      // Collect the base names of every named field in the exposed form. The
      // trailing `[…]` is stripped so a multi-select field named `type[]`
      // matches URL params serialized by PHP as `type[0]`, `type[1]`, or
      // `type[]`.
      const stripArraySuffix = (name) => name.replace(/\[.*$/, '');
      const exposedFieldBaseNames = new Set(
        this.$exposed_form
          .find('[name]')
          .toArray()
          .map((el) => stripArraySuffix(el.name)),
      );

      // Remove current exposed filters.
      const params = decodeURI(queryString)
        .split('&')
        .filter((param) => {
          const [name, value] = param.split('=');
          const [name] = param.split('=');
          return (
            this.$exposed_form.find(`input[name="${name}"]`).length === 0 &&
            !exposedFieldBaseNames.has(stripArraySuffix(name)) &&
            /*
            Submitting filters should reset paging and sorting
            because that is what happens without AJAX.
+55 −2
Changes for core/modules/views/tests/src/FunctionalJavascript/ExposedFilterAJAXTest.php: 55 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -117,7 +117,7 @@ public function testExposedFiltering(): void {
    $html = $session->getPage()->getHtml();
    $this->assertStringContainsString('Page Two', $html);
    $this->assertStringNotContainsString('Page One', $html);
    $this->assertSession()->addressEquals('admin/content?type=All&status=All&title=Page%20Two');
    $this->assertSession()->addressEquals('admin/content?title=Page%20Two&type=All&status=All');

    // Submit bulk actions form to ensure that the previous AJAX submit does not
    // break it.
@@ -128,7 +128,7 @@ public function testExposedFiltering(): void {

    // Verify that the action was performed.
    $this->assertSession()->pageTextContains('Make content sticky was applied to 1 item.');
    $this->assertSession()->addressEquals('admin/content?type=All&status=All&title=Page%20Two');
    $this->assertSession()->addressEquals('admin/content?title=Page%20Two&type=All&status=All');

    // Reset the form.
    $this->submitForm([], 'Reset');
@@ -252,6 +252,59 @@ public function testExposedFilteringWithButtonElement(): void {
    $this->assertSame($ajax_views_before, $ajax_views_after);
  }

  /**
   * Tests deselecting a multi-select exposed filter clears it from the URL.
   */
  public function testUnselectMultiSelectExposedFilter(): void {
    // Flip the type filter to allow multiple selections.
    \Drupal::configFactory()->getEditable('views.view.content')
      ->set('display.default.display_options.filters.type.expose.multiple', TRUE)
      ->save();

    // Create a second content type and a node of that type so we can tell the
    // filter was actually cleared.
    $this->createContentType(['type' => 'article']);
    $this->createNode(['type' => 'article', 'title' => 'Article One']);

    $assert = $this->assertSession();
    $page = $this->getSession()->getPage();

    $this->drupalGet('admin/content', ['query' => ['type[]' => 'page']]);
    $assert->pageTextContains('Page One');
    $assert->pageTextNotContains('Article One');

    $select = $page->find('css', 'select[name="type[]"]');
    $this->assertNotNull($select);
    $select->setValue([]);
    $page->pressButton('Filter');
    $assert->assertWaitOnAjaxRequest();

    $assert->pageTextContains('Page One');
    $assert->pageTextContains('Article One');
    $url = $this->getSession()->getCurrentUrl();
    $this->assertStringNotContainsString('type%5B%5D=page', $url);
    $this->assertStringNotContainsString('type[]=page', $url);

    $this->drupalGet('admin/content', [
      'query' => ['type[0]' => 'article', 'type[1]' => 'page'],
    ]);
    $assert->pageTextContains('Page One');
    $assert->pageTextContains('Article One');

    $select = $page->find('css', 'select[name="type[]"]');
    $this->assertNotNull($select);
    // Keep only article selected; drop page.
    $select->setValue(['article']);
    $page->pressButton('Filter');
    $assert->assertWaitOnAjaxRequest();

    $assert->pageTextContains('Article One');
    $assert->pageTextNotContains('Page One');
    $assert->pageTextNotContains('Page Two');
    $url = $this->getSession()->getCurrentUrl();
    $this->assertStringNotContainsString('=page', $url);
  }

  /**
   * Tests that errors messages are displayed for exposed filters via ajax.
   */
+3 −3
Changes for core/modules/views/tests/src/FunctionalJavascript/PaginationAJAXTest.php: 3 added lines, 3 removed lines.
Original line number Diff line number Diff line
@@ -116,7 +116,7 @@ public function testBasicPagination(): void {
    $this->assertNoDuplicateAssetsOnPage();

    // Test that no unwanted parameters are added to the URL.
    $this->assertEquals('?status=All&type=All&items_per_page=5&title=&order=changed&sort=asc&page=2', $link->getAttribute('href'));
    $this->assertEquals('?status=All&type=All&title=&items_per_page=5&order=changed&sort=asc&page=2', $link->getAttribute('href'));

    $this->clickLink('Go to page 3');
    $session_assert->assertWaitOnAjaxRequest();
@@ -200,7 +200,7 @@ public function testDefaultFilterPagination(): void {
    $this->assertNoDuplicateAssetsOnPage();

    // Test that no unwanted parameters are added to the URL.
    $this->assertEquals('?status=All&type=All&items_per_page=5&title=default_value&order=changed&sort=asc&page=0', $link->getAttribute('href'));
    $this->assertEquals('?status=All&type=All&title=default_value&items_per_page=5&order=changed&sort=asc&page=0', $link->getAttribute('href'));

    // Set the title filter to empty string using the exposed pager.
    $page->fillField('title', '');
@@ -220,7 +220,7 @@ public function testDefaultFilterPagination(): void {
    $this->assertNoDuplicateAssetsOnPage();

    // Test that no unwanted parameters are added to the URL.
    $this->assertEquals('?status=All&type=All&items_per_page=5&title=&page=0', $link->getAttribute('href'));
    $this->assertEquals('?status=All&type=All&title=&items_per_page=5&page=0', $link->getAttribute('href'));

    // Navigate back to the first page.
    $this->clickLink('Go to first page');