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

This commit is contained in:
odain
2025-11-07 15:39:53 +01:00
parent 12f23113f5
commit 890a2568c8
2110 changed files with 53099 additions and 63885 deletions

View File

@@ -1,4 +1,5 @@
<?php
/**
* Copyright (C) 2013-2024 Combodo SAS
* This file is part of iTop.
@@ -37,7 +38,7 @@ class DataSynchroTest extends ItopDataTestCase
{
protected const AUTH_USER = 'DataSynchroTest';
protected const AUTH_PWD = 'sdf234(-fgh;,dfgDFG';
const USE_TRANSACTION = false;
public const USE_TRANSACTION = false;
private $oOrg1;
private $oOrg2;
@@ -48,37 +49,36 @@ class DataSynchroTest extends ItopDataTestCase
// Create the login account if it does not exist yet
$oSearch = DBSearch::FromOQL('SELECT User WHERE login = "'.static::AUTH_USER.'"');
$oSet = new DBObjectSet($oSearch);
if ($oSet->Count() == 0)
{
if ($oSet->Count() == 0) {
$oProfileSearch = DBSearch::FromOQL('SELECT URP_Profiles WHERE id = 1');
$oProfileSet = new DBObjectSet($oProfileSearch);
$oAdminProfile = $oProfileSet->fetch();
$oUser = MetaModel::NewObject('UserLocal', array(
$oUser = MetaModel::NewObject('UserLocal', [
'login' => static::AUTH_USER,
'password' => static::AUTH_PWD,
'expiration' => UserLocal::EXPIRE_NEVER,
));
]);
$oProfiles = $oUser->Get('profile_list');
$oProfiles->AddItem(MetaModel::NewObject('URP_UserProfile', array(
'profileid' => $oAdminProfile->GetKey()
)));
$oProfiles->AddItem(MetaModel::NewObject('URP_UserProfile', [
'profileid' => $oAdminProfile->GetKey(),
]));
$oUser->Set('profile_list', $oProfiles);
$oUser->DBInsertNoReload();
}
/** @var \Organization $oOrganisation */
$sUniqueId1 = microtime();
$oOrganisation = $this->createObject('Organization', array(
'name' => 'datasynchro_org1_' . $sUniqueId1,
'code' => $sUniqueId1
));
$oOrganisation = $this->createObject('Organization', [
'name' => 'datasynchro_org1_'.$sUniqueId1,
'code' => $sUniqueId1,
]);
$this->oOrg1 = $oOrganisation;
$sUniqueId2 = microtime();
$oOrganisation2 = $this->createObject('Organization', array(
'name' => 'datasynchro_org2_' . $sUniqueId2,
'code' => $sUniqueId2
));
$oOrganisation2 = $this->createObject('Organization', [
'name' => 'datasynchro_org2_'.$sUniqueId2,
'code' => $sUniqueId2,
]);
$this->oOrg2 = $oOrganisation2;
}
@@ -94,7 +94,6 @@ class DataSynchroTest extends ItopDataTestCase
//$aParams['output'] = 'details';
$aParams['csvdata'] = file_get_contents($aParams['csvfile']);
$sUrl = \MetaModel::GetConfig()->Get('app_root_url').'/synchro/synchro_import.php?login_mode=form';
$sResult = utils::DoPostRequest($sUrl, $aParams, null, $aResponseHeaders, [
CURLOPT_SSL_VERIFYPEER => false,
@@ -104,7 +103,7 @@ class DataSynchroTest extends ItopDataTestCase
$aLines = explode("\n", trim(strip_tags($sResult)));
//$sLastLine = array_pop($aLines);
return array(0, $aLines);
return [0, $aLines];
}
/**
@@ -124,7 +123,7 @@ class DataSynchroTest extends ItopDataTestCase
$aSourceProperties = $aUserLoginUsecase['source_properties'];
$aSourceData = $aUserLoginUsecase['source_data'];
$aTargetData = $aUserLoginUsecase['target_data'];
$aAttributes =$aUserLoginUsecase['attributes'];
$aAttributes = $aUserLoginUsecase['attributes'];
$bSynchroByHttp = $aUserLoginUsecase['bSynchroByHttp'];
$sClass = $sTargetClass;
@@ -132,8 +131,7 @@ class DataSynchroTest extends ItopDataTestCase
$aTargetAttributes = array_shift($aTargetData);
$aSourceAttributes = array_shift($aSourceData);
if (count($aSourceData) + 1 != count($aTargetData))
{
if (count($aSourceData) + 1 != count($aTargetData)) {
throw new Exception("Target data must contain exactly ".(count($aSourceData) + 1)." items, found ".count($aTargetData));
}
@@ -145,27 +143,21 @@ class DataSynchroTest extends ItopDataTestCase
$oDataSource->Set('status', 'production');
$oDataSource->Set('user_id', 0);
$oDataSource->Set('scope_class', $sClass);
foreach ($aSourceProperties as $sProperty => $value)
{
foreach ($aSourceProperties as $sProperty => $value) {
$oDataSource->Set($sProperty, $value);
}
$iDataSourceId = $oDataSource->DBInsert();
$oAttributeSet = $oDataSource->Get('attribute_list');
while ($oAttribute = $oAttributeSet->Fetch())
{
if (array_key_exists($oAttribute->Get('attcode'), $aAttributes))
{
while ($oAttribute = $oAttributeSet->Fetch()) {
if (array_key_exists($oAttribute->Get('attcode'), $aAttributes)) {
$aAttribInfo = $aAttributes[$oAttribute->Get('attcode')];
if (array_key_exists('reconciliation_attcode', $aAttribInfo))
{
if (array_key_exists('reconciliation_attcode', $aAttribInfo)) {
$oAttribute->Set('reconciliation_attcode', $aAttribInfo['reconciliation_attcode']);
}
$oAttribute->Set('update', $aAttribInfo['do_update']);
$oAttribute->Set('reconcile', $aAttribInfo['do_reconcile']);
}
else
{
} else {
$oAttribute->Set('update', false);
$oAttribute->Set('reconcile', false);
}
@@ -173,15 +165,12 @@ class DataSynchroTest extends ItopDataTestCase
}
// Prepare list of prefixes -> make sure objects are unique with regard to the reconciliation scheme
$aPrefixes = array(); // attcode => prefix
foreach($aSourceAttributes as $iDummy => $sAttCode)
{
$aPrefixes = []; // attcode => prefix
foreach ($aSourceAttributes as $iDummy => $sAttCode) {
$aPrefixes[$sAttCode] = ''; // init with something
}
foreach($aAttributes as $sAttCode => $aAttribInfo)
{
if (isset($aAttribInfo['automatic_prefix']) && $aAttribInfo['automatic_prefix'])
{
foreach ($aAttributes as $sAttCode => $aAttribInfo) {
if (isset($aAttribInfo['automatic_prefix']) && $aAttribInfo['automatic_prefix']) {
$aPrefixes[$sAttCode] = 'TEST_'.$iDataSourceId.'_';
}
}
@@ -195,11 +184,9 @@ class DataSynchroTest extends ItopDataTestCase
// Create the initial object list
//
$aInitialTarget = $aTargetData[0];
foreach($aInitialTarget as $aObjFields)
{
foreach ($aInitialTarget as $aObjFields) {
$oNewTarget = MetaModel::NewObject($sClass);
foreach($aTargetAttributes as $iAtt => $sAttCode)
{
foreach ($aTargetAttributes as $iAtt => $sAttCode) {
$oNewTarget->Set($sAttCode, $aPrefixes[$sAttCode].$aObjFields[$iAtt]);
}
$oNewTarget->DBInsertNoReload();
@@ -207,47 +194,36 @@ class DataSynchroTest extends ItopDataTestCase
//add sleep to make sure expected objects will be found
usleep(10000);
foreach($aTargetData as $iRow => $aExpectedObjects)
{
foreach ($aTargetData as $iRow => $aExpectedObjects) {
// Check the status (while ignoring existing objects)
//
if (empty($sExistingIds))
{
if (empty($sExistingIds)) {
$oObjects = new DBObjectSet(DBObjectSearch::FromOQL("SELECT $sClass"));
}
else
{
} else {
$oObjects = new DBObjectSet(DBObjectSearch::FromOQL("SELECT $sClass WHERE id NOT IN($sExistingIds)"));
}
$aFound = $oObjects->ToArray();
$aErrors_Unexpected = array();
foreach($aFound as $iObj => $oObj)
{
$aErrors_Unexpected = [];
foreach ($aFound as $iObj => $oObj) {
// Is this object in the expected objects list
$bFoundMatch = false;
foreach($aExpectedObjects as $iExp => $aValues)
{
foreach ($aExpectedObjects as $iExp => $aValues) {
$bDoesMatch = true;
foreach($aTargetAttributes as $iCol => $sAttCode)
{
if ($oObj->Get($sAttCode) != $aPrefixes[$sAttCode].$aValues[$iCol])
{
foreach ($aTargetAttributes as $iCol => $sAttCode) {
if ($oObj->Get($sAttCode) != $aPrefixes[$sAttCode].$aValues[$iCol]) {
$bDoesMatch = false;
break;
}
}
if ($bDoesMatch)
{
if ($bDoesMatch) {
$bFoundMatch = true;
unset($aExpectedObjects[$iExp]);
break;
}
}
if (!$bFoundMatch)
{
$aObjDesc = array();
foreach($aTargetAttributes as $iCol => $sAttCode)
{
if (!$bFoundMatch) {
$aObjDesc = [];
foreach ($aTargetAttributes as $iCol => $sAttCode) {
$aObjDesc[$sAttCode] = $oObj->Get($sAttCode);
}
$aErrors_Unexpected[get_class($oObj).'::'.$oObj->GetKey()] = $aObjDesc;
@@ -256,7 +232,7 @@ class DataSynchroTest extends ItopDataTestCase
// Display the current status
//
$aErrors = array();
$aErrors = [];
if (count($aErrors_Unexpected) > 0) {
$aErrors[] = "Unexpected objects found in iTop DB after step $iRow (starting at 0):\n".print_r($aErrors_Unexpected, true);
}
@@ -265,15 +241,14 @@ class DataSynchroTest extends ItopDataTestCase
}
if (count($aErrors) > 0) {
$sAdditionalInfo = (isset($sResultsViewable)) ? $sResultsViewable : "";
static::fail(implode("\n", $aErrors) . "\n $sAdditionalInfo");
static::fail(implode("\n", $aErrors)."\n $sAdditionalInfo");
} else {
static::assertTrue(true);
}
// If not on the final row, run a data exchange sequence
//
if (array_key_exists($iRow, $aSourceData))
{
if (array_key_exists($iRow, $aSourceData)) {
$aToBeLoaded = $aSourceData[$iRow];
// First line
@@ -281,16 +256,14 @@ class DataSynchroTest extends ItopDataTestCase
$sTextQualifier = '"';
foreach($aToBeLoaded as $aDataRow)
{
$aFinalData = array();
foreach($aDataRow as $iCol => $value)
{
foreach ($aToBeLoaded as $aDataRow) {
$aFinalData = [];
foreach ($aDataRow as $iCol => $value) {
$sAttCode = $aSourceAttributes[$iCol];
$sRawValue = $aPrefixes[$sAttCode].$value;
$sFrom = array("\r\n", $sTextQualifier);
$sTo = array("\n", $sTextQualifier.$sTextQualifier);
$sFrom = ["\r\n", $sTextQualifier];
$sTo = ["\n", $sTextQualifier.$sTextQualifier];
$sCSVValue = $sTextQualifier.str_replace($sFrom, $sTo, (string)$sRawValue).$sTextQualifier;
$aFinalData[] = $sCSVValue;
@@ -300,25 +273,22 @@ class DataSynchroTest extends ItopDataTestCase
$sCSVTmpFile = tempnam(sys_get_temp_dir(), "CSV");
file_put_contents($sCSVTmpFile, $sCsvData);
$aParams = array(
$aParams = [
'csvfile' => $sCSVTmpFile,
'data_source_id' => $iDataSourceId,
'separator' => ';',
'simulate' => 0,
'output' => 'details',
);
];
list($iRetCode, $aOutputLines) = static::ExecSynchroImport($aParams, $bSynchroByHttp);
unlink($sCSVTmpFile);
// Report the load results
//
if (strlen($sCsvData) > 5000)
{
if (strlen($sCsvData) > 5000) {
$sCsvDataViewable = 'INPUT TOO LONG TO BE DISPLAYED ('.strlen($sCsvData).")\n".substr($sCsvData, 0, 500)."\n... TO BE CONTINUED";
}
else
{
} else {
$sCsvDataViewable = $sCsvData;
}
echo "Input Data:\n";
@@ -331,34 +301,32 @@ class DataSynchroTest extends ItopDataTestCase
echo $sResultsViewable;
echo "\n";
if ($iRetCode != 0)
{
if ($iRetCode != 0) {
static::fail("Execution of synchro_import failing with code '$iRetCode', see error.log for more details");
}
if (stripos($sResultsViewable, 'exception') !== false)
{
if (stripos($sResultsViewable, 'exception') !== false) {
self::fail('Encountered an Exception during the last import/synchro');
}
$aKeys = ["creation", "update", "deletion"];
foreach ($aKeys as $sKey){
$this->assertStringContainsString("$sKey errors: 0", $sResultsViewable, "step $iRow : below res should contain '$sKey errors: 0': " . $sResultsViewable);
foreach ($aKeys as $sKey) {
$this->assertStringContainsString("$sKey errors: 0", $sResultsViewable, "step $iRow : below res should contain '$sKey errors: 0': ".$sResultsViewable);
}
//N°3805 : potential javascript returned like
/*
Please wait...
Please wait...
var aListJsFiles = [];
$(document).ready(function () {
setTimeout(function () {
}, 50);
});
$(document).ready(function () {
setTimeout(function () {
}, 50);
});
*/
$sLastExpectedLine = "#Replica disappeared, no action taken: 0";
$aSplittedRes = explode($sLastExpectedLine, $sResultsViewable);
$this->assertNotFalse($aSplittedRes);
if (count($aSplittedRes)>1){
if (count($aSplittedRes) > 1) {
$sPotentialIssuesWithWebApplication = $aSplittedRes[1];
$this->assertEquals("", $sPotentialIssuesWithWebApplication, 'when failed it means data synchro result is polluted with some web application stuff like html or js');
}
@@ -368,11 +336,12 @@ class DataSynchroTest extends ItopDataTestCase
return $oDataSource;
}
private function GetNominalUsecaseData(){
return array(
private function GetNominalUsecaseData()
{
return [
'desc' => 'Load user logins',
'target_class' => 'UserLocal',
'source_properties' => array(
'source_properties' => [
'full_load_periodicity' => 3600, // should be ignored in this case
'reconciliation_policy' => 'use_attributes',
'action_on_zero' => 'create',
@@ -381,52 +350,55 @@ class DataSynchroTest extends ItopDataTestCase
'delete_policy' => 'delete',
'delete_policy_update' => '',
'delete_policy_retention' => 0,
),
'source_data' => array(
array('primary_key', 'login', 'password', 'profile_list'),
array(
array('user_A', 'login_A', 'password_A', 'profileid:10;reason:he/she is managing services'),
),
),
'target_data' => array(
array('login'), //columns
array(
],
'source_data' => [
['primary_key', 'login', 'password', 'profile_list'],
[
['user_A', 'login_A', 'password_A', 'profileid:10;reason:he/she is managing services'],
],
],
'target_data' => [
['login'], //columns
[
// Initial state
),
array(
array('login_A'), //expected values
),
),
'attributes' => array(
'login' => array(
],
[
['login_A'], //expected values
],
],
'attributes' => [
'login' => [
'do_reconcile' => true,
'do_update' => true,
'automatic_prefix' => true, // unique id (for unit testing)
),
'password' => array(
],
'password' => [
'do_reconcile' => false,
'do_update' => true,
),
'profile_list' => array(
],
'profile_list' => [
'do_reconcile' => false,
'do_update' => true,
),
),
'bSynchroByHttp' => false
);
],
],
'bSynchroByHttp' => false,
];
}
public function testDataSynchroByCli(){
public function testDataSynchroByCli()
{
$this->RunDataSynchroTest($this->GetNominalUsecaseData());
}
public function testDataSynchroByHttp(){
public function testDataSynchroByHttp()
{
$aUserLoginUsecase = $this->GetNominalUsecaseData();
$aUserLoginUsecase['bSynchroByHttp'] = true;
$this->RunDataSynchroTest($aUserLoginUsecase);
}
public function testDataSynchroDeletionCleanup(){
public function testDataSynchroDeletionCleanup()
{
// run test data synchro
$oDataSource = $this->RunDataSynchroTest($this->GetNominalUsecaseData());
@@ -584,7 +556,6 @@ class DataSynchroTest extends ItopDataTestCase
$this->RunDataSynchroTest($aUserLoginUsecase);
}*/
/*public function testLoadingApplicationSolution(){
$aUserLoginUsecase= array(
'desc' => 'Simple scenario loading a few ApplicationSolution',