Merge branch 'support/3.2' into develop

This commit is contained in:
odain
2025-11-07 20:33:14 +01:00
1837 changed files with 33034 additions and 34549 deletions

View File

@@ -2,21 +2,18 @@
namespace Combodo\iTop\Test\UnitTest\Core;
use CMDBObject;
use Combodo\iTop\Test\UnitTest\ItopDataTestCase;
use CoreException;
use Exception;
use MetaModel;
/**
* @since 2.7.7 3.0.2 3.1.0 N°3717 tests history objects creation
*
* @package Combodo\iTop\Test\UnitTest\Core
*/
class CMDBObjectTest extends ItopDataTestCase
{
private $sAdminLogin;
@@ -51,8 +48,11 @@ class CMDBObjectTest extends ItopDataTestCase
$oTestObject->Set('url', 'https://www.combodo.com');
$oTestObject->DBWrite();
self::assertFalse(CMDBObject::GetCurrentChange()->IsNew(), 'TrackInfo : Current change persisted');
self::assertEquals($sTrackInfo, CMDBObject::GetCurrentChange()->Get('userinfo'),
'TrackInfo : current change created with expected trackinfo');
self::assertEquals(
$sTrackInfo,
CMDBObject::GetCurrentChange()->Get('userinfo'),
'TrackInfo : current change created with expected trackinfo'
);
//-- new object with non persisted current change
$sTrackInfo2 = $sTrackInfo.'_2';
@@ -64,8 +64,11 @@ class CMDBObjectTest extends ItopDataTestCase
$oTestObject->Set('url', 'https://fr.wikipedia.org');
$oTestObject->DBUpdate();
self::assertFalse(CMDBObject::GetCurrentChange()->IsNew(), 'SetCurrentChange : Current change persisted');
self::assertEquals($sTrackInfo2, CMDBObject::GetCurrentChange()->Get('userinfo'),
'SetCurrentChange : current change created with expected trackinfo');
self::assertEquals(
$sTrackInfo2,
CMDBObject::GetCurrentChange()->Get('userinfo'),
'SetCurrentChange : current change created with expected trackinfo'
);
//-- new object with current change init using helper method
$sTrackInfo3 = $sTrackInfo.'_3';
@@ -73,8 +76,11 @@ class CMDBObjectTest extends ItopDataTestCase
$oTestObject->Set('url', 'https://en.wikipedia.org');
$oTestObject->DBUpdate();
self::assertFalse(CMDBObject::GetCurrentChange()->IsNew(), 'SetCurrentChangeFromParams : Current change persisted');
self::assertEquals($sTrackInfo3, CMDBObject::GetCurrentChange()->Get('userinfo'),
'SetCurrentChangeFromParams : current change created with expected trackinfo');
self::assertEquals(
$sTrackInfo3,
CMDBObject::GetCurrentChange()->Get('userinfo'),
'SetCurrentChangeFromParams : current change created with expected trackinfo'
);
// restore initial conditions
$oTestObject->DBDelete();
@@ -82,7 +88,8 @@ class CMDBObjectTest extends ItopDataTestCase
CMDBObject::SetTrackInfo($sInitialTrackInfo);
}
public function CurrentChangeUnderImpersonationProvider(){
public function CurrentChangeUnderImpersonationProvider()
{
return [
'no track info' => [ 'sTrackInfo' => null ],
'track info from approvalbase' => [
@@ -99,7 +106,8 @@ class CMDBObjectTest extends ItopDataTestCase
* @runInSeparateProcess
* @dataProvider CurrentChangeUnderImpersonationProvider
*/
public function testCurrentChangeUnderImpersonation($sTrackInfo=null, $sExpectedChangeLogWhenImpersonation=null) {
public function testCurrentChangeUnderImpersonation($sTrackInfo = null, $sExpectedChangeLogWhenImpersonation = null)
{
$this->CreateTestOrganization();
$sUid = date('dmYHis');
@@ -119,7 +127,7 @@ class CMDBObjectTest extends ItopDataTestCase
// reset current change
CMDBObject::SetCurrentChange(null);
if (is_null($sTrackInfo)){
if (is_null($sTrackInfo)) {
CMDBObject::SetTrackInfo(null);
} else {
$sTrackInfo = $this->ReplaceByFriendlyNames($sTrackInfo, $oAdminUser, $oImpersonatedUser);
@@ -127,43 +135,70 @@ class CMDBObjectTest extends ItopDataTestCase
}
$this->CreateSimpleObject();
if (is_null($sTrackInfo)){
self::assertEquals($oAdminUser->GetFriendlyName(), CMDBObject::GetCurrentChange()->Get('userinfo'),
'TrackInfo : no impersonation');
if (is_null($sTrackInfo)) {
self::assertEquals(
$oAdminUser->GetFriendlyName(),
CMDBObject::GetCurrentChange()->Get('userinfo'),
'TrackInfo : no impersonation'
);
} else {
self::assertEquals($sTrackInfo, CMDBObject::GetCurrentChange()->Get('userinfo'),
'TrackInfo : no impersonation');
self::assertEquals(
$sTrackInfo,
CMDBObject::GetCurrentChange()->Get('userinfo'),
'TrackInfo : no impersonation'
);
}
self::assertEquals($oAdminUser->GetKey(), CMDBObject::GetCurrentChange()->Get('user_id'),
'TrackInfo : admin userid');
self::assertEquals(
$oAdminUser->GetKey(),
CMDBObject::GetCurrentChange()->Get('user_id'),
'TrackInfo : admin userid'
);
\UserRights::Impersonate($sImpersonatedLogin);
$this->CreateSimpleObject();
if (is_null($sExpectedChangeLogWhenImpersonation)){
if (is_null($sExpectedChangeLogWhenImpersonation)) {
$sExpectedMsg = $this->ReplaceByFriendlyNames("AdminSurName AdminName on behalf of ImpersonatedSurName ImpersonatedName", $oAdminUser, $oImpersonatedUser);
self::assertEquals($sExpectedMsg, CMDBObject::GetCurrentChange()->Get('userinfo'),
'TrackInfo : impersonation');
self::assertEquals(
$sExpectedMsg,
CMDBObject::GetCurrentChange()->Get('userinfo'),
'TrackInfo : impersonation'
);
} else {
$sExpectedMsg = $this->ReplaceByFriendlyNames($sExpectedChangeLogWhenImpersonation, $oAdminUser, $oImpersonatedUser);
self::assertEquals($sExpectedMsg, CMDBObject::GetCurrentChange()->Get('userinfo'),
'TrackInfo : impersonation');
self::assertEquals(
$sExpectedMsg,
CMDBObject::GetCurrentChange()->Get('userinfo'),
'TrackInfo : impersonation'
);
}
self::assertEquals(null, CMDBObject::GetCurrentChange()->Get('user_id'),
'TrackInfo : no userid to force userinfo being displayed on UI caselog side');
self::assertEquals(
null,
CMDBObject::GetCurrentChange()->Get('user_id'),
'TrackInfo : no userid to force userinfo being displayed on UI caselog side'
);
\UserRights::Deimpersonate();
$this->CreateSimpleObject();
if (is_null($sTrackInfo)){
self::assertEquals($oAdminUser->GetFriendlyName(), CMDBObject::GetCurrentChange()->Get('userinfo'),
'TrackInfo : no impersonation');
if (is_null($sTrackInfo)) {
self::assertEquals(
$oAdminUser->GetFriendlyName(),
CMDBObject::GetCurrentChange()->Get('userinfo'),
'TrackInfo : no impersonation'
);
} else {
self::assertEquals($sTrackInfo, CMDBObject::GetCurrentChange()->Get('userinfo'),
'TrackInfo : no impersonation');
self::assertEquals(
$sTrackInfo,
CMDBObject::GetCurrentChange()->Get('userinfo'),
'TrackInfo : no impersonation'
);
}
self::assertEquals($oAdminUser->GetKey(), CMDBObject::GetCurrentChange()->Get('user_id'),
'TrackInfo : admin userid');
self::assertEquals(
$oAdminUser->GetKey(),
CMDBObject::GetCurrentChange()->Get('user_id'),
'TrackInfo : admin userid'
);
// restore initial conditions
CMDBObject::SetCurrentChange($oInitialCurrentChange);
@@ -196,7 +231,7 @@ class CMDBObjectTest extends ItopDataTestCase
* @dataProvider RecordObjDeletionProvider
*
*/
public function testRecordObjDeletion( string $sFirstName, string $sName)
public function testRecordObjDeletion(string $sFirstName, string $sName)
{
$oPerson = MetaModel::NewObject('Person', [
'first_name' => $sFirstName,
@@ -208,39 +243,40 @@ class CMDBObjectTest extends ItopDataTestCase
$bDeletionOK = true;
try {
$oDeletionPlan = $this->InvokeNonPublicMethod(CMDBObject::class, 'RecordObjDeletion', $oPerson, [$oPerson->GetKey()]);
}
catch (CoreException $e) {
} catch (CoreException $e) {
$bDeletionOK = false;
}
// We don't need to test the result (truncated string), it's already done in \DBObject::SetTrim() with N°3448
$this->assertTrue($bDeletionOK);
}
private function ReplaceByFriendlyNames($sMessage, $oAdminUser, $oImpersonatedUser) : string {
private function ReplaceByFriendlyNames($sMessage, $oAdminUser, $oImpersonatedUser): string
{
$sNewMessage = str_replace('AdminSurName AdminName', $oAdminUser->GetFriendlyName(), $sMessage);
$sNewMessage = str_replace('ImpersonatedSurName ImpersonatedName', $oImpersonatedUser->GetFriendlyName(), $sNewMessage);
return $sNewMessage;
}
private function CreateSimpleObject(){
private function CreateSimpleObject()
{
/** @var \DocumentWeb $oTestObject */
$oTestObject = MetaModel::NewObject('DocumentWeb');
$oTestObject->Set('name', 'PHPUnit test');
$oTestObject->Set('org_id', $this->getTestOrgId() );
$oTestObject->Set('org_id', $this->getTestOrgId());
$oTestObject->Set('url', 'https://www.combodo.com');
$oTestObject->DBWrite();
}
private function CreateUserForImpersonation($sLogin, $sProfileName, $sName, $sSurname): \UserLocal {
private function CreateUserForImpersonation($sLogin, $sProfileName, $sName, $sSurname): \UserLocal
{
/** @var \Person $oPerson */
$oPerson = $this->createObject('Person', array(
$oPerson = $this->createObject('Person', [
'name' => $sName,
'first_name' => $sSurname,
'org_id' => $this->getTestOrgId(),
));
]);
$oProfile = \MetaModel::GetObjectFromOQL("SELECT URP_Profiles WHERE name = :name", array('name' => $sProfileName), true);
$oProfile = \MetaModel::GetObjectFromOQL("SELECT URP_Profiles WHERE name = :name", ['name' => $sProfileName], true);
/** @var \UserLocal $oUser */
$oUser = $this->CreateUser($sLogin, $oProfile->GetKey(), "1234567Azert@", $oPerson->GetKey());

View File

@@ -1,4 +1,5 @@
<?php
/*
* @copyright Copyright (C) 2010-2024 Combodo SAS
* @license http://opensource.org/licenses/AGPL-3.0
@@ -23,6 +24,7 @@ use Server;
use Team;
use UserRequest;
use utils;
use const EVENT_DB_ABOUT_TO_DELETE;
use const EVENT_DB_AFTER_DELETE;
use const EVENT_DB_AFTER_WRITE;
@@ -35,11 +37,10 @@ use const EVENT_ENUM_TRANSITIONS;
class CRUDEventTest extends ItopDataTestCase
{
const USE_TRANSACTION = true;
const CREATE_TEST_ORG = true;
use DBObject\Utils\EventTest;
use DBObject\Utils\ClassesWithDebug;
public const USE_TRANSACTION = true;
public const CREATE_TEST_ORG = true;
private static string $sLogFile = 'log/test_error_CRUDEventTest.log';
@@ -447,10 +448,10 @@ class CRUDEventTest extends ItopDataTestCase
$oUserRequest->DBInsert();
// 1 insert for UserRequest, 3 insert for lnkFunctionalCIToTicket
$this->AssertEventCountEquals(4,EVENT_DB_COMPUTE_VALUES);
$this->AssertEventCountEquals(4,EVENT_DB_CHECK_TO_WRITE);
$this->AssertEventCountEquals(4,EVENT_DB_BEFORE_WRITE);
$this->AssertEventCountEquals(4,EVENT_DB_AFTER_WRITE);
$this->AssertEventCountEquals(4, EVENT_DB_COMPUTE_VALUES);
$this->AssertEventCountEquals(4, EVENT_DB_CHECK_TO_WRITE);
$this->AssertEventCountEquals(4, EVENT_DB_BEFORE_WRITE);
$this->AssertEventCountEquals(4, EVENT_DB_AFTER_WRITE);
$this->AssertEventNotReceived(EVENT_DB_LINKS_CHANGED, 'Event must not be fired if host object is created with links');
$this->AssertTotalEventCountEquals(16);
@@ -724,4 +725,4 @@ class CRUDEventTest extends ItopDataTestCase
$this->assertArrayNotHasKey('ev_assign', $aTransitions, 'Assign transition should have been removed by EVENT_ENUM_TRANSITIONS handler');
$this->assertEquals(1, count($aRefTransitions) - count($aTransitions), 'Only one transition should have been removed');
}
}
}

View File

@@ -1,4 +1,5 @@
<?php
/**
* @copyright Copyright (C) 2010-2025 Combodo SARL
* @license http://opensource.org/licenses/AGPL-3.0
@@ -14,20 +15,20 @@ use IssueLog;
use LogChannels;
use MetaModel;
use utils;
use const EVENT_DB_LINKS_CHANGED;
class CRUDEventWithModifiedDataModelTest extends ItopCustomDatamodelTestCase
{
use EventTest;
use ClassesWithDebug;
public function GetDatamodelDeltaAbsPath(): string
{
return __DIR__.'/Delta/dbobjecttest.xml';
}
const USE_TRANSACTION = true;
const CREATE_TEST_ORG = false;
use EventTest;
use ClassesWithDebug;
public const USE_TRANSACTION = true;
public const CREATE_TEST_ORG = false;
private static string $sLogFile = 'log/test_error_CRUDEventTest.log';
@@ -79,4 +80,3 @@ class CRUDEventWithModifiedDataModelTest extends ItopCustomDatamodelTestCase
$this->AssertEventCountEquals(0, EVENT_DB_LINKS_CHANGED, 'Event EVENT_DB_LINKS_CHANGED should not have been thrown on deleted objects');
}
}

View File

@@ -1,4 +1,5 @@
<?php
/**
* @copyright Copyright (C) 2010-2024 Combodo SAS
* @license http://opensource.org/licenses/AGPL-3.0

View File

@@ -1,4 +1,5 @@
<?php
/**
* @copyright Copyright (C) 2010-2024 Combodo SAS
* @license http://opensource.org/licenses/AGPL-3.0

View File

@@ -1,4 +1,5 @@
<?php
// Copyright (c) 2010-2024 Combodo SAS
//
// This file is part of iTop.
@@ -40,19 +41,17 @@ use UserRequest;
use UserRights;
use utils;
/**
* @group specificOrgInSampleData
*/
class DBObjectTest extends ItopDataTestCase
{
const CREATE_TEST_ORG = true;
const INVALID_OBJECT_KEY = 123456789;
public const CREATE_TEST_ORG = true;
public const INVALID_OBJECT_KEY = 123456789;
// Counts
public $aReloadCount = [];
protected function setUp(): void
{
parent::setUp();
@@ -83,18 +82,18 @@ class DBObjectTest extends ItopDataTestCase
public function keyProviderOK()
{
return array(
array(1, true),
array('255', true),
array(-24576, true),
array(0123, true),
array(0xCAFE, true),
array(PHP_INT_MIN, true),
array(PHP_INT_MAX, true),
array('test', false),
array('', false),
array('a255', false),
array('PHP_INT_MIN', false));
return [
[1, true],
['255', true],
[-24576, true],
[0123, true],
[0xCAFE, true],
[PHP_INT_MIN, true],
[PHP_INT_MAX, true],
['test', false],
['', false],
['a255', false],
['PHP_INT_MIN', false]];
}
/**
@@ -152,7 +151,7 @@ class DBObjectTest extends ItopDataTestCase
*/
public function testAttributeRefresh_FriendlyNameWithoutCascade()
{
$oObject = \MetaModel::NewObject('Person', array('name' => 'Foo', 'first_name' => 'John', 'org_id' => 3, 'location_id' => 2));
$oObject = \MetaModel::NewObject('Person', ['name' => 'Foo', 'first_name' => 'John', 'org_id' => 3, 'location_id' => 2]);
static::assertEquals('John Foo', $oObject->Get('friendlyname'));
$oObject->Set('name', 'Who');
@@ -181,7 +180,7 @@ class DBObjectTest extends ItopDataTestCase
*/
public function testAttributeRefresh_FriendlyNameFromDB()
{
$oObject = \MetaModel::NewObject('Person', array('name' => 'Gary', 'first_name' => 'Romain', 'org_id' => 3, 'location_id' => 2));
$oObject = \MetaModel::NewObject('Person', ['name' => 'Gary', 'first_name' => 'Romain', 'org_id' => 3, 'location_id' => 2]);
$oObject->DBInsert();
$iObjKey = $oObject->GetKey();
@@ -198,7 +197,7 @@ class DBObjectTest extends ItopDataTestCase
*/
public function testPartialAttributeEvaluation()
{
$oObject = \MetaModel::NewObject('Person', array('name' => 'Foo', 'org_id' => 3, 'location_id' => 2));
$oObject = \MetaModel::NewObject('Person', ['name' => 'Foo', 'org_id' => 3, 'location_id' => 2]);
static::assertEquals(' Foo', $oObject->Get('friendlyname'));
}
@@ -208,7 +207,7 @@ class DBObjectTest extends ItopDataTestCase
*/
public function testEmptyAttributeEvaluation()
{
$oObject = \MetaModel::NewObject('Person', array('org_id' => 3, 'location_id' => 2));
$oObject = \MetaModel::NewObject('Person', ['org_id' => 3, 'location_id' => 2]);
static::assertEquals(' ', $oObject->Get('friendlyname'));
}
@@ -232,7 +231,7 @@ class DBObjectTest extends ItopDataTestCase
*/
public function testAttributeRefresh_ObsolescenceFlagWithoutCascade()
{
$oObject = \MetaModel::NewObject('Person', array('name' => 'Foo', 'first_name' => 'John', 'org_id' => 3, 'location_id' => 2));
$oObject = \MetaModel::NewObject('Person', ['name' => 'Foo', 'first_name' => 'John', 'org_id' => 3, 'location_id' => 2]);
static::assertEquals(false, (bool)$oObject->Get('obsolescence_flag'));
$oObject->Set('status', 'inactive');
@@ -264,26 +263,26 @@ class DBObjectTest extends ItopDataTestCase
*/
public function testAttributeRefresh_ExternalKeysAndFields()
{
$this->assertDBQueryCount(0, function() use (&$oObject){
$oObject = \MetaModel::NewObject('Person', array('name' => 'Foo', 'first_name' => 'John', 'org_id' => 3, 'location_id' => 2));
$this->assertDBQueryCount(0, function () use (&$oObject) {
$oObject = \MetaModel::NewObject('Person', ['name' => 'Foo', 'first_name' => 'John', 'org_id' => 3, 'location_id' => 2]);
});
$this->assertDBQueryCount(2, function() use (&$oObject){
$this->assertDBQueryCount(2, function () use (&$oObject) {
static::assertEquals('Demo', $oObject->Get('org_id_friendlyname'));
static::assertEquals('Grenoble', $oObject->Get('location_id_friendlyname'));
});
// External key given as an id
$this->assertDBQueryCount(1, function() use (&$oObject){
$this->assertDBQueryCount(1, function () use (&$oObject) {
$oObject->Set('org_id', 2);
static::assertEquals('IT Department', $oObject->Get('org_id_friendlyname'));
});
// External key given as an object
$this->assertDBQueryCount(1, function() use (&$oBordeaux){
$this->assertDBQueryCount(1, function () use (&$oBordeaux) {
$oBordeaux = \MetaModel::GetObject('Location', 1);
});
$this->assertDBQueryCount(0, function() use (&$oBordeaux, &$oObject){
$this->assertDBQueryCount(0, function () use (&$oBordeaux, &$oObject) {
/** @var DBObject $oObject */
$oObject->Set('location_id', $oBordeaux);
static::assertEquals('IT Department', $oObject->Get('org_id_friendlyname'));
@@ -292,7 +291,7 @@ class DBObjectTest extends ItopDataTestCase
});
static::assertEquals('Bordeaux', $oObject->Get('location_id_friendlyname'));
// static::assertEquals('toto', $oObject->EvaluateExpression(\Expression::FromOQL("CONCAT(org_name, '-', location_id_friendlyname)")));
// static::assertEquals('toto', $oObject->EvaluateExpression(\Expression::FromOQL("CONCAT(org_name, '-', location_id_friendlyname)")));
}
/**
@@ -304,7 +303,7 @@ class DBObjectTest extends ItopDataTestCase
{
$this->ResetReloadCount();
$this->assertDBQueryCount(0, function() use (&$oObject){
$this->assertDBQueryCount(0, function () use (&$oObject) {
$oObject = \MetaModel::NewObject('Person', ['name' => 'Foo', 'first_name' => 'John', 'org_id' => 3, 'location_id' => 2]);
});
// The number of queries depends on the installed modules so it varies on CI
@@ -314,14 +313,14 @@ class DBObjectTest extends ItopDataTestCase
$this->debug("Created $sClass::$sKey");
$this->DebugReloadCount("Person::DBInsertNoReload()");
$this->assertDBQueryCount(0, function() use (&$oObject){
$this->assertDBQueryCount(0, function () use (&$oObject) {
static::assertEquals('Demo', $oObject->Get('org_id_friendlyname'));
static::assertEquals('Grenoble', $oObject->Get('location_id_friendlyname'));
});
$this->DebugReloadCount("Get('org_id_friendlyname') and Get('location_id_friendlyname')");
// External key given as an id
$this->assertDBQueryCount(1, function() use (&$oObject){
$this->assertDBQueryCount(1, function () use (&$oObject) {
$oObject->Set('org_id', 2);
static::assertEquals('IT Department', $oObject->Get('org_id_friendlyname'));
});
@@ -329,12 +328,12 @@ class DBObjectTest extends ItopDataTestCase
$this->DebugReloadCount("Set('org_id', 2) and Get('org_id_friendlyname')");
// External key given as an object
$this->assertDBQueryCount(1, function() use (&$oBordeaux){
$this->assertDBQueryCount(1, function () use (&$oBordeaux) {
$oBordeaux = MetaModel::GetObject('Location', 1);
});
$this->DebugReloadCount("GetObject('Location', 1)");
$this->assertDBQueryCount(0, function() use (&$oBordeaux, &$oObject){
$this->assertDBQueryCount(0, function () use (&$oBordeaux, &$oObject) {
$oObject->Set('location_id', $oBordeaux);
static::assertEquals('IT Department', $oObject->Get('org_id_friendlyname'));
static::assertEquals('IT Department', $oObject->Get('org_name'));
@@ -343,7 +342,6 @@ class DBObjectTest extends ItopDataTestCase
$this->DebugReloadCount("Set('location_id',...) Get('org_id_friendlyname') Get('org_name') Get('location_id_friendlyname')");
}
/**
* @covers DBObject::NewObject
* @covers DBObject::Get
@@ -353,7 +351,7 @@ class DBObjectTest extends ItopDataTestCase
{
$this->ResetReloadCount();
$this->assertDBQueryCount(0, function() use (&$oPerson){
$this->assertDBQueryCount(0, function () use (&$oPerson) {
$oPerson = MetaModel::NewObject('Person', ['name' => 'Foo', 'first_name' => 'John', 'org_id' => 3, 'location_id' => 2]);
});
// The number of queries depends on the installed modules so it varies on CI
@@ -363,7 +361,7 @@ class DBObjectTest extends ItopDataTestCase
$this->debug("Created $sPersonClass::$sPersonKey");
$this->DebugReloadCount("Person::DBInsertNoReload()");
$this->assertDBQueryCount(1, function() use (&$oTeam, &$oPerson){
$this->assertDBQueryCount(1, function () use (&$oTeam, &$oPerson) {
$oTeam = MetaModel::NewObject('Team', ['name' => 'Team Foo', 'org_id' => 3]);
// Add person to team
$oNewLink = new lnkPersonToTeam();
@@ -389,7 +387,7 @@ class DBObjectTest extends ItopDataTestCase
$this->assertCount(0, $oTeam->ListChanges());
// External key given as an id
$this->assertDBQueryCount(1, function() use (&$oTeam){
$this->assertDBQueryCount(1, function () use (&$oTeam) {
$oTeam->Set('org_id', 2);
static::assertEquals('IT Department', $oTeam->Get('org_id_friendlyname'));
});
@@ -397,10 +395,9 @@ class DBObjectTest extends ItopDataTestCase
$this->assertCount(1, $oTeam->ListChanges());
}
public function testSetExtKeyUnsetDependentAttribute()
{
$oObject = \MetaModel::NewObject('Person', array('name' => 'Foo', 'first_name' => 'John', 'org_id' => 3, 'location_id' => 2));
$oObject = \MetaModel::NewObject('Person', ['name' => 'Foo', 'first_name' => 'John', 'org_id' => 3, 'location_id' => 2]);
$oOrg = \MetaModel::GetObject('Organization', 2);
$oObject->Set('org_id', $oOrg);
@@ -408,13 +405,13 @@ class DBObjectTest extends ItopDataTestCase
static::assertEquals(2, $oObject->Get('location_id'));
// Dependent external field is updated because the Set('org_id') is done with an object
$this->assertDBQueryCount(0, function() use (&$oObject){
$this->assertDBQueryCount(0, function () use (&$oObject) {
static::assertNotEmpty($oObject->Get('org_name'));
});
// Dependent external field is reset and reloaded from DB
$oObject->Set('org_id', 3);
$this->assertDBQueryCount(1, function() use (&$oObject){
$this->assertDBQueryCount(1, function () use (&$oObject) {
static::assertNotEmpty($oObject->Get('org_name'));
});
}
@@ -471,17 +468,16 @@ class DBObjectTest extends ItopDataTestCase
*/
public function testModelExpressions()
{
foreach (\MetaModel::GetClasses() as $sClass)
{
if (\MetaModel::IsAbstract($sClass)) continue;
foreach (\MetaModel::GetClasses() as $sClass) {
if (\MetaModel::IsAbstract($sClass)) {
continue;
}
$oObject = \MetaModel::NewObject($sClass);
foreach (\MetaModel::ListAttributeDefs($sClass) as $sAttCode => $oAttDef)
{
if ($oAttDef->IsBasedOnOQLExpression())
{
foreach (\MetaModel::ListAttributeDefs($sClass) as $sAttCode => $oAttDef) {
if ($oAttDef->IsBasedOnOQLExpression()) {
$this->debug("$sClass::$sAttCode");
$this->assertDBQueryCount(0, function() use (&$oObject, &$oAttDef){
$this->assertDBQueryCount(0, function () use (&$oObject, &$oAttDef) {
$oObject->EvaluateExpression($oAttDef->GetOQLExpression());
});
}
@@ -604,7 +600,7 @@ class DBObjectTest extends ItopDataTestCase
$oTeam = MetaModel::NewObject(Team::class, [
'name' => 'The A Team',
'org_id' => $oDemoOrg->GetKey()
'org_id' => $oDemoOrg->GetKey(),
]);
// Part 1 - Test with an invalid id (non-existing object)
@@ -666,8 +662,7 @@ class DBObjectTest extends ItopDataTestCase
try {
$oTeam->CheckChangedExtKeysValues();
$this->fail('An unauthorized object should be detected as invalid');
}
catch (InvalidExternalKeyValueException $e) {
} catch (InvalidExternalKeyValueException $e) {
// Ok, the exception was expected
}
@@ -680,8 +675,7 @@ class DBObjectTest extends ItopDataTestCase
$oTeam->DBInsert(); // persisting invalid value and resets the object changed values
try {
$oTeam->CheckChangedExtKeysValues();
}
catch (InvalidExternalKeyValueException $e) {
} catch (InvalidExternalKeyValueException $e) {
$this->fail('An unauthorized value should be ignored when it is not being modified');
}
}
@@ -745,7 +739,7 @@ class DBObjectTest extends ItopDataTestCase
$oQueryOQL = \MetaModel::NewObject('QueryOQL', [
'name' => 'Test Query',
'description' => 'Test Query',
'oql' => 'SELECT Person'
'oql' => 'SELECT Person',
]);
$oQueryOQL->DBInsert();
@@ -768,12 +762,12 @@ class DBObjectTest extends ItopDataTestCase
*/
public function getAttributeIntegerDBIncrementProvider()
{
return array(
'Incrementation #1' => array('export_count', [5], 5),
'Incrementation #2' => array('export_count', [5, 10], 15),
'Incrementation #3' => array('export_count', [50, 20, 10, 100], 180),
'Incrementation #4' => array('export_count', [50, 20, -10, 1000], 1060)
);
return [
'Incrementation #1' => ['export_count', [5], 5],
'Incrementation #2' => ['export_count', [5, 10], 15],
'Incrementation #3' => ['export_count', [50, 20, 10, 100], 180],
'Incrementation #4' => ['export_count', [50, 20, -10, 1000], 1060],
];
}
/**
@@ -788,7 +782,7 @@ class DBObjectTest extends ItopDataTestCase
$oQueryOQL = \MetaModel::NewObject('QueryOQL', [
'name' => 'Test Query',
'description' => 'Test Query',
'oql' => 'SELECT Person'
'oql' => 'SELECT Person',
]);
$oQueryOQL->DBInsert();
@@ -811,7 +805,7 @@ class DBObjectTest extends ItopDataTestCase
$oQueryOQL = \MetaModel::NewObject('QueryOQL', [
'name' => 'Test Query',
'description' => 'Test Query',
'oql' => 'SELECT Person'
'oql' => 'SELECT Person',
]);
$oQueryOQL->DBInsert();
@@ -837,12 +831,12 @@ class DBObjectTest extends ItopDataTestCase
$oQueryOQL = \MetaModel::NewObject('QueryOQL', [
'name' => 'Test Query',
'description' => 'Test Query',
'oql' => 'SELECT Person'
'oql' => 'SELECT Person',
]);
$oQueryOQL->DBInsert();
// assert query count
$this->assertDBQueryCount(2, function() use (&$oQueryOQL) {
$this->assertDBQueryCount(2, function () use (&$oQueryOQL) {
$oQueryOQL->DBIncrement('export_count', 1);
});
}
@@ -978,8 +972,7 @@ class DBObjectTest extends ItopDataTestCase
try {
$oPerson->Set('email', 'test1@combodo.com');
$this->assertTrue(false, 'Set() should have raised a CoreException');
}
catch (\CoreException $e) {
} catch (\CoreException $e) {
$this->assertEquals($sMessage, $e->getMessage());
}
@@ -1045,9 +1038,9 @@ class DBObjectTest extends ItopDataTestCase
'xml',
[
'ev_assign',
'ev_timeout',
'ev_wait_for_approval',
'ev_autoresolve',
'ev_timeout',
'ev_wait_for_approval',
'ev_autoresolve',
],
],
'UserRequest - XML sort when in specific state' => [
@@ -1057,10 +1050,10 @@ class DBObjectTest extends ItopDataTestCase
'xml',
[
'ev_pending',
'ev_resolve',
'ev_reassign',
'ev_timeout',
'ev_autoresolve',
'ev_resolve',
'ev_reassign',
'ev_timeout',
'ev_autoresolve',
],
],
'UserRequest - Alphabetical (labels not codes) sort' => [
@@ -1070,9 +1063,9 @@ class DBObjectTest extends ItopDataTestCase
'alphabetical',
[
'ev_assign',
'ev_autoresolve',
'ev_timeout',
'ev_wait_for_approval',
'ev_autoresolve',
'ev_timeout',
'ev_wait_for_approval',
],
],
'UserRequest - Alphabetical (labels not codes) sort when in specific state' => [
@@ -1081,11 +1074,11 @@ class DBObjectTest extends ItopDataTestCase
'assigned',
'alphabetical',
[
'ev_autoresolve',
'ev_autoresolve',
'ev_resolve',
'ev_pending',
'ev_reassign',
'ev_timeout',
'ev_pending',
'ev_reassign',
'ev_timeout',
],
],
'UserRequest - Fixed sort' => [
@@ -1094,10 +1087,10 @@ class DBObjectTest extends ItopDataTestCase
null,
'fixed',
[
'ev_wait_for_approval',
'ev_wait_for_approval',
'ev_assign',
'ev_timeout',
'ev_autoresolve',
'ev_timeout',
'ev_autoresolve',
],
],
'UserRequest - Fixed sort when in specific state' => [
@@ -1117,10 +1110,10 @@ class DBObjectTest extends ItopDataTestCase
null,
'relative',
[
'ev_wait_for_approval',
'ev_wait_for_approval',
'ev_assign',
'ev_timeout',
'ev_autoresolve',
'ev_timeout',
'ev_autoresolve',
],
],
'UserRequest - Relative sort when in specific state' => [
@@ -1212,8 +1205,7 @@ class DBObjectTest extends ItopDataTestCase
$bDeletionOK = true;
try {
$oDeletionPlan = $oPerson->DBDelete();
}
catch (CoreException $e) {
} catch (CoreException $e) {
$bDeletionOK = false;
}
$this->assertTrue($bDeletionOK);
@@ -1265,7 +1257,7 @@ class DBObjectTest extends ItopDataTestCase
/**
* @since 3.1.0-3 3.1.1 3.2.0 N°6716 test creation
*/
public function testConstructorMemoryFootprint():void
public function testConstructorMemoryFootprint(): void
{
$idx = 0;
$fStart = microtime(true);
@@ -1287,7 +1279,7 @@ class DBObjectTest extends ItopDataTestCase
$sCurrPeak = \utils::BytesToFriendlyFormat($iMemoryPeakUsage, 4);
echo "$idx ".sprintf('%.1f ms', $fDuration * 1000)." - Peak Memory Usage: $sCurrPeak\n";
$this->assertTrue(($iMemoryPeakUsage - $iInitialPeak) <= $iMaxAllowedMemoryIncrease , "Peak memory changed from $sInitialPeak to $sCurrPeak after $i loops");
$this->assertTrue(($iMemoryPeakUsage - $iInitialPeak) <= $iMaxAllowedMemoryIncrease, "Peak memory changed from $sInitialPeak to $sCurrPeak after $i loops");
$fStartLoop = microtime(true);
}
@@ -1337,7 +1329,7 @@ class DBObjectTest extends ItopDataTestCase
$sPrefix = 'a'; // just a small prefix so that the emoji bytes won't have a power of 2 (we want a non even value)
$sEmojiToRepeat = '😎'; // this emoji is 4 bytes long
$sEmojiRepeats = str_repeat($sEmojiToRepeat, $iValueLength - mb_strlen($sPrefix));
$sValueToSet = $sPrefix . $sEmojiRepeats;
$sValueToSet = $sPrefix.$sEmojiRepeats;
$oTicket = MetaModel::NewObject('UserRequest', [
'ref' => 'Test Ticket',
@@ -1356,14 +1348,14 @@ class DBObjectTest extends ItopDataTestCase
$bIsValueToSetBelowAttrMaxSize = ($iValueLength <= $iAttrMaxSize);
/** @noinspection PhpUnusedLocalVariableInspection */
[$bCheckStatus, $aCheckIssues, $bSecurityIssue] = $oTicket->CheckToWrite();
$this->assertEquals($bIsValueToSetBelowAttrMaxSize, $bCheckStatus, "CheckResult result:" . var_export($aCheckIssues, true));
$this->assertEquals($bIsValueToSetBelowAttrMaxSize, $bCheckStatus, "CheckResult result:".var_export($aCheckIssues, true));
$oTicket->SetTrim($sAttrCode, $sValueToSet);
$sValueInObject = $oTicket->Get($sAttrCode);
if ($bIsValueToSetBelowAttrMaxSize) {
$this->assertEquals($sValueToSet, $sValueInObject,'Should not alter string that is already shorter than attribute max length');
$this->assertEquals($sValueToSet, $sValueInObject, 'Should not alter string that is already shorter than attribute max length');
} else {
$this->assertEquals($iAttrMaxSize, mb_strlen($sValueInObject),'Should truncate at the same length than attribute max length');
$this->assertEquals($iAttrMaxSize, mb_strlen($sValueInObject), 'Should truncate at the same length than attribute max length');
$sLastCharsOfValueInObject = mb_substr($sValueInObject, -30);
$this->assertStringContainsString(' -truncated', $sLastCharsOfValueInObject, 'Should end with "truncated" comment');
}
@@ -1373,13 +1365,13 @@ class DBObjectTest extends ItopDataTestCase
{
return [
'short string should not be truncated' => ['name','name'],
'simple ascii string longer than 255 characters truncated' => [
str_repeat('e',300),
str_repeat('e',232) . ' -truncated (300 chars)'
],
'simple ascii string longer than 255 characters truncated' => [
str_repeat('e', 300),
str_repeat('e', 232).' -truncated (300 chars)',
],
'smiley string longer than 255 characters truncated' => [
str_repeat('😃',300),
str_repeat('😃',232) . ' -truncated (300 chars)'
str_repeat('😃', 300),
str_repeat('😃', 232).' -truncated (300 chars)',
],
];
@@ -1389,7 +1381,8 @@ class DBObjectTest extends ItopDataTestCase
* @dataProvider SetTrimProvider
* @return void
*/
public function testSetTrim($sName, $sResult){
public function testSetTrim($sName, $sResult)
{
$oOrganisation = MetaModel::NewObject(Organization::class);
$oOrganisation->SetTrim('name', $sName);
$this->assertEquals($sResult, $oOrganisation->Get('name'), 'SetTrim must limit string to 255 characters');
@@ -1399,21 +1392,23 @@ class DBObjectTest extends ItopDataTestCase
* @covers DBObject::SetComputedDate
* @return void
*/
public function testSetComputedDateOnAttributeDate(){
$oObject = MetaModel::NewObject(\CustomerContract::class, ['name'=>'Test contract','org_id'=>'3','provider_id'=>'2']);
$oObject->Set('start_date',time());
public function testSetComputedDateOnAttributeDate()
{
$oObject = MetaModel::NewObject(\CustomerContract::class, ['name' => 'Test contract','org_id' => '3','provider_id' => '2']);
$oObject->Set('start_date', time());
$oObject->SetComputedDate('end_date', "+2 weeks", 'start_date');
$this->assertTrue(true,'No fatal error on computing date');
$this->assertTrue(true, 'No fatal error on computing date');
}
/**
* @covers DBObject::SetComputedDate
* @return void
*/
public function testSetComputedDateOnAttributeDateTime(){
$oObject = MetaModel::NewObject(\WorkOrder::class, ['name'=>'Test workorder','description'=>'Toto']);
$oObject->Set('start_date','2024-01-01 09:45:00');
public function testSetComputedDateOnAttributeDateTime()
{
$oObject = MetaModel::NewObject(\WorkOrder::class, ['name' => 'Test workorder','description' => 'Toto']);
$oObject->Set('start_date', '2024-01-01 09:45:00');
$oObject->SetComputedDate('end_date', "+2 weeks", 'start_date');
$this->assertTrue(true,'No fatal error on computing date');
$this->assertTrue(true, 'No fatal error on computing date');
$this->assertEquals("2024-01-15 09:45:00", $oObject->Get('end_date'), 'SetComputedDate +2 weeks on a WorkOrder DateTimeAttribute');
}

View File

@@ -1,4 +1,5 @@
<?php
/**
* @copyright Copyright (C) 2010-2025 Combodo SARL
* @license http://opensource.org/licenses/AGPL-3.0
@@ -11,6 +12,7 @@ use IssueLog;
use LogChannels;
use MetaModel;
use utils;
use const EVENT_DB_LINKS_CHANGED;
class DBObjectWithModifiedDataModelTest extends ItopCustomDatamodelTestCase
@@ -20,9 +22,8 @@ class DBObjectWithModifiedDataModelTest extends ItopCustomDatamodelTestCase
return __DIR__.'/Delta/dbobjecttest.xml';
}
const USE_TRANSACTION = true;
const CREATE_TEST_ORG = false;
public const USE_TRANSACTION = true;
public const CREATE_TEST_ORG = false;
private static string $sLogFile = 'log/test_error_CRUDEventTest.log';
@@ -81,4 +82,3 @@ class DBObjectWithModifiedDataModelTest extends ItopCustomDatamodelTestCase
$this->assertEquals('resolved', $oParent->Get('status'), 'The status should have been modified to resolved (the final state after a nested stimulus)');
}
}

View File

@@ -1,4 +1,5 @@
<?php
/**
* @copyright Copyright (C) 2010-2025 Combodo SARL
* @license http://opensource.org/licenses/AGPL-3.0
@@ -10,7 +11,6 @@ use Combodo\iTop\Service\Events\EventData;
use Combodo\iTop\Test\UnitTest\ItopDataTestCase;
use MetaModel;
/**
* Test support class used to count events received
* And allow callbacks on events
@@ -51,7 +51,6 @@ class CRUDEventReceiver
$this->bDBUpdateCalledSuccessfullyDuringEvent = false;
}
/**
* Event callbacks => this function counts the received events by event name and source class
* If AddCallback() method has been called a specific callback is called, else only the count is done

View File

@@ -1,4 +1,5 @@
<?php
/**
* @copyright Copyright (C) 2010-2025 Combodo SARL
* @license http://opensource.org/licenses/AGPL-3.0
@@ -6,11 +7,10 @@
namespace DBObject\Utils;
/**
* Add debug feature to test support class
*/
Trait ClassesWithDebug
trait ClassesWithDebug
{
public static function DebugStatic($sMsg)
{
@@ -22,4 +22,4 @@ Trait ClassesWithDebug
}
}
}
}
}

View File

@@ -1,4 +1,5 @@
<?php
/**
* @copyright Copyright (C) 2010-2025 Combodo SARL
* @license http://opensource.org/licenses/AGPL-3.0
@@ -42,4 +43,4 @@ trait EventTest
{
$this->assertArrayNotHasKey($sEvent, self::$aEventCallsCount, $sMessage);
}
}
}