View source
<?php
namespace Drupal\Tests\user\Functional;
use Drupal\Core\Cache\Cache;
use Drupal\Tests\BrowserTestBase;
class UserEditTest extends BrowserTestBase {
protected $defaultTheme = 'stark';
public function testUserEdit() {
$user1 = $this
->drupalCreateUser([
'change own username',
]);
$user2 = $this
->drupalCreateUser([]);
$this
->drupalLogin($user1);
$edit['name'] = $user2
->getAccountName();
$this
->drupalPostForm("user/" . $user1
->id() . "/edit", $edit, t('Save'));
$this
->assertRaw(t('The username %name is already taken.', [
'%name' => $edit['name'],
]));
\Drupal::state()
->set('user_hooks_test_user_format_name_alter', TRUE);
\Drupal::service('module_installer')
->install([
'user_hooks_test',
]);
Cache::invalidateTags([
'rendered',
]);
$this
->drupalGet('user/' . $user1
->id() . '/edit');
$this
->assertFieldByName('name', $user1
->getAccountName());
$this
->drupalGet('user/' . $user1
->id());
$this
->assertSession()
->responseContains($user1
->getDisplayName());
$this
->assertSession()
->titleEquals(strip_tags($user1
->getDisplayName()) . ' | Drupal');
$edit = [];
$edit['pass[pass1]'] = '';
$edit['pass[pass2]'] = $this
->randomMachineName();
$this
->drupalPostForm("user/" . $user1
->id() . "/edit", $edit, t('Save'));
$this
->assertText(t("The specified passwords do not match."), 'Typing mismatched passwords displays an error message.');
$edit['pass[pass1]'] = $this
->randomMachineName();
$edit['pass[pass2]'] = '';
$this
->drupalPostForm("user/" . $user1
->id() . "/edit", $edit, t('Save'));
$this
->assertText(t("The specified passwords do not match."), 'Typing mismatched passwords displays an error message.');
$edit = [];
$edit['mail'] = $this
->randomMachineName() . '@new.example.com';
$this
->drupalPostForm("user/" . $user1
->id() . "/edit", $edit, t('Save'));
$this
->assertRaw(t("Your current password is missing or incorrect; it's required to change the %name.", [
'%name' => t('Email'),
]));
$edit['current_pass'] = $user1->passRaw;
$this
->drupalPostForm("user/" . $user1
->id() . "/edit", $edit, t('Save'));
$this
->assertRaw(t("The changes have been saved."));
$edit = [];
$edit['pass[pass1]'] = $new_pass = $this
->randomMachineName();
$edit['pass[pass2]'] = $new_pass;
$this
->drupalPostForm("user/" . $user1
->id() . "/edit", $edit, t('Save'));
$this
->assertRaw(t("Your current password is missing or incorrect; it's required to change the %name.", [
'%name' => t('Password'),
]));
$edit['current_pass'] = $user1->passRaw;
$this
->drupalPostForm("user/" . $user1
->id() . "/edit", $edit, t('Save'));
$this
->assertRaw(t("The changes have been saved."));
$this
->assertEqual($user1
->getChangedTime(), REQUEST_TIME, 'Changing a user sets "changed" timestamp.');
$this
->drupalLogout();
$user1->passRaw = $new_pass;
$this
->drupalLogin($user1);
$this
->drupalLogout();
$config = $this
->config('user.settings');
$this
->drupalLogin($user1);
$config
->set('password_strength', TRUE)
->save();
$this
->drupalPostForm("user/" . $user1
->id() . "/edit", $edit, t('Save'));
$this
->assertRaw(t('Password strength:'), 'The password strength indicator is displayed.');
$config
->set('password_strength', FALSE)
->save();
$this
->drupalPostForm("user/" . $user1
->id() . "/edit", $edit, t('Save'));
$this
->assertNoRaw(t('Password strength:'), 'The password strength indicator is not displayed.');
$admin_user = $this
->drupalCreateUser([
'administer users',
]);
$this
->drupalLogin($admin_user);
$this
->drupalGet('user/' . $user1
->id() . '/edit');
$this
->assertNoFieldChecked('edit-status-0');
$this
->assertFieldChecked('edit-status-1');
$edit = [
'status' => 0,
];
$this
->drupalPostForm('user/' . $user1
->id() . '/edit', $edit, t('Save'));
$this
->assertText(t('The changes have been saved.'));
$this
->assertFieldChecked('edit-status-0');
$this
->assertNoFieldChecked('edit-status-1');
$edit = [
'status' => 1,
];
$this
->drupalPostForm('user/' . $user1
->id() . '/edit', $edit, t('Save'));
$this
->assertText(t('The changes have been saved.'));
$this
->assertNoFieldChecked('edit-status-0');
$this
->assertFieldChecked('edit-status-1');
}
public function testUserWith0Password() {
$admin = $this
->drupalCreateUser([
'administer users',
]);
$this
->drupalLogin($admin);
$user1 = $this
->drupalCreateUser([]);
$edit = [
'pass[pass1]' => '0',
'pass[pass2]' => '0',
];
$this
->drupalPostForm("user/" . $user1
->id() . "/edit", $edit, t('Save'));
$this
->assertRaw(t("The changes have been saved."));
}
public function testUserWithoutEmailEdit() {
$admin = $this
->drupalCreateUser([
'administer users',
]);
$this
->drupalLogin($admin);
$user1 = $this
->drupalCreateUser([]);
$user1->mail = '';
$user1
->save();
$this
->drupalPostForm("user/" . $user1
->id() . "/edit", [
'mail' => '',
], t('Save'));
$this
->assertRaw(t("The changes have been saved."));
}
public function testUserWellKnownChangePasswordAuth() {
$account = $this
->drupalCreateUser([]);
$this
->drupalLogin($account);
$this
->drupalGet('.well-known/change-password');
$this
->assertSession()
->addressEquals("user/" . $account
->id() . "/edit");
}
public function testUserWellKnownChangePasswordAnon() {
$this
->drupalGet('.well-known/change-password');
$this
->assertSession()
->statusCodeEquals(403);
}
public function testUserChangeSiteLanguage() {
\Drupal::service('module_installer')
->install([
'content_translation',
'language',
]);
$adminUser = $this
->drupalCreateUser([
'administer account settings',
'administer languages',
'administer content translation',
'administer users',
'translate any entity',
]);
$this
->drupalLogin($adminUser);
$edit = [
'predefined_langcode' => 'fr',
];
$this
->drupalPostForm('admin/config/regional/language/add', $edit, 'Add language');
$this
->assertSession()
->pageTextContains('French');
$edit = [
'language[content_translation]' => 1,
];
$this
->drupalPostForm('admin/config/people/accounts', $edit, 'Save configuration');
$this
->assertSession()
->pageTextContains('The configuration options have been saved.');
$webUser = $this
->drupalCreateUser();
$this
->drupalPostForm('user/' . $webUser
->id() . '/translations/add/en/fr', [], 'Save');
$this
->assertSession()
->pageTextContains('The changes have been saved.');
$edit = [
'preferred_langcode' => 'fr',
];
$this
->drupalPostForm('user/' . $webUser
->id() . '/edit', $edit, 'Save');
$this
->assertSession()
->statusCodeEquals(200);
}
}