You are here

public function MenuUiTest::testMenu in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/menu_ui/tests/src/Functional/MenuUiTest.php \Drupal\Tests\menu_ui\Functional\MenuUiTest::testMenu()

Tests menu functionality using the admin and user interfaces.

File

core/modules/menu_ui/tests/src/Functional/MenuUiTest.php, line 104

Class

MenuUiTest
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\Tests\menu_ui\Functional

Code

public function testMenu() {

  // Log in the user.
  $this
    ->drupalLogin($this->adminUser);
  $this->items = [];
  $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
    ->assertSame($before_count, $after_count, 'MenuLinkManager::rebuild() does not add more links');

  // Do standard user tests.
  // Log in 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);
  }

  // Log in 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
    ->assertSession()
    ->linkByHrefExists(Url::fromRoute('entity.menu_link_content.delete_form', [
    'menu_link_content' => $this->items[0]
      ->id(),
  ])
    ->toString());
  $this
    ->assertSession()
    ->linkByHrefNotExists(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
    ->assertSession()
    ->statusCodeEquals(200);
  $this
    ->drupalGet('admin/structure/menu/link/' . $this->items[0]
    ->getPluginId() . '/reset');
  $this
    ->assertSession()
    ->statusCodeEquals(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 = [];
  $edit['weight'] = 10;
  $id = $instance
    ->getPluginId();
  $this
    ->drupalGet("admin/structure/menu/link/{$id}/edit");
  $this
    ->submitForm($edit, 'Save');
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $this
    ->assertSession()
    ->pageTextContains('The menu link has been saved.');
  $menu_link_manager
    ->resetDefinitions();
  $instance = $menu_link_manager
    ->createInstance($instance
    ->getPluginId());
  $this
    ->assertEquals($edit['weight'], $instance
    ->getWeight(), 'Saving an existing link updates the weight.');
  $this
    ->resetMenuLink($instance, $old_weight);
}