Skip to content

Commit

Permalink
Merge pull request #14222 from marcusmoore/tests/company-get-id-for-c…
Browse files Browse the repository at this point in the history
…urrent-user

Added tests around getIdForCurrentUser method
  • Loading branch information
snipe committed Feb 5, 2024
2 parents e08d60e + 9e6e2de commit 3b36372
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
8 changes: 8 additions & 0 deletions app/Models/Company.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,14 @@ public static function getIdFromInput($unescaped_input)
}
}

/**
* Get the company id for the current user taking into
* account the full multiple company support setting
* and if the current user is a super user.
*
* @param $unescaped_input
* @return int|mixed|string|null
*/
public static function getIdForCurrentUser($unescaped_input)
{
if (! static::isFullMultipleCompanySupportEnabled()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
namespace Tests\Unit;
namespace Tests\Unit\Models\Company;

use App\Models\Company;
use App\Models\User;
Expand Down
45 changes: 45 additions & 0 deletions tests/Unit/Models/Company/GetIdForCurrentUserTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Tests\Unit\Models\Company;

use App\Models\Company;
use App\Models\User;
use Tests\Support\InteractsWithSettings;
use Tests\TestCase;

class GetIdForCurrentUserTest extends TestCase
{
use InteractsWithSettings;

public function testReturnsProvidedValueWhenFullCompanySupportDisabled()
{
$this->settings->disableMultipleFullCompanySupport();

$this->actingAs(User::factory()->create());
$this->assertEquals(1000, Company::getIdForCurrentUser(1000));
}

public function testReturnsProvidedValueForSuperUsersWhenFullCompanySupportEnabled()
{
$this->settings->enableMultipleFullCompanySupport();

$this->actingAs(User::factory()->superuser()->create());
$this->assertEquals(2000, Company::getIdForCurrentUser(2000));
}

public function testReturnsNonSuperUsersCompanyIdWhenFullCompanySupportEnabled()
{
$this->settings->enableMultipleFullCompanySupport();

$this->actingAs(User::factory()->forCompany(['id' => 2000])->create());
$this->assertEquals(2000, Company::getIdForCurrentUser(1000));
}

public function testReturnsProvidedValueForNonSuperUserWithoutCompanyIdWhenFullCompanySupportEnabled()
{
$this->settings->enableMultipleFullCompanySupport();

$this->actingAs(User::factory()->create(['company_id' => null]));
$this->assertEquals(1000, Company::getIdForCurrentUser(1000));
}
}

0 comments on commit 3b36372

Please sign in to comment.