View source
<?php
namespace Drupal\Tests\system\Functional\Menu;
use Drupal\Core\Url;
use Drupal\Tests\BrowserTestBase;
class MenuRouterTest extends BrowserTestBase {
public static $modules = [
'block',
'menu_test',
'test_page_test',
];
protected $defaultTheme = 'stark';
protected $adminTheme;
protected function setUp() {
parent::setUp();
$this
->drupalPlaceBlock('system_menu_block:tools');
$this
->drupalPlaceBlock('local_tasks_block');
$this
->drupalPlaceBlock('page_title_block');
}
public function testMenuIntegration() {
$this
->doTestTitleMenuCallback();
$this
->doTestMenuOptionalPlaceholders();
$this
->doTestMenuHierarchy();
$this
->doTestMenuOnRoute();
$this
->doTestMenuName();
$this
->doTestMenuLinksDiscoveredAlter();
$this
->doTestHookMenuIntegration();
$this
->doTestExoticPath();
}
protected function doTestHookMenuIntegration() {
$machine_name = $this
->randomMachineName(8);
$base_path = 'foo/' . $machine_name;
$this
->drupalGet($base_path);
$this
->assertText('test1');
$this
->assertSession()
->linkExists('Local task A');
$this
->assertSession()
->linkExists('Local task B');
$this
->assertSession()
->linkNotExists('Local task C');
$this
->assertEscaped("<script>alert('Welcome to the jungle!')</script>");
$this
->assertLinkByHref(Url::fromRoute('menu_test.router_test1', [
'bar' => $machine_name,
])
->toString());
$this
->assertLinkByHref(Url::fromRoute('menu_test.router_test2', [
'bar' => $machine_name,
])
->toString());
}
protected function doTestTitleCallbackFalse() {
$this
->drupalGet('test-page');
$this
->assertText('A title with @placeholder', 'Raw text found on the page');
$this
->assertNoText(t('A title with @placeholder', [
'@placeholder' => 'some other text',
]), 'Text with placeholder substitutions not found.');
}
protected function doTestTitleMenuCallback() {
$this
->drupalGet('');
$this
->assertNoText(t('Menu Callback Title'));
$this
->drupalGet('menu_callback_title');
$this
->assertText(t('Menu Callback Title'));
}
protected function doTestDescriptionMenuItems() {
$this
->drupalGet('menu_callback_description');
$this
->assertText(t('Menu item description text'));
}
protected function doTestMenuName() {
$admin_user = $this
->drupalCreateUser([
'administer site configuration',
]);
$this
->drupalLogin($admin_user);
$menu_link_manager = \Drupal::service('plugin.manager.menu.link');
$menu_links = $menu_link_manager
->loadLinksByRoute('menu_test.menu_name_test');
$menu_link = reset($menu_links);
$this
->assertEqual($menu_link
->getMenuName(), 'original', 'Menu name is "original".');
menu_test_menu_name('changed');
$menu_link_manager
->rebuild();
$menu_links = $menu_link_manager
->loadLinksByRoute('menu_test.menu_name_test');
$menu_link = reset($menu_links);
$this
->assertEqual($menu_link
->getMenuName(), 'changed', 'Menu name was successfully changed after rebuild.');
}
protected function doTestMenuLinksDiscoveredAlter() {
$menu_link_manager = \Drupal::service('plugin.manager.menu.link');
$menu_links = $menu_link_manager
->loadLinksByRoute('menu_test.custom');
$menu_link = reset($menu_links);
$this
->assertEqual($menu_link
->getPluginId(), 'menu_test.custom', 'Menu links added at hook_menu_links_discovered_alter() obtain the machine name from the $links key.');
\Drupal::service('router.builder')
->rebuild();
$this
->drupalGet('menu-test');
$this
->assertUniqueText('Custom link', 'Menu links added by hook_menu_links_discovered_alter() do not duplicate after a menu rebuild.');
}
protected function doTestMenuHierarchy() {
$menu_link_manager = \Drupal::service('plugin.manager.menu.link');
$menu_links = $menu_link_manager
->loadLinksByRoute('menu_test.hierarchy_parent');
$parent_link = reset($menu_links);
$menu_links = $menu_link_manager
->loadLinksByRoute('menu_test.hierarchy_parent_child');
$child_link = reset($menu_links);
$menu_links = $menu_link_manager
->loadLinksByRoute('menu_test.hierarchy_parent_child2');
$unattached_child_link = reset($menu_links);
$this
->assertEqual($child_link
->getParent(), $parent_link
->getPluginId(), 'The parent of a directly attached child is correct.');
$this
->assertEqual($unattached_child_link
->getParent(), $child_link
->getPluginId(), 'The parent of a non-directly attached child is correct.');
}
protected function doTestMenuOptionalPlaceholders() {
$this
->drupalGet('menu-test/optional');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertText('Sometimes there is no placeholder.');
$this
->drupalGet('menu-test/optional/foobar');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertText("Sometimes there is a placeholder: 'foobar'.");
}
protected function doTestMenuOnRoute() {
\Drupal::service('module_installer')
->install([
'router_test',
]);
\Drupal::service('router.builder')
->rebuild();
$this
->resetAll();
$this
->drupalGet('router_test/test2');
$this
->assertLinkByHref('menu_no_title_callback');
$this
->assertLinkByHref('menu-title-test/case1');
$this
->assertLinkByHref('menu-title-test/case2');
$this
->assertLinkByHref('menu-title-test/case3');
}
protected function doTestExoticPath() {
$path = "menu-test/ -._~!\$'\"()*@[]?&+%#,;=:" . "%23%25%26%2B%2F%3F" . "éøïвβ中國書۞";
$this
->drupalGet($path);
$this
->assertRaw('This is the menuTestCallback content.');
$this
->assertNoText(t('The website encountered an unexpected error. Please try again later.'));
}
public function testMaintenanceModeLoginPaths() {
$this->container
->get('state')
->set('system.maintenance_mode', TRUE);
$offline_message = t('@site is currently under maintenance. We should be back shortly. Thank you for your patience.', [
'@site' => $this
->config('system.site')
->get('name'),
]);
$this
->drupalGet('test-page');
$this
->assertText($offline_message);
$this
->drupalGet('menu_login_callback');
$this
->assertText('This is TestControllers::testLogin.', 'Maintenance mode can be bypassed using an event subscriber.');
$this->container
->get('state')
->set('system.maintenance_mode', FALSE);
}
public function testAuthUserUserLogin() {
$web_user = $this
->drupalCreateUser([]);
$this
->drupalLogin($web_user);
$this
->drupalGet('user/login');
$this
->assertUrl($this->loggedInUser
->toUrl('canonical', [
'absolute' => TRUE,
])
->toString());
$this
->drupalGet('user/register');
$this
->assertUrl($this->loggedInUser
->toUrl('edit-form', [
'absolute' => TRUE,
])
->toString());
}
public function testThemeIntegration() {
$this->defaultTheme = 'bartik';
$this->adminTheme = 'seven';
$theme_installer = $this->container
->get('theme_installer');
$theme_installer
->install([
$this->defaultTheme,
$this->adminTheme,
]);
$this
->config('system.theme')
->set('default', $this->defaultTheme)
->set('admin', $this->adminTheme)
->save();
$this
->doTestThemeCallbackMaintenanceMode();
$this
->doTestThemeCallbackFakeTheme();
$this
->doTestThemeCallbackAdministrative();
$this
->doTestThemeCallbackNoThemeRequested();
$this
->doTestThemeCallbackOptionalTheme();
}
protected function doTestThemeCallbackAdministrative() {
$this
->drupalGet('menu-test/theme-callback/use-admin-theme');
$this
->assertText('Active theme: seven. Actual theme: seven.', 'The administrative theme can be correctly set in a theme negotiation.');
$this
->assertRaw('seven/css/base/elements.css', "The administrative theme's CSS appears on the page.");
}
protected function doTestThemeCallbackMaintenanceMode() {
$this->container
->get('state')
->set('system.maintenance_mode', TRUE);
$this
->drupalGet('menu-test/theme-callback/use-admin-theme');
$this
->assertRaw('bartik/css/base/elements.css', "The maintenance theme's CSS appears on the page.");
$admin_user = $this
->drupalCreateUser([
'access site in maintenance mode',
]);
$this
->drupalLogin($admin_user);
$this
->drupalGet('menu-test/theme-callback/use-admin-theme');
$this
->assertText('Active theme: seven. Actual theme: seven.', 'The theme negotiation system is correctly triggered for an administrator when the site is in maintenance mode.');
$this
->assertRaw('seven/css/base/elements.css', "The administrative theme's CSS appears on the page.");
$this->container
->get('state')
->set('system.maintenance_mode', FALSE);
}
protected function doTestThemeCallbackOptionalTheme() {
$this
->drupalGet('menu-test/theme-callback/use-test-theme');
$this
->assertText('Active theme: bartik. Actual theme: bartik.', 'The theme negotiation system falls back on the default theme when a theme that is not installed is requested.');
$this
->assertRaw('bartik/css/base/elements.css', "The default theme's CSS appears on the page.");
$theme_installer = $this->container
->get('theme_installer');
$theme_installer
->install([
'test_theme',
]);
$this
->drupalGet('menu-test/theme-callback/use-test-theme');
$this
->assertText('Active theme: test_theme. Actual theme: test_theme.', 'The theme negotiation system uses an optional theme once it has been installed.');
$this
->assertRaw('test_theme/kitten.css', "The optional theme's CSS appears on the page.");
$theme_installer
->uninstall([
'test_theme',
]);
}
protected function doTestThemeCallbackFakeTheme() {
$this
->drupalGet('menu-test/theme-callback/use-fake-theme');
$this
->assertText('Active theme: bartik. Actual theme: bartik.', 'The theme negotiation system falls back on the default theme when a theme that does not exist is requested.');
$this
->assertRaw('bartik/css/base/elements.css', "The default theme's CSS appears on the page.");
}
protected function doTestThemeCallbackNoThemeRequested() {
$this
->drupalGet('menu-test/theme-callback/no-theme-requested');
$this
->assertText('Active theme: bartik. Actual theme: bartik.', 'The theme negotiation system falls back on the default theme when no theme is requested.');
$this
->assertRaw('bartik/css/base/elements.css', "The default theme's CSS appears on the page.");
}
}