Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<?php

/*
* @copyright Copyright (C) 2010-2023 Combodo SARL
* @license http://opensource.org/licenses/AGPL-3.0
*/

declare(strict_types = 1);
declare(strict_types=1);

/**
* This file is only here to allow setting a specific PHP version to run the analysis for without
Expand All @@ -21,4 +22,4 @@
$config = [];
$config['parameters']['phpVersion'] = PHP_VERSION_ID;

return $config;
return $config;
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ public function testCompileOperation()
$iLastCompilation = filemtime(APPROOT.'env-production');

// When
$sOutput = $this->CallItopUrl(
"/pages/exec.php?exec_module=itop-hub-connector&exec_page=ajax.php",
$sOutput = $this->CallItopUri(
"pages/exec.php?exec_module=itop-hub-connector&exec_page=ajax.php",
[
'auth_user' => $sLogin,
'auth_pwd' => self::AUTHENTICATION_PASSWORD,
'operation' => "compile",
'authent' => self::AUTHENTICATION_TOKEN,
]
],
);

// Then
Expand All @@ -53,26 +53,4 @@ public function testCompileOperation()
clearstatcache();
$this->assertGreaterThan($iLastCompilation, filemtime(APPROOT.'env-production'), 'The env-production directory should have been rebuilt');
}

protected function CallItopUrl($sUri, ?array $aPostFields = null, bool $bXDebugEnabled = false)
{
$ch = curl_init();
if ($bXDebugEnabled) {
curl_setopt($ch, CURLOPT_COOKIE, 'XDEBUG_SESSION=phpstorm');
}

$sUrl = \MetaModel::GetConfig()->Get('app_root_url')."/$sUri";
var_dump($sUrl);
curl_setopt($ch, CURLOPT_URL, $sUrl);
curl_setopt($ch, CURLOPT_POST, 1);// set post data to true
curl_setopt($ch, CURLOPT_POSTFIELDS, $aPostFields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$sOutput = curl_exec($ch);
//echo "$sUrl error code:".curl_error($ch);
curl_close($ch);

return $sOutput;
}
}
49 changes: 16 additions & 33 deletions tests/php-unit-tests/legacy-tests/display_cache_content.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Copyright (C) 2013-2024 Combodo SAS
*
Expand All @@ -24,72 +25,55 @@
require_once('../../../approot.inc.php');
require_once(APPROOT.'application/startup.inc.php');


$sEnvironment = MetaModel::GetEnvironmentId();
$aEntries = array();
$aEntries = [];
$aCacheUserData = apc_cache_info_compat();
if (is_array($aCacheUserData) && isset($aCacheUserData['cache_list']))
{
if (is_array($aCacheUserData) && isset($aCacheUserData['cache_list'])) {
$sPrefix = 'itop-'.$sEnvironment.'-query-cache-';

foreach($aCacheUserData['cache_list'] as $i => $aEntry)
{
foreach ($aCacheUserData['cache_list'] as $i => $aEntry) {
$sEntryKey = array_key_exists('info', $aEntry) ? $aEntry['info'] : $aEntry['key'];
if (strpos($sEntryKey, $sPrefix) === 0)
{
if (strpos($sEntryKey, $sPrefix) === 0) {
$aEntries[] = $sEntryKey;
}
}
}

echo "<pre>";

if (empty($aEntries))
{
if (empty($aEntries)) {
echo "No Data";
return;
}

$sKey = $aEntries[0];
$result = apc_fetch($sKey);
if (!is_object($result))
{
if (!is_object($result)) {
return;
}
$oSQLQuery = $result;

echo "NB Tables before;NB Tables after;";
foreach($oSQLQuery->m_aContextData as $sField => $oValue)
{
foreach ($oSQLQuery->m_aContextData as $sField => $oValue) {
echo $sField.';';
}
echo "\n";

sort($aEntries);

foreach($aEntries as $sKey)
{
foreach ($aEntries as $sKey) {
$result = apc_fetch($sKey);
if (is_object($result))
{
if (is_object($result)) {
$oSQLQuery = $result;
if (isset($oSQLQuery->m_aContextData))
{
if (isset($oSQLQuery->m_aContextData)) {
echo $oSQLQuery->m_iOriginalTableCount.";".$oSQLQuery->CountTables().';';
foreach($oSQLQuery->m_aContextData as $oValue)
{
if (is_array($oValue))
{
foreach ($oSQLQuery->m_aContextData as $oValue) {
if (is_array($oValue)) {
$sVal = json_encode($oValue);
}
else
{
if (empty($oValue))
{
} else {
if (empty($oValue)) {
$sVal = '';
}
else
{
} else {
$sVal = $oValue;
}
}
Expand All @@ -101,4 +85,3 @@
}

echo "</pre>";

39 changes: 39 additions & 0 deletions tests/php-unit-tests/src/BaseTestCase/ItopDataTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use CMDBObject;
use CMDBSource;
use Combodo\iTop\Service\Events\EventService;
use Config;
use Contact;
use DBObject;
use DBObjectSet;
Expand Down Expand Up @@ -65,6 +66,9 @@ abstract class ItopDataTestCase extends ItopTestCase
private $aCreatedObjects = [];
private $aEventListeners = [];

protected ?string $sConfigTmpBackupFile = null;
protected ?Config $oiTopConfig = null;

/**
* @var bool When testing with silo, there are some cache we need to update on tearDown. Doing it all the time will cost too much, so it's opt-in !
* @see tearDown
Expand Down Expand Up @@ -119,6 +123,8 @@ protected function setUp(): void
{
parent::setUp();

\IssueLog::Error($this->getName());

$this->PrepareEnvironment();

if (static::USE_TRANSACTION) {
Expand Down Expand Up @@ -185,6 +191,8 @@ protected function tearDown(): void

CMDBObject::SetCurrentChange(null);

$this->RestoreConfiguration();

parent::tearDown();
}

Expand Down Expand Up @@ -1434,4 +1442,35 @@ protected static function StopStopwatchInTheFuture(DBObject $oObject, string $sS
$oObject->Set($sStopwatchAttCode, $oStopwatch);
}

protected function BackupConfiguration(): void
{
$sConfigPath = MetaModel::GetConfig()->GetLoadedFile();
clearstatcache();
echo sprintf("rights via ls on %s:\n %s \n", $sConfigPath, exec("ls -al $sConfigPath"));
$sFilePermOutput = substr(sprintf('%o', fileperms('/etc/passwd')), -4);
echo sprintf("rights via fileperms on %s:\n %s \n", $sConfigPath, $sFilePermOutput);

$this->sConfigTmpBackupFile = tempnam(sys_get_temp_dir(), "config_");
MetaModel::GetConfig()->WriteToFile($this->sConfigTmpBackupFile);
$this->oiTopConfig = new Config($sConfigPath);
}

protected function RestoreConfiguration(): void
{
if (is_null($this->sConfigTmpBackupFile) || ! is_file($this->sConfigTmpBackupFile)) {
return;
}

if (is_null($this->oiTopConfig)) {
return;
}

//put config back
$sConfigPath = $this->oiTopConfig->GetLoadedFile();
@chmod($sConfigPath, 0770);
$oConfig = new Config($this->sConfigTmpBackupFile);
$oConfig->WriteToFile($sConfigPath);
@chmod($sConfigPath, 0440);
@unlink($this->sConfigTmpBackupFile);
}
}
62 changes: 60 additions & 2 deletions tests/php-unit-tests/src/BaseTestCase/ItopTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
use CMDBSource;
use DeprecatedCallsLog;
use MySQLTransactionNotClosedException;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use ReflectionMethod;
use SetupUtils;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\HttpKernel\KernelInterface;

use const DEBUG_BACKTRACE_IGNORE_ARGS;
Expand All @@ -28,6 +28,7 @@
abstract class ItopTestCase extends KernelTestCase
{
public const TEST_LOG_DIR = 'test';
protected array $aFileToClean = [];

/**
* @var bool
Expand All @@ -36,7 +37,7 @@ abstract class ItopTestCase extends KernelTestCase
public const DISABLE_DEPRECATEDCALLSLOG_ERRORHANDLER = true;
public static $DEBUG_UNIT_TEST = false;
protected static $aBackupStaticProperties = [];

public ?array $aLastCurlGetInfo = null;
/**
* @link https://docs.phpunit.de/en/9.6/annotations.html#preserveglobalstate PHPUnit `preserveGlobalState` annotation documentation
*
Expand Down Expand Up @@ -174,6 +175,15 @@ protected function tearDown(): void
}
throw new MySQLTransactionNotClosedException('Some DB transactions were opened but not closed ! Fix the code by adding ROLLBACK or COMMIT statements !', []);
}

foreach ($this->aFileToClean as $sPath) {
if (is_file($sPath)) {
@unlink($sPath);
continue;
}

SetupUtils::tidydir($sPath);
}
}

/**
Expand Down Expand Up @@ -631,4 +641,52 @@ protected static function ReadTail($sFilename, $iLines = 1)
fclose($handle);
return array_reverse($aLines);
}

/**
* @param $sUrl
* @param array|null $aPostFields
* @param array|null $aCurlOptions
* @param $bXDebugEnabled
* @return string
*/
protected function CallItopUrl($sUrl, ?array $aPostFields = [], ?array $aCurlOptions = [], $bXDebugEnabled = false): string
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@odain-cbd This can call any URL, not just iTop's? In that case we might want to rename it don't you think? Currently the difference between the 2 methods isn't obvious to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right. i will rename it.

{
$ch = curl_init();
if ($bXDebugEnabled) {
curl_setopt($ch, CURLOPT_COOKIE, "XDEBUG_SESSION=phpstorm");
}

curl_setopt($ch, CURLOPT_URL, $sUrl);
curl_setopt($ch, CURLOPT_POST, 1);// set post data to true
if (!is_array($aPostFields)) {
var_dump($aPostFields);
}
curl_setopt($ch, CURLOPT_POSTFIELDS, $aPostFields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Force disable of certificate check as most of dev / test env have a self-signed certificate
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);

var_dump($aCurlOptions);
curl_setopt_array($ch, $aCurlOptions);

$sOutput = curl_exec($ch);
//\IssueLog::Info("$sUrl error code:", null, ['error' => curl_error($ch)]);

$info = curl_getinfo($ch);
$this->aLastCurlGetInfo = $info;
$sErrorMsg = curl_error($ch);
$iErrorCode = curl_errno($ch);
curl_close($ch);

\IssueLog::Info(__METHOD__, null, ['url' => $sUrl, 'error' => $sErrorMsg, 'error_code' => $iErrorCode, 'post_fields' => $aPostFields, 'info' => $info]);

return $sOutput;
}

protected function CallItopUri(string $sUri, ?array $aPostFields = [], ?array $aCurlOptions = [], $bXDebugEnabled = false): string
{
$sUrl = \MetaModel::GetConfig()->Get('app_root_url')."/$sUri";
return $this->CallItopUrl($sUrl, $aPostFields, $aCurlOptions, $bXDebugEnabled);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

class LoginTest extends ItopDataTestCase
{
protected $sConfigTmpBackupFile;
protected $sConfigPath;
protected $sLoginMode;

Expand Down
33 changes: 5 additions & 28 deletions tests/php-unit-tests/unitary-tests/application/query/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,34 +172,11 @@ private function CallExportService(Query $oQuery)
{
// compute request url
$url = $oQuery->GetExportUrl();

// open curl
$curl = curl_init();

// curl options
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, self::USER.':'.self::PASSWORD);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
// Force disable of certificate check as most of dev / test env have a self-signed certificate
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);

// execute curl
$result = curl_exec($curl);
if (curl_errno($curl)) {
$info = curl_getinfo($curl);
var_export($info);
var_dump([
'url' => $url,
'app_root_url:' => MetaModel::GetConfig()->Get('app_root_url'),
'GetAbsoluteUrlAppRoot:' => \utils::GetAbsoluteUrlAppRoot(),
]);
}
// close curl
curl_close($curl);

return $result;
$aCurlOptions = [
CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
CURLOPT_USERPWD => self::USER.':'.self::PASSWORD,
];
return $this->CallItopUrl($url, [], $aCurlOptions);
}

/** @inheritDoc */
Expand Down
Loading