You are here

public function MenuLinksTest::testMenuLinkReparenting in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/menu_link_content/tests/src/Kernel/MenuLinksTest.php \Drupal\Tests\menu_link_content\Kernel\MenuLinksTest::testMenuLinkReparenting()
  2. 10 core/modules/menu_link_content/tests/src/Kernel/MenuLinksTest.php \Drupal\Tests\menu_link_content\Kernel\MenuLinksTest::testMenuLinkReparenting()

Test automatic reparenting of menu links.

File

core/modules/menu_link_content/tests/src/Kernel/MenuLinksTest.php, line 210

Class

MenuLinksTest
Tests handling of menu links hierarchies.

Namespace

Drupal\Tests\menu_link_content\Kernel

Code

public function testMenuLinkReparenting($module = 'menu_test') {

  // Check the initial hierarchy.
  $links = $this
    ->createLinkHierarchy($module);
  $expected_hierarchy = [
    'parent' => '',
    'child-1' => 'parent',
    'child-1-1' => 'child-1',
    'child-1-2' => 'child-1',
    'child-2' => 'parent',
  ];
  $this
    ->assertMenuLinkParents($links, $expected_hierarchy);

  // Start over, and move child-1 under child-2, and check that all the
  // children of child-1 have been moved too.
  $links = $this
    ->createLinkHierarchy($module);

  /* @var \Drupal\Core\Menu\MenuLinkInterface $menu_link_plugin  */
  $this->menuLinkManager
    ->updateDefinition($links['child-1'], [
    'parent' => $links['child-2'],
  ]);

  // Verify that the entity was updated too.
  $menu_link_plugin = $this->menuLinkManager
    ->createInstance($links['child-1']);
  $entity = \Drupal::service('entity.repository')
    ->loadEntityByUuid('menu_link_content', $menu_link_plugin
    ->getDerivativeId());
  $this
    ->assertEqual($entity
    ->getParentId(), $links['child-2']);
  $expected_hierarchy = [
    'parent' => '',
    'child-1' => 'child-2',
    'child-1-1' => 'child-1',
    'child-1-2' => 'child-1',
    'child-2' => 'parent',
  ];
  $this
    ->assertMenuLinkParents($links, $expected_hierarchy);

  // Start over, and delete child-1, and check that the children of child-1
  // have been reassigned to the parent.
  $links = $this
    ->createLinkHierarchy($module);
  $this->menuLinkManager
    ->removeDefinition($links['child-1']);
  $expected_hierarchy = [
    'parent' => FALSE,
    'child-1-1' => 'parent',
    'child-1-2' => 'parent',
    'child-2' => 'parent',
  ];
  $this
    ->assertMenuLinkParents($links, $expected_hierarchy);

  // Try changing the parent at the entity level.
  $definition = $this->menuLinkManager
    ->getDefinition($links['child-1-2']);
  $entity = MenuLinkContent::load($definition['metadata']['entity_id']);
  $entity->parent->value = '';
  $entity
    ->save();
  $expected_hierarchy = [
    'parent' => '',
    'child-1-1' => 'parent',
    'child-1-2' => '',
    'child-2' => 'parent',
  ];
  $this
    ->assertMenuLinkParents($links, $expected_hierarchy);

  // @todo Figure out what makes sense to test in terms of automatic
  //   re-parenting. https://www.drupal.org/node/2309531
}