diff --git a/email_registration.module b/email_registration.module index 9e07937..2a131e8 100644 --- a/email_registration.module +++ b/email_registration.module @@ -9,7 +9,6 @@ use Drupal\user\UserInterface; use Drupal\Core\Render\Element\Email; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Url; -use Drupal\Core\Link; /** * Implements hook_ENTITY_TYPE_insert(). @@ -184,8 +183,7 @@ function email_registration_user_login_validate($form, FormStateInterface $form_ elseif (!$config->get('login_with_username')) { $user_input = $form_state->getUserInput(); $query = isset($user_input['name']) ? ['name' => $user_input['name']] : []; - $link = Link::fromTextAndUrl(t('Forgot your password?'), Url::fromRoute('user.pass', [], ['query' => $query]))->toString(); - $form_state->setErrorByName('name', t('Unrecognized email address or password. :password', [':password' => $link])); + $form_state->setErrorByName('name', t('Unrecognized email address or password. Forgot your password?', [':password' => Url::fromRoute('user.pass', [], ['query' => $query])->toString()])); } } } diff --git a/src/Plugin/Commerce/CheckoutPane/EmailRegistrationLogin.php b/src/Plugin/Commerce/CheckoutPane/EmailRegistrationLogin.php index 14a1760..a809f3a 100644 --- a/src/Plugin/Commerce/CheckoutPane/EmailRegistrationLogin.php +++ b/src/Plugin/Commerce/CheckoutPane/EmailRegistrationLogin.php @@ -14,7 +14,6 @@ use Drupal\user\UserAuthInterface; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\HttpFoundation\RequestStack; use Drupal\Core\Config\ImmutableConfig; -use Drupal\Core\Link; /** * Provides the email registration login pane. @@ -119,8 +118,10 @@ class EmailRegistrationLogin extends Login { // bail out. $user_input = $form_state->getUserInput(); $query = isset($user_input[$subformId]['returning_customer']['name']) ? ['name' => $user_input[$subformId]['returning_customer']['name']] : []; - $link = Link::fromTextAndUrl($this->t('Forgot your password?'), Url::fromRoute('user.pass', [], ['query' => $query]))->toString(); - $form_state->setError($pane_form['returning_customer'], $this->t('Unrecognized email address or password. :password', [':password' => $link])); + $form_state->setError($pane_form['returning_customer'], $this->t('Unrecognized email address or password. Forgot your password?', [ + ':password' => Url::fromRoute('user.pass', [], ['query' => $query]) + ->toString(), + ])); return; } } @@ -140,7 +141,8 @@ class EmailRegistrationLogin extends Login { $password = trim($values['returning_customer']['password']); // Generate the "reset password" url. $query = !empty($username) ? ['name' => $username] : []; - $password_url = Url::fromRoute('user.pass', [], ['query' => $query]); + $password_url = Url::fromRoute('user.pass', [], ['query' => $query]) + ->toString(); if (user_is_blocked($username)) { $form_state->setError($name_element, $this->t('The account with email address %mail has not been activated or is blocked.', ['%mail' => $mail])); @@ -152,8 +154,7 @@ class EmailRegistrationLogin extends Login { $this->credentialsCheckFlood->register($this->clientIp, $username); // Changing the wrong credentials error message. if (!$this->config->get('login_with_username')) { - $link = Link::fromTextAndUrl($this->t('Forgot your password?'), $password_url)->toString(); - $form_state->setError($name_element, $this->t('Unrecognized email address or password. :password', [':password' => $link])); + $form_state->setError($name_element, $this->t('Unrecognized email address or password. Forgot your password?', [':password' => $password_url])); // Adding return to avoid the parent error when password is empty. return; } diff --git a/tests/src/Functional/EmailRegistrationTestCase.php b/tests/src/Functional/EmailRegistrationTestCase.php index e6fafb4..161fa96 100644 --- a/tests/src/Functional/EmailRegistrationTestCase.php +++ b/tests/src/Functional/EmailRegistrationTestCase.php @@ -20,6 +20,11 @@ class EmailRegistrationTestCase extends BrowserTestBase { */ public static $modules = ['email_registration']; + /** + * {@inheritdoc} + */ + protected $defaultTheme = 'stark'; + /** * Test various behaviors for anonymous users. */ @@ -72,6 +77,8 @@ class EmailRegistrationTestCase extends BrowserTestBase { $this->assertSession()->responseContains('Enter your email address or username.'); $this->assertSession()->responseContains('Email or username'); $this->drupalPostForm('user/login', $login, 'Log in'); + // When login_with_username is true, a user can login with just their + // username. $this->assertSession()->responseContains('' . $name . ' | Drupal'); $this->drupalLogout(); @@ -86,6 +93,7 @@ class EmailRegistrationTestCase extends BrowserTestBase { 'pass[pass2]' => $pass, ]; $this->drupalPostForm('/user/register', $register, 'Create new account'); + // User properly created, immediately logged in. $this->assertSession()->responseContains('Registration successful. You are now logged in.'); // Test email_registration_unique_username(). @@ -106,16 +114,11 @@ class EmailRegistrationTestCase extends BrowserTestBase { 'pass[pass2]' => $pass, ]; $this->drupalPostForm('/user/register', $register, 'Create new account'); - $accounts = $this->container->get('entity_type.manager') - ->getStorage('user') - ->loadByProperties(['mail' => $register['mail']]); - /** @var \Drupal\user\UserInterface $account */ - $account = reset($accounts); + $account = user_load_by_mail($register['mail']); $this->assertSame($next_unique_name, $account->getAccountName()); $this->drupalLogout(); // Check if custom username stays the same when user is edited. - /** @var \Drupal\user\UserInterface $user */ $user = $this->createUser(); $name = $user->label(); $this->drupalLogin($user); @@ -141,7 +144,7 @@ class EmailRegistrationTestCase extends BrowserTestBase { $this->drupalGet('user/' . $user->id() . '/edit'); $this->assertSession()->fieldExists('edit-name'); - $this->drupalLogout($user); + $this->drupalLogout(); $user = $this->createUser(); $username = $user->getAccountName(); diff --git a/tests/src/Functional/Plugin/Commerce/CheckoutPane/EmailRegistrationLoginTest.php b/tests/src/Functional/Plugin/Commerce/CheckoutPane/EmailRegistrationLoginTest.php index 7feac53..05fc27c 100644 --- a/tests/src/Functional/Plugin/Commerce/CheckoutPane/EmailRegistrationLoginTest.php +++ b/tests/src/Functional/Plugin/Commerce/CheckoutPane/EmailRegistrationLoginTest.php @@ -125,7 +125,7 @@ class EmailRegistrationLoginTest extends CommerceBrowserTestBase { $this->assertSession()->pageTextContains('Email address or username'); $edit = [ - 'email_registration_login[returning_customer][name]' => $account->getUsername(), + 'email_registration_login[returning_customer][name]' => $account->getAccountName(), 'email_registration_login[returning_customer][password]' => $account->passRaw, ]; $this->submitForm($edit, 'Log in'); @@ -180,7 +180,7 @@ class EmailRegistrationLoginTest extends CommerceBrowserTestBase { $this->assertSession()->pageTextContains('Enter your email address.'); $edit = [ - 'email_registration_login[returning_customer][name]' => $account->getUsername(), + 'email_registration_login[returning_customer][name]' => $account->getAccountName(), 'email_registration_login[returning_customer][password]' => $account->passRaw, ]; $this->submitForm($edit, 'Log in');