Skip to main content

Admin: Managing Users

Premium
12 min read
Richard A. Hoyle avatar

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.

Nerijus avatar

Changed. Thanks.

Jemmeli Nejmeddine avatar

here why testing for admin if can do the CRUD and we did not put restriction no in Route and no in middleware ?

Nerijus avatar

Dont skip text.

Notice: I specifically didn't add isAdmin middleware for this route because later it can be reused for users with the company owner role. We will only need to restrict access so that users couldn't see other companies. My plan for this is to use Policies.

Vitalii avatar

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,
]);
}
👍 2
joschuba avatar

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,
]);
Sylvain P avatar

thanks a lot

teebee avatar

Shouldn't the article reflect this in any way... I was wreaking my had for the last couple of hours ... time well spent

Nerijus avatar

Will be fixed as well as updated to the laravel 11 in the next week.

Babur avatar

Guys, the last test will fail intil you add SoftDeletes to User model. In the next lesson it'll be added. So, it's OK if you see your test fail. You can comment that test for now.

👍 1
Sylvain P avatar

"It doesn't make sense to show a screenshot with tests that have passed but are not successful in this step."

teebee avatar

agreed ... I'm wasting time trying to figure it out, when the solution is to actually ignore it

mrl666 avatar

I think the index screen should list all users of the company. So I don't understand why on the index screen we only show users with the role of Company Owner. Can you explain it to me, please?

Hossam Mohamed avatar

**I advise you to edit factory of Company to be like this : ** 'name' => $this->faker->words(2, true);

to avoid the error while testing :

[Illuminate\Database\Grammar::parameterize(): Argument #1 ($values) must be of type array, string given]