function LinksTest::testMenuLinkRouterReparenting

Test automatic reparenting of menu links derived from menu routers.

File

drupal/core/modules/system/lib/Drupal/system/Tests/Menu/LinksTest.php, line 180
Definition of Drupal\system\Tests\Menu\LinksTest.

Class

LinksTest
Tests for menu links.

Namespace

Drupal\system\Tests\Menu

Code

function testMenuLinkRouterReparenting() {

  // Run all the standard parenting tests on menu links derived from
  // menu routers.
  $this
    ->testMenuLinkReparenting('system');

  // Additionnaly, test reparenting based on path.
  $links = $this
    ->createLinkHierarchy('system');

  // Move child-1-2 has a child of child-2, making the link hierarchy
  // inconsistent with the path hierarchy.
  $links['child-1-2']['plid'] = $links['child-2']['mlid'];
  menu_link_save($links['child-1-2']);

  // Check the new hierarchy.
  $expected_hierarchy = array(
    'parent' => FALSE,
    'child-1' => 'parent',
    'child-1-1' => 'child-1',
    'child-2' => 'parent',
    'child-1-2' => 'child-2',
  );
  $this
    ->assertMenuLinkParents($links, $expected_hierarchy);

  // Now delete 'parent' directly from the database, simulating a database
  // crash. 'child-1' and 'child-2' should get moved to the
  // top-level.
  // Don't do that at home.
  db_delete('menu_links')
    ->condition('mlid', $links['parent']['mlid'])
    ->execute();
  $expected_hierarchy = array(
    'child-1' => FALSE,
    'child-1-1' => 'child-1',
    'child-2' => FALSE,
    'child-1-2' => 'child-2',
  );
  $this
    ->assertMenuLinkParents($links, $expected_hierarchy);

  // Now delete 'child-2' directly from the database, simulating a database
  // crash. 'child-1-2' will get reparented under 'child-1' based on its
  // path.
  // Don't do that at home.
  db_delete('menu_links')
    ->condition('mlid', $links['child-2']['mlid'])
    ->execute();
  $expected_hierarchy = array(
    'child-1' => FALSE,
    'child-1-1' => 'child-1',
    'child-1-2' => 'child-1',
  );
  $this
    ->assertMenuLinkParents($links, $expected_hierarchy);
}