Summer Sale! All accounts are 50% off this week.

lara28580's avatar

Laravel tests do not pass

I want start testing but I cant get the existing laravel tests to work.

public function test_password_can_be_confirmed()
    {
        $user = User::factory()->create();

        $response = $this->actingAs($user)->post('/confirm-password', [
            'password' => 'password',
        ]);

        $response->assertRedirect();
        $response->assertSessionHasNoErrors();
    }

This test fails with

The expected [Illuminate\Auth\Notifications\ResetPassword] notification was not sent.
  Failed asserting that false is true.

I have to say in may user migration I changed name to username, but this doesn't matter here I think.

0 likes
6 replies
lara28580's avatar

Got most of the functions to work with

php artisan config:clear

Here I get The user is not authenticated. I changed name to username because I changed that in the whole app.

$response = $this->post('/register', [
            'username' => 'Test User',
            'email' => '[email protected]',
            'password' => 'password',
            'password_confirmation' => 'password',
        ]);
        
        $this->assertAuthenticated();
        $response->assertRedirect(RouteServiceProvider::HOME);
drehimself's avatar

Try adding $this->withoutExceptionHandling(); at the top of your test for more useful error messages to help you find your issue.

The RegisteredUserController.php file has the code that handles this logic. Change all instances of name to username. You also might have to update your User.php model and replace name with username in the filled array.

1 like
lara28580's avatar

Thanks for your answer. I have added $this->withoutExceptionHandling(); and I get The given data was invalid.

What I have to say is I have the "verified" middleware in place if that changes something.

The RegisteredUserController.php file has the code that handles this logic. Change all instances of name to >username. You also might have to update your User.php model and replace name with username in the filled >array.

Have done that already.

lara28580's avatar

Now I get The user is not authenticated because of that $this->assertAuthenticated();

In my RegisteredUserController the user gets logged in before the rdirect.


        $user = User::create([
            'username' => $request->username,
            'email' => $request->email,
            'password' => Hash::make($request->password),
        ]);

        event(new Registered($user));

        Auth::login($user); 

        return redirect(RouteServiceProvider::HOME);
    }
lara28580's avatar

I now know what was/is wrong. I added google recaptcha to the registercontroller. This is why the unit test fails. But how could I consider or how should I treat the recaptcha code in my test.

public function store(Request $request)
    {
        $request->validate([
            'username' => ['required', 'string', 'max:255'],
            'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
            'password' => ['required', 'confirmed', Rules\Password::defaults()],
        ]);

        // Google Recaptcha
        $url = 'https://www.google.com/recaptcha/api/siteverify';
        $data = [
            'secret' =>  config('google-recaptcha.recaptcha_secret_key'),
            'response' =>  $request->recaptcha,
        ];  

        $options = [
            'http' => [
                    'header' => 'Content-type: application/x-www-form-urlencoded\r\n',
                    'method' => 'POST',
                    'content' => http_build_query($data),
            ]
        ];

        $context = stream_context_create($options);
        $result = file_get_contents($url, false, $context);
        $resultJson = json_decode($result);

        if($resultJson->success != true) return back()->withErrors('Captcha Error');
        // Google Recaptcha End

        $user = User::create([
            'username' => $request->username,
            'email' => $request->email,
            'password' => Hash::make($request->password),
        ]);

        event(new Registered($user));

        Auth::login($user);

        return redirect(RouteServiceProvider::HOME);
    }

Please or to participate in this conversation.