You are here

public function MenuUiTest::doMenuTests in Drupal 8

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

Tests menu functionality.

1 call to MenuUiTest::doMenuTests()
MenuUiTest::testMenu in core/modules/menu_ui/tests/src/Functional/MenuUiTest.php
Tests menu functionality using the admin and user interfaces.

File

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

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 doMenuTests() {
  $menu_name = $this->menu
    ->id();

  // Test the 'Add link' local action.
  $this
    ->drupalGet(Url::fromRoute('entity.menu.edit_form', [
    'menu' => $menu_name,
  ]));
  $this
    ->clickLink(t('Add link'));
  $link_title = $this
    ->randomString();
  $this
    ->drupalPostForm(NULL, [
    'link[0][uri]' => '/',
    'title[0][value]' => $link_title,
  ], t('Save'));
  $this
    ->assertUrl(Url::fromRoute('entity.menu.edit_form', [
    'menu' => $menu_name,
  ]));

  // Test the 'Edit' operation.
  $this
    ->clickLink(t('Edit'));
  $this
    ->assertFieldByName('title[0][value]', $link_title);
  $link_title = $this
    ->randomString();
  $this
    ->drupalPostForm(NULL, [
    'title[0][value]' => $link_title,
  ], t('Save'));
  $this
    ->assertUrl(Url::fromRoute('entity.menu.edit_form', [
    'menu' => $menu_name,
  ]));

  // Test the 'Delete' operation.
  $this
    ->clickLink(t('Delete'));
  $this
    ->assertRaw(t('Are you sure you want to delete the custom menu link %item?', [
    '%item' => $link_title,
  ]));
  $this
    ->drupalPostForm(NULL, [], t('Delete'));
  $this
    ->assertUrl(Url::fromRoute('entity.menu.edit_form', [
    'menu' => $menu_name,
  ]));

  // Add nodes to use as links for menu links.
  $node1 = $this
    ->drupalCreateNode([
    'type' => 'article',
  ]);
  $node2 = $this
    ->drupalCreateNode([
    'type' => 'article',
  ]);
  $node3 = $this
    ->drupalCreateNode([
    'type' => 'article',
  ]);
  $node4 = $this
    ->drupalCreateNode([
    'type' => 'article',
  ]);

  // Create a node with an alias.
  $node5 = $this
    ->drupalCreateNode([
    'type' => 'article',
    'path' => [
      'alias' => '/node5',
    ],
  ]);

  // Verify add link button.
  $this
    ->drupalGet('admin/structure/menu');
  $this
    ->assertLinkByHref('admin/structure/menu/manage/' . $menu_name . '/add', 0, "The add menu link button URL is correct");

  // Verify form defaults.
  $this
    ->doMenuLinkFormDefaultsTest();

  // Add menu links.
  $item1 = $this
    ->addMenuLink('', '/node/' . $node1
    ->id(), $menu_name, TRUE);
  $item2 = $this
    ->addMenuLink($item1
    ->getPluginId(), '/node/' . $node2
    ->id(), $menu_name, FALSE);
  $item3 = $this
    ->addMenuLink($item2
    ->getPluginId(), '/node/' . $node3
    ->id(), $menu_name);

  // Hierarchy
  // <$menu_name>
  // - item1
  // -- item2
  // --- item3
  $this
    ->assertMenuLink([
    'children' => [
      $item2
        ->getPluginId(),
      $item3
        ->getPluginId(),
    ],
    'parents' => [
      $item1
        ->getPluginId(),
    ],
    // We assert the language code here to make sure that the language
    // selection element degrades gracefully without the Language module.
    'langcode' => 'en',
  ], $item1
    ->getPluginId());
  $this
    ->assertMenuLink([
    'children' => [
      $item3
        ->getPluginId(),
    ],
    'parents' => [
      $item2
        ->getPluginId(),
      $item1
        ->getPluginId(),
    ],
    // See above.
    'langcode' => 'en',
  ], $item2
    ->getPluginId());
  $this
    ->assertMenuLink([
    'children' => [],
    'parents' => [
      $item3
        ->getPluginId(),
      $item2
        ->getPluginId(),
      $item1
        ->getPluginId(),
    ],
    // See above.
    'langcode' => 'en',
  ], $item3
    ->getPluginId());

  // 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('', '/node/' . $node4
    ->id(), $menu_name);
  $item5 = $this
    ->addMenuLink($item4
    ->getPluginId(), '/node/' . $node5
    ->id(), $menu_name);

  // Create a menu link pointing to an alias.
  $item6 = $this
    ->addMenuLink($item4
    ->getPluginId(), '/node5', $menu_name, TRUE, '0');

  // Hierarchy
  // <$menu_name>
  // - item1
  // -- item2
  // --- item3
  // - item4
  // -- item5
  // -- item6
  $this
    ->assertMenuLink([
    'children' => [
      $item5
        ->getPluginId(),
      $item6
        ->getPluginId(),
    ],
    'parents' => [
      $item4
        ->getPluginId(),
    ],
    // See above.
    'langcode' => 'en',
  ], $item4
    ->getPluginId());
  $this
    ->assertMenuLink([
    'children' => [],
    'parents' => [
      $item5
        ->getPluginId(),
      $item4
        ->getPluginId(),
    ],
    'langcode' => 'en',
  ], $item5
    ->getPluginId());
  $this
    ->assertMenuLink([
    'children' => [],
    'parents' => [
      $item6
        ->getPluginId(),
      $item4
        ->getPluginId(),
    ],
    'route_name' => 'entity.node.canonical',
    'route_parameters' => [
      'node' => $node5
        ->id(),
    ],
    'url' => '',
    // See above.
    'langcode' => 'en',
  ], $item6
    ->getPluginId());

  // 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
    ->getPluginId(), $menu_name);

  // Hierarchy
  // <$menu_name>
  // - item1
  // - item4
  // -- item5
  // --- item2
  // ---- item3
  // -- item6
  $this
    ->assertMenuLink([
    'children' => [],
    'parents' => [
      $item1
        ->getPluginId(),
    ],
    // See above.
    'langcode' => 'en',
  ], $item1
    ->getPluginId());
  $this
    ->assertMenuLink([
    'children' => [
      $item5
        ->getPluginId(),
      $item6
        ->getPluginId(),
      $item2
        ->getPluginId(),
      $item3
        ->getPluginId(),
    ],
    'parents' => [
      $item4
        ->getPluginId(),
    ],
    // See above.
    'langcode' => 'en',
  ], $item4
    ->getPluginId());
  $this
    ->assertMenuLink([
    'children' => [
      $item2
        ->getPluginId(),
      $item3
        ->getPluginId(),
    ],
    'parents' => [
      $item5
        ->getPluginId(),
      $item4
        ->getPluginId(),
    ],
    // See above.
    'langcode' => 'en',
  ], $item5
    ->getPluginId());
  $this
    ->assertMenuLink([
    'children' => [
      $item3
        ->getPluginId(),
    ],
    'parents' => [
      $item2
        ->getPluginId(),
      $item5
        ->getPluginId(),
      $item4
        ->getPluginId(),
    ],
    // See above.
    'langcode' => 'en',
  ], $item2
    ->getPluginId());
  $this
    ->assertMenuLink([
    'children' => [],
    'parents' => [
      $item3
        ->getPluginId(),
      $item2
        ->getPluginId(),
      $item5
        ->getPluginId(),
      $item4
        ->getPluginId(),
    ],
    // See above.
    'langcode' => 'en',
  ], $item3
    ->getPluginId());

  // Add 102 menu links with increasing weights, then make sure the last-added
  // item's weight doesn't get changed because of the old hardcoded delta=50.
  $items = [];
  for ($i = -50; $i <= 51; $i++) {
    $items[$i] = $this
      ->addMenuLink('', '/node/' . $node1
      ->id(), $menu_name, TRUE, strval($i));
  }
  $this
    ->assertMenuLink([
    'weight' => '51',
  ], $items[51]
    ->getPluginId());

  // Disable a link and then re-enable the link via the overview form.
  $this
    ->disableMenuLink($item1);
  $edit = [];
  $edit['links[menu_plugin_id:' . $item1
    ->getPluginId() . '][enabled]'] = TRUE;
  $this
    ->drupalPostForm('admin/structure/menu/manage/' . $item1
    ->getMenuName(), $edit, t('Save'));

  // Mark item2, item4 and item5 as expanded.
  // This is done in order to show them on the frontpage.
  $item2->expanded->value = 1;
  $item2
    ->save();
  $item4->expanded->value = 1;
  $item4
    ->save();
  $item5->expanded->value = 1;
  $item5
    ->save();

  // Verify in the database.
  $this
    ->assertMenuLink([
    'enabled' => 1,
  ], $item1
    ->getPluginId());

  // Add an external link.
  $item7 = $this
    ->addMenuLink('', 'https://www.drupal.org', $menu_name);
  $this
    ->assertMenuLink([
    'url' => 'https://www.drupal.org',
  ], $item7
    ->getPluginId());

  // Add <front> menu item.
  $item8 = $this
    ->addMenuLink('', '/', $menu_name);
  $this
    ->assertMenuLink([
    'route_name' => '<front>',
  ], $item8
    ->getPluginId());
  $this
    ->drupalGet('');
  $this
    ->assertSession()
    ->statusCodeEquals(200);

  // Make sure we get routed correctly.
  $this
    ->clickLink($item8
    ->getTitle());
  $this
    ->assertSession()
    ->statusCodeEquals(200);

  // Check invalid menu link parents.
  $this
    ->checkInvalidParentMenuLinks();

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