TreeTestBase.php in Views tree 8.2
File
tests/src/Kernel/Plugin/views/style/TreeTestBase.php
View source
<?php
namespace Drupal\Tests\views_tree\Kernel\Plugin\views\style;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\Tests\field\Traits\EntityReferenceTestTrait;
use Drupal\Tests\views\Kernel\ViewsKernelTestBase;
use Drupal\views\Tests\ViewTestData;
abstract class TreeTestBase extends ViewsKernelTestBase {
use EntityReferenceTestTrait;
protected $parents;
public static $modules = [
'entity_test',
'field',
'views_tree',
'views_tree_test',
];
protected function setUp($import_test_views = TRUE) {
parent::setUp(FALSE);
ViewTestData::createTestViews(get_class($this), [
'views_tree_test',
]);
$this
->installEntitySchema('entity_test');
$this
->createEntityReferenceField('entity_test', 'entity_test', 'field_test_parent', 'field_test_parent', 'entity_test', 'default', [], FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
$this
->createHierarchy();
}
protected function createHierarchy() {
foreach (range(1, 3) as $i) {
$entity = EntityTest::create([
'name' => 'parent ' . $i,
]);
$entity
->save();
$this->parents[$entity
->id()] = $entity;
foreach (range(1, 3) as $j) {
$child = EntityTest::create([
'name' => 'child ' . $j . ' (parent ' . $i . ')',
'field_test_parent' => [
'target_id' => $entity
->id(),
],
]);
$child
->save();
if ($i === 2 && $j === 1) {
foreach (range(1, 3) as $k) {
$grand_child = EntityTest::create([
'name' => 'grand child ' . $k . ' (c ' . $j . ', p ' . $i . ')',
'field_test_parent' => [
'target_id' => $child
->id(),
],
]);
$grand_child
->save();
}
}
}
}
}
}