Skip to main content

Company Owner: Manages Users

Premium
9 min read
Richard A. Hoyle avatar

Grate class having a lot of fun learning new things at least for me. However on this part of the test I am not getting all green I have one error that I am having a bit of trouble figuring out

public function test_company_owner_cannot_edit_user_for_other_company()
{
$company = Company::factory()->create();
$company2 = Company::factory()->create();
$user = User::factory()->companyOwner()->create(['company_id' => $company->id]);
 
$response = $this->actingAs($user)->put(route('companies.users.update', [$company2->id, $user->id]), [
'name' => 'updated user',
'email' => 'test@update.com',
]);
 
$response->assertForbidden();
}
 
FAILED Tests\Feature\CompanyUserTest > company owner cannot edit user for other company
Expected response status code [403] but received 302.
Failed asserting that 403 is identical to 302.
 
at tests\Feature\CompanyUserTest.php:167
163 'name' => 'updated user',
164 'email' => 'test@update.com',
165 ]);
166
167 $response->assertForbidden();
168 }
169
170 public function test_company_owner_can_delete_user_for_his_company()
171 {
 
 
Tests: 1 failed, 11 passed (25 assertions)
Duration: 0.85s
 
public function edit(Company $company, User $user)
{
$this->authorize('update', $company);
 
return view('companies.users.edit', compact('company', 'user'));
}

Can’t find the edit in the policies\Company User Policy page

Richard A. Hoyle avatar

I started over from scratch and BOY AM I GLADE! All test passed even the one on this page.

Ali Al Qahtani avatar

Small things, in these methods:

test_company_owner_cannot_delete_user_for_other_company

test_company_owner_can_delete_user_for_his_company

test_admin_can_delete_user_for_a_company

( route name should be destroy not update )

$response = $this->actingAs($user)->delete(route('companies.users.destroy', [$company2->id, $user->id]));

Nerijus avatar

Updated tutorial. Thanks

davidvm avatar

Hi, it seems that in an update of the course the update route name is being used again in de delete tests.

Vitalii avatar

Shouldn't we use $this->assertSoftDeleted within test_company_owner_can_delete_user_for_his_company?

Nerijus avatar

Yes it would be logical.

andywong31 avatar

question, why did you use the ff code in the navigation section:

request()->routeIs('companies.users.*')

instead of:

request()->routeIs('companies.users.index', auth()->user()->company_id)

is it to make the code shorter?

Nerijus avatar

RouteIs checks if the current URL is the one user is on right now. The * means everything, so every route name that starts with companies. users. will be a true value and navigation item will be set as active. What you are saying is a route model binding

Modestas avatar

To just make it more obvious:

companies.users.index will only match that route. But what if you go inside the create/edit pages? Then the route is not going to be marked as active. This means that the navigation will be in an incorrect state.

Now adding the * to the url as companies.users.* will make it match ANYTHING that is inside there. For example: companies.users.index companies.users.create companies.users.edit

All of the above will match and correctly mark the active navigation due to the * wildcard

andywong31 avatar

thanks guys!

Solomon Iroegbu avatar

i got an error after runing the test.

An error occurred inside PHPUnit.

Message: syntax error, unexpected token "use" Location: C:\laragon\www\reservation\tests\Feature\CompanyTest.php:17

Nerijus avatar

Its like your test file has syntax errors

teebee avatar

Soo many new things to learn ... daaimn :D. Yet it's still is fun