View source
<?php
namespace Drupal\Tests\entity_hierarchy\Kernel;
use Drupal\views\Tests\ViewResultAssertionTrait;
use Drupal\views\Views;
class ViewsIntegrationTest extends EntityHierarchyKernelTestBase {
use ViewResultAssertionTrait;
public static $modules = [
'entity_hierarchy',
'entity_test',
'system',
'user',
'dbal',
'field',
'views',
'entity_hierarchy_test_views',
];
protected function additionalSetup() {
parent::additionalSetup();
$this
->installConfig('entity_hierarchy_test_views');
$this
->installConfig('system');
$this
->installSchema('system', [
'key_value_expire',
]);
}
public function testViewsIntegrationDirectChildren() {
$children = $this
->createChildEntities($this->parent
->id(), 3);
$child = reset($children);
$this
->createChildEntities($child
->id(), 5);
$expected = [
[
'name' => 'Child 3',
'id' => 4,
],
[
'name' => 'Child 2',
'id' => 3,
],
[
'name' => 'Child 1',
'id' => 2,
],
];
$executable = Views::getView('entity_hierarchy_test_children_view');
$executable
->preview('block_1', [
$this->parent
->id(),
]);
$this
->assertCount(3, $executable->result);
$this
->assertIdenticalResultset($executable, $expected, [
'name' => 'name',
'id' => 'id',
]);
}
public function testViewsIntegrationIncludingGrandChildren() {
$children = $this
->createChildEntities($this->parent
->id(), 3);
$child = reset($children);
$this
->createChildEntities($child
->id(), 5);
$expected = [
[
'name' => 'Child 3',
'id' => 4,
],
[
'name' => 'Child 2',
'id' => 3,
],
[
'name' => 'Child 1',
'id' => 2,
],
[
'name' => 'Child 5',
'id' => 9,
],
[
'name' => 'Child 4',
'id' => 8,
],
[
'name' => 'Child 3',
'id' => 7,
],
[
'name' => 'Child 2',
'id' => 6,
],
[
'name' => 'Child 1',
'id' => 5,
],
];
$executable = Views::getView('entity_hierarchy_test_children_view');
$executable
->preview('block_2', [
$this->parent
->id(),
]);
$this
->assertCount(8, $executable->result);
$this
->assertIdenticalResultset($executable, $expected, [
'name' => 'name',
'id' => 'id',
]);
}
public function testViewsIntegrationParents() {
$children = $this
->createChildEntities($this->parent
->id(), 1);
$child = reset($children);
$grandchildren = $this
->createChildEntities($child
->id(), 1);
$expected = [
[
'name' => 'Parent',
'id' => 1,
],
[
'name' => 'Child 1',
'id' => 2,
],
];
$executable = Views::getView('entity_hierarchy_test_children_view');
$executable
->preview('block_3', [
reset($grandchildren)
->id(),
]);
$this
->assertCount(2, $executable->result);
$this
->assertIdenticalResultset($executable, $expected, [
'name' => 'name',
'id' => 'id',
]);
}
public function testViewsIntegrationSiblings() {
$children = $this
->createChildEntities($this->parent
->id(), 3);
$child = reset($children);
$this
->createChildEntities($child
->id(), 5);
$expected = [
[
'name' => 'Child 3',
'id' => 4,
],
[
'name' => 'Child 2',
'id' => 3,
],
];
$executable = Views::getView('entity_hierarchy_test_children_view');
$executable
->preview('block_4', [
$child
->id(),
]);
$this
->assertCount(2, $executable->result);
$this
->assertIdenticalResultset($executable, $expected, [
'name' => 'name',
'id' => 'id',
]);
}
public function testViewsIntegrationSiblingsShowSelf() {
$children = $this
->createChildEntities($this->parent
->id(), 3);
$child = reset($children);
$this
->createChildEntities($child
->id(), 5);
$expected = [
[
'name' => 'Child 3',
'id' => 4,
],
[
'name' => 'Child 2',
'id' => 3,
],
[
'name' => 'Child 1',
'id' => 2,
],
];
$executable = Views::getView('entity_hierarchy_test_children_view');
$executable
->preview('block_5', [
$child
->id(),
]);
$this
->assertCount(3, $executable->result);
$this
->assertIdenticalResultset($executable, $expected, [
'name' => 'name',
'id' => 'id',
]);
}
public function testDepthField() {
$children = $this
->createChildEntities($this->parent
->id(), 1);
$child = reset($children);
$this
->createChildEntities($child
->id(), 1);
$executable = Views::getView('entity_hierarchy_test_fields_view');
$output = $executable
->preview('field_depth');
$output = \Drupal::service('renderer')
->renderRoot($output);
$this
->assertStringContainsString('Parent at depth 0', $output);
$this
->assertStringContainsString('Child 1 at depth 2', $output);
}
}