Commit d7bd8ac4 authored by catch's avatar catch
Browse files

Issue #2900248 by Lendude, vakulrai, mohit_aghera, bkosborne, Krzysztof...

Issue #2900248 by Lendude, vakulrai, mohit_aghera, bkosborne, Krzysztof Domański, anmolgoyal74, kreatIL, mikeker, smustgrave, alexpott, dawehner: Exposed term filter is not updated when terms are added, deleted, or rearranged (caching issue?)
parent f6932126
Loading
Loading
Loading
Loading
+14 −0
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@

namespace Drupal\taxonomy\Plugin\views\filter;

use Drupal\Core\Cache\Cache;
use Drupal\Core\Entity\Element\EntityAutocomplete;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface;
@@ -413,6 +414,19 @@ public function getCacheContexts() {
    return $contexts;
  }

  /**
   * {@inheritdoc}
   */
  public function getCacheTags() {
    $tags = parent::getCacheTags();
    $vocabulary = $this->vocabularyStorage->load($this->options['vid']);
    if ($vocabulary) {
      $tags = Cache::mergeTags($tags, $vocabulary->getCacheTags());
    }
    $tags = Cache::mergeTags($tags, ['taxonomy_term_list:' . $this->options['vid']]);
    return $tags;
  }

  /**
   * {@inheritdoc}
   */
+19 −0
Original line number Diff line number Diff line
<?php

/**
 * @file
 * Test fixture.
 */

use Drupal\Core\Database\Database;
use Drupal\Core\Serialization\Yaml;

$connection = Database::getConnection();

$connection->insert('config')
  ->fields([
    'collection' => '',
    'name' => 'views.view.test_filter_taxonomy_index_tid',
    'data' => serialize(Yaml::decode(file_get_contents('core/modules/taxonomy/tests/modules/taxonomy_test_views/test_views/views.view.test_filter_taxonomy_index_tid.yml'))),
  ])
  ->execute();
+245 −0
Original line number Diff line number Diff line
langcode: en
status: true
dependencies:
  config:
    - taxonomy.vocabulary.tags
  module:
    - node
    - taxonomy
    - user
id: test_taxonomy_exposed_filter
label: 'Test Taxonomy Exposed Filter'
module: views
description: ''
tag: ''
base_table: node_field_data
base_field: nid
display:
  default:
    display_plugin: default
    id: default
    display_title: Default
    position: 0
    display_options:
      access:
        type: perm
        options:
          perm: 'access content'
      cache:
        type: tag
        options: {  }
      query:
        type: views_query
        options:
          disable_sql_rewrite: false
          distinct: false
          replica: false
          query_comment: ''
          query_tags: {  }
      exposed_form:
        type: basic
        options:
          submit_button: Apply
          reset_button: false
          reset_button_label: Reset
          exposed_sorts_label: 'Sort by'
          expose_sort_order: true
          sort_asc_label: Asc
          sort_desc_label: Desc
      pager:
        type: mini
        options:
          items_per_page: 10
          offset: 0
          id: 0
          total_pages: null
          expose:
            items_per_page: false
            items_per_page_label: 'Items per page'
            items_per_page_options: '5, 10, 25, 50'
            items_per_page_options_all: false
            items_per_page_options_all_label: '- All -'
            offset: false
            offset_label: Offset
          tags:
            previous: ‹‹
            next: ››
      style:
        type: default
        options:
          grouping: {  }
          row_class: ''
          default_row_class: true
          uses_fields: false
      row:
        type: fields
        options:
          inline: {  }
          separator: ''
          hide_empty: false
          default_field_elements: true
      fields:
        title:
          id: title
          table: node_field_data
          field: title
          entity_type: node
          entity_field: title
          label: ''
          alter:
            alter_text: false
            make_link: false
            absolute: false
            trim: false
            word_boundary: false
            ellipsis: false
            strip_tags: false
            html: false
          hide_empty: false
          empty_zero: false
          settings:
            link_to_entity: true
          plugin_id: field
          relationship: none
          group_type: group
          admin_label: ''
          exclude: false
          element_type: ''
          element_class: ''
          element_label_type: ''
          element_label_class: ''
          element_label_colon: true
          element_wrapper_type: ''
          element_wrapper_class: ''
          element_default_classes: true
          empty: ''
          hide_alter_empty: true
          click_sort_column: value
          type: string
          group_column: value
          group_columns: {  }
          group_rows: true
          delta_limit: 0
          delta_offset: 0
          delta_reversed: false
          delta_first_last: false
          multi_type: separator
          separator: ', '
          field_api_classes: false
      filters:
        status:
          value: '1'
          table: node_field_data
          field: status
          plugin_id: boolean
          entity_type: node
          entity_field: status
          id: status
          expose:
            operator: ''
            operator_limit_selection: false
            operator_list: {  }
          group: 1
        tid:
          id: tid
          table: taxonomy_index
          field: tid
          relationship: none
          group_type: group
          admin_label: ''
          operator: or
          value: {  }
          group: 1
          exposed: true
          expose:
            operator_id: tid_op
            label: 'Has taxonomy term'
            description: ''
            use_operator: false
            operator: tid_op
            operator_limit_selection: false
            operator_list: {  }
            identifier: tid
            required: false
            remember: false
            multiple: false
            remember_roles:
              authenticated: authenticated
              anonymous: '0'
              administrator: '0'
            reduce: false
          is_grouped: false
          group_info:
            label: ''
            description: ''
            identifier: ''
            optional: true
            widget: select
            multiple: false
            remember: false
            default_group: All
            default_group_multiple: {  }
            group_items: {  }
          reduce_duplicates: false
          type: select
          limit: true
          vid: tags
          hierarchy: true
          error_message: true
          plugin_id: taxonomy_index_tid
      sorts:
        created:
          id: created
          table: node_field_data
          field: created
          order: DESC
          entity_type: node
          entity_field: created
          plugin_id: date
          relationship: none
          group_type: group
          admin_label: ''
          exposed: false
          expose:
            label: ''
          granularity: second
      header: {  }
      footer: {  }
      empty: {  }
      relationships: {  }
      arguments: {  }
      display_extenders: {  }
    cache_metadata:
      max-age: -1
      contexts:
        - 'languages:language_content'
        - 'languages:language_interface'
        - url
        - url.query_args
        - user
        - 'user.node_grants:view'
        - user.permissions
      tags:
        - 'config:taxonomy.vocabulary.tags'
        - 'taxonomy_term_list:tags'
  page_1:
    display_plugin: page
    id: page_1
    display_title: Page
    position: 1
    display_options:
      display_extenders: {  }
      path: test-taxonomy-exposed-filter
    cache_metadata:
      max-age: -1
      contexts:
        - 'languages:language_content'
        - 'languages:language_interface'
        - url
        - url.query_args
        - user
        - 'user.node_grants:view'
        - user.permissions
      tags:
        - 'config:taxonomy.vocabulary.tags'
        - 'taxonomy_term_list:tags'
+61 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\Tests\taxonomy\Functional\Update;

use Drupal\FunctionalTests\Update\UpdatePathTestBase;
use Drupal\views\Entity\View;

/**
 * Tests the upgrade path for adding cache tags to views.
 *
 * For the views with "taxonomy_index_tid" filter plugin, we are re-saving
 * and adding necessary cache tags.
 *
 * @see views_post_update_add_tid_cache_tags()
 *
 * @group taxonomy
 */
class TaxonomyFilterCacheTagsUpdateTest extends UpdatePathTestBase {

  /**
   * {@inheritdoc}
   */
  protected function setDatabaseDumpFiles(): void {
    $this->databaseDumpFiles = [
      __DIR__ . '/../../../../../system/tests/fixtures/update/drupal-9.4.0.bare.standard.php.gz',
      __DIR__ . '/../../../fixtures/update/test-taxonomy-exposed-filter-plugin.php',
    ];
  }

  /**
   * Tests the upgrade path for adding cache tags to views.
   *
   * For the views with "taxonomy_index_tid" filter plugin, we are re-saving
   * and adding necessary cache tags.
   */
  public function testViewsPostUpdateAddTidCacheTags(): void {
    /** @var \Drupal\views\Entity\View $view */
    $config = \Drupal::configFactory()->get('views.view.test_filter_taxonomy_index_tid');
    $cache_tags = [
      'config:taxonomy.vocabulary.tags',
      'taxonomy_term_list:tags',
    ];
    $this->assertNotEquals($cache_tags, $config->getCacheTags());

    $this->runUpdates();

    /** @var \Drupal\views\Entity\View $view */
    $view = View::load('test_filter_taxonomy_index_tid');
    $view_executable = $view->getExecutable();
    $view_executable->initDisplay();
    $cache_metadata = $view_executable->getDisplay()->calculateCacheMetadata();

    $cache_tags = [
      'config:taxonomy.vocabulary.tags',
      'taxonomy_term_list:tags',
    ];
    $this->assertEquals($cache_tags, $cache_metadata->getCacheTags());

  }

}
+17 −0
Original line number Diff line number Diff line
@@ -28,6 +28,7 @@ class TaxonomyIndexTidUiTest extends UITestBase {
    'test_filter_taxonomy_index_tid',
    'test_taxonomy_term_name',
    'test_taxonomy_exposed_grouped_filter',
    'test_taxonomy_exposed_filter',
  ];

  /**
@@ -358,4 +359,20 @@ public function testExposedUnpublishedFilterOptions() {
    $this->assertEmpty($this->cssSelect('option[value="' . $this->terms[1][0]->id() . '"]'));
  }

  /**
   * Test to ensure that term exists in views exposed form after it is created.
   */
  public function testExposedFilterOptions() {
    $this->drupalGet('test-taxonomy-exposed-filter');
    $this->assertSession()->optionExists('edit-tid', 'Term 1.0');
    $term = Term::create([
      'vid' => 'tags',
      'name' => "Test Term",
      'parent' => 0,
    ]);
    $term->save();
    $this->drupalGet('test-taxonomy-exposed-filter');
    $this->assertSession()->optionExists('edit-tid', 'Test Term');
  }

}
Loading