1 layout.test LayoutInterfaceTest::testLayoutMenuItems()

Tests conditions and menu item navigation.

File

core/modules/layout/tests/layout.test, line 1369
Tests for the Layout module.

Class

LayoutInterfaceTest
Tests the interface for adding, removing, and moving blocks.

Code

function testLayoutMenuItems() {
  $layout_name = strtolower($this->randomName());
  $layout_path = $layout_name . '/' . strtolower($this->randomName());
  $edit = array(
    'name' => $layout_name,
    'title' => $layout_name,
    'path' => $layout_path,
    'layout_template' => 'moscone_flipped',
  );
  $this->backdropPost('admin/structure/layouts/add', $edit, t('Create layout'));

  $edit = array(
    'title_display' => LAYOUT_TITLE_CUSTOM,
    'title' => 'Menu item test',
  );
  $this->backdropPost('admin/structure/layouts/manage/' . $layout_name . '/edit-title/editor/title', $edit, t('Save configuration'));
  $this->backdropPost('admin/structure/layouts/manage/' . $layout_name, array(), t('Save layout'));
  $this->backdropGet($layout_path);
  $this->assertResponse(200, 'Layout displayed at the given path.');

  // At this point there should only be a single custom path, so there should
  // only be a single "Menu settings" link on the listing page. If other
  // layouts are added in tests in this class, be sure they are deleted at the
  // end of the test.
  $this->backdropGet('admin/structure/layouts');
  $this->clickLink(t('Menu settings'));
  $this->assertUrl('admin/structure/layouts/menu/' . $layout_name, array(), 'Menu settings link goes to the correct layout menu item settings.');

  // Set as a normal menu item.
  $menu_title = $this->randomName();
  $edit = array(
    'menu[type]' => 'normal',
    'menu[title]' => $menu_title,
    'menu[weight]' => 1,
  );
  $this->backdropPost(NULL, $edit, t('Save menu settings'));

  // Check that the link is available on the homepage (via the main menu).
  $this->backdropGet('<front>');
  $this->assertLink($menu_title, 0, 'Menu link shown on the homepage.');

  $this->backdropPost('admin/structure/layouts/menu/' . $layout_name, array(), t('Add visibility condition'));
  $edit = array(
    'condition' => 'path',
  );
  $this->backdropPost(NULL, $edit, t('Load condition'));

  // This condition will make the menu item available on the home page, and
  // on the layout path itself. Note that the "layout_name" is the first part
  // of the path, so we allow access on it and its children paths.
  $edit = array(
    'paths' => "<front>\n$layout_name*\n",
  );
  $this->backdropPost(NULL, $edit, t('Add visibility condition'));
  $this->backdropPost(NULL, array(), t('Save menu settings'));

  $this->backdropGet('<front>');
  $this->assertLink($menu_title, 0, 'Menu link found on the homepage.');
  $this->backdropGet('node/' . $this->test_node1->nid);
  $this->assertNoLink($menu_title, 'Menu link not shown when conditions prevent it.');
  $this->backdropGet($layout_path);
  $this->assertLink($menu_title, 0, 'Menu link shown on the layout page itself.');

  // Change the menu item to a default local tab.
  $menu_tab_title = $this->randomName();
  $edit = array(
    'menu[type]' => 'default tab',
    'menu[title]' => $menu_tab_title,
    'menu[weight]' => 1,
    'menu[parent][type]' => 'normal',
    'menu[parent][title]' => $menu_title,
  );
  $this->backdropPost('admin/structure/layouts/menu/' . $layout_name, $edit, t('Save menu settings'));

  // Ensure that the parent item link still shows up.
  $this->backdropGet('<front>');
  $this->assertLink($menu_title, 0, 'Menu link found on the homepage when registered as a parent menu item link.');
  $this->clickLink($menu_title);
  $this->assertUrl($layout_name, array(), 'Menu link uses the parent path, without the default tab portion of the path.');

  // Create another layout to use as a second tab.
  $tab_layout_name = strtolower($this->randomName());
  $tab_layout_path = $layout_name . '/' . strtolower($this->randomName());
  $edit = array(
    'name' => $tab_layout_name,
    'title' => $tab_layout_name,
    'path' => $tab_layout_path,
    'layout_template' => 'moscone_flipped',
  );
  $this->backdropPost('admin/structure/layouts/add', $edit, t('Create layout'));

  $tab_layout_title = $this->randomName();
  $edit = array(
    'menu[type]' => 'tab',
    'menu[title]' => $tab_layout_title,
    'menu[weight]' => 1,
  );
  $this->backdropPost('admin/structure/layouts/menu/' . $tab_layout_name, $edit, t('Save menu settings'));

  // On the "parent" path, we should now see the default tab and this new tab.
  $this->backdropGet($layout_name);
  $active_tab = $this->xpath('//ul[contains(@class,"tabs")]/li[contains(@class,"active")]/a[@href=:path]', array(
    ':path' => url($layout_name),
  ));
  $this->assertEqual(count($active_tab), 1, 'The menu default tab for a layout shows up properly.');
  $second_tab = $this->xpath('//ul[contains(@class,"tabs")]/li/a[@href=:path]', array(
    ':path' => url($tab_layout_path),
  ));
  $this->assertEqual(count($second_tab), 1, 'The normal menu tab for a layout shows up properly.');

  // Make a 3rd layout at a yet deeper path, creating another tab via its parent.
  $subtab_layout_name = strtolower($this->randomName());
  $subtab_layout_path = $tab_layout_path . '/' . strtolower($this->randomName());
  $subtab_layout_path_last = $subtab_layout_path . '/' . strtolower($this->randomName());
  $edit = array(
    'name' => $subtab_layout_name,
    'title' => $subtab_layout_name,
    'path' => $subtab_layout_path_last,
    'layout_template' => 'moscone_flipped',
  );
  $this->backdropPost('admin/structure/layouts/add', $edit, t('Create layout'));

  $subtab_layout_title = $this->randomName();
  $sub_subtab_layout_title = $this->randomName();
  $edit = array(
    'menu[type]' => 'default tab',
    'menu[title]' => $sub_subtab_layout_title,
    'menu[weight]' => 0,
    'menu[parent][type]' => 'tab',
    'menu[parent][title]' => $subtab_layout_title,
  );
  $this->backdropPost('admin/structure/layouts/menu/' . $subtab_layout_name, $edit, t('Save menu settings'));

  // Reuse the "tab" item to be a child of the subtab.
  $subtab_layout_path_last = $subtab_layout_path . '/' . strtolower($this->randomName());
  $edit = array(
    'path' => $subtab_layout_path_last,
  );
  $this->backdropPost('admin/structure/layouts/manage/' . $tab_layout_name . '/configure', $edit, t('Save layout'));

  // Now we should have an active tab with subtabs, the first one is active.
  $this->backdropGet($subtab_layout_path);
  $active_tab = $this->xpath('//ul[contains(@class,"tabs")][contains(@class,"secondary")]/li[contains(@class,"active")]/a[@href=:path]', array(
    ':path' => url($subtab_layout_path),
  ));
  $this->assertEqual(count($active_tab), 1, 'The sub-tab default menu item for a layout shows up properly.');
  $second_tab = $this->xpath('//ul[contains(@class,"tabs")][contains(@class,"secondary")]/li/a[@href=:path]', array(
    ':path' => url($subtab_layout_path_last),
  ));
  $this->assertEqual(count($second_tab), 1, 'The sub-tab menu item for a layout shows up properly.');

  // Delete all the generated layouts.
  $this->backdropPost('admin/structure/layouts/manage/' . $layout_name . '/delete', array(), t('Delete layout'));
  $this->backdropPost('admin/structure/layouts/manage/' . $tab_layout_name . '/delete', array(), t('Delete layout'));
  $this->backdropPost('admin/structure/layouts/manage/' . $subtab_layout_name . '/delete', array(), t('Delete layout'));

  // Check that both layouts and menu items are deleted.
  layout_reset_caches();
  $this->assertFalse(layout_load($layout_name), 'Base layout deleted.');
  $this->assertFalse(layout_load($tab_layout_name), 'Tab layout deleted.');
  $this->assertFalse(layout_load($subtab_layout_name), 'Sub-tab layout deleted.');
  $this->assertFalse(layout_menu_item_load($layout_name), 'Base menu item deleted.');
  $this->assertFalse(layout_menu_item_load($tab_layout_name), 'Tab menu item deleted.');
  $this->assertFalse(layout_menu_item_load($subtab_layout_name), 'Sub-tab menu item deleted.');
}