/**
  * {@inheritdoc}
  */
 public function menuAccess(AccountInterface $account, Menu $menu)
 {
     $permission = 'administer ' . $menu->get('id') . ' menu items';
     $permissions = $this::getPerMenuPermissions($account);
     if ($account->hasPermission('administer menu') || isset($permissions[$permission])) {
         return AccessResult::allowed();
     }
     return AccessResult::neutral();
 }
 /**
  * Verifies the logged in user has the desired access to various menu pages.
  *
  * @param integer $response
  *   The expected HTTP response code. Defaults to 200.
  */
 private function verifyAccess($response = 200)
 {
     // View menu help page.
     $this->drupalGet('admin/help/menu');
     $this->assertResponse($response);
     if ($response == 200) {
         $this->assertText(t('Menu'), 'Menu help was displayed');
     }
     // View menu build overview page.
     $this->drupalGet('admin/structure/menu');
     $this->assertResponse($response);
     if ($response == 200) {
         $this->assertText(t('Menus'), 'Menu build overview page was displayed');
     }
     // View tools menu customization page.
     $this->drupalGet('admin/structure/menu/manage/' . $this->menu->id());
     $this->assertResponse($response);
     if ($response == 200) {
         $this->assertText(t('Tools'), 'Tools menu page was displayed');
     }
     // View menu edit page for a static link.
     $item = $this->getStandardMenuLink();
     $this->drupalGet('admin/structure/menu/link/' . $item->getPluginId() . '/edit');
     $this->assertResponse($response);
     if ($response == 200) {
         $this->assertText(t('Edit menu item'), 'Menu edit page was displayed');
     }
     // View add menu page.
     $this->drupalGet('admin/structure/menu/add');
     $this->assertResponse($response);
     if ($response == 200) {
         $this->assertText(t('Menus'), 'Add menu page was displayed');
     }
 }
  /**
   * Tests the Drupal 6 menu to Drupal 8 migration.
   */
  public function testMenu() {
    $navigation_menu = Menu::load('navigation');
    $this->assertSame('navigation', $navigation_menu->id());
    $this->assertSame('Navigation', $navigation_menu->label());
    $expected = <<<EOT
The navigation menu is provided by Drupal and is the main interactive menu for any site. It is usually the only menu that contains personalized links for authenticated users, and is often not even visible to anonymous users.
EOT;
    $this->assertSame($expected, $navigation_menu->getDescription());

    // Test that we can re-import using the ConfigEntityBase destination.
    Database::getConnection('default', 'migrate')
      ->update('menu_custom')
      ->fields(array('title' => 'Home Navigation'))
      ->condition('menu_name', 'navigation')
      ->execute();

    $migration = $this->getMigration('d6_menu');
    \Drupal::database()
        ->truncate($migration->getIdMap()->mapTableName())
        ->execute();
    $this->executeMigration($migration);

    $navigation_menu = Menu::load('navigation');
    $this->assertSame('Home Navigation', $navigation_menu->label());
  }
Exemple #4
0
 /**
  * Tests XSS coming from Menu block labels.
  */
 protected function doMenuTest()
 {
     Menu::create(['id' => $this->randomMachineName(), 'label' => '<script>alert("menu");</script>'])->save();
     $this->drupalGet(Url::fromRoute('block.admin_display'));
     $this->clickLink('<script>alert("menu");</script>');
     $this->assertRaw('&lt;script&gt;alert(&quot;menu&quot;);&lt;/script&gt;');
     $this->assertNoRaw('<script>alert("menu");</script>');
 }
 /**
  * Return an associative array of menus names.
  *
  * @return array
  *   An array with the machine-readable names as the keys, and human-readable
  *   titles as the values.
  */
 protected function getMenuLabels()
 {
     $menus = [];
     foreach (Menu::loadMultiple() as $menu_name => $menu) {
         $menus[$menu_name] = $menu->label();
     }
     asort($menus);
     return $menus;
 }
Exemple #6
0
 /**
  * Tests XSS coming from Menu block labels.
  */
 protected function doMenuTest()
 {
     Menu::create(['id' => $this->randomMachineName(), 'label' => '<script>alert("menu");</script>'])->save();
     $this->drupalGet(Url::fromRoute('block.admin_display'));
     $this->clickLinkPartialName('Place block');
     // The block admin label is automatically XSS admin filtered.
     $this->assertRaw('alert("menu");');
     $this->assertNoRaw('<script>alert("menu");</script>');
 }
Exemple #7
0
 /**
  * Tests cache tags presence and invalidation of the Menu entity.
  *
  * Tests the following cache tags:
  * - "menu:<menu ID>"
  */
 public function testMenuBlock()
 {
     $url = Url::fromRoute('test_page_test.test_page');
     // Create a Llama menu, add a link to it and place the corresponding block.
     $menu = Menu::create(array('id' => 'llama', 'label' => 'Llama', 'description' => 'Description text'));
     $menu->save();
     /** @var \Drupal\Core\Menu\MenuLinkManagerInterface $menu_link_manager */
     $menu_link_manager = \Drupal::service('plugin.manager.menu.link');
     // Move a link into the new menu.
     $menu_link = $menu_link_manager->updateDefinition('test_page_test.test_page', array('menu_name' => 'llama', 'parent' => ''));
     $block = $this->drupalPlaceBlock('system_menu_block:llama', array('label' => 'Llama', 'provider' => 'system', 'region' => 'footer'));
     // Prime the page cache.
     $this->verifyPageCache($url, 'MISS');
     // Verify a cache hit, but also the presence of the correct cache tags.
     $expected_tags = array('rendered', 'block_view', 'config:block_list', 'config:block.block.' . $block->id(), 'config:system.menu.llama', 'config:user.role.anonymous');
     $this->verifyPageCache($url, 'HIT', $expected_tags);
     // Verify that after modifying the menu, there is a cache miss.
     $this->pass('Test modification of menu.', 'Debug');
     $menu->set('label', 'Awesome llama');
     $menu->save();
     $this->verifyPageCache($url, 'MISS');
     // Verify a cache hit.
     $this->verifyPageCache($url, 'HIT');
     // Verify that after modifying the menu link weight, there is a cache miss.
     $menu_link_manager->updateDefinition('test_page_test.test_page', array('weight' => -10));
     $this->pass('Test modification of menu link.', 'Debug');
     $this->verifyPageCache($url, 'MISS');
     // Verify a cache hit.
     $this->verifyPageCache($url, 'HIT');
     // Verify that after adding a menu link, there is a cache miss.
     $this->pass('Test addition of menu link.', 'Debug');
     $menu_link_2 = MenuLinkContent::create(array('id' => '', 'parent' => '', 'title' => 'Alpaca', 'menu_name' => 'llama', 'link' => [['uri' => 'internal:/']], 'bundle' => 'menu_name'));
     $menu_link_2->save();
     $this->verifyPageCache($url, 'MISS');
     // Verify a cache hit.
     $this->verifyPageCache($url, 'HIT');
     // Verify that after resetting the first menu link, there is a cache miss.
     $this->pass('Test reset of menu link.', 'Debug');
     $this->assertTrue($menu_link->isResettable(), 'First link can be reset');
     $menu_link = $menu_link_manager->resetLink($menu_link->getPluginId());
     $this->verifyPageCache($url, 'MISS');
     // Verify a cache hit.
     $this->verifyPageCache($url, 'HIT', $expected_tags);
     // Verify that after deleting the menu, there is a cache miss.
     $this->pass('Test deletion of menu.', 'Debug');
     $menu->delete();
     $this->verifyPageCache($url, 'MISS');
     // Verify a cache hit.
     $this->verifyPageCache($url, 'HIT', ['config:block_list', 'config:user.role.anonymous', 'rendered']);
 }
 /**
  * Tests the Drupal 6 menu to Drupal 8 migration.
  */
 public function testMenu()
 {
     $navigation_menu = Menu::load('navigation');
     $this->assertEqual($navigation_menu->id(), 'navigation');
     $this->assertEqual($navigation_menu->label(), 'Navigation');
     $this->assertEqual($navigation_menu->description, 'The navigation menu is provided by Drupal and is the main interactive menu for any site. It is usually the only menu that contains personalized links for authenticated users, and is often not even visible to anonymous users.');
     // Test that we can re-import using the ConfigEntityBase destination.
     Database::getConnection('default', 'migrate')->update('menu_custom')->fields(array('title' => 'Home Navigation'))->condition('menu_name', 'navigation')->execute();
     db_truncate(entity_load('migration', 'd6_menu')->getIdMap()->mapTableName())->execute();
     $migration = entity_load_unchanged('migration', 'd6_menu');
     $executable = new MigrateExecutable($migration, $this);
     $executable->import();
     $navigation_menu = entity_load_unchanged('menu', 'navigation');
     $this->assertEqual($navigation_menu->label(), 'Home Navigation');
 }
Exemple #9
0
 /**
  * Tests menu block settings.
  */
 protected function doTestMenuBlock()
 {
     $menu_id = $this->menu->id();
     $block_id = $this->blockPlacements[$menu_id];
     $this->drupalGet('admin/structure/block/manage/' . $block_id);
     $this->drupalPostForm(NULL, ['settings[depth]' => 3, 'settings[level]' => 2], t('Save block'));
     $block = Block::load($block_id);
     $settings = $block->getPlugin()->getConfiguration();
     $this->assertEqual($settings['depth'], 3);
     $this->assertEqual($settings['level'], 2);
     // Reset settings.
     $block->getPlugin()->setConfigurationValue('depth', 0);
     $block->getPlugin()->setConfigurationValue('level', 1);
     $block->save();
 }
 /**
  * Tests the MenuLinkContentDeleteForm class.
  */
 public function testMenuLinkContentDeleteForm()
 {
     // Add new menu item.
     $this->drupalPostForm('admin/structure/menu/manage/admin/add', ['title[0][value]' => t('Front page'), 'link[0][uri]' => '<front>'], t('Save'));
     $this->assertText(t('The menu link has been saved.'));
     $menu_link = MenuLinkContent::load(1);
     $this->drupalGet($menu_link->urlInfo('delete-form'));
     $this->assertRaw(t('Are you sure you want to delete the custom menu link %name?', ['%name' => $menu_link->label()]));
     $this->assertLink(t('Cancel'));
     // Make sure cancel link points to link edit
     $this->assertLinkByHref($menu_link->url('edit-form'));
     \Drupal::service('module_installer')->install(['menu_ui']);
     // Make sure cancel URL points to menu_ui route now.
     $this->drupalGet($menu_link->urlInfo('delete-form'));
     $menu = Menu::load($menu_link->getMenuName());
     $this->assertLinkByHref($menu->url('edit-form'));
     $this->drupalPostForm(NULL, [], t('Delete'));
     $this->assertRaw(t('The menu link %title has been deleted.', ['%title' => $menu_link->label()]));
 }
Exemple #11
0
 public function createMenu($crawler)
 {
     $menu_name = $crawler->filterXPath('//*[@data-menu]')->getAttribute('data-menu');
     //check and see if the menu name exists
     if (!\Drupal::entityQuery('menu')->condition('id', $menu_name)->execute()) {
         $menu = Menu::create(array('id' => $menu_name, 'label' => ucwords(str_replace('-', ' ', $menu_name))));
         $menu->save();
         // check if menu has sub menu
         if ($crawler->filter('.w-dropdown')->count()) {
             $this->handleDropdownMenu($crawler, $menu_name);
         }
         // proceed with single menu item
         $menu_links = $crawler->filter('.w-nav-link')->each(function (Crawler $node, $i) use($menu_name) {
             $link = $node->extract(array('href'))[0];
             $link = str_replace('.html', '', $link);
             $title = $node->extract(array('_text'))[0];
             $this->createMenuLink($title, 'internal:/' . $link, $menu_name, TRUE, NULL);
         });
     }
 }
  /**
   * Tests the Drupal 7 menu to Drupal 8 migration.
   */
  public function testMenu() {
    $this->assertEntity('main', 'Main menu', 'The <em>Main</em> menu is used on many sites to show the major sections of the site, often in a top navigation bar.');
    $this->assertEntity('admin', 'Management', 'The <em>Management</em> menu contains links for administrative tasks.');
    $this->assertEntity('menu-test-menu', 'Test Menu', 'Test menu description.');
    $this->assertEntity('tools', 'Navigation', 'The <em>Navigation</em> menu contains links intended for site visitors. Links are added to the <em>Navigation</em> menu automatically by some modules.');
    $this->assertEntity('account', 'User menu', 'The <em>User</em> menu contains links related to the user\'s account, as well as the \'Log out\' link.');

    // Test that we can re-import using the ConfigEntityBase destination.
    Database::getConnection('default', 'migrate')
      ->update('menu_custom')
      ->fields(array('title' => 'Home Navigation'))
      ->condition('menu_name', 'navigation')
      ->execute();

    $migration = $this->getMigration('d7_menu');
    \Drupal::database()
        ->truncate($migration->getIdMap()->mapTableName())
        ->execute();
    $this->executeMigration($migration);

    $navigation_menu = Menu::load('tools');
    $this->assertSame('Home Navigation', $navigation_menu->label());
  }
Exemple #13
0
 /**
  * Tests XSS coming from Menu block labels.
  */
 protected function doMenuTest()
 {
     Menu::create(['id' => $this->randomMachineName(), 'label' => '<script>alert("menu");</script>'])->save();
     $this->drupalGet(Url::fromRoute('block.admin_display'));
     $this->clickLinkPartialName('Place block');
     $this->assertEscaped('<script>alert("menu");</script>');
     $this->assertNoRaw('<script>alert("menu");</script>');
 }
 /**
  * Tests menu configuration is still English after English has been deleted.
  */
 function testMenuLanguageRemovedEnglish()
 {
     // Create a test menu to test language settings.
     // Machine name has to be lowercase.
     $menu_name = Unicode::strtolower($this->randomMachineName(16));
     $edit = array('id' => $menu_name, 'description' => '', 'label' => $this->randomString(), 'langcode' => 'en');
     $this->drupalPostForm('admin/structure/menu/add', $edit, t('Save'));
     // Check that the language settings were saved.
     $menu = Menu::load($menu_name);
     $this->assertEqual($menu->language()->getId(), 'en');
     // Remove English language. To do that another language has to be set as
     // default.
     $language = language_load('cs');
     $language->default = TRUE;
     language_save($language);
     language_delete('en');
     // Save the menu again and check if the language is still the same.
     $this->drupalPostForm("admin/structure/menu/manage/{$menu_name}", array(), t('Save'));
     $menu = Menu::load($menu_name);
     $this->assertEqual($menu->language()->getId(), 'en');
 }
 /**
  * {@inheritdoc}
  */
 public function buildForm(array $form, FormStateInterface $form_state)
 {
     $config = $this->configFactory->get('site_map.settings');
     $form['page_title'] = array('#type' => 'textfield', '#title' => $this->t('Page title'), '#default_value' => $config->get('page_title'), '#description' => $this->t('Page title that will be used on the @link.', array('@link' => $this->l($this->t('site map page'), Url::fromRoute('site_map.page')))));
     $site_map_message = $config->get('message');
     $form['message'] = array('#type' => 'text_format', '#format' => isset($site_map_message['format']) ? $site_map_message['format'] : NULL, '#title' => $this->t('Site map message'), '#default_value' => $site_map_message['value'], '#description' => $this->t('Define a message to be displayed above the site map.'));
     $form['site_map_content'] = array('#type' => 'details', '#title' => $this->t('Site map content'), '#open' => TRUE);
     $site_map_ordering = array();
     $form['site_map_content']['show_front'] = array('#type' => 'checkbox', '#title' => $this->t('Show front page'), '#default_value' => $config->get('show_front'), '#description' => $this->t('When enabled, this option will include the front page in the site map.'));
     $site_map_ordering['front'] = t('Front page');
     $form['site_map_content']['show_titles'] = array('#type' => 'checkbox', '#title' => $this->t('Show titles'), '#default_value' => $config->get('show_titles'), '#description' => $this->t('When enabled, this option will show titles. Disable to not show section titles.'));
     if ($this->moduleHandler->moduleExists('blog')) {
         $form['site_map_content']['show_blogs'] = array('#type' => 'checkbox', '#title' => t('Show active blog authors'), '#default_value' => $config->get('show_blogs'), '#description' => t('When enabled, this option will show the 10 most active blog authors.'));
         $site_map_ordering['blogs'] = t('Active blog authors');
     }
     if ($this->moduleHandler->moduleExists('book')) {
         $book_options = array();
         foreach ($this->bookManager->getAllBooks() as $book) {
             $book_options[$book['bid']] = $book['title'];
         }
         $form['site_map_content']['show_books'] = array('#type' => 'checkboxes', '#title' => $this->t('Books to include in the site map'), '#default_value' => $config->get('show_books'), '#options' => $book_options, '#multiple' => TRUE);
         $form['site_map_content']['books_expanded'] = array('#type' => 'checkbox', '#title' => $this->t('Show books expanded'), '#default_value' => $config->get('books_expanded'), '#description' => $this->t('When enabled, this option will show all children pages for each book.'));
         $site_map_ordering['books'] = t('Books');
     }
     $menu_options = array();
     $menus = Menu::loadMultiple();
     foreach ($menus as $id => $menu) {
         $menu_options[$id] = $menu->label();
         $site_map_ordering['menus_' . $id] = $menu->label();
     }
     $form['site_map_content']['show_menus'] = array('#type' => 'checkboxes', '#title' => $this->t('Menus to include in the site map'), '#default_value' => $config->get('show_menus'), '#options' => $menu_options);
     // Thanks for fix by zhuber at
     // https://drupal.org/node/1331104#comment-5200266.
     $form['site_map_content']['show_menus_hidden'] = array('#type' => 'checkbox', '#title' => $this->t('Show disabled menu items'), '#default_value' => $config->get('show_menus_hidden'), '#description' => $this->t('When enabled, hidden menu links will also be shown.'));
     if ($this->moduleHandler->moduleExists('faq')) {
         $form['site_map_content']['show_faq'] = array('#type' => 'checkbox', '#title' => $this->t('Show FAQ content'), '#default_value' => $config->get('show_faq'), '#description' => $this->t('When enabled, this option will include the content from the FAQ module in the site map.'));
         $site_map_ordering['faq'] = t('FAQ content');
     }
     if ($this->moduleHandler->moduleExists('taxonomy')) {
         $vocab_options = array();
         foreach (taxonomy_vocabulary_load_multiple() as $vocabulary) {
             $vocab_options[$vocabulary->id()] = $vocabulary->label();
             $site_map_ordering['vocabularies_' . $vocabulary->id()] = $vocabulary->label();
         }
         $form['site_map_content']['show_vocabularies'] = array('#type' => 'checkboxes', '#title' => $this->t('Vocabularies to include in the site map'), '#default_value' => $config->get('show_vocabularies'), '#options' => $vocab_options, '#multiple' => TRUE);
     }
     $form['site_map_content']['order'] = array('#type' => 'item', '#title' => t('Site map order'), '#theme' => 'site_map_order');
     $site_map_order_defaults = $config->get('order');
     foreach ($site_map_ordering as $content_id => $content_title) {
         $form['site_map_content']['order'][$content_id] = array('content' => array('#markup' => $content_title), 'weight' => array('#type' => 'weight', '#title' => t('Weight for @title', array('@title' => $content_title)), '#title_display' => 'invisible', '#delta' => 50, '#default_value' => isset($site_map_order_defaults[$content_id]) ? $site_map_order_defaults[$content_id] : -50, '#parents' => array('order', $content_id)), '#weight' => isset($site_map_order_defaults[$content_id]) ? $site_map_order_defaults[$content_id] : -50);
     }
     $form['site_map_taxonomy_options'] = array('#type' => 'details', '#title' => $this->t('Taxonomy settings'));
     $form['site_map_taxonomy_options']['show_description'] = array('#type' => 'checkbox', '#title' => $this->t('Show vocabulary description'), '#default_value' => $config->get('show_description'), '#description' => $this->t('When enabled, this option will show the vocabulary description.'));
     $form['site_map_taxonomy_options']['show_count'] = array('#type' => 'checkbox', '#title' => $this->t('Show node counts by taxonomy terms'), '#default_value' => $config->get('show_count'), '#description' => $this->t('When enabled, this option will show the number of nodes in each taxonomy term.'));
     $form['site_map_taxonomy_options']['vocabulary_depth'] = array('#type' => 'textfield', '#title' => $this->t('Vocabulary depth'), '#default_value' => $config->get('vocabulary_depth'), '#size' => 3, '#maxlength' => 10, '#description' => $this->t('Specify how many levels taxonomy terms should be included. Enter "-1" to include all terms, "0" not to include terms at all, or "1" to only include top-level terms.'));
     $form['site_map_taxonomy_options']['term_threshold'] = array('#type' => 'textfield', '#title' => $this->t('Term count threshold'), '#default_value' => $config->get('term_threshold'), '#size' => 3, '#description' => $this->t('Only show taxonomy terms whose node counts are greater than this threshold. Set to -1 to disable.'));
     if ($this->moduleHandler->moduleExists('forum')) {
         $form['site_map_taxonomy_options']['forum_threshold'] = array('#type' => 'textfield', '#title' => $this->t('Forum count threshold'), '#default_value' => $config->get('forum_threshold'), '#size' => 3, '#description' => $this->t('Only show forums whose node counts are greater than this threshold. Set to -1 to disable.'));
     }
     $form['site_map_rss_options'] = array('#type' => 'details', '#title' => $this->t('RSS settings'));
     $form['site_map_rss_options']['rss_front'] = array('#type' => 'textfield', '#title' => $this->t('RSS feed for front page'), '#default_value' => $config->get('rss_front'), '#description' => $this->t('The RSS feed for the front page, default is rss.xml.'));
     $form['site_map_rss_options']['show_rss_links'] = array('#type' => 'select', '#title' => $this->t('Include RSS links'), '#default_value' => $config->get('show_rss_links'), '#options' => array(0 => $this->t('None'), 1 => $this->t('Include on the right side'), 2 => $this->t('Include on the left side')), '#description' => $this->t('When enabled, this option will show links to the RSS feeds for the front page and taxonomy terms, if enabled.'));
     $form['site_map_rss_options']['rss_taxonomy'] = array('#type' => 'textfield', '#title' => $this->t('RSS depth for vocabularies'), '#default_value' => $config->get('rss_taxonomy'), '#size' => 3, '#maxlength' => 10, '#description' => $this->t('Specify how many RSS feed links should be displayed with taxonomy terms. Enter "-1" to include with all terms, "0" not to include with any terms, or "1" to show only for top-level taxonomy terms.'));
     $form['site_map_css_options'] = array('#type' => 'details', '#title' => $this->t('CSS settings'));
     $form['site_map_css_options']['css'] = array('#type' => 'checkbox', '#title' => $this->t('Do not include site map CSS file'), '#default_value' => $config->get('css'), '#description' => $this->t("If you don't want to load the included CSS file you can check this box."));
     // Make use of the Checkall module if it's installed.
     if ($this->moduleHandler->moduleExists('checkall')) {
         $form['site_map_content']['show_books']['#checkall'] = TRUE;
         $form['site_map_content']['show_menus']['#checkall'] = TRUE;
         $form['site_map_content']['show_vocabularies']['#checkall'] = TRUE;
     }
     return parent::buildForm($form, $form_state);
 }
Exemple #16
0
 /**
  * {@inheritdoc}
  */
 protected function setUp()
 {
     parent::setUp();
     $this->menuLinkManager = \Drupal::service('plugin.manager.menu.link');
     Menu::create(array('id' => 'menu_test', 'label' => 'Test menu', 'description' => 'Description text'))->save();
 }
 /**
  * Tests menu link parents token.
  */
 public function testMenuLinkParentsToken()
 {
     // Create a menu with a simple link hierarchy :
     // - parent
     //   - child-1
     //      - child-1-1
     Menu::create(array('id' => 'menu_test', 'label' => 'Test menu'))->save();
     $base_options = ['provider' => 'menu_test', 'menu_name' => 'menu_test'];
     $parent = $base_options + ['title' => 'parent title', 'link' => ['uri' => 'internal:/menu-test/hierarchy/parent']];
     $parent = MenuLinkContent::create($parent);
     $parent->save();
     $child_1 = $base_options + ['title' => 'child_1 title', 'link' => ['uri' => 'internal:/menu-test/hierarchy/parent/child_1'], 'parent' => $parent->getPluginId()];
     $child_1 = MenuLinkContent::create($child_1);
     $child_1->save();
     $child_1_1 = $base_options + ['title' => 'child_1_1 title', 'link' => ['uri' => 'internal:/menu-test/hierarchy/parent/child_1/child_1_1'], 'parent' => $child_1->getPluginId()];
     $child_1_1 = MenuLinkContent::create($child_1_1);
     $child_1_1->save();
     $this->assertTokens('menu-link', ['menu-link' => $child_1_1], ['parents' => 'parent title, child_1 title']);
     // Change the parent of child_1_1 to 'parent' at the entity level.
     $child_1_1->parent->value = $parent->getPluginId();
     $child_1_1->save();
     $this->assertTokens('menu-link', ['menu-link' => $child_1_1], ['parents' => 'parent title']);
     // Change the parent of child_1_1 to 'main', at the entity level.
     $child_1_1->parent->value = '';
     $child_1_1->save();
     // The token shouldn't have been generated; the menu link has no parent.
     $this->assertNoTokens('menu-link', ['menu-link' => $child_1_1], ['parents']);
 }
 /**
  * {@inheritdoc}
  */
 protected function setUp()
 {
     parent::setUp();
     $this->installSchema('system', 'sequences');
     $this->installEntitySchema('user');
     $this->installEntitySchema('menu_link_content');
     $account = User::create(['name' => $this->randomMachineName(), 'status' => 1]);
     $account->save();
     $this->container->get('current_user')->setAccount($account);
     $this->menuLinkManager = $this->container->get('plugin.manager.menu.link');
     $this->linkTree = $this->container->get('menu.link_tree');
     $this->blockManager = $this->container->get('plugin.manager.block');
     $routes = new RouteCollection();
     $requirements = array('_access' => 'TRUE');
     $options = array('_access_checks' => array('access_check.default'));
     $routes->add('example1', new Route('/example1', array(), $requirements, $options));
     $routes->add('example2', new Route('/example2', array(), $requirements, $options));
     $routes->add('example3', new Route('/example3', array(), $requirements, $options));
     $routes->add('example4', new Route('/example4', array(), $requirements, $options));
     $routes->add('example5', new Route('/example5', array(), $requirements, $options));
     $routes->add('example6', new Route('/example6', array(), $requirements, $options));
     $routes->add('example7', new Route('/example7', array(), $requirements, $options));
     $routes->add('example8', new Route('/example8', array(), $requirements, $options));
     $mock_route_provider = new MockRouteProvider($routes);
     $this->container->set('router.route_provider', $mock_route_provider);
     // Add a new custom menu.
     $menu_name = 'mock';
     $label = $this->randomMachineName(16);
     $this->menu = Menu::create(array('id' => $menu_name, 'label' => $label, 'description' => 'Description text'));
     $this->menu->save();
     // This creates a tree with the following structure:
     // - 1
     // - 2
     //   - 3
     //     - 4
     // - 5
     //   - 7
     // - 6
     // - 8
     // With link 6 being the only external link.
     $links = array(1 => MenuLinkMock::create(array('id' => 'test.example1', 'route_name' => 'example1', 'title' => 'foo', 'parent' => '', 'weight' => 0)), 2 => MenuLinkMock::create(array('id' => 'test.example2', 'route_name' => 'example2', 'title' => 'bar', 'parent' => '', 'route_parameters' => array('foo' => 'bar'), 'weight' => 1)), 3 => MenuLinkMock::create(array('id' => 'test.example3', 'route_name' => 'example3', 'title' => 'baz', 'parent' => 'test.example2', 'weight' => 2)), 4 => MenuLinkMock::create(array('id' => 'test.example4', 'route_name' => 'example4', 'title' => 'qux', 'parent' => 'test.example3', 'weight' => 3)), 5 => MenuLinkMock::create(array('id' => 'test.example5', 'route_name' => 'example5', 'title' => 'foofoo', 'parent' => '', 'expanded' => TRUE, 'weight' => 4)), 6 => MenuLinkMock::create(array('id' => 'test.example6', 'route_name' => '', 'url' => 'https://www.drupal.org/', 'title' => 'barbar', 'parent' => '', 'weight' => 5)), 7 => MenuLinkMock::create(array('id' => 'test.example7', 'route_name' => 'example7', 'title' => 'bazbaz', 'parent' => 'test.example5', 'weight' => 6)), 8 => MenuLinkMock::create(array('id' => 'test.example8', 'route_name' => 'example8', 'title' => 'quxqux', 'parent' => '', 'weight' => 7)));
     foreach ($links as $instance) {
         $this->menuLinkManager->addDefinition($instance->getPluginId(), $instance->getPluginDefinition());
     }
 }
 /**
  * Deletes custom generated menus
  */
 protected function deleteMenus()
 {
     if (\Drupal::moduleHandler()->moduleExists('menu_ui')) {
         foreach (menu_ui_get_menus(FALSE) as $menu => $menu_title) {
             if (strpos($menu, 'devel-') === 0) {
                 Menu::load($menu)->delete();
             }
         }
     }
     // Delete menu links generated by devel.
     $result = db_select('menu_links', 'm')->fields('m', array('mlid'))->condition('m.menu_name', 'devel', '<>')->condition('m.options', '%' . db_like('s:5:"devel";b:1') . '%', 'LIKE')->execute();
     foreach ($result as $link) {
         menu_link_delete($link->mlid);
     }
 }
 /**
  * Deletes custom generated menus
  */
 protected function deleteMenus()
 {
     if (\Drupal::moduleHandler()->moduleExists('menu_ui')) {
         foreach (menu_ui_get_menus(FALSE) as $menu => $menu_title) {
             if (strpos($menu, 'devel-') === 0) {
                 Menu::load($menu)->delete();
             }
         }
     }
     // Delete menu links generated by devel.
     $result = db_select('menu_link_content_data', 'm')->fields('m', array('id'))->condition('m.menu_name', 'devel', '<>')->condition('m.link__options', '%' . db_like('s:5:"devel";b:1') . '%', 'LIKE')->execute()->fetchCol();
     if ($result) {
         entity_delete_multiple('menu_link_content', $result);
     }
 }
Exemple #21
0
 /**
  * Tests Menu uninstall.
  */
 public function testMenuUninstall()
 {
     \Drupal::service('module_installer')->uninstall(array('menu_ui'));
     \Drupal::entityManager()->getStorage('menu')->resetCache(array('admin'));
     $this->assertTrue(Menu::load('admin'), 'The \'admin\' menu still exists after uninstalling Menu UI module.');
 }
 /**
  * Responds to GET requests.
  *
  * Returns the menu structure for the given menu name.
  *
  * @param string $menu_name
  *   The menu name.
  *
  * @return \Drupal\rest\ResourceResponse
  *   The response containing the log entry.
  *
  * @TODO: make $menu parameter a type-hinted MenuInterface parameter
  * @TODO: cacheability and access checks for menu links?
  * @TODO: optional whitelist to only allow menu links from defined providers.
  */
 public function get($menu_name)
 {
     $menu = Menu::load($menu_name);
     if ($menu) {
         $tree = $this->menuTree->load($menu->id(), new MenuTreeParameters());
         $result = array_values($tree);
         return new ResourceResponse($result);
     }
     throw new HttpException(t('No valid menu ID was provided.'));
 }