Erwin's avatar
Level 3

Test fails due to redirect to root

Hi Guys,

I have a very basic test where I test the logout process. This is my test:

<?php

namespace Tests\Feature;

use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Validation\ValidationException;

class LoginTest extends TestCase
{
    use DatabaseMigrations;

    /** @test */
    public function logging_out_the_current_user()
    {
        $this->withExceptionHandling();

        \Auth::login(factory(\App\User::class)->create());

        $response = $this->get('/logout');

        $response->assertRedirect('/login');
        $this->assertFalse(\Auth::check());
    }
}

When I dump the $response->getContents() I get the following response:

<!DOCTYPE html>\n
<html>\n
    <head>\n
        <meta charset="UTF-8" />\n
        <meta http-equiv="refresh" content="0;url=http://site" />\n
\n
        <title>Redirecting to http://site</title>\n
    </head>\n
    <body>\n
        Redirecting to <a href="http://site">http://site</a>.\n
    </body>\n
</html>

Why is this happening? I don't have any clue.

Kind regards,

Erwin

0 likes
13 replies
rin4ik's avatar

please try this

$this->get('/logout')->assertRedirect('/login');
rin4ik's avatar

btw why it should go to login page after logout? by default it will redirect to home page

$this->get('/logout')->assertRedirect('/');
Erwin's avatar
Level 3

Same result when I chain it. Why to te login? Thats where I prefer the user to go to after the logout.

rin4ik's avatar

Illuminate\Foundation\Auth\AuthenticatesUsers; change method like below

public function logout(Request $request)
{
    $this->guard()->logout();

    $request->session()->flush();

    $request->session()->regenerate();

    return redirect('/login');
}
Erwin's avatar
Level 3

I use Spark and the function for logging out is.

```
public function logout()
{
    $this->guard()->logout();

    session()->flush();

    return redirect(
        property_exists($this, 'redirectAfterLogout')
                ? $this->redirectAfterLogout : '/'
    );
}
Cronix's avatar

@rin4ik Please stop asking people to just blindly alter vendor files. You NEVER want to do that. Especially if you aren't warning them that updating their app will undo their changes. It's just almost never an appropriate thing to do.

Since AuthenticatesUsers is a trait used in the login controller, all you'd have to do is override the logout function in your login controller with your own.

1 like
Cronix's avatar

@Erwin Since you're using spark, you can just change the redirect in SparkServiceProvider.

In the booted() method:

Spark::redirectAfterLogout('/the-new-url');
Erwin's avatar
Level 3

Well the logout is working just fine when I use it on the website itself. But only when testing it it redirects to the root.

Erwin's avatar
Level 3

The same issue with the following test:

    /** @test */
    public function password_is_required()
    {
        $response = $this->post('/register', [
            'email' => '[email protected]',
            'password' => ''
        ]);

        $response->assertRedirect('/register');
        $response->assertSessionHasErrors('password');
        $this->assertEquals(0, \App\User::count());
    }

This results in the following error:

  1. Tests\Feature\RegisterTest::password_is_required Failed asserting that two strings are equal. --- Expected +++ Actual @@ @@ -'http://site/register' +'http://site'

When I do this on the website itself it returns normal to the register page with an password error. Am I missing something?

Erwin's avatar
Level 3

That is not redirecting at all, but give a JSON response.

Erwin's avatar
Level 3

You already suggested that and that isnt working either. When I dump the response of the post I get:

<!DOCTYPE html>\n
<html>\n
    <head>\n
        <meta charset="UTF-8" />\n
        <meta http-equiv="refresh" content="0;url=http://site" />\n
\n
        <title>Redirecting to http://site</title>\n
    </head>\n
    <body>\n
        Redirecting to <a href="http://site">http://site</a>.\n
    </body>\n
</html>

Please or to participate in this conversation.