View source
<?php
namespace Drupal\Tests\taxonomy\Kernel\Views;
use Drupal\views\Views;
class TaxonomyTermArgumentDepthTest extends TaxonomyTestBase {
protected static $modules = [
'taxonomy',
'taxonomy_test_views',
'views',
'node',
];
public static $testViews = [
'test_argument_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(),
]);
$fourth = $this
->createTerm([
'name' => 'Fourth',
'parent' => $third
->id(),
]);
$fifth = $this
->createTerm([
'name' => 'Fifth',
'parent' => $fourth
->id(),
]);
$this->terms = [
$first,
$second,
$third,
$fourth,
$fifth,
];
$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);
$settings['field_views_testing_tags'][0]['target_id'] = $fifth
->id();
$this->nodes[] = $this
->drupalCreateNode($settings);
$this->view = Views::getView(self::$testViews[0]);
$time = \Drupal::time();
foreach ($this->nodes as $i => $node) {
$node
->setCreatedTime($time
->getRequestTime() - $i)
->save();
}
}
public function testTermWithDepthFilter() : void {
$expected = [
[
'nid' => 1,
],
[
'nid' => 2,
],
[
'nid' => 3,
],
[
'nid' => 4,
],
[
'nid' => 5,
],
[
'nid' => 6,
],
];
$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,
],
[
'nid' => 6,
],
];
$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' => 4,
],
[
'nid' => 5,
],
[
'nid' => 6,
],
];
$this
->assertTermWithDepthResult($expected, $this->terms[4]
->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, TRUE);
$expected = [
[
'nid' => 4,
],
[
'nid' => 5,
],
];
$this
->assertTermWithDepthResult($expected, $this->terms[2]
->id() . ',' . $this->terms[1]
->id(), -1, TRUE);
$expected = [
[
'nid' => 4,
],
[
'nid' => 5,
],
];
$this
->assertTermWithDepthResult($expected, $this->terms[0]
->id() . '+' . $this->terms[1]
->id(), 1, TRUE);
$expected = [
[
'nid' => 4,
],
[
'nid' => 5,
],
];
$this
->assertTermWithDepthResult($expected, $this->terms[0]
->id() . ',' . $this->terms[1]
->id(), 1, TRUE);
}
protected function assertTermWithDepthResult(array $expected, $tid, int $depth, bool $break_phrase = FALSE) : void {
$this->view
->destroy();
$this->view
->initDisplay();
$arguments = $this->view->displayHandlers
->get('default')
->getOption('arguments');
$arguments['term_node_tid_depth']['depth'] = $depth;
$arguments['term_node_tid_depth']['break_phrase'] = $break_phrase;
$this->view->displayHandlers
->get('default')
->setOption('arguments', $arguments);
$this
->executeView($this->view, [
$tid,
]);
$this
->assertIdenticalResultsetHelper($this->view, $expected, [
'nid' => 'nid',
], 'assertIdentical');
}
}