Commit 753be28f authored by Ide Braakman's avatar Ide Braakman
Browse files

Issue #3294049 by idebr: Generate an email address for test accounts

parent df69acb0
Loading
Loading
Loading
Loading
+14 −1
Original line number Diff line number Diff line
@@ -30,6 +30,13 @@ class RoleTestAccountsManager implements RoleTestAccountsManagerInterface {
   */
  protected $config;

  /**
   * The site configuration.
   *
   * @var \Drupal\Core\Config\ImmutableConfig
   */
  protected $siteConfig;

  /**
   * The time service.
   *
@@ -38,7 +45,7 @@ class RoleTestAccountsManager implements RoleTestAccountsManagerInterface {
  protected $time;

  /**
   * Constructs a new RoleTestAccountsManager constructor.
   * Constructs a new RoleTestAccountsManager.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
@@ -50,6 +57,7 @@ class RoleTestAccountsManager implements RoleTestAccountsManagerInterface {
  public function __construct(EntityTypeManagerInterface $entity_type_manager, ConfigFactoryInterface $config_factory, TimeInterface $time) {
    $this->entityTypeManager = $entity_type_manager;
    $this->config = $config_factory->get('role_test_accounts.settings');
    $this->siteConfig = $config_factory->get('system.site');
    $this->time = $time;
  }

@@ -64,6 +72,11 @@ class RoleTestAccountsManager implements RoleTestAccountsManagerInterface {
      /** @var \Drupal\user\UserInterface $user */
      $user = $user_storage->create(['name' => 'test.' . $role_id]);
    }

    $site_mail = $this->siteConfig->get('mail');
    $site_mail_parts = explode('@', $site_mail);
    $site_mail_username = array_shift($site_mail_parts);
    $user->setEmail($site_mail_username . '+' . $role_id . '@' . implode('', $site_mail_parts));
    $user->setPassword($this->config->get('password'));
    $user->activate();
    if ($role_id !== AccountInterface::AUTHENTICATED_ROLE) {
+9 −1
Original line number Diff line number Diff line
@@ -27,6 +27,12 @@ class RoleTestAccountsConfigTest extends KernelTestBase {
    $this->installSchema('system', ['sequences']);
    $this->installEntitySchema('user');
    $this->installConfig('role_test_accounts');

    $this->config('system.site')
      ->set('langcode', 'en')
      ->set('mail', 'site@example.com')
      ->save();
    $this->container->get('kernel')->rebuildContainer();
  }

  /**
@@ -35,7 +41,9 @@ class RoleTestAccountsConfigTest extends KernelTestBase {
  public function testAccountCreateAndDelete() {
    $role = Role::create(['id' => 'test_role', 'label' => 'Test role']);
    $role->save();
    $this->assertNotFalse(user_load_by_name('test.test_role'));
    $user = user_load_by_name('test.test_role');
    $this->assertInstanceOf(UserInterface::class, $user);
    $this->assertSame('site+test_role@example.com', $user->getEmail());

    $role->delete();
    $this->assertFalse(user_load_by_name('test.test_role'));