diff --git a/core/modules/user/tests/src/Functional/UserRegistrationTest.php b/core/modules/user/tests/src/Functional/UserRegistrationTest.php index 0a55dbbb0c..67711dcacc 100644 --- a/core/modules/user/tests/src/Functional/UserRegistrationTest.php +++ b/core/modules/user/tests/src/Functional/UserRegistrationTest.php @@ -56,8 +56,8 @@ public function testRegistrationWithEmailVerification() { $edit = []; $edit['name'] = $name = $this->randomMachineName(); $edit['mail'] = $mail = $edit['name'] . '@example.com'; - $this->drupalPostForm('user/register', $edit, $this->t('Create new account')); - $this->assertText($this->t('A welcome message with further instructions has been sent to your email address.'), 'User registered successfully.'); + $this->submitForm('user/register', $edit, $this->t('Create new account')); + $this->assertSession()->responseContains($this->t('A welcome message with further instructions has been sent to your email address.'), 'User registered successfully.'); /** @var EntityStorageInterface $storage */ $storage = $this->container->get('entity_type.manager')->getStorage('user'); @@ -73,7 +73,7 @@ public function testRegistrationWithEmailVerification() { $edit = []; $edit['name'] = $name = $this->randomMachineName(); $edit['mail'] = $mail = $edit['name'] . '@example.com'; - $this->drupalPostForm('user/register', $edit, $this->t('Create new account')); + $this->submitForm('user/register', $edit, $this->t('Create new account')); $this->container->get('entity_type.manager')->getStorage('user')->resetCache(); $accounts = $storage->loadByProperties(['name' => $name, 'mail' => $mail]); $new_user = reset($accounts); @@ -99,19 +99,19 @@ public function testRegistrationWithoutEmailVerification() { // Try entering a mismatching password. $edit['pass[pass1]'] = '99999.0'; $edit['pass[pass2]'] = '99999'; - $this->drupalPostForm('user/register', $edit, $this->t('Create new account')); - $this->assertText($this->t('The specified passwords do not match.'), 'Typing mismatched passwords displays an error message.'); + $this->submitForm('user/register', $edit, $this->t('Create new account')); + $this->assertSession()->responseContains($this->t('The specified passwords do not match.'), 'Typing mismatched passwords displays an error message.'); // Enter a correct password. $edit['pass[pass1]'] = $new_pass = $this->randomMachineName(); $edit['pass[pass2]'] = $new_pass; - $this->drupalPostForm('user/register', $edit, $this->t('Create new account')); + $this->submitForm('user/register', $edit, $this->t('Create new account')); $this->container->get('entity_type.manager')->getStorage('user')->resetCache(); $accounts = $this->container->get('entity_type.manager')->getStorage('user') ->loadByProperties(['name' => $name, 'mail' => $mail]); $new_user = reset($accounts); $this->assertNotNull($new_user, 'New account successfully created with matching passwords.'); - $this->assertText($this->t('Registration successful. You are now logged in.'), 'Users are logged in after registering.'); + $this->assertSession()->responseContains($this->t('Registration successful. You are now logged in.'), 'Users are logged in after registering.'); $this->drupalLogout(); // Allow registration by site visitors, but require administrator approval. @@ -121,16 +121,16 @@ public function testRegistrationWithoutEmailVerification() { $edit['mail'] = $mail = $edit['name'] . '@example.com'; $edit['pass[pass1]'] = $pass = $this->randomMachineName(); $edit['pass[pass2]'] = $pass; - $this->drupalPostForm('user/register', $edit, $this->t('Create new account')); - $this->assertText($this->t('Thank you for applying for an account. Your account is currently pending approval by the site administrator.'), 'Users are notified of pending approval'); + $this->submitForm('user/register', $edit, $this->t('Create new account')); + $this->assertSession()->responseContains($this->t('Thank you for applying for an account. Your account is currently pending approval by the site administrator.'), 'Users are notified of pending approval'); // Try to log in before administrator approval. $auth = [ 'name' => $name, 'pass' => $pass, ]; - $this->drupalPostForm('user/login', $auth, $this->t('Log in')); - $this->assertText($this->t('The username @name has not been activated or is blocked.', ['@name' => $name]), 'User cannot log in yet.'); + $this->submitForm('user/login', $auth, $this->t('Log in')); + $this->assertSession()->responseContains($this->t('The username @name has not been activated or is blocked.', ['@name' => $name]), 'User cannot log in yet.'); // Activate the new account. $accounts = $this->container->get('entity_type.manager')->getStorage('user') @@ -141,12 +141,12 @@ public function testRegistrationWithoutEmailVerification() { $edit = [ 'status' => 1, ]; - $this->drupalPostForm('user/' . $new_user->id() . '/edit', $edit, $this->t('Save')); + $this->submitForm('user/' . $new_user->id() . '/edit', $edit, $this->t('Save')); $this->drupalLogout(); // Log in after administrator approval. - $this->drupalPostForm('user/login', $auth, $this->t('Log in')); - $this->assertText($this->t('Member for'), 'User can log in after administrator approval.'); + $this->submitForm('user/login', $auth, $this->t('Log in')); + $this->assertSession()->responseContains($this->t('Member for'), 'User can log in after administrator approval.'); } /** @@ -169,8 +169,8 @@ public function testRegistrationWithPasswordSet() { $edit['pass[pass2]'] = $new_pass; // Create a new user. - $this->drupalPostForm('user/register', $edit, $this->t('Create new account')); - $this->assertText($this->t('A welcome message with further instructions has been sent to your email address.'), 'Send e-mail to user after registering.'); + $this->submitForm('user/register', $edit, $this->t('Create new account')); + $this->assertSession()->responseContains($this->t('A welcome message with further instructions has been sent to your email address.'), 'Send e-mail to user after registering.'); // Make sure the user is still blocked. $this->container->get('entity_type.manager')->getStorage('user')->resetCache(); @@ -185,18 +185,21 @@ public function testRegistrationWithPasswordSet() { $edit2 = []; $edit2['name'] = $name; $edit2['pass'] = $new_pass; - $this->drupalPostForm('user/login', $edit2, $this->t('Log in')); - $this->assertRaw($this->t('The username %name has not been activated or is blocked.', ['%name' => $name]), $this->t('User cannot login yet.')); + $this->submitForm('user/login', $edit2, $this->t('Log in')); + $this->assertSession()->responseContains($this->t('The username %name has not been activated or is blocked.', ['%name' => $name]), $this->t('User cannot login yet.')); // Try to activate the user. $_emails = $this->drupalGetMails(); + $this->assertCount(2, $_emails); $email = end($_emails); - $one_time_link = []; - preg_match('@user/reset/[0-9]+/([0-9]+)/([a-zA-Z0-9_\-]+)\n@', $email['body'], $one_time_link); + $urls = []; + preg_match('#.+user/reset/.+#', $email['body'], $urls); + + return $urls[0]; $_uid = $new_user->id(); - $this->drupalGet("user/reset/$_uid/$one_time_link[1]/$one_time_link[2]"); - $this->assertText($this->t('You have just used your one-time login link. Your account is now active.'), $this->t('User account activated.')); + $this->drupalGet($urls[0]); + $this->assertSession()->responseContains($this->t('You have just used your one-time login link. Your account is now active.'), $this->t('User account activated.')); } /** @@ -218,14 +221,14 @@ public function testRegistrationEmailDuplicates() { $edit['mail'] = $duplicate_user->getEmail(); // Attempt to create a new account using an existing email address. - $this->drupalPostForm('user/register', $edit, $this->t('Create new account')); - $this->assertText($this->t('The email address @email is already taken.', ['@email' => $duplicate_user->getEmail()]), 'Supplying an exact duplicate email address displays an error message'); + $this->submitForm('user/register', $edit, $this->t('Create new account')); + $this->assertSession()->responseContains($this->t('The email address @email is already taken.', ['@email' => $duplicate_user->getEmail()]), 'Supplying an exact duplicate email address displays an error message'); // Attempt to bypass duplicate email validation by adding spaces. $edit['mail'] = ' ' . $duplicate_user->getEmail() . ' '; - $this->drupalPostForm('user/register', $edit, $this->t('Create new account')); - $this->assertText($this->t('The email address @email is already taken.', ['@email' => $duplicate_user->getEmail()]), 'Supplying a duplicate email address with added whitespace displays an error message'); + $this->submitForm('user/register', $edit, $this->t('Create new account')); + $this->assertSession()->responseContains($this->t('The email address @email is already taken.', ['@email' => $duplicate_user->getEmail()]), 'Supplying a duplicate email address with added whitespace displays an error message'); } /** @@ -277,7 +280,7 @@ public function testUuidFormState() { $edit['pass[pass2]'] = $edit['pass[pass1]'] = $this->randomMachineName(); // Create one account. - $this->drupalPostForm('user/register', $edit, $this->t('Create new account')); + $this->submitForm('user/register', $edit, $this->t('Create new account')); $this->assertSession()->statusCodeEquals(200); $user_storage = \Drupal::entityTypeManager()->getStorage('user'); @@ -290,7 +293,7 @@ public function testUuidFormState() { $edit['mail'] = $edit['name'] . '@example.com'; $edit['pass[pass2]'] = $edit['pass[pass1]'] = $this->randomMachineName(); - $this->drupalPostForm('user/register', $edit, $this->t('Create new account')); + $this->submitForm('user/register', $edit, $this->t('Create new account')); $this->assertSession()->statusCodeEquals(200); $this->assertNotEmpty($user_storage->loadByProperties(['name' => $edit['name']])); @@ -322,20 +325,20 @@ public function testRegistrationDefaultValues() { $edit['mail'] = $mail = $edit['name'] . '@example.com'; $edit['pass[pass1]'] = $new_pass = $this->randomMachineName(); $edit['pass[pass2]'] = $new_pass; - $this->drupalPostForm(NULL, $edit, $this->t('Create new account')); + $this->submitForm(NULL, $edit, $this->t('Create new account')); // Check user fields. $accounts = $this->container->get('entity_type.manager')->getStorage('user') ->loadByProperties(['name' => $name, 'mail' => $mail]); $new_user = reset($accounts); - $this->assertEqual($new_user->getAccountName(), $name, 'Username matches.'); - $this->assertEqual($new_user->getEmail(), $mail, 'Email address matches.'); + $this->assertEquals($new_user->getAccountName(), $name, 'Username matches.'); + $this->assertEquals($new_user->getEmail(), $mail, 'Email address matches.'); $this->assertTrue(($new_user->getCreatedTime() > REQUEST_TIME - 20), 'Correct creation time.'); - $this->assertEqual($new_user->isActive(), $config_user_settings->get('register') == UserInterface::REGISTER_VISITORS ? 1 : 0, 'Correct status field.'); - $this->assertEqual($new_user->getTimezone(), $config_system_date->get('timezone.default'), 'Correct time zone field.'); - $this->assertEqual($new_user->langcode->value, \Drupal::languageManager()->getDefaultLanguage()->getId(), 'Correct language field.'); - $this->assertEqual($new_user->preferred_langcode->value, \Drupal::languageManager()->getDefaultLanguage()->getId(), 'Correct preferred language field.'); - $this->assertEqual($new_user->init->value, $mail, 'Correct init field.'); + $this->assertEquals($new_user->isActive(), $config_user_settings->get('register') == UserInterface::REGISTER_VISITORS ? 1 : 0, 'Correct status field.'); + $this->assertEquals($new_user->getTimezone(), $config_system_date->get('timezone.default'), 'Correct time zone field.'); + $this->assertEquals($new_user->langcode->value, \Drupal::languageManager()->getDefaultLanguage()->getId(), 'Correct language field.'); + $this->assertEquals($new_user->preferred_langcode->value, \Drupal::languageManager()->getDefaultLanguage()->getId(), 'Correct preferred language field.'); + $this->assertEquals($new_user->init->value, $mail, 'Correct init field.'); } /** @@ -351,12 +354,12 @@ public function testUniqueFields() { 'mail' => 'test@example.com', 'name' => $account->getAccountName(), ]; - $this->drupalPostForm('user/register', $edit, $this->t('Create new account')); - $this->assertRaw(new FormattableMarkup('The username %value is already taken.', ['%value' => $account->getAccountName()])); + $this->submitForm('user/register', $edit, $this->t('Create new account')); + $this->assertSession()->responseContains(new FormattableMarkup('The username %value is already taken.', ['%value' => $account->getAccountName()])); $edit = ['mail' => $account->getEmail(), 'name' => $this->randomString()]; - $this->drupalPostForm('user/register', $edit, $this->t('Create new account')); - $this->assertRaw(new FormattableMarkup('The email address %value is already taken.', ['%value' => $account->getEmail()])); + $this->submitForm('user/register', $edit, $this->t('Create new account')); + $this->assertSession()->responseContains(new FormattableMarkup('The email address %value is already taken.', ['%value' => $account->getEmail()])); } /** @@ -390,7 +393,7 @@ public function testRegistrationWithUserFields() { // Check that the field does not appear on the registration form. $this->drupalGet('user/register'); - $this->assertNoText($field->label(), 'The field does not appear on user registration form'); + $this->assertSession()->responseNotContains($field->label(), 'The field does not appear on user registration form'); $this->assertSession()->responseHeaderContains('X-Drupal-Cache-Tags', 'config:core.entity_form_display.user.user.register'); $this->assertSession()->responseHeaderContains('X-Drupal-Cache-Tags', 'config:user.settings'); @@ -400,7 +403,7 @@ public function testRegistrationWithUserFields() { ->save(); $this->drupalGet('user/register'); - $this->assertText($field->label(), 'The field appears on user registration form'); + $this->assertSession()->responseContains($field->label(), 'The field appears on user registration form'); $this->assertRegistrationFormCacheTagsWithUserFields(); // Check that validation errors are correctly reported. @@ -409,24 +412,24 @@ public function testRegistrationWithUserFields() { $edit['mail'] = $mail = $edit['name'] . '@example.com'; // Missing input in required field. $edit['test_user_field[0][value]'] = ''; - $this->drupalPostForm(NULL, $edit, $this->t('Create new account')); + $this->submitForm(NULL, $edit, $this->t('Create new account')); $this->assertRegistrationFormCacheTagsWithUserFields(); - $this->assertRaw($this->t('@name field is required.', ['@name' => $field->label()])); + $this->assertSession()->responseContains($this->t('@name field is required.', ['@name' => $field->label()])); // Invalid input. $edit['test_user_field[0][value]'] = '-1'; - $this->drupalPostForm(NULL, $edit, $this->t('Create new account')); + $this->submitForm(NULL, $edit, $this->t('Create new account')); $this->assertRegistrationFormCacheTagsWithUserFields(); - $this->assertRaw($this->t('%name does not accept the value -1.', ['%name' => $field->label()])); + $this->assertSession()->responseContains($this->t('%name does not accept the value -1.', ['%name' => $field->label()])); // Submit with valid data. $value = rand(1, 255); $edit['test_user_field[0][value]'] = $value; - $this->drupalPostForm(NULL, $edit, $this->t('Create new account')); + $this->submitForm(NULL, $edit, $this->t('Create new account')); // Check user fields. $accounts = $this->container->get('entity_type.manager')->getStorage('user') ->loadByProperties(['name' => $name, 'mail' => $mail]); $new_user = reset($accounts); - $this->assertEqual($new_user->test_user_field->value, $value, 'The field value was correctly saved.'); + $this->assertEquals($new_user->test_user_field->value, $value, 'The field value was correctly saved.'); // Check that the 'add more' button works. $field_storage->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED); @@ -437,21 +440,21 @@ public function testRegistrationWithUserFields() { $value = rand(1, 255); $edit = []; $edit['test_user_field[0][value]'] = $value; - $this->drupalPostForm(NULL, $edit, $this->t('Add another item')); - $this->drupalPostForm(NULL, $edit, $this->t('Add another item')); + $this->submitForm(NULL, $edit, $this->t('Add another item')); + $this->submitForm(NULL, $edit, $this->t('Add another item')); // Submit with three values. $edit['test_user_field[1][value]'] = $value + 1; $edit['test_user_field[2][value]'] = $value + 2; $edit['name'] = $name = $this->randomMachineName(); $edit['mail'] = $mail = $edit['name'] . '@example.com'; - $this->drupalPostForm(NULL, $edit, $this->t('Create new account')); + $this->submitForm(NULL, $edit, $this->t('Create new account')); // Check user fields. $accounts = $this->container->get('entity_type.manager')->getStorage('user') ->loadByProperties(['name' => $name, 'mail' => $mail]); $new_user = reset($accounts); - $this->assertEqual($new_user->test_user_field[0]->value, $value, 'The field value was correctly saved.'); - $this->assertEqual($new_user->test_user_field[1]->value, $value + 1, 'The field value was correctly saved.'); - $this->assertEqual($new_user->test_user_field[2]->value, $value + 2, 'The field value was correctly saved.'); + $this->assertEquals($new_user->test_user_field[0]->value, $value, 'The field value was correctly saved.'); + $this->assertEquals($new_user->test_user_field[1]->value, $value + 1, 'The field value was correctly saved.'); + $this->assertEquals($new_user->test_user_field[2]->value, $value + 2, 'The field value was correctly saved.'); } /**