diff --git a/core/modules/taxonomy/src/Tests/Views/TaxonomyTermFilterDepthTest.php b/core/modules/taxonomy/src/Tests/Views/TaxonomyTermFilterDepthTest.php new file mode 100644 index 0000000..4c181c4 --- /dev/null +++ b/core/modules/taxonomy/src/Tests/Views/TaxonomyTermFilterDepthTest.php @@ -0,0 +1,140 @@ +createTerm(['name' => 'First']); + $second = $this->createTerm(['name' => 'Second', 'parent' => $first->id()]); + $third = $this->createTerm(['name' => 'Third', 'parent' => $second->id()]); + + // Create a node w/o any terms. + $settings = ['type' => 'article']; + $this->nodes[] = $this->drupalCreateNode($settings); + + // Create a node with only the top level term. + $settings['field_views_testing_tags'][0]['target_id'] = $first->id(); + $this->nodes[] = $this->drupalCreateNode($settings); + + // Create a node with only the third level term. + $settings['field_views_testing_tags'][0]['target_id'] = $third->id(); + $this->nodes[] = $this->drupalCreateNode($settings); + + $this->terms[0] = $first; + $this->terms[1] = $second; + $this->terms[2] = $third; + + $this->view = Views::getView('test_filter_taxonomy_index_tid_depth'); + } + + /** + * Tests the terms with depth filter. + */ + public function testTermWithDepthFilter() { + $column_map = ['nid' => 'nid']; + $assert_method = 'assertIdentical'; + + // Default view has an empty value for this filter, so all nodes should be + // returned. + $expected = [ + ['nid' => 1], + ['nid' => 2], + ['nid' => 3], + ['nid' => 4], + ['nid' => 5], + ]; + $this->executeView($this->view); + $this->assertIdenticalResultsetHelper($this->view, $expected, $column_map, $assert_method); + + // Set filter to search on top-level term, with depth 0. + $expected = [['nid' => 4]]; + $this->assertTermWithDepthResult($this->terms[0]->id(), 0, $expected); + + // Top-level term, depth 1. + $expected = [['nid' => 4]]; + $this->assertTermWithDepthResult($this->terms[0]->id(), 0, $expected); + + // Top-level term, depth 2. + $expected = [['nid' => 4], ['nid' => 5]]; + $this->assertTermWithDepthResult($this->terms[0]->id(), 2, $expected); + + // Second-level term, depth 1. + $expected = [['nid' => 5]]; + $this->assertTermWithDepthResult($this->terms[1]->id(), 1, $expected); + + // Third-level term, depth 0. + $expected = [['nid' => 5]]; + $this->assertTermWithDepthResult($this->terms[2]->id(), 0, $expected); + + // Third-level term, depth 1. + $expected = [['nid' => 5]]; + $this->assertTermWithDepthResult($this->terms[2]->id(), 1, $expected); + + // Third-level term, depth -2. + $expected = [['nid' => 4], ['nid' => 5]]; + $this->assertTermWithDepthResult($this->terms[2]->id(), -2, $expected); + } + + /** + * Changes the tid filter to given term and depth. + * + * @param integer $tid + * The term ID to filter on. + * @param integer $depth + * The depth to search. + */ + protected function assertTermWithDepthResult($tid, $depth, array $expected) { + $this->view->destroy(); + $this->view->initDisplay(); + $filters = $this->view->displayHandlers->get('default') + ->getOption('filters'); + $filters['tid_depth']['depth'] = $depth; + $filters['tid_depth']['value'] = [$tid]; + $this->view->displayHandlers->get('default') + ->setOption('filters', $filters); + $this->executeView($this->view); + $this->assertIdenticalResultsetHelper($this->view, $expected, ['nid' => 'nid'], 'assertIdentical'); + } + +} diff --git a/core/modules/taxonomy/src/Tests/Views/TaxonomyTestBase.php b/core/modules/taxonomy/src/Tests/Views/TaxonomyTestBase.php index 9fa56bc..6677952 100644 --- a/core/modules/taxonomy/src/Tests/Views/TaxonomyTestBase.php +++ b/core/modules/taxonomy/src/Tests/Views/TaxonomyTestBase.php @@ -121,20 +121,24 @@ protected function mockStandardInstall() { /** * Returns a new term with random properties in vocabulary $vid. * + * @param array $settings + * (Optional) An associative array of settings to pass to `entity_create`. + * * @return \Drupal\taxonomy\Term * The created taxonomy term. */ - protected function createTerm() { + protected function createTerm(array $settings = []) { $filter_formats = filter_formats(); $format = array_pop($filter_formats); - $term = entity_create('taxonomy_term', array( + $settings += [ 'name' => $this->randomMachineName(), 'description' => $this->randomMachineName(), // Use the first available text format. 'format' => $format->id(), 'vid' => $this->vocabulary->id(), 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED, - )); + ]; + $term = entity_create('taxonomy_term', $settings); $term->save(); return $term; } diff --git a/core/modules/taxonomy/tests/modules/taxonomy_test_views/test_views/views.view.test_filter_taxonomy_index_tid_depth.yml b/core/modules/taxonomy/tests/modules/taxonomy_test_views/test_views/views.view.test_filter_taxonomy_index_tid_depth.yml new file mode 100644 index 0000000..500e8b9 --- /dev/null +++ b/core/modules/taxonomy/tests/modules/taxonomy_test_views/test_views/views.view.test_filter_taxonomy_index_tid_depth.yml @@ -0,0 +1,178 @@ +langcode: en +status: true +dependencies: + module: + - node + - taxonomy + - user +id: test_filter_taxonomy_index_tid_depth +label: test_filter_taxonomy_index_tid_depth +module: views +description: '' +tag: '' +base_table: node_field_data +base_field: nid +core: 8.x +display: + default: + display_plugin: default + id: default + display_title: Master + position: 1 + display_options: + access: + type: perm + options: + perm: 'access content' + cache: + type: none + 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: full + 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: '‹ previous' + next: 'next ›' + first: '« first' + last: 'last »' + quantity: 9 + 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 + 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 + 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 + plugin_id: field + entity_type: node + entity_field: title + filters: + status: + value: true + table: node_field_data + field: status + id: status + expose: + operator: '0' + group: 1 + plugin_id: boolean + entity_type: node + entity_field: status + tid_depth: + id: tid_depth + table: node_field_data + field: term_node_tid_depth + relationship: none + group_type: group + admin_label: '' + operator: or + value: { } + group: 1 + exposed: false + expose: + operator_id: '0' + label: '' + description: '' + use_operator: false + operator: '' + identifier: '' + required: false + remember: false + multiple: false + remember_roles: + authenticated: authenticated + 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: views_testing_tags + hierarchy: true + depth: -2 + error_message: true + plugin_id: taxonomy_index_tid_depth + sorts: { } + header: { } + footer: { } + empty: { } + relationships: { } + arguments: { }