UserAccountFormPasswordResetTest.php in Drupal 10
File
core/modules/user/tests/src/Kernel/UserAccountFormPasswordResetTest.php
View source
<?php
namespace Drupal\Tests\user\Kernel;
use Drupal\KernelTests\KernelTestBase;
use Drupal\user\Entity\User;
use Symfony\Component\HttpFoundation\Session\Session;
class UserAccountFormPasswordResetTest extends KernelTestBase {
protected static $modules = [
'system',
'user',
];
protected $user;
protected function setUp() : void {
parent::setUp();
$this
->installConfig([
'user',
]);
$this
->installSchema('system', [
'sequences',
]);
$this
->installEntitySchema('user');
$this->user = User::create([
'name' => 'test',
]);
$this->user
->save();
$this->container
->set('current_user', $this->user);
\Drupal::service('router.builder')
->rebuild();
}
public function testPasswordResetToken() {
$request = $this->container
->get('request_stack')
->getCurrentRequest();
$session = new Session();
$request
->setSession($session);
$token = 'VALID_TOKEN';
$session
->set('pass_reset_1', $token);
$request->query
->set('pass-reset-token', $token);
$form = $this
->buildAccountForm('default');
$this
->assertFalse($form['account']['current_pass']['#access']);
$request->query
->set('pass-reset-token', NULL);
$request->attributes
->set('pass-reset-token', $token);
$form = $this
->buildAccountForm('default');
$this
->assertTrue($form['account']['current_pass']['#access']);
}
protected function buildAccountForm($operation) {
$entity_type = 'user';
$fields = [];
if ($operation != 'register') {
$fields['uid'] = $this->user
->id();
}
$entity = $this->container
->get('entity_type.manager')
->getStorage($entity_type)
->create($fields);
return $this->container
->get('entity.form_builder')
->getForm($entity, $operation);
}
}