0
votes

After Upgrading to laravel 5.8 / PHPUnit 8 I had several tests that started failing. For example the following test.

public function testAdminCanPromoteUsers()
{
    // While using a admin account try to promote non-admin user
    $this->actingAs($this->admin)
        ->post('user/promote', ['userPromoteId' => $this->user->id, 'name' => $this->user->name]);

    // check if user was promoted to admin
    $user = User::find($this->user->id);
    $this->assertTrue((bool) $user->isAdmin);
}

Routes

| POST     | user/allow    | App\Http\Controllers\UserController@allow    | web,admin|
| POST     | user/demote   | App\Http\Controllers\UserController@demote   | web,admin|
| POST     | user/promote  | App\Http\Controllers\UserController@promote  | web,admin|
| POST     | user/reset    | App\Http\Controllers\UserController@reset    | web,admin|
| POST     | user/restrict | App\Http\Controllers\UserController@restrict | web,admin|

user model

<?php

namespace App\Models;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Auth;

class User extends Authenticatable
{
    use Notifiable;
    public static $snakeAttributes = false;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

}

user migration

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function(Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('email', 191)->unique();
            $table->string('password');
            $table->boolean('isAdmin')->default(false);
            $table->boolean('hasAccess')->default(true);
            $table->rememberToken();
            $table->timestamps();
        });
    }

UserController function

    /**
    * Make an user as admin
    */
    public function promote(Request $request) {
        // Create the collection name
        $thisUsr = User::findOrFail($request->userPromoteId);
        if (strcasecmp($thisUsr->name, $request->name) == 0) {
          $thisUsr->isAdmin = true;
          $thisUsr->save();
          return redirect()->route('userIndex');
        }

        // Error message
        return redirect()->route('userIndex')->withErrors("Failed to Promote User to Admin. User wasn't found.");
    }

PHPUnit Results

1) UserControllerTest::testAdminCanPromoteUsers
Failed asserting that false is true.

/var/www/tests/Feature/Controllers/UserControllerTest.php:61

Everything still is working in the browser just the test is failing. Any ideas on what may have changed that would cause this to fail.

1
Can you post the whole User model please?IGP
done updated the question.Tracy McCormick
Is $user->isAdmin a field? Is $this->user persisted to the db in the tests?IGP
yes that is correctTracy McCormick
What kind of field is isAdmin?IGP

1 Answers

1
votes

I've seen people suggest adding $this->withoutMiddleware(); to the function, but that hasn't worked well for me. Try running it with a token present.

$this->actingAs($this->admin)
        ->post('user/promote', ['userPromoteId' => $this->user->id, 'name' => $this->user->name, '_token' => csrf_token()]);