Skip to main content

Admin: Managing Users

Premium
12 min read

Comments & Discussion

RA
Richard A. Hoyle ✓ Link copied!

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.

N
Nerijus ✓ Link copied!

Changed. Thanks.

JN
Jemmeli Nejmeddine ✓ Link copied!

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

N
Nerijus ✓ Link copied!

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.

V
Vitalii ✓ Link copied!

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
jwinder ✓ Link copied!

thanks

J
joschuba ✓ Link copied!

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,
]);
SP
Sylvain P ✓ Link copied!

thanks a lot

T
teebee ✓ Link copied!

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

N
Nerijus ✓ Link copied!

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

B
Babur ✓ Link copied!

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.

SP
Sylvain P ✓ Link copied!

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

T
teebee ✓ Link copied!

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

M
mrl666 ✓ Link copied!

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?

HM
Hossam Mohamed ✓ Link copied!

**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]

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.