/** * 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('<script>alert("menu");</script>'); $this->assertNoRaw('<script>alert("menu");</script>'); }
/** * 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>'); }
/** * 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']); }
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); }); } }
/** * Adds a custom menu using CRUD functions. */ function addCustomMenuCRUD() { // Add a new custom menu. $menu_name = substr(hash('sha256', $this->randomMachineName(16)), 0, MENU_MAX_MENU_NAME_LENGTH_UI); $label = $this->randomMachineName(16); $menu = Menu::create(array('id' => $menu_name, 'label' => $label, 'description' => 'Description text')); $menu->save(); // Assert the new menu. $this->drupalGet('admin/structure/menu/manage/' . $menu_name); $this->assertRaw($label, 'Custom menu was added.'); // Edit the menu. $new_label = $this->randomMachineName(16); $menu->set('label', $new_label); $menu->save(); $this->drupalGet('admin/structure/menu/manage/' . $menu_name); $this->assertRaw($new_label, 'Custom menu was edited.'); }
/** * {@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(); }
/** * {@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()); } }
/** * 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']); }
/** * 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>'); }