From 76cc5995d9a311b706b6d2ed3f54fbadf1a7eb1b Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Mon, 5 Feb 2024 12:38:33 -0800 Subject: [PATCH 1/2] Write tests around getIdForCurrentUser method [sc-24748] --- .../Unit/{ => Models/Company}/CompanyTest.php | 2 +- .../Company/GetIdForCurrentUserTest.php | 45 +++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) rename tests/Unit/{ => Models/Company}/CompanyTest.php (92%) create mode 100644 tests/Unit/Models/Company/GetIdForCurrentUserTest.php diff --git a/tests/Unit/CompanyTest.php b/tests/Unit/Models/Company/CompanyTest.php similarity index 92% rename from tests/Unit/CompanyTest.php rename to tests/Unit/Models/Company/CompanyTest.php index 6e0450484374..6fd17e554b7b 100644 --- a/tests/Unit/CompanyTest.php +++ b/tests/Unit/Models/Company/CompanyTest.php @@ -1,5 +1,5 @@ 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)); + } +} From 9e6e2de71eaed34dcfb5be0deb974412706aba48 Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Mon, 5 Feb 2024 12:49:29 -0800 Subject: [PATCH 2/2] Add docblock --- app/Models/Company.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/app/Models/Company.php b/app/Models/Company.php index c3a2fdae7f97..60a8022ed708 100644 --- a/app/Models/Company.php +++ b/app/Models/Company.php @@ -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()) {