RptTestGenerated.php in Registration Password Token 8
File
tests/src/Kernel/RptTestGenerated.php
View source
<?php
namespace Drupal\Tests\rpt\Kernel;
use Drupal\Core\Form\FormState;
use Drupal\KernelTests\KernelTestBase;
class RptTestGenerated extends KernelTestBase {
public static $modules = [
'rpt',
'user',
];
protected function setUp() {
parent::setUp();
$this
->installEntitySchema('user');
\Drupal::service('router.builder')
->rebuild();
$configFactory = \Drupal::service('config.factory');
$configFactory
->getEditable('user.settings')
->set('verify_mail', FALSE)
->save();
$configFactory
->getEditable('rpt.settings')
->set('password_generate', TRUE)
->save();
}
public function testGeneratedPassword($length) {
$configFactory = \Drupal::service('config.factory');
$configFactory
->getEditable('rpt.settings')
->set('password_length', $length)
->save();
$formBuilder = $this->container
->get('form_builder');
$entityTypeManager = $this->container
->get('entity_type.manager');
$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));
}
public function getGeneratedPasswordData() {
return [
[
10,
],
[
20,
],
[
15,
],
[
35,
],
[
1,
],
];
}
}