You are here

protected function TreeTestBase::createHierarchy in Views tree 8.2

Creates a hierarchy of entity_test entities.

1 call to TreeTestBase::createHierarchy()
TreeTestBase::setUp in tests/src/Kernel/Plugin/views/style/TreeTestBase.php

File

tests/src/Kernel/Plugin/views/style/TreeTestBase.php, line 53

Class

TreeTestBase
Base class for testing tree style plugins.

Namespace

Drupal\Tests\views_tree\Kernel\Plugin\views\style

Code

protected function createHierarchy() {

  // Create 3 parent nodes.
  foreach (range(1, 3) as $i) {
    $entity = EntityTest::create([
      'name' => 'parent ' . $i,
    ]);
    $entity
      ->save();
    $this->parents[$entity
      ->id()] = $entity;

    // Add 3 child entities for each parent.
    foreach (range(1, 3) as $j) {
      $child = EntityTest::create([
        'name' => 'child ' . $j . ' (parent ' . $i . ')',
        'field_test_parent' => [
          'target_id' => $entity
            ->id(),
        ],
      ]);
      $child
        ->save();

      // For parent 2, child 1, add 3 grandchildren.
      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();
        }
      }
    }
  }
}