Does it work if you remove the authorize and acting as?
Aug 14, 2019
10
Level 20
testing that a user can edit profile
Simple enough situation, a user can edit their own profile. Run it through a browser test, works like a charm. Make a test for it... well now we have something interesting.
/** @test */
function auth_user_can_edit_own_profile()
{
$user = factory('App\User')->create();
$user->setProfile(); //this just makes an empty profile so a 1-1 relation exists
$this->actingAs($user);
$response = $this->post('profile/'.$user->id.'/update',[
'quote'=>'Hello World',
]);
$response->assertSee('Hello World');
}
I get a failure. what displays on the screen is a redirect to the main page. O.o Nothing in my code should make that happen.
I believe it has something to do with my form request's authorize() method. However, I fail to see how. I have tried using $this->withoutExceptionHandling() in my test with no change.
storeProfile form request:
public function authorize()
{
$profile = $this->route('profile');
if ($this->user()->can('edit profile') || $this->user()->id == $profile->user_id)
{
return true;
}
return false;
}
Profile Controller update:
public function update(storeProfile $request, Profile $profile)
{
$profile->update($request->except('name'));
$profile->save();
if($profile->user->name != $request->input('name'))
{
$profile->user()->update(['name' => $request->input('name')]);
//TODO event to track name changes
}
return redirect()->action('ProfileController@show',['id' => $profile->user_id]);
}
Any insights to this would be really helpful, but I truly don't understand what is happening. Thank you all for taking the time to read and help!
Please or to participate in this conversation.