View source
<?php
namespace Drupal\Tests\email_registration\Functional;
use Drupal\Tests\BrowserTestBase;
use Drupal\user\Entity\User;
use Drupal\user\UserInterface;
class EmailRegistrationTestCase extends BrowserTestBase {
public static $modules = [
'email_registration',
];
protected $defaultTheme = 'stark';
public function testRegistration() {
$user_config = $this->container
->get('config.factory')
->getEditable('user.settings');
$email_registration_config = $this->container
->get('config.factory')
->getEditable('email_registration.settings');
$user_config
->set('verify_mail', FALSE)
->set('register', UserInterface::REGISTER_VISITORS)
->save();
$name = $this
->randomMachineName();
$pass = $this
->randomString(10);
$register = [
'mail' => $name . '@example.com',
'pass[pass1]' => $pass,
'pass[pass2]' => $pass,
];
$this
->drupalPostForm('/user/register', $register, 'Create new account');
$this
->drupalLogout();
$login = [
'name' => $name . '@example.com',
'pass' => $pass,
];
$this
->drupalPostForm('user/login', $login, 'Log in');
$this
->assertSession()
->responseContains('<title>' . $name . ' | Drupal</title>');
$this
->drupalLogout();
$this
->drupalGet('user/login');
$this
->assertSession()
->responseContains('Enter your email address.');
$this
->assertSession()
->responseContains('Email');
$this
->assertSession()
->responseNotContains('Email or username');
$login = [
'name' => $name,
'pass' => $pass,
];
$this
->drupalPostForm('user/login', $login, 'Log in');
$this
->assertSession()
->responseContains('Unrecognized email address or password.');
$email_registration_config
->set('login_with_username', TRUE)
->save();
$this
->drupalGet('user/login');
$this
->assertSession()
->responseContains('Enter your email address or username.');
$this
->assertSession()
->responseContains('Email or username');
$this
->drupalPostForm('user/login', $login, 'Log in');
$this
->assertSession()
->responseContains('<title>' . $name . ' | Drupal</title>');
$this
->drupalLogout();
$user_config
->set('verify_mail', FALSE)
->save();
$name = $this
->randomMachineName();
$pass = $this
->randomString(10);
$register = [
'mail' => $name . '@example.com',
'pass[pass1]' => $pass,
'pass[pass2]' => $pass,
];
$this
->drupalPostForm('/user/register', $register, 'Create new account');
$this
->assertSession()
->responseContains('Registration successful. You are now logged in.');
$this
->drupalLogout();
$user_config
->set('verify_mail', FALSE)
->set('register', UserInterface::REGISTER_VISITORS)
->save();
$name = $this
->randomMachineName(32);
$pass = $this
->randomString(10);
$this
->createUser([], $name);
$next_unique_name = email_registration_unique_username($name);
$register = [
'mail' => $name . '@example2.com',
'pass[pass1]' => $pass,
'pass[pass2]' => $pass,
];
$this
->drupalPostForm('/user/register', $register, 'Create new account');
$account = user_load_by_mail($register['mail']);
$this
->assertSame($next_unique_name, $account
->getAccountName());
$this
->drupalLogout();
$user = $this
->createUser();
$name = $user
->label();
$this
->drupalLogin($user);
$this
->drupalPostForm('/user/' . $user
->id() . '/edit', [], 'Save');
$this
->assertEqual($name, User::load($user
->id())
->label(), 'Username should not change after empty edit.');
$this
->drupalLogout();
$this
->drupalLogin($user);
$this
->assertSame($next_unique_name, $account
->getAccountName());
}
public function testUsernamePermissions() {
$this->container
->get('config.factory')
->getEditable('email_registration.settings')
->set('login_with_username', TRUE)
->save(TRUE);
$user = $this
->createUser([
'change own username',
]);
$this
->drupalLogin($user);
$this
->drupalGet('user/' . $user
->id() . '/edit');
$this
->assertSession()
->fieldExists('edit-name');
$this
->drupalLogout();
$user = $this
->createUser();
$username = $user
->getAccountName();
$this
->drupalLogin($user);
$this
->drupalGet('user/' . $user
->id() . '/edit');
$this
->assertSession()
->fieldNotExists('edit-name');
$this
->assertSession()
->pageTextContains($username);
$this
->drupalPostForm('user/' . $user
->id() . '/edit', [], 'Save');
$this
->assertSession()
->pageTextContains($username);
}
}