View source
<?php
namespace Drupal\Tests\taxonomy\Kernel\Views;
use Drupal\views\Views;
class TaxonomyTermFilterDepthTest extends TaxonomyTestBase {
protected static $modules = [
'taxonomy',
'taxonomy_test_views',
'views',
'node',
];
public static $testViews = [
'test_filter_taxonomy_index_tid_depth',
];
protected $terms = [];
protected $view;
protected function setUp($import_test_views = TRUE) : void {
parent::setUp($import_test_views);
$this
->installSchema('node', [
'node_access',
]);
$first = $this
->createTerm([
'name' => 'First',
]);
$second = $this
->createTerm([
'name' => 'Second',
'parent' => $first
->id(),
]);
$third = $this
->createTerm([
'name' => 'Third',
'parent' => $second
->id(),
]);
$this->terms = [
$first,
$second,
$third,
];
$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->view = Views::getView(self::$testViews[0]);
$request_time = \Drupal::time()
->getRequestTime();
foreach ($this->nodes as $i => $node) {
$node
->setCreatedTime($request_time - $i)
->save();
}
}
public function testTermWithDepthFilter() : void {
$expected = [
[
'nid' => 1,
],
[
'nid' => 2,
],
[
'nid' => 3,
],
[
'nid' => 4,
],
[
'nid' => 5,
],
];
$this
->executeView($this->view);
$this
->assertIdenticalResultsetHelper($this->view, $expected, [
'nid' => 'nid',
], 'assertIdentical');
$expected = [
[
'nid' => 4,
],
];
$this
->assertTermWithDepthResult($expected, $this->terms[0]
->id(), 0);
$expected = [
[
'nid' => 4,
],
];
$this
->assertTermWithDepthResult($expected, $this->terms[0]
->id(), 1);
$expected = [
[
'nid' => 4,
],
[
'nid' => 5,
],
];
$this
->assertTermWithDepthResult($expected, $this->terms[0]
->id(), 2);
$expected = [
[
'nid' => 4,
],
[
'nid' => 5,
],
];
$this
->assertTermWithDepthResult($expected, $this->terms[0]
->id(), 9);
$expected = [
[
'nid' => 5,
],
];
$this
->assertTermWithDepthResult($expected, $this->terms[1]
->id(), 1);
$expected = [
[
'nid' => 5,
],
];
$this
->assertTermWithDepthResult($expected, $this->terms[2]
->id(), 0);
$expected = [
[
'nid' => 5,
],
];
$this
->assertTermWithDepthResult($expected, $this->terms[2]
->id(), 1);
$expected = [
[
'nid' => 4,
],
[
'nid' => 5,
],
];
$this
->assertTermWithDepthResult($expected, $this->terms[2]
->id(), -2);
$expected = [
[
'nid' => 4,
],
[
'nid' => 5,
],
];
$this
->assertTermWithDepthResult($expected, $this->terms[2]
->id(), -9);
$expected = [
[
'nid' => 5,
],
];
$this
->assertTermWithDepthResult($expected, $this->terms[2]
->id(), -1);
$expected = [
[
'nid' => 4,
],
[
'nid' => 5,
],
];
$this
->assertTermWithDepthResult($expected, [
$this->terms[2]
->id(),
$this->terms[1]
->id(),
], -1);
$expected = [
[
'nid' => 4,
],
[
'nid' => 5,
],
];
$this
->assertTermWithDepthResult($expected, [
$this->terms[0]
->id(),
$this->terms[1]
->id(),
], 1);
}
protected function assertTermWithDepthResult(array $expected, $tid, int $depth) : void {
$this->view
->destroy();
$this->view
->initDisplay();
$filters = $this->view->displayHandlers
->get('default')
->getOption('filters');
$filters['tid_depth']['depth'] = $depth;
$filters['tid_depth']['value'] = (array) $tid;
$this->view->displayHandlers
->get('default')
->setOption('filters', $filters);
$this
->executeView($this->view);
$this
->assertIdenticalResultsetHelper($this->view, $expected, [
'nid' => 'nid',
], 'assertIdentical');
}
}