Skip to content

Commit

Permalink
13730: Fixing "Error when creating a user without password" (mautic#1…
Browse files Browse the repository at this point in the history
…4107)

* 13730: Fixing "Error when creating a user without password"

* 13730-Added more coverage
  • Loading branch information
shinde-rahul authored Sep 17, 2024
1 parent f346a80 commit 93db721
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 5 deletions.
15 changes: 10 additions & 5 deletions app/bundles/UserBundle/Entity/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ public static function loadValidatorMetadata(ClassMetadata $metadata): void
$metadata->addPropertyConstraint('plainPassword', new Assert\NotBlank(
[
'message' => 'mautic.user.user.password.notblank',
'groups' => ['CheckPassword'],
'groups' => ['CheckPasswordNotBlank'],
]
));

Expand All @@ -262,10 +262,15 @@ public static function determineValidationGroups(Form $form): array
{
$data = $form->getData();
$groups = ['User', 'SecondPass'];

// check if creating a new user or editing an existing user and the password has been updated
if ($data instanceof User && (!$data->getId() || ($data->getId() && $data->getPlainPassword()))) {
$groups[] = 'CheckPassword';
if ($data instanceof User) {
$isNewUser = !$data->getId();
$hasPlainPassword = !empty($data->getPlainPassword());

if ($isNewUser) {
$groups[] = $hasPlainPassword ? 'CheckPassword' : 'CheckPasswordNotBlank';
} elseif ($hasPlainPassword) {
$groups[] = 'CheckPassword';
}
}

return $groups;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,112 @@ public function testEditActionFormSubmissionInvalid(): void
$this->assertStringContainsString('The email entered is invalid.', $this->client->getResponse()->getContent());
}

/**
* @param array<string, string> $data
*
* @dataProvider dataNewUserForPasswordField
*/
public function testNewUserForPasswordField(array $data, string $message): void
{
$crawler = $this->client->request('GET', '/s/users/new');

$formData = [
'user[firstName]' => 'John',
'user[lastName]' => 'Doe',
'user[email]' => '[email protected]',
];

$form = $crawler->selectButton('Save')->form($formData + $data);

$this->client->submit($form);

$this->assertSame(Response::HTTP_OK, $this->client->getResponse()->getStatusCode());
$this->assertStringContainsString($message, $this->client->getResponse()->getContent());
}

/**
* @return iterable<string, array<int, string|array<string, string>>>
*/
public function dataNewUserForPasswordField(): iterable
{
yield 'Blank' => [
[
'user[plainPassword][password]' => '',
'user[plainPassword][confirm]' => '',
],
'Password cannot be blank.',
];

yield 'Do not match with confirm' => [
[
'user[plainPassword][password]' => 'same',
],
'Passwords do not match.',
];

yield 'Minimum length' => [
[
'user[plainPassword][password]' => 'same',
'user[plainPassword][confirm]' => 'same',
],
'Password must be at least 6 characters.',
];

yield 'No stronger' => [
[
'user[plainPassword][password]' => 'same123',
'user[plainPassword][confirm]' => 'same123',
],
'Please enter a stronger password. Your password must use a combination of upper and lower case, special characters and numbers.',
];
}

/**
* @param array<string, string> $data
*
* @dataProvider dataForEditUserForPasswordField
*/
public function testEditUserForPasswordField(array $data, string $message): void
{
$crawler = $this->client->request('GET', '/s/users/edit/1');

$form = $crawler->selectButton('Save')->form($data);

$this->client->submit($form);

$this->assertSame(Response::HTTP_OK, $this->client->getResponse()->getStatusCode());
$this->assertStringContainsString($message, $this->client->getResponse()->getContent());
}

/**
* @return iterable<string, array<int, string|array<string, string>>>
*/
public function dataForEditUserForPasswordField(): iterable
{
yield 'Do not match with confirm' => [
[
'user[plainPassword][password]' => 'same',
],
'Passwords do not match.',
];

yield 'Minimum length' => [
[
'user[plainPassword][password]' => 'same',
'user[plainPassword][confirm]' => 'same',
],
'Password must be at least 6 characters.',
];

yield 'No stronger' => [
[
'user[plainPassword][password]' => 'same123',
'user[plainPassword][confirm]' => 'same123',
],
'Please enter a stronger password. Your password must use a combination of upper and lower case, special characters and numbers.',
];
}

/**
* @param array<mixed> $details
*/
Expand Down

0 comments on commit 93db721

Please sign in to comment.