ProfileRegisterFormTest.php in Profile 8
File
tests/src/Functional/ProfileRegisterFormTest.php
View source
<?php
namespace Drupal\Tests\profile\Functional;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Session\AccountInterface;
use Drupal\user\UserInterface;
class ProfileRegisterFormTest extends ProfileTestBase {
public function testUserRegisterForm() {
$id = $this->type
->id();
$field_name = $this->field
->getName();
$this->field
->setRequired(TRUE);
$this->field
->save();
\Drupal::configFactory()
->getEditable('user.settings')
->set('register', UserInterface::REGISTER_VISITORS)
->set('verify_mail', 0)
->save();
user_role_grant_permissions(AccountInterface::AUTHENTICATED_ROLE, [
'view own test profile',
]);
$this
->drupalGet('user/register');
$name = $this
->randomMachineName();
$pass_raw = $this
->randomMachineName();
$edit = [
'name' => $name,
'mail' => $this->adminUser
->getEmail(),
'pass[pass1]' => $pass_raw,
'pass[pass2]' => $pass_raw,
];
$this
->submitForm($edit, t('Create new account'));
$this
->assertSession()
->pageTextContains(new FormattableMarkup('@name field is required.', [
'@name' => $this->field
->getLabel(),
]));
$this
->assertSession()
->pageTextContains(new FormattableMarkup('The email address @email is already taken.', [
'@email' => $this->adminUser
->getEmail(),
]));
$edit['mail'] = $this
->randomMachineName() . '@example.com';
$edit[$id . "_profiles[0][entity][{$field_name}][0][value]"] = $this
->randomMachineName();
$this
->submitForm($edit, t('Create new account'));
$this
->assertSession()
->pageTextContains(new FormattableMarkup('Registration successful. You are now logged in.', []));
$new_user = user_load_by_name($name);
$this
->assertTrue($new_user
->isActive(), 'New account is active after registration.');
$storage = $this->container
->get('entity_type.manager')
->getStorage('profile');
$profile = $storage
->loadByUser($new_user, $this->type
->id());
$this
->assertEquals($profile
->get($field_name)->value, $edit[$id . "_profiles[0][entity][{$field_name}][0][value]"], 'Field value found in loaded profile.');
$this
->assertTrue($profile
->isDefault());
$this
->drupalGet($profile
->toUrl());
$this
->assertSession()
->pageTextContains($edit[$id . "_profiles[0][entity][{$field_name}][0][value]"]);
}
}