You are here

function MenuTest::testMenus in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/views/src/Tests/Wizard/MenuTest.php \Drupal\views\Tests\Wizard\MenuTest::testMenus()

Tests the menu functionality.

File

core/modules/views/src/Tests/Wizard/MenuTest.php, line 23
Contains \Drupal\views\Tests\Wizard\MenuTest.

Class

MenuTest
Tests the ability of the views wizard to put views in a menu.

Namespace

Drupal\views\Tests\Wizard

Code

function testMenus() {
  $this
    ->drupalPlaceBlock('system_menu_block:main');

  // Create a view with a page display and a menu link in the Main Menu.
  $view = array();
  $view['label'] = $this
    ->randomMachineName(16);
  $view['id'] = strtolower($this
    ->randomMachineName(16));
  $view['description'] = $this
    ->randomMachineName(16);
  $view['page[create]'] = 1;
  $view['page[title]'] = $this
    ->randomMachineName(16);
  $view['page[path]'] = $this
    ->randomMachineName(16);
  $view['page[link]'] = 1;
  $view['page[link_properties][menu_name]'] = 'main';
  $view['page[link_properties][title]'] = $this
    ->randomMachineName(16);
  $this
    ->drupalPostForm('admin/structure/views/add', $view, t('Save and edit'));

  // Make sure there is a link to the view from the front page (where we
  // expect the main menu to display).
  $this
    ->drupalGet('');
  $this
    ->assertResponse(200);
  $this
    ->assertLink($view['page[link_properties][title]']);
  $this
    ->assertLinkByHref(Url::fromUri('base:' . $view['page[path]'])
    ->toString());

  // Make sure the link is associated with the main menu.

  /** @var \Drupal\Core\Menu\MenuLinkManagerInterface $menu_link_manager */
  $menu_link_manager = \Drupal::service('plugin.manager.menu.link');

  /** @var \Drupal\Core\Menu\MenuLinkInterface $link */
  $link = $menu_link_manager
    ->createInstance('views_view:views.' . $view['id'] . '.page_1');
  $url = $link
    ->getUrlObject();
  $this
    ->assertEqual($url
    ->getRouteName(), 'view.' . $view['id'] . '.page_1', SafeMarkup::format('Found a link to %path in the main menu', array(
    '%path' => $view['page[path]'],
  )));
  $metadata = $link
    ->getMetaData();
  $this
    ->assertEqual(array(
    'view_id' => $view['id'],
    'display_id' => 'page_1',
  ), $metadata);
}