You are here

public function SiteMapContentTest::testMenus in Site map 8

Tests menus.

File

src/Tests/SiteMapContentTest.php, line 99

Class

SiteMapContentTest
Test case class for site map's content tests.

Namespace

Drupal\site_map\Tests

Code

public function testMenus() {

  // Assert that main menu is not included in the site map by default.
  $this
    ->drupalGet('/sitemap');
  $elements = $this
    ->cssSelect(".site-map-box h2:contains('" . t('Main navigation') . "')");
  $this
    ->assertEqual(count($elements), 0, 'Main menu is not included.');

  // Configure module to show main menu, with enabled menu items only.
  $edit = array(
    'show_menus[main]' => 'main',
    'show_menus_hidden' => FALSE,
  );
  $this
    ->drupalPostForm('admin/config/search/sitemap', $edit, t('Save configuration'));

  // Create dummy node with enabled menu item.
  $node_1_title = $this
    ->randomString();
  $edit = array(
    'title[0][value]' => $node_1_title,
    'menu[enabled]' => TRUE,
    'menu[title]' => $node_1_title,
    // In oder to make main navigation menu displayed, there must be at least
    // one child menu item of that menu.
    'menu[menu_parent]' => 'main:',
  );
  $this
    ->drupalPostForm('node/add/article', $edit, t('Save and publish'));

  // Create dummy node with disabled menu item.
  $node_2_title = $this
    ->randomString();
  $edit = array(
    'title[0][value]' => $node_2_title,
    'menu[enabled]' => TRUE,
    'menu[title]' => $node_2_title,
    'menu[menu_parent]' => 'main:',
  );
  $this
    ->drupalPostForm('node/add/article', $edit, t('Save and publish'));

  // Disable menu item.
  $menu_links = entity_load_multiple_by_properties('menu_link_content', array(
    'title' => $node_2_title,
  ));
  $menu_link = reset($menu_links);
  $mlid = $menu_link
    ->id();
  $edit = array(
    'enabled[value]' => FALSE,
  );
  $this
    ->drupalPostForm("admin/structure/menu/item/{$mlid}/edit", $edit, t('Save'));

  // Assert that main menu is included in the site map.
  $this
    ->drupalGet('/sitemap');
  $elements = $this
    ->cssSelect(".site-map-box h2:contains('" . t('Main navigation') . "')");
  $this
    ->assertEqual(count($elements), 1, 'Main menu is included.');

  // Assert that node 1 is listed in the site map, but not node 2.
  $this
    ->assertLink($node_1_title);
  $this
    ->assertNoLink($node_2_title);

  // Configure module to show all menu items.
  $edit = array(
    'show_menus_hidden' => TRUE,
  );
  $this
    ->drupalPostForm('admin/config/search/sitemap', $edit, t('Save configuration'));

  // Assert that both node 1 and node 2 are listed in the site map.
  $this
    ->drupalGet('/sitemap');
  $this
    ->assertLink($node_1_title);
  $this
    ->assertLink($node_2_title);
}