You are here

public function TreeRebuilderUnitTest::testTreeSort in Entity Reference Hierarchy 8.2

Same name and namespace in other branches
  1. 3.x tests/src/Unit/TreeRebuilderUnitTest.php \Drupal\Tests\entity_hierarchy\Unit\TreeRebuilderUnitTest::testTreeSort()

Tests tree sort.

Structure

  • 789

-- 991 -- 891 --- 784 ---- 999 --- 23

  • 123
  • 781
  • 782

-- 783

File

tests/src/Unit/TreeRebuilderUnitTest.php, line 31

Class

TreeRebuilderUnitTest
Defines a class for testing tree rebuilder.

Namespace

Drupal\Tests\entity_hierarchy\Unit

Code

public function testTreeSort() {
  $entity_type_manager = $this
    ->prophesize(EntityTypeManagerInterface::class);
  $treeRebuilder = new TreeRebuilder($entity_type_manager
    ->reveal());
  $reflection = new \ReflectionClass($treeRebuilder);
  $method = $reflection
    ->getMethod('treeSort');
  $method
    ->setAccessible(TRUE);
  $records = [
    [
      'nid' => 789,
      'field_parent_target_id' => 456,
      'field_parent_weight' => -50,
    ],
    [
      'nid' => 123,
      'field_parent_target_id' => 456,
      'field_parent_weight' => -49,
    ],
    [
      'nid' => 781,
      'field_parent_target_id' => 656,
      'field_parent_weight' => -50,
    ],
    [
      'nid' => 782,
      'field_parent_target_id' => 656,
      'field_parent_weight' => -50,
    ],
    [
      'nid' => 783,
      'field_parent_target_id' => 782,
      'field_parent_weight' => -50,
    ],
    [
      'nid' => 999,
      'field_parent_target_id' => 784,
      'field_parent_weight' => -50,
    ],
    [
      'nid' => 991,
      'field_parent_target_id' => 789,
      'field_parent_weight' => -50,
    ],
    [
      'nid' => 891,
      'field_parent_target_id' => 789,
      'field_parent_weight' => -49,
    ],
    [
      'nid' => 784,
      'field_parent_target_id' => 891,
      'field_parent_weight' => -50,
    ],
    [
      'nid' => 23,
      'field_parent_target_id' => 891,
      'field_parent_weight' => -49,
    ],
  ];
  $result = $method
    ->invoke($treeRebuilder, 'field_parent', $records, 'nid');
  $this
    ->assertSame([
    789,
    991,
    891,
    784,
    999,
    23,
    123,
    781,
    782,
    783,
  ], array_keys($result));
}