Commit ca3422f1 authored by webchick's avatar webchick

Issue #2467895 by jhedstrom, lgalanter: taxonomy filter with depth completely broken

parent 33f0f0fd
......@@ -70,12 +70,8 @@ public function query() {
$this->tableAlias = $this->relationship;
}
// If no relationship, then use the alias of the base table.
elseif (isset($this->query->table_queue[$this->view->storage->get('base_table')]['alias'])) {
$this->tableAlias = $this->query->table_queue[$this->view->storage->get('base_table')]['alias'];
}
// This should never happen, but if it does, we fail quietly.
else {
return;
$this->tableAlias = $this->query->ensureTable($this->view->storage->get('base_table'));
}
// Now build the subqueries.
......
<?php
/**
* @file
* Contains \Drupal\taxonomy\Tests\Views\TaxonomyTermFilterDepthTest.
*/
namespace Drupal\taxonomy\Tests\Views;
use Drupal\views\Views;
/**
* Test the taxonomy term with depth filter.
*
* @group taxonomy
*/
class TaxonomyTermFilterDepthTest extends TaxonomyTestBase {
/**
* {@inheritdoc}
*/
public static $modules = ['taxonomy', 'taxonomy_test_views', 'views', 'node'];
/**
* {@inheritdoc}
*/
public static $testViews = ['test_filter_taxonomy_index_tid_depth'];
/**
* @var \Drupal\taxonomy\TermInterface[]
*/
protected $terms = [];
/**
* @var \Drupal\views\ViewExecutable
*/
protected $view;
/**
* {@inheritdoc}
*/
public function setUp() {
parent::setUp();
// Create a hierarchy 3 deep. Note the parent setup function creates two
// top-level terms w/o children.
$first = $this->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.
* @param array $expected
* The expected views result.
*/
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');
}
}
......@@ -81,7 +81,7 @@ protected function setUp() {
*/
public function testTaxonomyTermView() {
// Create terms in the vocabulary.
$term = $this->createTerm($this->vocabulary);
$term = $this->createTerm();
// Post an article.
$edit = array();
......
......@@ -121,20 +121,24 @@ protected function mockStandardInstall() {
/**
* Returns a new term with random properties in vocabulary $vid.
*
* @return \Drupal\taxonomy\Term
* @param array $settings
* (Optional) An associative array of settings to pass to `entity_create`.
*
* @return \Drupal\taxonomy\Entity\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;
}
......
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: { }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment