View source
<?php
namespace Drupal\Tests\system\Functional\Menu;
use Drupal\Core\Url;
use Drupal\Tests\BrowserTestBase;
class MenuRouterTest extends BrowserTestBase {
protected static $modules = [
'block',
'menu_test',
'test_page_test',
];
protected $defaultTheme = 'stark';
protected $adminTheme;
protected function setUp() : void {
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
->assertSession()
->pageTextContains('test1');
$this
->assertSession()
->linkExists('Local task A');
$this
->assertSession()
->linkExists('Local task B');
$this
->assertSession()
->linkNotExists('Local task C');
$this
->assertSession()
->assertEscaped("<script>alert('Welcome to the jungle!')</script>");
$this
->assertSession()
->linkByHrefExists(Url::fromRoute('menu_test.router_test1', [
'bar' => $machine_name,
])
->toString());
$this
->assertSession()
->linkByHrefExists(Url::fromRoute('menu_test.router_test2', [
'bar' => $machine_name,
])
->toString());
}
protected function doTestTitleCallbackFalse() {
$this
->drupalGet('test-page');
$this
->assertSession()
->pageTextContains('A title with @placeholder', 'Raw text found on the page');
$this
->assertSession()
->pageTextNotContains('A title with some other text', 'Text with placeholder substitutions not found.');
}
protected function doTestTitleMenuCallback() {
$this
->drupalGet('');
$this
->assertSession()
->pageTextNotContains('Menu Callback Title');
$this
->drupalGet('menu_callback_title');
$this
->assertSession()
->pageTextContains('Menu Callback Title');
}
protected function doTestDescriptionMenuItems() {
$this
->drupalGet('menu_callback_description');
$this
->assertSession()
->pageTextContains('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
->assertEquals('original', $menu_link
->getMenuName(), '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
->assertEquals('changed', $menu_link
->getMenuName(), '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
->assertEquals('menu_test.custom', $menu_link
->getPluginId(), 'Menu links added at hook_menu_links_discovered_alter() obtain the machine name from the $links key.');
$this
->drupalGet('menu-test');
$this
->assertSession()
->pageTextContainsOnce('Custom link');
}
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
->assertEquals($parent_link
->getPluginId(), $child_link
->getParent(), 'The parent of a directly attached child is correct.');
$this
->assertEquals($child_link
->getPluginId(), $unattached_child_link
->getParent(), 'The parent of a non-directly attached child is correct.');
}
protected function doTestMenuOptionalPlaceholders() {
$this
->drupalGet('menu-test/optional');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->pageTextContains('Sometimes there is no placeholder.');
$this
->drupalGet('menu-test/optional/foobar');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->pageTextContains("Sometimes there is a placeholder: 'foobar'.");
}
protected function doTestMenuOnRoute() {
\Drupal::service('module_installer')
->install([
'router_test',
]);
$this
->resetAll();
$this
->drupalGet('router_test/test2');
$this
->assertSession()
->linkByHrefExists('menu_no_title_callback');
$this
->assertSession()
->linkByHrefExists('menu-title-test/case1');
$this
->assertSession()
->linkByHrefExists('menu-title-test/case2');
$this
->assertSession()
->linkByHrefExists('menu-title-test/case3');
}
protected function doTestExoticPath() {
$path = "menu-test/ -._~!\$'\"()*@[]?&+%#,;=:" . "%23%25%26%2B%2F%3F" . "éøïвβ中國書۞";
$this
->drupalGet($path);
$this
->assertSession()
->pageTextContains('This is the menuTestCallback content.');
$this
->assertSession()
->pageTextNotContains('The website encountered an unexpected error. Please try again later.');
}
public function testMaintenanceModeLoginPaths() {
$this->container
->get('state')
->set('system.maintenance_mode', TRUE);
$offline_message = $this
->config('system.site')
->get('name') . ' is currently under maintenance. We should be back shortly. Thank you for your patience.';
$this
->drupalGet('test-page');
$this
->assertSession()
->pageTextContains($offline_message);
$this
->drupalGet('menu_login_callback');
$this
->assertSession()
->pageTextContains('This is TestControllers::testLogin.');
$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
->assertSession()
->addressEquals($this->loggedInUser
->toUrl('canonical'));
$this
->drupalGet('user/register');
$this
->assertSession()
->addressEquals($this->loggedInUser
->toUrl('edit-form'));
}
public function testThemeIntegration() {
$this->defaultTheme = 'olivero';
$this->adminTheme = 'claro';
$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
->assertSession()
->pageTextContains('Active theme: claro. Actual theme: claro.');
$this
->assertSession()
->responseContains('claro/css/base/elements.css');
}
protected function doTestThemeCallbackMaintenanceMode() {
$this->container
->get('state')
->set('system.maintenance_mode', TRUE);
$this
->drupalGet('menu-test/theme-callback/use-admin-theme');
$this
->assertSession()
->responseContains('olivero/css/base/base.css');
$admin_user = $this
->drupalCreateUser([
'access site in maintenance mode',
]);
$this
->drupalLogin($admin_user);
$this
->drupalGet('menu-test/theme-callback/use-admin-theme');
$this
->assertSession()
->pageTextContains('Active theme: claro. Actual theme: claro.');
$this
->assertSession()
->responseContains('claro/css/base/elements.css');
$this->container
->get('state')
->set('system.maintenance_mode', FALSE);
}
protected function doTestThemeCallbackOptionalTheme() {
$this
->drupalGet('menu-test/theme-callback/use-test-theme');
$this
->assertSession()
->pageTextContains('Active theme: olivero. Actual theme: olivero.');
$this
->assertSession()
->responseContains('olivero/css/base/base.css');
$theme_installer = $this->container
->get('theme_installer');
$theme_installer
->install([
'test_theme',
]);
$this
->drupalGet('menu-test/theme-callback/use-test-theme');
$this
->assertSession()
->pageTextContains('Active theme: test_theme. Actual theme: test_theme.');
$this
->assertSession()
->responseContains('test_theme/kitten.css');
$theme_installer
->uninstall([
'test_theme',
]);
}
protected function doTestThemeCallbackFakeTheme() {
$this
->drupalGet('menu-test/theme-callback/use-fake-theme');
$this
->assertSession()
->pageTextContains('Active theme: olivero. Actual theme: olivero.');
$this
->assertSession()
->responseContains('olivero/css/base/base.css');
}
protected function doTestThemeCallbackNoThemeRequested() {
$this
->drupalGet('menu-test/theme-callback/no-theme-requested');
$this
->assertSession()
->pageTextContains('Active theme: olivero. Actual theme: olivero.');
$this
->assertSession()
->responseContains('olivero/css/base/base.css');
}
}