I'm currently trying to add validation to my forms. It works fine when I test it but it fails when I run my unit test on it. Removing the validation code allows it to pass with no problems. I tried rewriting the validation portion of the code to do the check manually but no luck. I'm not sure what else I can try. Any help is appreciated.
The error I get:
Did not land on expected page [http://localhost/dashboard].
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'http://localhost/dashboard'
+'http://localhost/reset-password'
Here's what the form validator looks like:
public function submit(Request $request)
{
$validation_errors = [];
$currentUser = $request->user();
$currentUser->password = Hash::make($request->input('new_password'));
$currentUser->reset_password = false;
$user = User::where('id', $currentUser->id)->first();
// >>>>>> Validation Code Start <<<<<<<< //
// >>>>>> Removing this block allows it to pass <<<<<<< //
$newPassword = $request->input('new_password');
// Check if password is the same as the one in the database
if(Hash::check($newPassword, $user->password))
{
return back()->withErrors('Your password can not be the same as your old password.');
}
// Check for special characters
if (!preg_match('/[\'\/~`\!@#\$%\^&\*\(\)_\-\+=\{\}\[\]\|;:"\<\>,\.\?\\\]/',
$newPassword))
{
array_push($validation_errors, "Please make sure your password contains a special character.");
}
// Check for numbers
if (!preg_match('~[0-9]~', $newPassword))
{
array_push($validation_errors, "Please make sure your password contains a number.");
}
// Check for Upper/Lower case
if (!preg_match('/[A-Z][a-z]*/', $newPassword))
{
array_push($validation_errors, "Please make sure your password contains a combination of uppercase and lowercase letters.");
}
// Check for Password length
if (strlen($newPassword) < 10)
{
array_push($validation_errors, "Please make sure your password length is a minimum than 10 characters.");
}
if (count($validation_errors) > 0)
{
return back()->withErrors($validation_errors);
}
// >>>>>> Validation Code End <<<<<<<< //
$currentUser->save();
return redirect()->route('/dashboard');
}
And this is the unit test code:
public function testResetPasswordSuccess()
{
$this->betestUser();
$this->visit('/reset-password')
->type('123', 'password')
->type('123', 'password_confirm')
->press('Save')
->seePageIs('/dashboard');
}
And for anyone curious, this is how I originally validated the form:
$this->validate($request, [
'password' => [
'bail',
'required',
'confirmed',
],
'password_confirm' => [
'bail',
'required',
],
]);
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire