Skip to main content

Company Owner: Manages Users

Premium
9 min read

Comments & Discussion

RA
Richard A. Hoyle ✓ Link copied!

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

RA
Richard A. Hoyle ✓ Link copied!

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

AA
Ali Al Qahtani ✓ Link copied!

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]));

N
Nerijus ✓ Link copied!

Updated tutorial. Thanks

D
davidvm ✓ Link copied!

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

V
Vitalii ✓ Link copied!

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

N
Nerijus ✓ Link copied!

Yes it would be logical.

A
andywong31 ✓ Link copied!

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?

N
Nerijus ✓ Link copied!

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

M
Modestas ✓ Link copied!

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

A
andywong31 ✓ Link copied!

thanks guys!

SI
Solomon Iroegbu ✓ Link copied!

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

N
Nerijus ✓ Link copied!

Its like your test file has syntax errors

T
teebee ✓ Link copied!

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