View source
<?php
namespace Drupal\Tests\user\Functional;
use Drupal\Tests\BrowserTestBase;
class UserAccountLinksTest extends BrowserTestBase {
protected static $modules = [
'menu_ui',
'block',
'test_page_test',
];
protected $defaultTheme = 'classy';
protected function setUp() : void {
parent::setUp();
$this
->drupalPlaceBlock('system_menu_block:account');
$this
->config('system.site')
->set('page.front', '/test-page')
->save();
}
public function testSecondaryMenu() {
$user = $this
->drupalCreateUser([]);
$this
->drupalLogin($user);
$this
->drupalGet('<front>');
$this
->assertSession()
->elementsCount('xpath', '//ul[@class="menu"]/li/a[contains(@href, "user") and text()="My account"]', 1);
$this
->assertSession()
->elementsCount('xpath', '//ul[@class="menu"]/li/a[contains(@href, "user/logout") and text()="Log out"]', 1);
$this
->drupalLogout();
$this
->drupalGet('<front>');
$this
->assertSession()
->elementsCount('xpath', '//ul[@class="menu"]/li/a[contains(@href, "user/login") and text()="Log in"]', 1);
}
public function testDisabledAccountLink() {
$this
->drupalLogin($this
->drupalCreateUser([
'access administration pages',
'administer menu',
]));
$this
->assertSession()
->elementsCount('xpath', '//ul[@class="menu"]/li/a[contains(@href, "user") and text()="My account"]', 1);
$this
->drupalGet('admin/structure/menu/manage/account');
$label = $this
->xpath('//label[contains(.,:text)]/@for', [
':text' => 'Enable My account menu link',
]);
$this
->assertSession()
->checkboxChecked($label[0]
->getText());
$edit['links[menu_plugin_id:user.page][enabled]'] = FALSE;
$this
->drupalGet('admin/structure/menu/manage/account');
$this
->submitForm($edit, 'Save');
$this
->drupalGet('<front>');
$this
->assertSession()
->elementNotExists('xpath', '//ul[@class="menu"]/li/a[contains(@href, "user") and text()="My account"]');
}
public function testAccountPageTitles() {
$title_suffix = ' | Drupal';
$this
->drupalGet('user');
$this
->assertSession()
->titleEquals('Log in' . $title_suffix);
$this
->drupalGet('user/login');
$this
->assertSession()
->titleEquals('Log in' . $title_suffix);
$this
->drupalGet('user/register');
$this
->assertSession()
->titleEquals('Create new account' . $title_suffix);
$this
->drupalGet('user/password');
$this
->assertSession()
->titleEquals('Reset your password' . $title_suffix);
$this
->drupalLogin($this
->drupalCreateUser());
$this
->assertSession()
->linkExists('My account', 0, "Page title of /user is 'My Account' in menus for registered users");
$this
->assertSession()
->linkByHrefExists(\Drupal::urlGenerator()
->generate('user.page'), 0);
}
public function testAnonymousLogout() {
$this
->drupalGet('user/logout');
$this
->assertSession()
->addressEquals('/');
$this
->assertSession()
->statusCodeEquals(200);
$this
->drupalGet('admin');
$this
->assertSession()
->addressEquals('/admin');
$this
->assertSession()
->statusCodeEquals(403);
}
}