Theodore
Theodore

Reputation: 59

Laravel voyager with phpunit

I'm trying to write some tests for my admin panel using laravel voyager however there seems to be some issues doing some research i found that the problem may be have something to do with permissions

 public function A_admin_user_can_visit_the_admin_page()
    {
        \DB::table('permissions')->insert(
            ['key' => 'browse_admin', 'id' => 1]
        );

        \DB::table('roles')->insert(
            ['name' => 'admin', 'display_name' => 'Administrator']
        );

        \DB::table('permission_role')->insert(
            ['permission_id' => 1, 'role_id' => 1]
        );

        $user = factory('App\User')->create(['role_id' => 1]);
        $this->actingAs($user);
        $this->get('/admin')->assertStatus(200);
    }

This tests works fine given i have the correct permissions

    public function A_admin_can_browse_users_data()
    {
       $this->admin();
        $this->adminPermissions();
        $this->adminRoles();
        $user = factory('App\User')->create(['role_id' => 1]);
        $this->actingAs($user);
        $this->get('/admin')->assertStatus(200);
//      dd(\Voyager::canOrFail('browse_bread'));
        $this->get('/admin/users')->assertStatus(200);

    }

Here this tests fails but I assure the functions admin adminRoles and adminPermissions have created the necessary db entries that give my user full access even when i die and dump the permissions i get true so it can't be the permissions is the problem something else is going wrong and I don't what it is any help would be appreciated.

Upvotes: 1

Views: 417

Answers (1)

Drudge Rajen
Drudge Rajen

Reputation: 7987

Voyager directly require route file in web.php calling method Voyager::routes() because of it the laravel RouteServiceProvider doesn't know about the new route file in case of test environment. So, in order to fix this the issue I did this workaround.

Just add the below code in setUp() method of your test file.

Route::prefix('admin')
            ->namespace('App\Http\Controllers')
            ->group(base_path('vendor/tcg/voyager/routes/voyager.php'));

Upvotes: 1

Related Questions