View source
<?php
namespace Drupal\Tests\taxonomy_entity_index\Functional\Views;
use Drupal\node\NodeInterface;
use Drupal\views\Views;
class TaxonomyEntityIndexTermFilterDepthTest extends TaxonomyEntityIndexTestBase {
public static $modules = [
'taxonomy_entity_index',
'taxonomy',
'taxonomy_entity_index_test_views',
'views',
'node',
];
protected $defaultTheme = 'stark';
public static $testViews = [
'test_filter_taxonomy_entity_index_index_tid_depth',
];
protected $terms = [];
protected $view;
protected function setUp($import_test_views = TRUE) {
parent::setUp($import_test_views);
$first = $this
->createTerm([
'name' => 'First',
]);
$second = $this
->createTerm([
'name' => 'Second',
'parent' => $first
->id(),
]);
$third = $this
->createTerm([
'name' => 'Third',
'parent' => $second
->id(),
]);
$settings = [
'type' => 'article',
];
$this->nodes[] = $this
->drupalCreateNode($settings);
$settings['field_views_testing_tags'][0]['target_id'] = $first
->id();
$this->nodes[] = $this
->drupalCreateNode($settings);
$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_entity_index_index_tid_depth');
}
public function testTermWithDepthFilter() {
$column_map = [
'nid' => 'nid',
];
$assert_method = 'assertIdentical';
$expected = array_map(function (NodeInterface $node) {
return [
'nid' => $node
->id(),
];
}, $this->nodes);
$this
->executeView($this->view);
$this
->assertIdenticalResultsetHelper($this->view, $expected, $column_map, $assert_method);
$expected = [
[
'nid' => 4,
],
];
$this
->assertTermWithDepthResult($this->terms[0]
->id(), 0, $expected);
$expected = [
[
'nid' => 4,
],
];
$this
->assertTermWithDepthResult($this->terms[0]
->id(), 1, $expected);
$expected = [
[
'nid' => 4,
],
[
'nid' => 5,
],
];
$this
->assertTermWithDepthResult($this->terms[0]
->id(), 2, $expected);
$expected = [
[
'nid' => 5,
],
];
$this
->assertTermWithDepthResult($this->terms[1]
->id(), 1, $expected);
$expected = [
[
'nid' => 5,
],
];
$this
->assertTermWithDepthResult($this->terms[2]
->id(), 0, $expected);
$expected = [
[
'nid' => 5,
],
];
$this
->assertTermWithDepthResult($this->terms[2]
->id(), 1, $expected);
$expected = [
[
'nid' => 4,
],
[
'nid' => 5,
],
];
$this
->assertTermWithDepthResult($this->terms[2]
->id(), -2, $expected);
}
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');
}
}