Comments & Discussion
JN
here why testing for admin if can do the CRUD and we did not put restriction no in Route and no in middleware ?
V
It seems that we have a route name's typo within the test_admin_can_delete_user_for_a_company(). Should it be ...->delete(route('companies.users.destroy',...); instead of 'update'?
And one more question. Is that "updated user" from the previous test retained, or is the database cleared before each test is run and our check is meaningless here? In this case, do we need to write something like this?
public function test_admin_can_delete_user_for_a_company(): void{ $company = Company::factory()->create(); $admin = User::factory()->admin()->create([ 'company_id' => $company->id ]); $deletedUser = User::factory()->create([ 'email' => 'delete@user.com', 'company_id' => $company->id ]); $this->assertDatabaseHas('users', [ 'email' => 'delete@user.com', 'company_id' => $company->id, ]); $response = $this->actingAs($admin)->delete( route('companies.users.destroy', [$company, $deletedUser]) ); $response->assertRedirect(route('companies.users.index', $company)); $this->assertDatabaseMissing('users', [ 'name' => 'delete user', 'company_id' => $company->id, ]);}
J
In case someone is wondering: The last test of this lesson is failing, because the soft deletes in the user model aren't implemented yet. They follow in the next lesson. Until then you could use this as the last assertion:
$this->assertDatabaseMissing('users', [ 'id' => $user->id,]);
B
SP
M
Please make changes to the CompanyFactory.php page your 'name' => fake()->words(3), Will not work you changed it in the GitHub version of the class to: 'name' => fake()->words(3, asText: true), This did work and the test did pass.
Changed. Thanks.