You are here

function MenuTest::doMenuTests in Zircon Profile 8.0

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

Tests menu functionality.

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

File

core/modules/menu_ui/src/Tests/MenuTest.php, line 264
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 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, array(
    '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, array(
    '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?', array(
    '%item' => $link_title,
  )));
  $this
    ->drupalPostForm(NULL, array(), 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(array(
    'type' => 'article',
  ));
  $node2 = $this
    ->drupalCreateNode(array(
    'type' => 'article',
  ));
  $node3 = $this
    ->drupalCreateNode(array(
    'type' => 'article',
  ));
  $node4 = $this
    ->drupalCreateNode(array(
    'type' => 'article',
  ));

  // Create a node with an alias.
  $node5 = $this
    ->drupalCreateNode(array(
    'type' => 'article',
    'path' => array(
      '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($item1
    ->getPluginId(), array(
    'children' => array(
      $item2
        ->getPluginId(),
      $item3
        ->getPluginId(),
    ),
    'parents' => array(
      $item1
        ->getPluginId(),
    ),
    // We assert the language code here to make sure that the language
    // selection element degrades gracefully without the Language module.
    'langcode' => 'en',
  ));
  $this
    ->assertMenuLink($item2
    ->getPluginId(), array(
    'children' => array(
      $item3
        ->getPluginId(),
    ),
    'parents' => array(
      $item2
        ->getPluginId(),
      $item1
        ->getPluginId(),
    ),
    // See above.
    'langcode' => 'en',
  ));
  $this
    ->assertMenuLink($item3
    ->getPluginId(), array(
    'children' => array(),
    'parents' => array(
      $item3
        ->getPluginId(),
      $item2
        ->getPluginId(),
      $item1
        ->getPluginId(),
    ),
    // See above.
    'langcode' => 'en',
  ));

  // 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($item4
    ->getPluginId(), array(
    'children' => array(
      $item5
        ->getPluginId(),
      $item6
        ->getPluginId(),
    ),
    'parents' => array(
      $item4
        ->getPluginId(),
    ),
    // See above.
    'langcode' => 'en',
  ));
  $this
    ->assertMenuLink($item5
    ->getPluginId(), array(
    'children' => array(),
    'parents' => array(
      $item5
        ->getPluginId(),
      $item4
        ->getPluginId(),
    ),
    'langcode' => 'en',
  ));
  $this
    ->assertMenuLink($item6
    ->getPluginId(), array(
    'children' => array(),
    'parents' => array(
      $item6
        ->getPluginId(),
      $item4
        ->getPluginId(),
    ),
    'route_name' => 'entity.node.canonical',
    'route_parameters' => array(
      'node' => $node5
        ->id(),
    ),
    'url' => '',
    // See above.
    'langcode' => 'en',
  ));

  // 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($item1
    ->getPluginId(), array(
    'children' => array(),
    'parents' => array(
      $item1
        ->getPluginId(),
    ),
    // See above.
    'langcode' => 'en',
  ));
  $this
    ->assertMenuLink($item4
    ->getPluginId(), array(
    'children' => array(
      $item5
        ->getPluginId(),
      $item6
        ->getPluginId(),
      $item2
        ->getPluginId(),
      $item3
        ->getPluginId(),
    ),
    'parents' => array(
      $item4
        ->getPluginId(),
    ),
    // See above.
    'langcode' => 'en',
  ));
  $this
    ->assertMenuLink($item5
    ->getPluginId(), array(
    'children' => array(
      $item2
        ->getPluginId(),
      $item3
        ->getPluginId(),
    ),
    'parents' => array(
      $item5
        ->getPluginId(),
      $item4
        ->getPluginId(),
    ),
    // See above.
    'langcode' => 'en',
  ));
  $this
    ->assertMenuLink($item2
    ->getPluginId(), array(
    'children' => array(
      $item3
        ->getPluginId(),
    ),
    'parents' => array(
      $item2
        ->getPluginId(),
      $item5
        ->getPluginId(),
      $item4
        ->getPluginId(),
    ),
    // See above.
    'langcode' => 'en',
  ));
  $this
    ->assertMenuLink($item3
    ->getPluginId(), array(
    'children' => array(),
    'parents' => array(
      $item3
        ->getPluginId(),
      $item2
        ->getPluginId(),
      $item5
        ->getPluginId(),
      $item4
        ->getPluginId(),
    ),
    // See above.
    'langcode' => 'en',
  ));

  // 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 = array();
  for ($i = -50; $i <= 51; $i++) {
    $items[$i] = $this
      ->addMenuLink('', '/node/' . $node1
      ->id(), $menu_name, TRUE, strval($i));
  }
  $this
    ->assertMenuLink($items[51]
    ->getPluginId(), array(
    'weight' => '51',
  ));

  // Disable a link and then re-enable the link via the overview form.
  $this
    ->disableMenuLink($item1);
  $edit = array();
  $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($item1
    ->getPluginId(), array(
    'enabled' => 1,
  ));

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

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

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

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

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