Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / user / tests / src / Functional / UserAccountLinksTest.php
1 <?php
2
3 namespace Drupal\Tests\user\Functional;
4
5 use Drupal\Tests\BrowserTestBase;
6
7 /**
8  * Tests user-account links.
9  *
10  * @group user
11  */
12 class UserAccountLinksTest extends BrowserTestBase {
13
14   /**
15    * Modules to enable.
16    *
17    * @var array
18    */
19   public static $modules = ['menu_ui', 'block', 'test_page_test'];
20
21   /**
22    * {@inheritdoc}
23    */
24   protected function setUp() {
25     parent::setUp();
26     $this->drupalPlaceBlock('system_menu_block:account');
27     // Make test-page default.
28     $this->config('system.site')->set('page.front', '/test-page')->save();
29   }
30
31   /**
32    * Tests the secondary menu.
33    */
34   public function testSecondaryMenu() {
35     // Create a regular user.
36     $user = $this->drupalCreateUser([]);
37
38     // Log in and get the homepage.
39     $this->drupalLogin($user);
40     $this->drupalGet('<front>');
41
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',
46       ':href' => 'user',
47       ':text' => 'My account',
48     ]);
49     $this->assertEqual(count($link), 1, 'My account link is in secondary menu.');
50
51     $link = $this->xpath('//ul[@class=:menu_class]/li/a[contains(@href, :href) and text()=:text]', [
52       ':menu_class' => 'menu',
53       ':href' => 'user/logout',
54       ':text' => 'Log out',
55     ]);
56     $this->assertEqual(count($link), 1, 'Log out link is in secondary menu.');
57
58     // Log out and get the homepage.
59     $this->drupalLogout();
60     $this->drupalGet('<front>');
61
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',
66       ':text' => 'Log in',
67     ]);
68     $this->assertEqual(count($link), 1, 'Log in link is in secondary menu.');
69   }
70
71   /**
72    * Tests disabling the 'My account' link.
73    */
74   public function testDisabledAccountLink() {
75     // Create an admin user and log in.
76     $this->drupalLogin($this->drupalCreateUser(['access administration pages', 'administer menu']));
77
78     // Verify that the 'My account' link exists before we check for its
79     // disappearance.
80     $link = $this->xpath('//ul[@class=:menu_class]/li/a[contains(@href, :href) and text()=:text]', [
81       ':menu_class' => 'menu',
82       ':href' => 'user',
83       ':text' => 'My account',
84     ]);
85     $this->assertEqual(count($link), 1, 'My account link is in the secondary menu.');
86
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.");
93
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'));
97
98     // Get the homepage.
99     $this->drupalGet('<front>');
100
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',
104       ':href' => 'user',
105       ':text' => 'My account',
106     ]);
107     $this->assertEqual(count($link), 0, 'My account link is not in the secondary menu.');
108   }
109
110   /**
111    * Tests page title is set correctly on user account tabs.
112    */
113   public function testAccountPageTitles() {
114     // Default page titles are suffixed with the site name - Drupal.
115     $title_suffix = ' | Drupal';
116
117     $this->drupalGet('user');
118     $this->assertTitle('Log in' . $title_suffix, "Page title of /user is 'Log in'");
119
120     $this->drupalGet('user/login');
121     $this->assertTitle('Log in' . $title_suffix, "Page title of /user/login is 'Log in'");
122
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.");
125
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.");
128
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);
134   }
135
136 }