Loading email_registration.module +5 −1 Original line number Diff line number Diff line Loading @@ -123,7 +123,11 @@ function email_registration_cleanup_username($name) { */ function email_registration_form_user_form_alter(&$form, FormStateInterface $form_state) { $form['account']['name']['#type'] = 'value'; // Provide a random user name only if we are creating a new user, i.e. this // is a registration. if ($form_state->getFormObject()->getEntity()->isNew()) { $form['account']['name']['#value'] = 'email_registration_' . user_password(); } $form['account']['mail']['#title'] = t('Email'); } Loading tests/src/Functional/EmailRegistrationTestCase.php +11 −0 Original line number Diff line number Diff line Loading @@ -3,6 +3,7 @@ namespace Drupal\Tests\email_registration\Functional; use Drupal\Tests\BrowserTestBase; use Drupal\user\Entity\User; /** * Tests the email registration module. Loading Loading @@ -106,6 +107,16 @@ class EmailRegistrationTestCase extends BrowserTestBase { $this->drupalPostForm('/user/register', $register, t('Create new account')); $account = user_load_by_mail($register['mail']); $this->assertTrue($next_unique_name === $account->getAccountName()); $this->drupalLogout(); // Check if custom username stays the same when user is edited. $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($user); $this->drupalLogin($user); } } Loading
email_registration.module +5 −1 Original line number Diff line number Diff line Loading @@ -123,7 +123,11 @@ function email_registration_cleanup_username($name) { */ function email_registration_form_user_form_alter(&$form, FormStateInterface $form_state) { $form['account']['name']['#type'] = 'value'; // Provide a random user name only if we are creating a new user, i.e. this // is a registration. if ($form_state->getFormObject()->getEntity()->isNew()) { $form['account']['name']['#value'] = 'email_registration_' . user_password(); } $form['account']['mail']['#title'] = t('Email'); } Loading
tests/src/Functional/EmailRegistrationTestCase.php +11 −0 Original line number Diff line number Diff line Loading @@ -3,6 +3,7 @@ namespace Drupal\Tests\email_registration\Functional; use Drupal\Tests\BrowserTestBase; use Drupal\user\Entity\User; /** * Tests the email registration module. Loading Loading @@ -106,6 +107,16 @@ class EmailRegistrationTestCase extends BrowserTestBase { $this->drupalPostForm('/user/register', $register, t('Create new account')); $account = user_load_by_mail($register['mail']); $this->assertTrue($next_unique_name === $account->getAccountName()); $this->drupalLogout(); // Check if custom username stays the same when user is edited. $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($user); $this->drupalLogin($user); } }