View source
<?php
namespace Drupal\Tests\user\Functional;
use Drupal\Tests\BrowserTestBase;
class UserAccountLinksTest extends BrowserTestBase {
public static $modules = [
'menu_ui',
'block',
'test_page_test',
];
protected $defaultTheme = 'classy';
protected function setUp() {
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>');
$link = $this
->xpath('//ul[@class=:menu_class]/li/a[contains(@href, :href) and text()=:text]', [
':menu_class' => 'menu',
':href' => 'user',
':text' => 'My account',
]);
$this
->assertCount(1, $link, 'My account link is in secondary menu.');
$link = $this
->xpath('//ul[@class=:menu_class]/li/a[contains(@href, :href) and text()=:text]', [
':menu_class' => 'menu',
':href' => 'user/logout',
':text' => 'Log out',
]);
$this
->assertCount(1, $link, 'Log out link is in secondary menu.');
$this
->drupalLogout();
$this
->drupalGet('<front>');
$link = $this
->xpath('//ul[@class=:menu_class]/li/a[contains(@href, :href) and text()=:text]', [
':menu_class' => 'menu',
':href' => 'user/login',
':text' => 'Log in',
]);
$this
->assertCount(1, $link, 'Log in link is in secondary menu.');
}
public function testDisabledAccountLink() {
$this
->drupalLogin($this
->drupalCreateUser([
'access administration pages',
'administer menu',
]));
$link = $this
->xpath('//ul[@class=:menu_class]/li/a[contains(@href, :href) and text()=:text]', [
':menu_class' => 'menu',
':href' => 'user',
':text' => 'My account',
]);
$this
->assertCount(1, $link, 'My account link is in the secondary menu.');
$this
->drupalGet('admin/structure/menu/manage/account');
$label = $this
->xpath('//label[contains(.,:text)]/@for', [
':text' => 'Enable My account menu link',
]);
$this
->assertFieldChecked($label[0]
->getText(), "The 'My account' link is enabled by default.");
$edit['links[menu_plugin_id:user.page][enabled]'] = FALSE;
$this
->drupalPostForm('admin/structure/menu/manage/account', $edit, t('Save'));
$this
->drupalGet('<front>');
$link = $this
->xpath('//ul[@class=:menu_class]/li/a[contains(@href, :href) and text()=:text]', [
':menu_class' => 'menu',
':href' => 'user',
':text' => 'My account',
]);
$this
->assertCount(0, $link, 'My account link is not in the secondary menu.');
}
public function testAccountPageTitles() {
$title_suffix = ' | Drupal';
$this
->drupalGet('user');
$this
->assertTitle('Log in' . $title_suffix);
$this
->drupalGet('user/login');
$this
->assertTitle('Log in' . $title_suffix);
$this
->drupalGet('user/register');
$this
->assertTitle('Create new account' . $title_suffix);
$this
->drupalGet('user/password');
$this
->assertTitle('Reset your password' . $title_suffix);
$this
->drupalLogin($this
->drupalCreateUser());
$this
->assertSession()
->linkExists(t('My account'), 0, "Page title of /user is 'My Account' in menus for registered users");
$this
->assertLinkByHref(\Drupal::urlGenerator()
->generate('user.page'), 0);
}
}