You are here

function MenuTest::testMenu in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/menu_ui/src/Tests/MenuTest.php \Drupal\menu_ui\Tests\MenuTest::testMenu()

Tests menu functionality using the admin and user interfaces.

File

core/modules/menu_ui/src/Tests/MenuTest.php, line 84
Contains \Drupal\menu_ui\Tests\MenuTest.

Class

MenuTest
Add a custom menu, add menu links to the custom menu and Tools menu, check their data, and delete them using the UI.

Namespace

Drupal\menu_ui\Tests

Code

function testMenu() {

  // Login the user.
  $this
    ->drupalLogin($this->adminUser);
  $this->items = array();
  $this->menu = $this
    ->addCustomMenu();
  $this
    ->doMenuTests();
  $this
    ->doTestMenuBlock();
  $this
    ->addInvalidMenuLink();
  $this
    ->addCustomMenuCRUD();

  // Verify that the menu links rebuild is idempotent and leaves the same
  // number of links in the table.

  /** @var \Drupal\Core\Menu\MenuLinkManagerInterface $menu_link_manager */
  $menu_link_manager = \Drupal::service('plugin.manager.menu.link');
  $before_count = $menu_link_manager
    ->countMenuLinks(NULL);
  $menu_link_manager
    ->rebuild();
  $after_count = $menu_link_manager
    ->countMenuLinks(NULL);
  $this
    ->assertIdentical($before_count, $after_count, 'MenuLinkManager::rebuild() does not add more links');

  // Do standard user tests.
  // Login the user.
  $this
    ->drupalLogin($this->authenticatedUser);
  $this
    ->verifyAccess(403);
  foreach ($this->items as $item) {

    // Menu link URIs are stored as 'internal:/node/$nid'.
    $node = Node::load(str_replace('internal:/node/', '', $item->link->uri));
    $this
      ->verifyMenuLink($item, $node);
  }

  // Login the administrator.
  $this
    ->drupalLogin($this->adminUser);

  // Verify delete link exists and reset link does not exist.
  $this
    ->drupalGet('admin/structure/menu/manage/' . $this->menu
    ->id());
  $this
    ->assertLinkByHref(Url::fromRoute('entity.menu_link_content.delete_form', [
    'menu_link_content' => $this->items[0]
      ->id(),
  ])
    ->toString());
  $this
    ->assertNoLinkByHref(Url::fromRoute('menu_ui.link_reset', [
    'menu_link_plugin' => $this->items[0]
      ->getPluginId(),
  ])
    ->toString());

  // Check delete and reset access.
  $this
    ->drupalGet('admin/structure/menu/item/' . $this->items[0]
    ->id() . '/delete');
  $this
    ->assertResponse(200);
  $this
    ->drupalGet('admin/structure/menu/link/' . $this->items[0]
    ->getPluginId() . '/reset');
  $this
    ->assertResponse(403);

  // Delete menu links.
  foreach ($this->items as $item) {
    $this
      ->deleteMenuLink($item);
  }

  // Delete custom menu.
  $this
    ->deleteCustomMenu();

  // Modify and reset a standard menu link.
  $instance = $this
    ->getStandardMenuLink();
  $old_weight = $instance
    ->getWeight();

  // Edit the static menu link.
  $edit = array();
  $edit['weight'] = 10;
  $id = $instance
    ->getPluginId();
  $this
    ->drupalPostForm("admin/structure/menu/link/{$id}/edit", $edit, t('Save'));
  $this
    ->assertResponse(200);
  $this
    ->assertText('The menu link has been saved.');
  $menu_link_manager
    ->resetDefinitions();
  $instance = $menu_link_manager
    ->createInstance($instance
    ->getPluginId());
  $this
    ->assertEqual($edit['weight'], $instance
    ->getWeight(), 'Saving an existing link updates the weight.');
  $this
    ->resetMenuLink($instance, $old_weight);
}