1 menu.test MenuTestCase::doMenuTests($menu_name = 'main-menu')

Test menu functionality using main-menu menu.

File

core/modules/menu/tests/menu.test, line 218
Tests for menu.module.

Class

MenuTestCase

Code

function doMenuTests($menu_name = 'main-menu') {
  // Add nodes to use as links for menu links.
  $node1 = $this->backdropCreateNode(array('type' => 'post'));
  $node2 = $this->backdropCreateNode(array('type' => 'post'));
  $node3 = $this->backdropCreateNode(array('type' => 'post'));
  $node4 = $this->backdropCreateNode(array('type' => 'post'));
  $node5 = $this->backdropCreateNode(array('type' => 'post'));

  // Add menu links.
  $item1 = $this->addMenuLink(0, 'node/' . $node1->nid, $menu_name);
  $item2 = $this->addMenuLink($item1['mlid'], 'node/' . $node2->nid, $menu_name, FALSE);
  $item3 = $this->addMenuLink($item2['mlid'], 'node/' . $node3->nid, $menu_name);
  $this->assertMenuLink($item1['mlid'], array('depth' => 1, 'has_children' => 1, 'p1' => $item1['mlid'], 'p2' => 0));
  $this->assertMenuLink($item2['mlid'], array('depth' => 2, 'has_children' => 1, 'p1' => $item1['mlid'], 'p2' => $item2['mlid'], 'p3' => 0));
  $this->assertMenuLink($item3['mlid'], array('depth' => 3, 'has_children' => 0, 'p1' => $item1['mlid'], 'p2' => $item2['mlid'], 'p3' => $item3['mlid'], 'p4' => 0));

  // Add 102 menu links with increasing weights.
  $items = array();
  for ($i = -51; $i <= 51; $i++) {
    $items[$i] = $this->addMenuLink(0, 'node/' . $node1->nid, $menu_name, TRUE, strval($i));
  }
  // Make sure that the first-added and last-added items' weights don't get
  // changed because of the old hardcoded delta = 50.
  $this->assertMenuLink($items[-51]['mlid'], array('weight' => '-51'));
  $this->assertMenuLink($items[51]['mlid'], array('weight' => '51'));

  // Verify menu links.
  $this->verifyMenuLink($item1, $node1);
  $this->verifyMenuLink($item2, $node2, $item1, $node1);
  $this->verifyMenuLink($item3, $node3, $item2, $node2);

  // Add more menu links.
  $item4 = $this->addMenuLink(0, 'node/' . $node4->nid, $menu_name);
  $item5 = $this->addMenuLink($item4['mlid'], 'node/' . $node5->nid, $menu_name);
  $this->assertMenuLink($item4['mlid'], array('depth' => 1, 'has_children' => 1, 'p1' => $item4['mlid'], 'p2' => 0));
  $this->assertMenuLink($item5['mlid'], array('depth' => 2, 'has_children' => 0, 'p1' => $item4['mlid'], 'p2' => $item5['mlid'], 'p3' => 0));

  // Add an alias to node 4 to test adding a menu link via alias.
  $node4->path = array(
    'pid' => NULL,
    'source' => 'node/' . $node4->nid,
    'alias' => 'node' . $node4->nid . '-alias',
    'langcode' => LANGUAGE_NONE,
    'auto' => FALSE,
  );
  $node4->save();

  $item6 = $this->addMenuLink(0, 'node' . $node4->nid . '-alias', $menu_name);
  $this->assertMenuLink($item6['mlid'], array('link_path' => 'node/' . $node4->nid));

  // Check that the menu UI shows the alias for the node and not the source.
  $this->backdropGet('admin/structure/menu/item/' . $item6['mlid'] . '/edit');
  $this->assertFieldByXPath('//input[@name="link_path"]', 'node' . $node4->nid . '-alias');

  // Modify menu links.
  $this->modifyMenuLink($item1);
  $this->modifyMenuLink($item2);

  // Toggle menu links.
  $this->toggleMenuLink($item1);
  $this->toggleMenuLink($item2);

  // Move link and verify that descendants are updated.
  $this->moveMenuLink($item2, $item5['mlid'], $menu_name);
  $this->assertMenuLink($item1['mlid'], array('depth' => 1, 'has_children' => 0, 'p1' => $item1['mlid'], 'p2' => 0));
  $this->assertMenuLink($item4['mlid'], array('depth' => 1, 'has_children' => 1, 'p1' => $item4['mlid'], 'p2' => 0));
  $this->assertMenuLink($item5['mlid'], array('depth' => 2, 'has_children' => 1, 'p1' => $item4['mlid'], 'p2' => $item5['mlid'], 'p3' => 0));
  $this->assertMenuLink($item2['mlid'], array('depth' => 3, 'has_children' => 1, 'p1' => $item4['mlid'], 'p2' => $item5['mlid'], 'p3' => $item2['mlid'], 'p4' => 0));
  $this->assertMenuLink($item3['mlid'], array('depth' => 4, 'has_children' => 0, 'p1' => $item4['mlid'], 'p2' => $item5['mlid'], 'p3' => $item2['mlid'], 'p4' => $item3['mlid'], 'p5' => 0));

  // Enable a link via the overview form.
  $this->disableMenuLink($item1);
  $edit = array();

  // Note in the UI the 'mlid:x[hidden]' form element maps to enabled, or
  // NOT hidden.
  $edit['mlid:' . $item1['mlid'] . '[hidden]'] = TRUE;
  $this->backdropPost('admin/structure/menu/manage/' . $item1['menu_name'], $edit, t('Save configuration'));

  // Verify in the database.
  $this->assertMenuLink($item1['mlid'], array('hidden' => 0));

  // Save menu links for later tests.
  $this->items[] = $item1;
  $this->items[] = $item2;
}