diff --git a/tests/php-unit-tests/ItopDataTestCase.php b/tests/php-unit-tests/ItopDataTestCase.php index d28fb40a5..751d15e3f 100644 --- a/tests/php-unit-tests/ItopDataTestCase.php +++ b/tests/php-unit-tests/ItopDataTestCase.php @@ -487,6 +487,35 @@ class ItopDataTestCase extends ItopTestCase return $oUser; } + /** + * @param string $sLogin + * @param int $iProfileId + * + * @return \UserLocal + * @throws Exception + */ + protected function CreateContactlessUser($sLogin, $iProfileId, $sPassword = null) + { + if (empty($sPassword)) { + $sPassword = $sLogin; + } + + $oUserProfile = new URP_UserProfile(); + $oUserProfile->Set('profileid', $iProfileId); + $oUserProfile->Set('reason', 'UNIT Tests'); + $oSet = DBObjectSet::FromObject($oUserProfile); + /** @var \UserLocal $oUser */ + $oUser = $this->createObject('UserLocal', array( + 'login' => $sLogin, + 'password' => $sPassword, + 'language' => 'EN US', + 'profile_list' => $oSet, + )); + $this->debug("Created {$oUser->GetName()} ({$oUser->GetKey()})"); + + return $oUser; + } + /** * @param \DBObject $oUser * @param int $iProfileId diff --git a/tests/php-unit-tests/unitary-tests/datamodels/2.x/authent-local/UserLocalTest.php b/tests/php-unit-tests/unitary-tests/datamodels/2.x/authent-local/UserLocalTest.php index a7b4403bb..f558ef125 100644 --- a/tests/php-unit-tests/unitary-tests/datamodels/2.x/authent-local/UserLocalTest.php +++ b/tests/php-unit-tests/unitary-tests/datamodels/2.x/authent-local/UserLocalTest.php @@ -11,11 +11,16 @@ namespace Combodo\iTop\Test\UnitTest\Module\AuthentLocal; use AttributeDate; use Combodo\iTop\Test\UnitTest\ItopDataTestCase; use Config; +use DBObjectSearch; +use DBObjectSet; use Dict; use MetaModel; use ormLinkSet; use URP_UserProfile; +use User; use UserLocal; +use UserRights; +use utils; /** * test class for UserLocal class @@ -392,5 +397,62 @@ class UserLocalTest extends ItopDataTestCase ), ); } + + public function testGetUserProfileList() + { + utils::GetConfig()->SetModuleSetting('authent-local', 'password_validation.pattern', ''); + $sAdminLogin = 'admin'; + $oExistingAdminUser = MetaModel::GetObjectByColumn(User::class, 'login', $sAdminLogin, false); + if (\is_null($oExistingAdminUser)) { + $sAdministratorProfileId = 1; + $this->CreateContactlessUser($sAdminLogin, $sAdministratorProfileId); + } + + // By default should see all profiles + $oProfilesSet = $this->GetAdminUserProfileList(); + $this->assertIsObject($oProfilesSet); + $this->assertInstanceOf(ormLinkSet::class, $oProfilesSet); + $this->assertGreaterThan(0, $oProfilesSet->Count()); + + // non admin user : seeing profiles depends on the security.hide_administrators config param value + $sSupportAgentProfileId = 5; + $sSupportAgentLogin = 'support_agent'; + $this->CreateContactlessUser($sSupportAgentLogin, $sSupportAgentProfileId); + UserRights::Login($sSupportAgentLogin); + MetaModel::GetConfig()->Set('security.hide_administrators', true); + $oProfilesSet = $this->GetAdminUserProfileList(); + $this->assertIsObject($oProfilesSet); + $this->assertInstanceOf(ormLinkSet::class, $oProfilesSet); + $this->assertEquals(0, $oProfilesSet->Count()); + MetaModel::GetConfig()->Set('security.hide_administrators', false); + $oProfilesSet = $this->GetAdminUserProfileList(); + $this->assertIsObject($oProfilesSet); + $this->assertInstanceOf(ormLinkSet::class, $oProfilesSet); + $this->assertGreaterThan(0, $oProfilesSet->Count()); + + // admin user : will always see profiles whatever the security.hide_administrators config param value is + UserRights::Login($sAdminLogin); + MetaModel::GetConfig()->Set('security.hide_administrators', true); + $oProfilesSet = $this->GetAdminUserProfileList(); + $this->assertIsObject($oProfilesSet); + $this->assertInstanceOf(ormLinkSet::class, $oProfilesSet); + $this->assertGreaterThan(0, $oProfilesSet->Count()); + MetaModel::GetConfig()->Set('security.hide_administrators', false); + $oProfilesSet = $this->GetAdminUserProfileList(); + $this->assertIsObject($oProfilesSet); + $this->assertInstanceOf(ormLinkSet::class, $oProfilesSet); + $this->assertGreaterThan(0, $oProfilesSet->Count()); + } + + private function GetAdminUserProfileList(): ormLinkSet + { + $oSearch = new DBObjectSearch(UserLocal::class); + $oSearch->AllowAllData(); + $oSearch->AddCondition('login', 'admin', '='); + $oObjectSet = new DBObjectSet($oSearch); + /** @noinspection OneTimeUseVariablesInspection */ + $oUser = $oObjectSet->Fetch(); + return $oUser->Get('profile_list'); + } }