You are here

public function HierarchyNestedSetIntegrationTest::testDeleteParent in Entity Reference Hierarchy 3.x

Same name and namespace in other branches
  1. 8.2 tests/src/Kernel/HierarchyNestedSetIntegrationTest.php \Drupal\Tests\entity_hierarchy\Kernel\HierarchyNestedSetIntegrationTest::testDeleteParent()

Tests deleting parent node reparents children.

File

tests/src/Kernel/HierarchyNestedSetIntegrationTest.php, line 83

Class

HierarchyNestedSetIntegrationTest
Tests integration with entity_hierarchy.

Namespace

Drupal\Tests\entity_hierarchy\Kernel

Code

public function testDeleteParent() {
  $child = $this
    ->createTestEntity($this->parent
    ->id());
  $child2 = $this
    ->createTestEntity($this->parent
    ->id());
  $this
    ->createTestEntity($child
    ->id());
  $grandchild2 = $this
    ->createTestEntity($child2
    ->id());
  $grandchildNodeKey = $this->nodeFactory
    ->fromEntity($grandchild2);
  $grandchild2_node = $this->treeStorage
    ->getNode($grandchildNodeKey);
  $this
    ->assertEquals(2, $grandchild2_node
    ->getDepth());
  $root_node = $this->treeStorage
    ->getNode($this->parentStub);
  $children = $this
    ->getChildren($root_node);
  $this
    ->assertCount(2, $children);

  // Now we delete child2, grandchild2 should go up a layer.
  $child2
    ->delete();
  $children = $this
    ->getChildren($root_node);
  $this
    ->assertCount(2, $children);
  $reload = function ($id) {
    return \Drupal::entityTypeManager()
      ->getStorage(static::ENTITY_TYPE)
      ->loadUnchanged($id);
  };
  $grandchild2 = $reload($grandchild2
    ->id());
  $field_name = static::FIELD_NAME;
  $this
    ->assertNotNull($grandchild2);
  $this
    ->assertEquals($this->parent
    ->id(), $grandchild2->{$field_name}->target_id);
  $grandchildNodeKey = $this->nodeFactory
    ->fromEntity($grandchild2);
  $grandchild2_node = $this->treeStorage
    ->getNode($grandchildNodeKey);
  $this
    ->assertEquals(1, $grandchild2_node
    ->getDepth());

  // Confirm field values were updated.
  $this->parent
    ->delete();

  // Grandchild2 and child should now be parentless.
  $grandchild2 = $reload($grandchild2
    ->id());
  $grandchild2_node = $this->treeStorage
    ->getNode($this->nodeFactory
    ->fromEntity($grandchild2));
  $this
    ->assertEquals(0, $grandchild2_node
    ->getDepth());
  $grandchild2 = $reload($grandchild2
    ->id());
  $child = $reload($grandchild2
    ->id());

  // Confirm field values were updated.
  $this
    ->assertEquals(NULL, $grandchild2->{self::FIELD_NAME}->target_id);
  $this
    ->assertEquals(NULL, $child->{self::FIELD_NAME}->target_id);
}