function MenuUiTest::testMenuAdministration

Same name in other branches
  1. 9 core/modules/menu_ui/tests/src/Functional/MenuUiTest.php \Drupal\Tests\menu_ui\Functional\MenuUiTest::testMenuAdministration()
  2. 11.x core/modules/menu_ui/tests/src/Functional/MenuUiTest.php \Drupal\Tests\menu_ui\Functional\MenuUiTest::testMenuAdministration()

Tests menu functionality using the admin and user interfaces.

File

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

Class

MenuUiTest
Tests the menu UI.

Namespace

Drupal\Tests\menu_ui\Functional

Code

public function testMenuAdministration() : void {
    // 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);
    // Tests the menus are listed alphabetically
    // Delete all existing menus.
    $existing = Menu::loadMultiple();
    foreach ($existing as $existingMenu) {
        $existingMenu->delete();
    }
    // Test alphabetical order without pager.
    $menu_entities = [];
    for ($i = 1; $i < 6; $i++) {
        $menu = strtolower($this->getRandomGenerator()
            ->name());
        $menu_entity = Menu::create([
            'id' => $menu,
            'label' => $menu,
        ]);
        $menu_entities[] = $menu_entity;
        $menu_entity->save();
    }
    uasort($menu_entities, [
        Menu::class,
        'sort',
    ]);
    $menu_entities = array_values($menu_entities);
    $this->drupalGet('/admin/structure/menu');
    $base_path = parse_url($this->baseUrl, PHP_URL_PATH) ?? '';
    $first_link = $this->assertSession()
        ->elementExists('css', 'tbody tr:nth-of-type(1) a');
    $last_link = $this->assertSession()
        ->elementExists('css', 'tbody tr:nth-of-type(5) a');
    $this->assertEquals($first_link->getAttribute('href'), sprintf('%s/admin/structure/menu/manage/%s', $base_path, $menu_entities[0]->label()));
    $this->assertEquals($last_link->getAttribute('href'), sprintf('%s/admin/structure/menu/manage/%s', $base_path, $menu_entities[4]->label()));
    // Test alphabetical order with pager.
    $new_menu_entities = [];
    for ($i = 1; $i < 61; $i++) {
        $new_menu = strtolower($this->getRandomGenerator()
            ->name());
        $new_menu_entity = Menu::create([
            'id' => $new_menu,
            'label' => $new_menu,
        ]);
        $new_menu_entities[] = $new_menu_entity;
        $new_menu_entity->save();
    }
    $menu_entities = array_merge($menu_entities, $new_menu_entities);
    // To accommodate the current non-natural sorting of the pager, we have to
    // first non-natural sort the array of menu entities, and then do a
    // natural-sort on the ones that are on page 1.
    sort($menu_entities);
    $menu_entities_page_one = array_slice($menu_entities, 50, 64, TRUE);
    uasort($menu_entities_page_one, [
        Menu::class,
        'sort',
    ]);
    $menu_entities_page_one = array_values($menu_entities_page_one);
    $this->drupalGet('/admin/structure/menu', [
        'query' => [
            'page' => 1,
        ],
    ]);
    $first_link = $this->assertSession()
        ->elementExists('css', 'tbody tr:nth-of-type(1) a');
    $last_link = $this->assertSession()
        ->elementExists('css', 'tbody tr:nth-of-type(15) a');
    $this->assertEquals($first_link->getAttribute('href'), sprintf('%s/admin/structure/menu/manage/%s', $base_path, $menu_entities_page_one[0]->label()));
    $this->assertEquals($last_link->getAttribute('href'), sprintf('%s/admin/structure/menu/manage/%s', $base_path, $menu_entities_page_one[14]->label()));
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.