Skip to content

Commit a827857

Browse files
committed
add few APIs to ease tests
1 parent cef4a52 commit a827857

File tree

6 files changed

+119
-103
lines changed

6 files changed

+119
-103
lines changed

tests/php-unit-tests/integration-tests/itop-hub-connector/AjaxPageTest.php

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@ public function testCompileOperation()
2828
$iLastCompilation = filemtime(APPROOT.'env-production');
2929

3030
// When
31-
$sOutput = $this->CallItopUrl(
32-
"/pages/exec.php?exec_module=itop-hub-connector&exec_page=ajax.php",
31+
$sOutput = $this->CallItopUri(
32+
"pages/exec.php?exec_module=itop-hub-connector&exec_page=ajax.php",
3333
[
3434
'auth_user' => $sLogin,
3535
'auth_pwd' => self::AUTHENTICATION_PASSWORD,
3636
'operation' => "compile",
3737
'authent' => self::AUTHENTICATION_TOKEN,
38-
]
38+
],
3939
);
4040

4141
// Then
@@ -53,26 +53,4 @@ public function testCompileOperation()
5353
clearstatcache();
5454
$this->assertGreaterThan($iLastCompilation, filemtime(APPROOT.'env-production'), 'The env-production directory should have been rebuilt');
5555
}
56-
57-
protected function CallItopUrl($sUri, ?array $aPostFields = null, bool $bXDebugEnabled = false)
58-
{
59-
$ch = curl_init();
60-
if ($bXDebugEnabled) {
61-
curl_setopt($ch, CURLOPT_COOKIE, 'XDEBUG_SESSION=phpstorm');
62-
}
63-
64-
$sUrl = \MetaModel::GetConfig()->Get('app_root_url')."/$sUri";
65-
var_dump($sUrl);
66-
curl_setopt($ch, CURLOPT_URL, $sUrl);
67-
curl_setopt($ch, CURLOPT_POST, 1);// set post data to true
68-
curl_setopt($ch, CURLOPT_POSTFIELDS, $aPostFields);
69-
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
70-
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
71-
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
72-
$sOutput = curl_exec($ch);
73-
//echo "$sUrl error code:".curl_error($ch);
74-
curl_close($ch);
75-
76-
return $sOutput;
77-
}
7856
}

tests/php-unit-tests/src/BaseTestCase/ItopDataTestCase.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use CMDBObject;
1919
use CMDBSource;
2020
use Combodo\iTop\Service\Events\EventService;
21+
use Config;
2122
use Contact;
2223
use CoreException;
2324
use CoreUnexpectedValue;
@@ -70,6 +71,9 @@ abstract class ItopDataTestCase extends ItopTestCase
7071
private $aCreatedObjects = [];
7172
private $aEventListeners = [];
7273

74+
protected ?string $sConfigTmpBackupFile = null;
75+
protected ?Config $oiTopConfig = null;
76+
7377
/**
7478
* @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 !
7579
* @see tearDown
@@ -190,6 +194,8 @@ protected function tearDown(): void
190194

191195
CMDBObject::SetCurrentChange(null);
192196

197+
$this->RestoreConfiguration();
198+
193199
parent::tearDown();
194200
}
195201

@@ -1517,4 +1523,35 @@ protected static function StopStopwatchInTheFuture(DBObject $oObject, string $sS
15171523
$oObject->Set($sStopwatchAttCode, $oStopwatch);
15181524
}
15191525

1526+
protected function BackupConfiguration(): void
1527+
{
1528+
$sConfigPath = MetaModel::GetConfig()->GetLoadedFile();
1529+
clearstatcache();
1530+
echo sprintf("rights via ls on %s:\n %s \n", $sConfigPath, exec("ls -al $sConfigPath"));
1531+
$sFilePermOutput = substr(sprintf('%o', fileperms('/etc/passwd')), -4);
1532+
echo sprintf("rights via fileperms on %s:\n %s \n", $sConfigPath, $sFilePermOutput);
1533+
1534+
$this->sConfigTmpBackupFile = tempnam(sys_get_temp_dir(), "config_");
1535+
MetaModel::GetConfig()->WriteToFile($this->sConfigTmpBackupFile);
1536+
$this->oiTopConfig = new Config($sConfigPath);
1537+
}
1538+
1539+
protected function RestoreConfiguration(): void
1540+
{
1541+
if (is_null($this->sConfigTmpBackupFile) || ! is_file($this->sConfigTmpBackupFile)) {
1542+
return;
1543+
}
1544+
1545+
if (is_null($this->oiTopConfig)) {
1546+
return;
1547+
}
1548+
1549+
//put config back
1550+
$sConfigPath = $this->oiTopConfig->GetLoadedFile();
1551+
@chmod($sConfigPath, 0770);
1552+
$oConfig = new Config($this->sConfigTmpBackupFile);
1553+
$oConfig->WriteToFile($sConfigPath);
1554+
@chmod($sConfigPath, 0440);
1555+
@unlink($this->sConfigTmpBackupFile);
1556+
}
15201557
}

tests/php-unit-tests/src/BaseTestCase/ItopTestCase.php

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
abstract class ItopTestCase extends KernelTestCase
3030
{
3131
public const TEST_LOG_DIR = 'test';
32+
protected array $aFileToClean = [];
3233

3334
/**
3435
* @var bool
@@ -37,7 +38,7 @@ abstract class ItopTestCase extends KernelTestCase
3738
public const DISABLE_DEPRECATEDCALLSLOG_ERRORHANDLER = true;
3839
public static $DEBUG_UNIT_TEST = false;
3940
protected static $aBackupStaticProperties = [];
40-
41+
public ?array $aLastCurlGetInfo=null;
4142
/**
4243
* @link https://docs.phpunit.de/en/9.6/annotations.html#preserveglobalstate PHPUnit `preserveGlobalState` annotation documentation
4344
*
@@ -149,6 +150,7 @@ public static function ExportFunctionParameterValues(array $args, string $sExpor
149150
protected function setUp(): void
150151
{
151152
parent::setUp();
153+
\IssueLog::Error($this->getName());
152154

153155
// Hack - Required the first time the Portal kernel is booted on a newly installed iTop
154156
$_ENV['COMBODO_PORTAL_BASE_ABSOLUTE_PATH'] = __DIR__.'/../../../../../env-production/itop-portal-base/portal/public/';
@@ -175,6 +177,15 @@ protected function tearDown(): void
175177
}
176178
throw new MySQLTransactionNotClosedException('Some DB transactions were opened but not closed ! Fix the code by adding ROLLBACK or COMMIT statements !', []);
177179
}
180+
181+
foreach ($this->aFileToClean as $sPath) {
182+
if (is_file($sPath)) {
183+
@unlink($sPath);
184+
continue;
185+
}
186+
187+
SetupUtils::tidydir($sPath);
188+
}
178189
}
179190

180191
/**
@@ -631,4 +642,52 @@ protected static function ReadTail($sFilename, $iLines = 1)
631642
fclose($handle);
632643
return array_reverse($aLines);
633644
}
645+
646+
/**
647+
* @param $sUrl
648+
* @param array|null $aPostFields
649+
* @param array|null $aCurlOptions
650+
* @param $bXDebugEnabled
651+
* @return string
652+
*/
653+
protected function CallItopUrl($sUrl, ?array $aPostFields = [], ?array $aCurlOptions = [], $bXDebugEnabled = false): string
654+
{
655+
$ch = curl_init();
656+
if ($bXDebugEnabled) {
657+
curl_setopt($ch, CURLOPT_COOKIE, "XDEBUG_SESSION=phpstorm");
658+
}
659+
660+
curl_setopt($ch, CURLOPT_URL, $sUrl);
661+
curl_setopt($ch, CURLOPT_POST, 1);// set post data to true
662+
if (!is_array($aPostFields)) {
663+
var_dump($aPostFields);
664+
}
665+
curl_setopt($ch, CURLOPT_POSTFIELDS, $aPostFields);
666+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
667+
// Force disable of certificate check as most of dev / test env have a self-signed certificate
668+
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
669+
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
670+
671+
var_dump($aCurlOptions);
672+
curl_setopt_array($ch, $aCurlOptions);
673+
674+
$sOutput = curl_exec($ch);
675+
//\IssueLog::Info("$sUrl error code:", null, ['error' => curl_error($ch)]);
676+
677+
$info = curl_getinfo($ch);
678+
$this->aLastCurlGetInfo=$info;
679+
$sErrorMsg = curl_error($ch);
680+
$iErrorCode = curl_errno($ch);
681+
curl_close($ch);
682+
683+
\IssueLog::Info(__METHOD__, null, ['url' => $sUrl, 'error' => $sErrorMsg, 'error_code' => $iErrorCode, 'post_fields' => $aPostFields, 'info' => $info]);
684+
685+
return $sOutput;
686+
}
687+
688+
protected function CallItopUri(string $sUri, ?array $aPostFields = [], ?array $aCurlOptions = [], $bXDebugEnabled = false): string
689+
{
690+
$sUrl = \MetaModel::GetConfig()->Get('app_root_url')."/$sUri";
691+
return $this->CallItopUrl($sUrl, $aPostFields, $aCurlOptions, $bXDebugEnabled);
692+
}
634693
}

tests/php-unit-tests/unitary-tests/application/query/QueryTest.php

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -143,34 +143,11 @@ private function CallExportService(Query $oQuery)
143143
{
144144
// compute request url
145145
$url = $oQuery->GetExportUrl();
146-
147-
// open curl
148-
$curl = curl_init();
149-
150-
// curl options
151-
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
152-
curl_setopt($curl, CURLOPT_USERPWD, self::USER.':'.self::PASSWORD);
153-
curl_setopt($curl, CURLOPT_URL, $url);
154-
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
155-
// Force disable of certificate check as most of dev / test env have a self-signed certificate
156-
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
157-
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
158-
159-
// execute curl
160-
$result = curl_exec($curl);
161-
if (curl_errno($curl)) {
162-
$info = curl_getinfo($curl);
163-
var_export($info);
164-
var_dump([
165-
'url' => $url,
166-
'app_root_url:' => MetaModel::GetConfig()->Get('app_root_url'),
167-
'GetAbsoluteUrlAppRoot:' => \utils::GetAbsoluteUrlAppRoot(),
168-
]);
169-
}
170-
// close curl
171-
curl_close($curl);
172-
173-
return $result;
146+
$aCurlOptions = [
147+
CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
148+
CURLOPT_USERPWD => self::USER.':'.self::PASSWORD,
149+
];
150+
return $this->CallItopUrl($url, [], $aCurlOptions);
174151
}
175152

176153
/** @inheritDoc */

tests/php-unit-tests/unitary-tests/webservices/CliResetSessionTest.php

Lines changed: 13 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,8 @@ class CliResetSessionTest extends ItopDataTestCase
1212
public const USE_TRANSACTION = false;
1313

1414
private $sCookieFile = "";
15-
private $sUrl;
1615
private $sLogin;
1716
private $sPassword = "Iuytrez9876543ç_è-(";
18-
protected $sConfigTmpBackupFile;
1917

2018
/**
2119
* @throws Exception
@@ -24,16 +22,13 @@ protected function setUp(): void
2422
{
2523
parent::setUp();
2624

27-
$this->sConfigTmpBackupFile = tempnam(sys_get_temp_dir(), "config_");
28-
MetaModel::GetConfig()->WriteToFile($this->sConfigTmpBackupFile);
25+
$this->BackupConfiguration();
2926

3027
$this->sLogin = "rest-user-".date('dmYHis');
3128
$this->CreateTestOrganization();
3229

3330
$this->sCookieFile = tempnam(sys_get_temp_dir(), 'jsondata_');
3431

35-
$this->sUrl = \MetaModel::GetConfig()->Get('app_root_url');
36-
3732
$oRestProfile = \MetaModel::GetObjectFromOQL("SELECT URP_Profiles WHERE name = :name", ['name' => 'REST Services User'], true);
3833
$oAdminProfile = \MetaModel::GetObjectFromOQL("SELECT URP_Profiles WHERE name = :name", ['name' => 'Administrator'], true);
3934

@@ -47,16 +42,6 @@ protected function tearDown(): void
4742
{
4843
parent::tearDown();
4944

50-
if (! is_null($this->sConfigTmpBackupFile) && is_file($this->sConfigTmpBackupFile)) {
51-
//put config back
52-
$sConfigPath = MetaModel::GetConfig()->GetLoadedFile();
53-
@chmod($sConfigPath, 0770);
54-
$oConfig = new Config($this->sConfigTmpBackupFile);
55-
$oConfig->WriteToFile($sConfigPath);
56-
@chmod($sConfigPath, 0444);
57-
unlink($this->sConfigTmpBackupFile);
58-
}
59-
6045
if (!empty($this->sCookieFile)) {
6146
unlink($this->sCookieFile);
6247
}
@@ -150,43 +135,34 @@ public function testVariousWebServices($sUri)
150135
*/
151136
private function SendHTTPRequestWithCookies($sUri, $aPostFields, $sForcedLoginMode = null): string
152137
{
153-
$ch = curl_init();
154-
155-
curl_setopt($ch, CURLOPT_COOKIEJAR, $this->sCookieFile);
156-
curl_setopt($ch, CURLOPT_COOKIEFILE, $this->sCookieFile);
157-
158-
$sUrl = "$this->sUrl/$sUri";
159138
if (!is_null($sForcedLoginMode)) {
160-
$sUrl .= "?login_mode=$sForcedLoginMode";
139+
$sUri .= "?login_mode=$sForcedLoginMode";
161140
}
162-
curl_setopt($ch, CURLOPT_URL, $sUrl);
163-
curl_setopt($ch, CURLOPT_POST, 1);// set post data to true
164-
curl_setopt($ch, CURLOPT_POSTFIELDS, $aPostFields);
165-
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
166-
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
167-
curl_setopt($ch, CURLOPT_HEADER, 1);
168-
// Force disable of certificate check as most of dev / test env have a self-signed certificate
169-
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
170-
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
171-
172-
$sResponse = curl_exec($ch);
141+
142+
$aCurlOptions = [
143+
CURLOPT_COOKIEJAR => $this->sCookieFile,
144+
CURLOPT_COOKIEFILE => $this->sCookieFile,
145+
CURLOPT_HEADER => 1,
146+
];
147+
148+
$sResponse = $this->CallItopUri($sUri, $aPostFields, $aCurlOptions);
149+
var_dump($this->aLastCurlGetInfo);
173150
/** $sResponse example
174151
* "HTTP/1.1 200 OK
175152
Date: Wed, 07 Jun 2023 05:00:40 GMT
176153
Server: Apache/2.4.29 (Ubuntu)
177154
Set-Cookie: itop-2e83d2e9b00e354fdc528621cac532ac=q7ldcjq0rvbn33ccr9q8u8e953; path=/
178155
*/
179156
//var_dump($sResponse);
180-
$iHeaderSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
157+
$iHeaderSize = $this->aLastCurlGetInfo['header_size'] ?? 0;
181158
$sBody = substr($sResponse, $iHeaderSize);
182159

183160
//$iHttpCode = intval(curl_getinfo($ch, CURLINFO_HTTP_CODE));
184161
if (preg_match('/HTTP.* (\d*) /', $sResponse, $aMatches)) {
185162
$sHttpCode = $aMatches[1];
186163
} else {
187-
$sHttpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
164+
$sHttpCode = $this->aLastCurlGetInfo['http_code'] ?? -1;
188165
}
189-
curl_close($ch);
190166

191167
$this->assertEquals(200, $sHttpCode, "The test logic assumes that the HTTP request is correctly handled");
192168
return $sBody;

tests/php-unit-tests/unitary-tests/webservices/RestTest.php

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ class RestTest extends ItopDataTestCase
1717
public const USE_TRANSACTION = false;
1818
public const CREATE_TEST_ORG = false;
1919

20-
private static $sUrl;
2120
private static $sLogin;
2221
private static $sPassword = "Iuytrez9876543ç_è-(";
2322

@@ -44,7 +43,6 @@ protected function setUp(): void
4443
{
4544
parent::setUp();
4645

47-
static::$sUrl = MetaModel::GetConfig()->Get('app_root_url');
4846
static::$sLogin = "rest-user-".date('dmYHis');
4947

5048
$this->CreateTestOrganization();
@@ -297,16 +295,7 @@ private function CallRestApi_HTTP(string $sJsonDataContent = null, string $sCall
297295
$aPostFields['callback'] = $sCallbackName;
298296
}
299297

300-
curl_setopt($ch, CURLOPT_URL, static::$sUrl."/webservices/rest.php");
301-
curl_setopt($ch, CURLOPT_POST, 1);// set post data to true
302-
curl_setopt($ch, CURLOPT_POSTFIELDS, $aPostFields);
303-
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
304-
// Force disable of certificate check as most of dev / test env have a self-signed certificate
305-
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
306-
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
307-
308-
$sJson = curl_exec($ch);
309-
curl_close($ch);
298+
$sJson = $this->CallItopUri('webservices/rest.php', $aPostFields);
310299

311300
if (!is_null($sTmpFile)) {
312301
unlink($sTmpFile);

0 commit comments

Comments
 (0)