N°6275 - Propose CaseLog extensibility API to manage history rotate

This commit is contained in:
odain
2023-05-09 12:47:29 +02:00
parent 5c252849fa
commit a27de39593
6 changed files with 287 additions and 25 deletions

View File

@@ -22,6 +22,32 @@ use ormCaseLog;
*/
class ormCaseLogTest extends ItopDataTestCase
{
const USE_TRANSACTION = false;
private $sLogin;
private $sPassword = "Iuytrez9876543ç_è-(";
public function setUp() :void{
parent::setUp(); // TODO: Change the autogenerated stub
//require_once APPROOT . "core/CaseLogService.php";
$oAdminProfile = \MetaModel::GetObjectFromOQL("SELECT URP_Profiles WHERE name = :name", array('name' => 'Administrator'), true);
if (is_object($oAdminProfile)) {
$this->sLogin = "admin-".date('dmYHis');
$this->CreateTestOrganization();
/** @var \Person $oPerson */
$oPerson = $this->createObject('Person', array(
'name' => $this->sLogin,
'first_name' => 'Test',
'org_id' => $this->getTestOrgId(),
));
$this->CreateUser($this->sLogin, $oAdminProfile->GetKey(), $this->sPassword, $oPerson->GetKey());
}
}
/**
* @covers \ormCaseLog::GetEntryCount()
* @throws \ArchivedObjectException
@@ -38,4 +64,141 @@ class ormCaseLogTest extends ItopDataTestCase
$oLog->AddLogEntry('First entry');
$this->assertEquals($oLog->GetEntryCount(), 1, 'Should be 1 entry, returned '.$oLog->GetEntryCount());
}
public function testConstructorWithoutRebuild(){
$oOrmCaseLogService = $this->createMock(\ormCaseLogService::class);
$sLog = "aaaaa";
$aInitialIndex = ['a' => 'b'];
$oOrmCaseLogService->expects($this->exactly(1))
->method('Rebuild')
->with($sLog, $aInitialIndex)
->willReturn(null);
$oLog = new ormCaseLog($sLog, $aInitialIndex, $oOrmCaseLogService);
$this->assertEquals($aInitialIndex, $oLog->GetIndex());
$this->assertEquals($sLog, $oLog->GetText());
}
public function testConstructorWithRebuild(){
$oOrmCaseLogService = $this->createMock(\ormCaseLogService::class);
$sLog = "aaaaa";
$sRebuiltLog = "bbbb";
$aInitialIndex = ['a' => 'b'];
$aRebuiltIndex = ['c' => 'd'];
$oOrmCaseLogService->expects($this->exactly(1))
->method('Rebuild')
->with($sLog, $aInitialIndex)
->willReturn(new ormCaseLog($sRebuiltLog, $aRebuiltIndex));
$oLog = new ormCaseLog($sLog, $aInitialIndex, $oOrmCaseLogService);
$this->assertEquals($aRebuiltIndex, $oLog->GetIndex());
$this->assertEquals($sRebuiltLog, $oLog->GetText());
}
public function AddLogEntryProvider(){
return [
'AddLogEntry' => [
'bTestAddLogEntry' => 'true'
],
'AddLogEntryFromJSON' => [
'bTestAddLogEntry' => 'false'
]
];
}
/**
* @dataProvider AddLogEntryProvider
*/
public function testAddLogEntryNoRebuild($bTestAddLogEntry=true){
$_SESSION = array();
$this->assertTrue(\UserRights::Login($this->sLogin));
$sLog = "aaaaa";
$sDate = date(\AttributeDateTime::GetInternalFormat());
$iUserId = \UserRights::GetUserId();
$sOnBehalfOf = \UserRights::GetUserFriendlyName();
$sSeparator = sprintf(CASELOG_SEPARATOR, $sDate, $sOnBehalfOf, $iUserId);
$sExpectedLog = $sSeparator."<p>$sLog</p>";
$oOrmCaseLogService = $this->createMock(\ormCaseLogService::class);
$aExpectedIndex = [
[
'user_name' => $sOnBehalfOf,
'user_id' => $iUserId,
'date' => time(),
'text_length' => 12,
'separator_length' => strlen($sSeparator),
'format' => 'html',
]
];
$oOrmCaseLogService->expects($this->exactly(2))
->method('Rebuild')
->withConsecutive(['', []], [$sExpectedLog, $aExpectedIndex])
->willReturnOnConsecutiveCalls(null, null);
$oLog = new ormCaseLog('', [], $oOrmCaseLogService);
if ($bTestAddLogEntry){
$oLog->AddLogEntry($sLog);
} else {
$sJson = json_encode(
[
'user_login' => 'gabuzmeu',
'message' => $sLog,
]
);
$oJson = json_decode($sJson);
$oLog->AddLogEntryFromJSON($oJson, false);
}
$this->assertEquals($sExpectedLog, $oLog->GetText());
$this->assertEquals($aExpectedIndex, $oLog->GetIndex());
}
/**
* @dataProvider AddLogEntryProvider
*/
public function testAddLogEntry($bTestAddLogEntry=true){
$_SESSION = array();
$this->assertTrue(\UserRights::Login($this->sLogin));
$sLog = "aaaaa";
$sDate = date(\AttributeDateTime::GetInternalFormat());
$iUserId = \UserRights::GetUserId();
$sOnBehalfOf = \UserRights::GetUserFriendlyName();
$sSeparator = sprintf(CASELOG_SEPARATOR, $sDate, $sOnBehalfOf, $iUserId);
$sExpectedLog = $sSeparator."<p>$sLog</p>";
$aRebuiltIndex = ['c' => 'd'];
$sRebuiltLog = "bbbb";
$oOrmCaseLogService = $this->createMock(\ormCaseLogService::class);
$aExpectedIndex = [
[
'user_name' => $sOnBehalfOf,
'user_id' => $iUserId,
'date' => time(),
'text_length' => 12,
'separator_length' => strlen($sSeparator),
'format' => 'html',
]
];
$oOrmCaseLogService->expects($this->exactly(2))
->method('Rebuild')
->withConsecutive(['', []], [$sExpectedLog, $aExpectedIndex])
->willReturnOnConsecutiveCalls(
null,
new ormCaseLog($sRebuiltLog, $aRebuiltIndex)
);
$oLog = new ormCaseLog('', [], $oOrmCaseLogService);
$oLog->AddLogEntry($sLog);
$this->assertEquals($sRebuiltLog, $oLog->GetText());
$this->assertEquals($aRebuiltIndex, $oLog->GetIndex());
}
}