You are here

public function RptTestGenerated::testGeneratedPassword in Registration Password Token 8

Test generated passwords.

@dataProvider getGeneratedPasswordData

File

tests/src/Kernel/RptTestGenerated.php, line 47

Class

RptTestGenerated
Test RPT fully.

Namespace

Drupal\Tests\rpt\Kernel

Code

public function testGeneratedPassword($length) {
  $configFactory = \Drupal::service('config.factory');
  $configFactory
    ->getEditable('rpt.settings')
    ->set('password_length', $length)
    ->save();

  /** @var \Drupal\Core\Form\FormBuilderInterface $formBuilder */
  $formBuilder = $this->container
    ->get('form_builder');

  /** @var \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager */
  $entityTypeManager = $this->container
    ->get('entity_type.manager');

  /** @var \Drupal\Core\Utility\Token $entityTypeManager */
  $token = $this->container
    ->get('token');
  $entity = $entityTypeManager
    ->getStorage('user')
    ->create([]);
  $formObject = $entityTypeManager
    ->getFormObject('user', 'register')
    ->setEntity($entity);
  $formState = new FormState();
  $formObject
    ->buildForm([], $formState);
  $formState
    ->setValues([
    'mail' => 'test@example.com',
    'name' => 'test',
  ]);
  $formBuilder
    ->submitForm($formObject, $formState);
  $replacedToken = $token
    ->replace('[user:password]', [
    'user' => $formState
      ->getFormObject()
      ->getEntity(),
  ]);
  $this
    ->assertSame($length, mb_strlen($replacedToken));
}