3 namespace Drupal\Tests\user\Functional;
5 use Drupal\Tests\BrowserTestBase;
8 * Tests user-account links.
12 class UserAccountLinksTest extends BrowserTestBase {
19 public static $modules = ['menu_ui', 'block', 'test_page_test'];
24 protected function setUp() {
26 $this->drupalPlaceBlock('system_menu_block:account');
27 // Make test-page default.
28 $this->config('system.site')->set('page.front', '/test-page')->save();
32 * Tests the secondary menu.
34 public function testSecondaryMenu() {
35 // Create a regular user.
36 $user = $this->drupalCreateUser([]);
38 // Log in and get the homepage.
39 $this->drupalLogin($user);
40 $this->drupalGet('<front>');
42 // For a logged-in user, expect the secondary menu to have links for "My
43 // account" and "Log out".
44 $link = $this->xpath('//ul[@class=:menu_class]/li/a[contains(@href, :href) and text()=:text]', [
45 ':menu_class' => 'menu',
47 ':text' => 'My account',
49 $this->assertEqual(count($link), 1, 'My account link is in secondary menu.');
51 $link = $this->xpath('//ul[@class=:menu_class]/li/a[contains(@href, :href) and text()=:text]', [
52 ':menu_class' => 'menu',
53 ':href' => 'user/logout',
56 $this->assertEqual(count($link), 1, 'Log out link is in secondary menu.');
58 // Log out and get the homepage.
59 $this->drupalLogout();
60 $this->drupalGet('<front>');
62 // For a logged-out user, expect the secondary menu to have a "Log in" link.
63 $link = $this->xpath('//ul[@class=:menu_class]/li/a[contains(@href, :href) and text()=:text]', [
64 ':menu_class' => 'menu',
65 ':href' => 'user/login',
68 $this->assertEqual(count($link), 1, 'Log in link is in secondary menu.');
72 * Tests disabling the 'My account' link.
74 public function testDisabledAccountLink() {
75 // Create an admin user and log in.
76 $this->drupalLogin($this->drupalCreateUser(['access administration pages', 'administer menu']));
78 // Verify that the 'My account' link exists before we check for its
80 $link = $this->xpath('//ul[@class=:menu_class]/li/a[contains(@href, :href) and text()=:text]', [
81 ':menu_class' => 'menu',
83 ':text' => 'My account',
85 $this->assertEqual(count($link), 1, 'My account link is in the secondary menu.');
87 // Verify that the 'My account' link is enabled. Do not assume the value of
88 // auto-increment is 1. Use XPath to obtain input element id and name using
89 // the consistent label text.
90 $this->drupalGet('admin/structure/menu/manage/account');
91 $label = $this->xpath('//label[contains(.,:text)]/@for', [':text' => 'Enable My account menu link']);
92 $this->assertFieldChecked($label[0]->getText(), "The 'My account' link is enabled by default.");
94 // Disable the 'My account' link.
95 $edit['links[menu_plugin_id:user.page][enabled]'] = FALSE;
96 $this->drupalPostForm('admin/structure/menu/manage/account', $edit, t('Save'));
99 $this->drupalGet('<front>');
101 // Verify that the 'My account' link does not appear when disabled.
102 $link = $this->xpath('//ul[@class=:menu_class]/li/a[contains(@href, :href) and text()=:text]', [
103 ':menu_class' => 'menu',
105 ':text' => 'My account',
107 $this->assertEqual(count($link), 0, 'My account link is not in the secondary menu.');
111 * Tests page title is set correctly on user account tabs.
113 public function testAccountPageTitles() {
114 // Default page titles are suffixed with the site name - Drupal.
115 $title_suffix = ' | Drupal';
117 $this->drupalGet('user');
118 $this->assertTitle('Log in' . $title_suffix, "Page title of /user is 'Log in'");
120 $this->drupalGet('user/login');
121 $this->assertTitle('Log in' . $title_suffix, "Page title of /user/login is 'Log in'");
123 $this->drupalGet('user/register');
124 $this->assertTitle('Create new account' . $title_suffix, "Page title of /user/register is 'Create new account' for anonymous users.");
126 $this->drupalGet('user/password');
127 $this->assertTitle('Reset your password' . $title_suffix, "Page title of /user/register is 'Reset your password' for anonymous users.");
129 // Check the page title for registered users is "My Account" in menus.
130 $this->drupalLogin($this->drupalCreateUser());
131 // After login, the client is redirected to /user.
132 $this->assertLink(t('My account'), 0, "Page title of /user is 'My Account' in menus for registered users");
133 $this->assertLinkByHref(\Drupal::urlGenerator()->generate('user.page'), 0);