Hello,
What I am trying to test is very basic
class ReadPostsTest extends TestCase
{
use DatabaseMigrations;
public function setUp()
{
parent::setUp();
$user = factory('App\User')->create();
$this->signIn($user);
$this->topic = factory('App\Topic')->create();
$this->post = factory('App\Post')->create(['topic_id' => $this->topic->id]);
}
/** @test */
public function an_authenticated_user_can_see_posts_associated_to_a_topic()
{
$this->get('/'.$this->topic->slug.'/')
->assertSee($this->post->title);
}
However, after implementing a policy to my Post model its giving me this exception:
Undefined property: App\User::$is_admin
Here is my User model:
class User extends Authenticatable
{
protected $guarded = ['id', 'is_admin'];
protected $hidden = [
'password', 'remember_token',
];
public function posts()
{
return $this->hasMany(Post::class);
}
public function votes()
{
return $this->hasMany(Vote::class);
}
public function is_admin()
{
return $this->is_admin;
}
And here is the Post policy:
class PostPolicy
{
use HandlesAuthorization;
/**
* Policies do not apply if user is admin.
*
* @param $user
* @param $ability
* @return mixed
*/
public function before($user, $ability)
{
if($user->is_admin()) {
return true;
}
}
/**
* Determine whether the user can edit the post.
*
* @param \App\User $user
* @param \App\Post $post
* @return mixed
*/
public function edit(User $user, Post $post)
{
return $user->id === $post->user_id;
}
Thanks in advance!