N°8796 - Add PHP code style validation in iTop and extensions - format whole code base

This commit is contained in:
odain
2025-11-07 20:39:38 +01:00
parent 7681c157ec
commit b0a792afab
369 changed files with 22041 additions and 26866 deletions

View File

@@ -1,4 +1,5 @@
<?php
/*
* @copyright Copyright (C) 2010-2024 Combodo SAS
* @license http://opensource.org/licenses/AGPL-3.0
@@ -48,7 +49,6 @@ use VirtualHost;
use VirtualMachine;
use XMLDataLoader;
/** @see \Combodo\iTop\Test\UnitTest\ItopDataTestCase::CreateObjectWithTagSet() */
define('TAG_CLASS', 'FAQ');
define('TAG_ATTCODE', 'domains');
@@ -80,9 +80,9 @@ abstract class ItopDataTestCase extends ItopTestCase
/**
* @var string Default environment to use for test cases
*/
const DEFAULT_TEST_ENVIRONMENT = 'production';
const USE_TRANSACTION = true;
const CREATE_TEST_ORG = false;
public const DEFAULT_TEST_ENVIRONMENT = 'production';
public const USE_TRANSACTION = true;
public const CREATE_TEST_ORG = false;
protected static $aURP_Profiles = [
'Administrator' => 1,
@@ -126,15 +126,13 @@ abstract class ItopDataTestCase extends ItopTestCase
$this->PrepareEnvironment();
if (static::USE_TRANSACTION)
{
if (static::USE_TRANSACTION) {
CMDBSource::Query('START TRANSACTION');
}
if (static::CREATE_TEST_ORG)
{
if (static::CREATE_TEST_ORG) {
// Create a specific organization for the tests
$this->iTestOrgId = $this->GivenObjectInDB('Organization', [
'name' => 'UnitTestOrganization',
'name' => 'UnitTestOrganization',
]);
}
@@ -166,17 +164,14 @@ abstract class ItopDataTestCase extends ItopTestCase
} else {
$this->debug("");
$this->aCreatedObjects = array_reverse($this->aCreatedObjects);
foreach ($this->aCreatedObjects as $oObject)
{
foreach ($this->aCreatedObjects as $oObject) {
/** @var DBObject $oObject */
try
{
try {
$sClass = get_class($oObject);
$iKey = $oObject->GetKey();
$this->debug("Removing $sClass::$iKey");
$oObject->DBDelete();
}
catch (Exception $e) {
} catch (Exception $e) {
$this->debug("Error when removing created objects: $sClass::$iKey. Exception message: ".$e->getMessage());
}
}
@@ -201,7 +196,8 @@ abstract class ItopDataTestCase extends ItopTestCase
/**
* Helper to reset the metamodel cache : for a class and a key it will contain the SQL query, that could include silo filter
*/
protected function ResetMetaModelQueyCacheGetObject() {
protected function ResetMetaModelQueyCacheGetObject()
{
$this->SetNonPublicStaticProperty(MetaModel::class, 'aQueryCacheGetObject', []);
}
@@ -316,8 +312,7 @@ abstract class ItopDataTestCase extends ItopTestCase
protected function createObject($sClass, $aParams)
{
$oMyObj = MetaModel::NewObject($sClass);
foreach ($aParams as $sAttCode => $oValue)
{
foreach ($aParams as $sAttCode => $oValue) {
$oMyObj->Set($sAttCode, $oValue);
}
$oMyObj->DBInsert();
@@ -341,8 +336,7 @@ abstract class ItopDataTestCase extends ItopTestCase
protected static function updateObject($sClass, $iKey, $aParams)
{
$oMyObj = MetaModel::GetObject($sClass, $iKey);
foreach ($aParams as $sAttCode => $oValue)
{
foreach ($aParams as $sAttCode => $oValue) {
$oMyObj->Set($sAttCode, $oValue);
}
$oMyObj->DBUpdate();
@@ -361,9 +355,9 @@ abstract class ItopDataTestCase extends ItopTestCase
protected function CreateOrganization($sName)
{
/** @var \Organization $oObj */
$oObj = $this->createObject('Organization', array(
$oObj = $this->createObject('Organization', [
'name' => $sName,
));
]);
$this->debug("Created Organization {$oObj->Get('name')}");
return $oObj;
@@ -380,13 +374,13 @@ abstract class ItopDataTestCase extends ItopTestCase
protected function CreateTicket($iNum)
{
/** @var Ticket $oTicket */
$oTicket = $this->createObject('UserRequest', array(
$oTicket = $this->createObject('UserRequest', [
'ref' => 'Ticket_'.$iNum,
'title' => 'TICKET_'.$iNum,
//'request_type' => 'incident',
'description' => 'Created for unit tests.',
'org_id' => $this->getTestOrgId(),
));
]);
$this->debug("Created {$oTicket->Get('title')} ({$oTicket->Get('ref')})");
return $oTicket;
@@ -412,13 +406,13 @@ abstract class ItopDataTestCase extends ItopTestCase
protected function CreateTagData($sClass, $sAttCode, $sTagCode, $sTagLabel, $sTagDescription = '')
{
$sTagClass = TagSetFieldData::GetTagDataClassName($sClass, $sAttCode);
$oTagData = $this->createObject($sTagClass, array(
$oTagData = $this->createObject($sTagClass, [
'code' => $sTagCode,
'label' => $sTagLabel,
'obj_class' => $sClass,
'obj_attcode' => $sAttCode,
'description' => $sTagDescription,
));
]);
$this->debug("Created {$oTagData->Get('code')} ({$oTagData->Get('label')})");
/** @var \TagSetFieldData $oTagData */
@@ -451,7 +445,7 @@ abstract class ItopDataTestCase extends ItopTestCase
private function RemoveObjects($sClass, $sOQL)
{
$oFilter = DBSearch::FromOQL($sOQL);
$aRes = $oFilter->ToDataArray(array('id'));
$aRes = $oFilter->ToDataArray(['id']);
foreach ($aRes as $aRow) {
$this->debug($aRow);
$iKey = $aRow['id'];
@@ -462,7 +456,8 @@ abstract class ItopDataTestCase extends ItopTestCase
}
}
protected function GetUserRequestParams($iNum) {
protected function GetUserRequestParams($iNum)
{
return [
'ref' => 'Ticket_'.$iNum,
'title' => 'BUG 1161_'.$iNum,
@@ -488,7 +483,8 @@ abstract class ItopDataTestCase extends ItopTestCase
* @uses \array_merge()
* @uses createObject
*/
protected function CreateUserRequest($iNum, $aUserRequestCustomParams = []) {
protected function CreateUserRequest($iNum, $aUserRequestCustomParams = [])
{
$aUserRequestDefaultParams = $this->GetUserRequestParams($iNum);
$aUserRequestParams = array_merge($aUserRequestDefaultParams, $aUserRequestCustomParams);
@@ -512,11 +508,11 @@ abstract class ItopDataTestCase extends ItopTestCase
protected function CreateServer($iNum, $iRackUnit = null)
{
/** @var Server $oServer */
$oServer = $this->createObject('Server', array(
$oServer = $this->createObject('Server', [
'name' => 'Server_'.$iNum,
'org_id' => $this->getTestOrgId(),
'nb_u' => $iRackUnit,
));
]);
$this->debug("Created {$oServer->GetName()} ({$oServer->GetKey()})");
return $oServer;
@@ -534,11 +530,11 @@ abstract class ItopDataTestCase extends ItopTestCase
*/
protected function CreatePhysicalInterface($iNum, $iSpeed, $iConnectableCiId)
{
$oObj = $this->createObject('PhysicalInterface', array(
$oObj = $this->createObject('PhysicalInterface', [
'name' => "$iNum",
'speed' => $iSpeed,
'connectableci_id' => $iConnectableCiId,
));
]);
$this->debug("Created {$oObj->GetName()} ({$oObj->GetKey()})");
return $oObj;
@@ -556,11 +552,11 @@ abstract class ItopDataTestCase extends ItopTestCase
*/
protected function CreateFiberChannelInterface($iNum, $iSpeed, $iConnectableCiId)
{
$oObj = $this->createObject('FiberChannelInterface', array(
$oObj = $this->createObject('FiberChannelInterface', [
'name' => "$iNum",
'speed' => $iSpeed,
'datacenterdevice_id' => $iConnectableCiId,
));
]);
$this->debug("Created {$oObj->GetName()} ({$oObj->GetKey()})");
return $oObj;
@@ -578,11 +574,11 @@ abstract class ItopDataTestCase extends ItopTestCase
protected function CreatePerson($iNum, $iOrgId = 0)
{
/** @var Person $oPerson */
$oPerson = $this->createObject('Person', array(
$oPerson = $this->createObject('Person', [
'name' => 'Person_'.$iNum,
'first_name' => 'Test',
'org_id' => ($iOrgId == 0 ? $this->getTestOrgId() : $iOrgId),
));
]);
$this->debug("Created {$oPerson->GetName()} ({$oPerson->GetKey()})");
return $oPerson;
@@ -595,7 +591,7 @@ abstract class ItopDataTestCase extends ItopTestCase
* @return \UserLocal
* @throws Exception
*/
protected function CreateUser($sLogin, $iProfileId, $sPassword=null, $iContactid=2)
protected function CreateUser($sLogin, $iProfileId, $sPassword = null, $iContactid = 2)
{
$oUser = $this->CreateContactlessUser($sLogin, $iProfileId, $sPassword);
$oUser->Set('contactid', $iContactid);
@@ -610,9 +606,9 @@ abstract class ItopDataTestCase extends ItopTestCase
* @return \UserLocal
* @throws Exception
*/
protected function CreateContactlessUser($sLogin, $iProfileId, $sPassword=null)
protected function CreateContactlessUser($sLogin, $iProfileId, $sPassword = null)
{
if (empty($sPassword)){
if (empty($sPassword)) {
$sPassword = $sLogin;
}
@@ -620,12 +616,12 @@ abstract class ItopDataTestCase extends ItopTestCase
$oSet->AddItem(MetaModel::NewObject('URP_UserProfile', ['profileid' => $iProfileId, 'reason' => 'UNIT Tests']));
/** @var \UserLocal $oUser */
$oUser = $this->createObject('UserLocal', array(
$oUser = $this->createObject('UserLocal', [
'login' => $sLogin,
'password' => $sPassword,
'language' => 'EN US',
'profile_list' => $oSet,
));
]);
$this->debug("Created {$oUser->GetName()} ({$oUser->GetKey()})");
return $oUser;
@@ -646,15 +642,14 @@ abstract class ItopDataTestCase extends ItopTestCase
/** @var \ormLinkSet $oSet */
$oSet = $oUser->Get('profile_list');
$oSet->AddItem($oUserProfile);
$oUser = $this->updateObject(User::class, $oUser->GetKey(), array(
$oUser = $this->updateObject(User::class, $oUser->GetKey(), [
'profile_list' => $oSet,
));
]);
$this->debug("Updated {$oUser->GetName()} ({$oUser->GetKey()})");
return $oUser;
}
/**
* Create a Hypervisor in database
*
@@ -668,18 +663,15 @@ abstract class ItopDataTestCase extends ItopTestCase
protected function CreateHypervisor($iNum, $oServer, $oFarm = null)
{
/** @var Hypervisor $oHypervisor */
$oHypervisor = $this->createObject('Hypervisor', array(
$oHypervisor = $this->createObject('Hypervisor', [
'name' => 'Hypervisor_'.$iNum,
'org_id' => $this->getTestOrgId(),
'server_id' => $oServer->GetKey(),
'farm_id' => is_null($oFarm) ? 0 : $oFarm->GetKey(),
));
if (is_null($oFarm))
{
]);
if (is_null($oFarm)) {
$this->debug("Created {$oHypervisor->GetName()} ({$oHypervisor->GetKey()}) on {$oServer->GetName()}");
}
else
{
} else {
$this->debug("Created {$oHypervisor->GetName()} ({$oHypervisor->GetKey()}) on {$oServer->GetName()} part of {$oFarm->GetName()}");
}
@@ -698,11 +690,11 @@ abstract class ItopDataTestCase extends ItopTestCase
protected function CreateFarm($iNum, $sRedundancy = '1')
{
/** @var Farm $oFarm */
$oFarm = $this->createObject('Farm', array(
$oFarm = $this->createObject('Farm', [
'name' => 'Farm_'.$iNum,
'org_id' => $this->getTestOrgId(),
'redundancy' => $sRedundancy,
));
]);
$this->debug("Created {$oFarm->GetName()} ({$oFarm->GetKey()}) redundancy $sRedundancy");
return $oFarm;
@@ -720,11 +712,11 @@ abstract class ItopDataTestCase extends ItopTestCase
protected function CreateVirtualMachine($iNum, $oVirtualHost)
{
/** @var VirtualMachine $oVirtualMachine */
$oVirtualMachine = $this->createObject('VirtualMachine', array(
$oVirtualMachine = $this->createObject('VirtualMachine', [
'name' => 'VirtualMachine_'.$iNum,
'org_id' => $this->getTestOrgId(),
'virtualhost_id' => $oVirtualHost->GetKey(),
));
]);
$this->debug("Created {$oVirtualMachine->GetName()} ({$oVirtualMachine->GetKey()}) on {$oVirtualHost->GetName()}");
return $oVirtualMachine;
@@ -733,24 +725,22 @@ abstract class ItopDataTestCase extends ItopTestCase
protected function CreateObjectWithTagSet()
{
$oFaqCategory = MetaModel::GetObject('FAQCategory', 1, false);
if (empty($oFaqCategory))
{
$oFaqCategory = $this->createObject('FAQCategory', array(
if (empty($oFaqCategory)) {
$oFaqCategory = $this->createObject('FAQCategory', [
'name' => 'FAQCategory_phpunit',
));
]);
}
/** @var \FAQ $oFaq */
$oFaq = $this->createObject('FAQ', array(
$oFaq = $this->createObject('FAQ', [
'category_id' => $oFaqCategory->GetKey(),
'title' => 'FAQ_phpunit',
));
]);
$this->debug("Created {$oFaq->GetName()}");
return $oFaq;
}
/**
* Add a link between a contact and a CI.
* The database is not updated.
@@ -786,10 +776,8 @@ abstract class ItopDataTestCase extends ItopTestCase
protected function RemoveContactFromCI($oContact, $oCI)
{
$oContacts = $oCI->Get('contacts_list');
foreach ($oContacts as $oLnk)
{
if ($oLnk->Get('contact_id') == $oContact->GetKey())
{
foreach ($oContacts as $oLnk) {
if ($oLnk->Get('contact_id') == $oContact->GetKey()) {
$oContacts->RemoveItem($oLnk->GetKey());
$oCI->Set('contacts_list', $oContacts);
$this->debug("Removed {$oContact->GetName()} from {$oCI->Get('name')}");
@@ -821,7 +809,7 @@ abstract class ItopDataTestCase extends ItopTestCase
$this->debug("Added {$oCI->GetName()} to {$oTicket->Get('ref')} with {$sImpactCode}");
return array($oCI->GetKey() => $sImpactCode);
return [$oCI->GetKey() => $sImpactCode];
}
/**
@@ -836,10 +824,8 @@ abstract class ItopDataTestCase extends ItopTestCase
protected function RemoveCIFromTicket($oCI, $oTicket)
{
$oCIs = $oTicket->Get('functionalcis_list');
foreach ($oCIs as $oLnk)
{
if ($oLnk->Get('functionalci_id') == $oCI->GetKey())
{
foreach ($oCIs as $oLnk) {
if ($oLnk->Get('functionalci_id') == $oCI->GetKey()) {
$sImpactCode = $oLnk->Get('impact_code');
$oCIs->RemoveItem($oLnk->GetKey());
$oTicket->Set('functionalcis_list', $oCIs);
@@ -864,13 +850,12 @@ abstract class ItopDataTestCase extends ItopTestCase
* @return array
* @throws Exception
*/
protected function AddContactToTicket($oContact, $oTicket, $sRoleCode, $aParams = array())
protected function AddContactToTicket($oContact, $oTicket, $sRoleCode, $aParams = [])
{
$oNewLink = new lnkContactToTicket();
$oNewLink->Set('contact_id', $oContact->GetKey());
$oNewLink->Set('role_code', $sRoleCode);
foreach ($aParams as $sAttCode => $oValue)
{
foreach ($aParams as $sAttCode => $oValue) {
$oNewLink->Set($sAttCode, $oValue);
}
$oCIs = $oTicket->Get('contacts_list');
@@ -879,7 +864,7 @@ abstract class ItopDataTestCase extends ItopTestCase
$this->debug("Added {$oContact->GetName()} to {$oTicket->Get('ref')} with {$sRoleCode}");
return array($oContact->GetKey() => $sRoleCode);
return [$oContact->GetKey() => $sRoleCode];
}
/**
@@ -894,10 +879,8 @@ abstract class ItopDataTestCase extends ItopTestCase
protected function RemoveContactFromTicket($oContact, $oTicket)
{
$oContacts = $oTicket->Get('contacts_list');
foreach ($oContacts as $oLnk)
{
if ($oLnk->Get('contact_id') == $oContact->GetKey())
{
foreach ($oContacts as $oLnk) {
if ($oLnk->Get('contact_id') == $oContact->GetKey()) {
$sRoleCode = $oLnk->Get('role_code');
$oContacts->RemoveItem($oLnk->GetKey());
$oTicket->Set('contacts_list', $oContacts);
@@ -948,18 +931,14 @@ abstract class ItopDataTestCase extends ItopTestCase
$oFunction();
$iFinalCount = (int) CMDBSource::QueryToScalar("SHOW SESSION STATUS LIKE 'Queries'", 1);
$iCount = $iFinalCount - 1 - $iInitialCount;
if ($iCount != $iExpectedCount)
{
if ($iCount != $iExpectedCount) {
if ($sMessage === '') {
$sMessage = "Expected $iExpectedCount queries. $iCount have been executed.";
}
else {
} else {
$sMessage .= " - Expected $iExpectedCount queries. $iCount have been executed.";
}
$this->fail($sMessage);
}
else
{
} else {
// Otherwise, PHP Unit will consider that no assertion has been made
$this->assertTrue(true);
}
@@ -1001,8 +980,6 @@ abstract class ItopDataTestCase extends ItopTestCase
$this->assertStringContainsString($sNeedle, $aLastLines[0], $sMessage);
}
/**
* Import a set of XML files describing a consistent set of iTop objects
* @param string[] $aFiles
@@ -1014,8 +991,7 @@ abstract class ItopDataTestCase extends ItopTestCase
{
$oLoader = new XMLDataLoader();
$oLoader->StartSession(CMDBObject::GetCurrentChange());
foreach($aFiles as $sFilePath)
{
foreach ($aFiles as $sFilePath) {
$oLoader->LoadFile($sFilePath, false, $bSearch);
}
$oLoader->EndSession();
@@ -1042,7 +1018,6 @@ abstract class ItopDataTestCase extends ItopTestCase
return $ret;
}
protected function DBInsertSingleTable($sTableClass, $aValues, $iKey = 0)
{
$sTable = MetaModel::DBGetTable($sTableClass);
@@ -1052,8 +1027,8 @@ abstract class ItopDataTestCase extends ItopTestCase
}
// fields in first array, values in the second
$aFieldsToWrite = array();
$aValuesToWrite = array();
$aFieldsToWrite = [];
$aValuesToWrite = [];
if (!empty($iKey) && ($iKey >= 0)) {
// Add it to the list of fields to write
@@ -1061,7 +1036,7 @@ abstract class ItopDataTestCase extends ItopTestCase
$aValuesToWrite[] = CMDBSource::Quote($iKey);
}
$aHierarchicalKeys = array();
$aHierarchicalKeys = [];
foreach (MetaModel::ListAttributeDefs($sTableClass) as $sAttCode => $oAttDef) {
// Skip this attribute if not defined in this table
@@ -1118,8 +1093,7 @@ abstract class ItopDataTestCase extends ItopTestCase
protected function GivenObject(string $sClass, array $aParams): DBObject
{
$oMyObj = MetaModel::NewObject($sClass);
foreach ($aParams as $sAttCode => $oValue)
{
foreach ($aParams as $sAttCode => $oValue) {
$oMyObj->Set($sAttCode, $oValue);
}
@@ -1374,16 +1348,14 @@ abstract class ItopDataTestCase extends ItopTestCase
$this->debug("Added CI $sCIId to {$oTicket->Get('ref')} with {$sImpactCode}");
return array($sCIId => $sImpactCode);
return [$sCIId => $sImpactCode];
}
protected function RemoveLnkFunctionalCIToTicketObject($sCIId, $oTicket)
{
$oCIs = $oTicket->Get('functionalcis_list');
foreach ($oCIs as $oLnk)
{
if ($oLnk->Get('functionalci_id') == $sCIId)
{
foreach ($oCIs as $oLnk) {
if ($oLnk->Get('functionalci_id') == $sCIId) {
$sImpactCode = $oLnk->Get('impact_code');
$oCIs->RemoveItem($oLnk->GetKey());
$oTicket->Set('functionalcis_list', $oCIs);
@@ -1396,7 +1368,7 @@ abstract class ItopDataTestCase extends ItopTestCase
$this->assertTrue(false);
}
protected function AddLnkContactToTicketObject($sContactId, $oTicket, $sRoleCode, $aParams = array()): array
protected function AddLnkContactToTicketObject($sContactId, $oTicket, $sRoleCode, $aParams = []): array
{
$aParams['contact_id'] = $sContactId;
$aParams['role_code'] = $sRoleCode;
@@ -1408,16 +1380,14 @@ abstract class ItopDataTestCase extends ItopTestCase
$this->debug("Added contact $sContactId to {$oTicket->Get('ref')} with {$sRoleCode}");
return array($sContactId => $sRoleCode);
return [$sContactId => $sRoleCode];
}
protected function RemoveLnkContactToTicketObject($sContactId, $oTicket)
{
$oContacts = $oTicket->Get('contacts_list');
foreach ($oContacts as $oLnk)
{
if ($oLnk->Get('contact_id') == $sContactId)
{
foreach ($oContacts as $oLnk) {
if ($oLnk->Get('contact_id') == $sContactId) {
$sRoleCode = $oLnk->Get('role_code');
$oContacts->RemoveItem($oLnk->GetKey());
$oTicket->Set('contacts_list', $oContacts);
@@ -1436,10 +1406,10 @@ abstract class ItopDataTestCase extends ItopTestCase
'password' => 'tagada-Secret,007',
'language' => 'EN US',
'profile_list' => [
'profileid:'.$sProfileId
'profileid:'.$sProfileId,
],
'allowed_org_list' => [
'allowed_org_id:'.$iOrganization
'allowed_org_id:'.$iOrganization,
],
]);
return $sLogin;
@@ -1456,7 +1426,7 @@ abstract class ItopDataTestCase extends ItopTestCase
{
$sLogin = 'demo_test_'.uniqid(__CLASS__, true);
$aProfileList = array_map(function($sProfileId) {
$aProfileList = array_map(function ($sProfileId) {
return 'profileid:'.self::$aURP_Profiles[$sProfileId];
}, $aProfiles);
@@ -1495,16 +1465,14 @@ abstract class ItopDataTestCase extends ItopTestCase
protected function AssertUniqueObjectInDB(string $sClass, array $aCriteria, string $sMessage = ''): void
{
$oSearch = new \DBObjectSearch($sClass);
foreach($aCriteria as $sAttCode => $value)
{
foreach ($aCriteria as $sAttCode => $value) {
$oSearch->AddCondition($sAttCode, $value);
}
$oSet = new DBObjectSet($oSearch);
$this->assertEquals(1, $oSet->Count(), $sMessage);
}
static protected function StartStopwatchInThePast(DBObject $oObject, string $sStopwatchAttCode, int $iDelayInSecond)
protected static function StartStopwatchInThePast(DBObject $oObject, string $sStopwatchAttCode, int $iDelayInSecond)
{
$iStartDate = time() - $iDelayInSecond;
/** @var \ormStopWatch $oStopwatch */
@@ -1515,8 +1483,7 @@ abstract class ItopDataTestCase extends ItopTestCase
$oObject->Set($sStopwatchAttCode, $oStopwatch);
}
static protected function StopStopwatchInTheFuture(DBObject $oObject, string $sStopwatchAttCode, int $iDelayInSecond)
protected static function StopStopwatchInTheFuture(DBObject $oObject, string $sStopwatchAttCode, int $iDelayInSecond)
{
$iEndDate = time() + $iDelayInSecond;
/** @var \ormStopWatch $oStopwatch */