Compare commits

...

4 Commits

Author SHA1 Message Date
odain
948bcb032a N°5341 - fix test 2023-04-07 07:06:32 +02:00
odain
128e7eed42 N°5341 - start testing ormcaselog rebuild when broken 2023-04-06 17:30:06 +02:00
odain
1b0dedaf31 N°5341 - refactoring to have a dedicated ormcase log service 2023-04-06 17:24:32 +02:00
Eric Espie
8d711fb37b N°5341 - Repair misalignment between case-log and case-log index (update only) 2023-02-03 15:51:43 +01:00
5 changed files with 423 additions and 38 deletions

View File

@@ -3,7 +3,7 @@
//
// This file is part of iTop.
//
// iTop is free software; you can redistribute it and/or modify
// iTop is free software; you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
@@ -19,10 +19,11 @@
define('CASELOG_VISIBLE_ITEMS', 2);
define('CASELOG_SEPARATOR', "\n".'========== %1$s : %2$s (%3$d) ============'."\n\n");
require_once('ormcaselogservice.inc.php');
/**
* Class to store a "case log" in a structured way, keeping track of its successive entries
*
*
* @copyright Copyright (C) 2010-2017 Combodo SARL
* @license http://opensource.org/licenses/AGPL-3.0
*/
@@ -30,19 +31,22 @@ class ormCaseLog {
protected $m_sLog;
protected $m_aIndex;
protected $m_bModified;
protected \ormCaseLogService $oOrmCaseLogService;
/**
* Initializes the log with the first (initial) entry
* @param $sLog string The text of the whole case log
* @param $aIndex array The case log index
*/
public function __construct($sLog = '', $aIndex = array())
public function __construct($sLog = '', $aIndex = [], \ormCaseLogService $oOrmCaseLogService=null)
{
$this->m_sLog = $sLog;
$this->m_aIndex = $aIndex;
$this->m_bModified = false;
$this->oOrmCaseLogService = (is_null($oOrmCaseLogService)) ? new \ormCaseLogService() : $oOrmCaseLogService;
$this->RebuildIndex();
}
public function GetText($bConvertToPlainText = false)
{
if ($bConvertToPlainText)
@@ -55,7 +59,7 @@ class ormCaseLog {
return $this->m_sLog;
}
}
public static function FromJSON($oJson)
{
if (!isset($oJson->items))
@@ -71,8 +75,8 @@ class ormCaseLog {
}
/**
* Return a value that will be further JSON encoded
*/
* Return a value that will be further JSON encoded
*/
public function GetForJSON()
{
// Order by ascending date
@@ -182,9 +186,9 @@ class ormCaseLog {
$sSeparator = sprintf(CASELOG_SEPARATOR, $aData['date'], $aData['user_login'], $aData['user_id']);
$sPlainText .= $sSeparator.$aData['message'];
}
return $sPlainText;
return $sPlainText;
}
public function GetIndex()
{
return $this->m_aIndex;
@@ -201,7 +205,7 @@ class ormCaseLog {
{
return ($this->m_sLog === null);
}
public function ClearModifiedFlag()
{
$this->m_bModified = false;
@@ -209,7 +213,7 @@ class ormCaseLog {
/**
* Produces an HTML representation, aimed at being used within an email
*/
*/
public function GetAsEmailHtml()
{
$sStyleCaseLogHeader = '';
@@ -290,10 +294,10 @@ class ormCaseLog {
$sHtml .= '</td></tr></table>';
return $sHtml;
}
/**
* Produces an HTML representation, aimed at being used to produce a PDF with TCPDF (no table)
*/
*/
public function GetAsSimpleHtml($aTransfoHandler = null)
{
$sStyleCaseLogEntry = '';
@@ -322,7 +326,7 @@ class ormCaseLog {
$sTextEntry = call_user_func($aTransfoHandler, $sTextEntry, true /* wiki "links" only */);
}
$sTextEntry = InlineImage::FixUrls($sTextEntry);
}
}
$iPos += $aIndex[$index]['text_length'];
$sEntry = '<li>';
@@ -384,7 +388,7 @@ class ormCaseLog {
/**
* Produces an HTML representation, aimed at being used within the iTop framework
*/
*/
public function GetAsHTML(WebPage $oP = null, $bEditMode = false, $aTransfoHandler = null)
{
$bPrintableVersion = (utils::ReadParam('printable', '0') == '1');
@@ -504,16 +508,19 @@ class ormCaseLog {
$sHtml .= '</td></tr></table>';
return $sHtml;
}
/**
* Add a new entry to the log or merge the given text into the currently modified entry
* Add a new entry to the log or merge the given text into the currently modified entry
* and updates the internal index
* @param $sText string The text of the new entry
* @param $sText string The text of the new entry
*/
public function AddLogEntry($sText, $sOnBehalfOf = '')
{
$sText = HTMLSanitizer::Sanitize($sText);
//date/time ops moved here for test stability
$iNow = time();
$sDate = date(AttributeDateTime::GetInternalFormat());
$sText = HTMLSanitizer::Sanitize($sText);
if ($sOnBehalfOf == '')
{
$sOnBehalfOf = UserRights::GetUserFriendlyName();
@@ -539,23 +546,28 @@ class ormCaseLog {
}
$sSeparator = sprintf(CASELOG_SEPARATOR, $sDate, $sOnBehalfOf, $iUserId);
$iSepLength = strlen($sSeparator);
$iTextlength = strlen($sText);
$this->m_sLog = $sSeparator.$sText.$this->m_sLog; // Latest entry printed first
// Rebuild failed
$iSepLength = strlen($sSeparator);
$iTextLength = strlen($sText);
$this->m_aIndex[] = array(
'user_name' => $sOnBehalfOf,
'user_id' => $iUserId,
'date' => time(),
'text_length' => $iTextlength,
'date' => $iNow,
'text_length' => $iTextLength,
'separator_length' => $iSepLength,
'format' => 'html',
);
$this->RebuildIndex();
$this->m_bModified = true;
}
public function AddLogEntryFromJSON($oJson, $bCheckUserId = true)
{
//date/time ops moved here for test stability
$iNow = time();
if (isset($oJson->user_id))
{
if (!UserRights::IsAdministrator())
@@ -586,7 +598,7 @@ class ormCaseLog {
$iUserId = UserRights::GetUserId();
$sOnBehalfOf = UserRights::GetUserFriendlyName();
}
if (isset($oJson->date))
{
$oDate = new DateTime($oJson->date);
@@ -594,7 +606,7 @@ class ormCaseLog {
}
else
{
$iDate = time();
$iDate = $iNow;
}
if (isset($oJson->format))
{
@@ -615,18 +627,19 @@ class ormCaseLog {
$sDate = date(AttributeDateTime::GetInternalFormat(), $iDate);
$sSeparator = sprintf(CASELOG_SEPARATOR, $sDate, $sOnBehalfOf, $iUserId);
$iSepLength = strlen($sSeparator);
$iTextlength = strlen($sText);
$this->m_sLog = $sSeparator.$sText.$this->m_sLog; // Latest entry printed first
// Rebuild failed
$iSepLength = strlen($sSeparator);
$iTextLength = strlen($sText);
$this->m_aIndex[] = array(
'user_name' => $sOnBehalfOf,
'user_id' => $iUserId,
'date' => $iDate,
'text_length' => $iTextlength,
'user_name' => $sOnBehalfOf,
'user_id' => $iUserId,
'date' => $iNow,
'text_length' => $iTextLength,
'separator_length' => $iSepLength,
'format' => $sFormat,
'format' => 'html',
);
$this->RebuildIndex();
$this->m_bModified = true;
}
@@ -643,7 +656,7 @@ class ormCaseLog {
/**
* Get the latest entry from the log
* @param string The expected output format text|html
* @param string $sFormat The expected output format text|html
* @return string
*/
public function GetLatestEntry($sFormat = 'text')
@@ -663,7 +676,7 @@ class ormCaseLog {
$sRes = utils::HtmlToText($sRaw);
}
break;
case 'html':
if ($aLastEntry['format'] == 'text')
{
@@ -688,7 +701,7 @@ class ormCaseLog {
$iLast = end($aKeys); // Strict standards: the parameter passed to 'end' must be a variable since it is passed by reference
return $iLast;
}
/**
* Get the text string corresponding to the given entry in the log (zero based index, older entries first)
* @param integer $iIndex
@@ -708,5 +721,13 @@ class ormCaseLog {
$sText = substr($this->m_sLog, $iPos, $this->m_aIndex[$index]['text_length']);
return $sText;
}
public function RebuildIndex(): void
{
$aIndex = $this->oOrmCaseLogService->RebuildIndex($this->m_sLog, $this->m_aIndex);
if (count($aIndex) !== 0) {
//index rebuild required
$this->m_aIndex = $aIndex;
}
}
}
?>

View File

@@ -0,0 +1,57 @@
<?php
/**
* @copyright Copyright (C) 2010-2022 Combodo SARL
* @license http://opensource.org/licenses/AGPL-3.0
*/
class ormCaseLogService
{
const CASE_LOG_SEPARATOR_REGEX_FIND = "\n?========== \w+-\d+-\d+ \d+:\d+:\d+ : .*\s\(\d+\) ============\n\n";
const CASE_LOG_SEPARATOR_REGEX_EXTRACT = "\n?========== (?<date>\w+-\d+-\d+ \d+:\d+:\d+) : (?<user_name>.*)\s\((?<user_id>\d+)\) ============\n\n";
public function __construct()
{
}
/**
* @param $sLog
*
* @return array
*/
public function RebuildIndex(string $sLog, array $aIndex)
{
$aTexts = preg_split('/'.self::CASE_LOG_SEPARATOR_REGEX_FIND.'/', $sLog, 0, PREG_SPLIT_NO_EMPTY);
preg_match_all('/'.self::CASE_LOG_SEPARATOR_REGEX_FIND.'/', $sLog, $aMatches);
if (count($aTexts) != count($aMatches[0])) {
return [];
}
$aRebuiltIndex = [];
$iPrevDate = 0;
for ($index = count($aTexts) - 1; $index >= 0; $index--) {
$sSeparator = $aMatches[0][$index];
preg_match('/'.self::CASE_LOG_SEPARATOR_REGEX_EXTRACT.'/', $sSeparator, $aSeparatorParts);
try {
$iDate = (int)AttributeDateTime::GetAsUnixSeconds($aSeparatorParts['date']);
$iPrevDate = $iDate + 1;
}
catch (Exception $e) {
$iDate = $iPrevDate;
}
$user_id = $aSeparatorParts['user_id'];
$aRebuiltIndex[] = array(
'user_name' => $aSeparatorParts['user_name'],
'user_id' => $user_id ==='0' ? null : $user_id,
'date' => $iDate,
'text_length' => strlen($aTexts[$index]),
'separator_length' => strlen($sSeparator),
'format' => 'html',
);
}
return $aRebuiltIndex;
}
}

View File

@@ -2740,6 +2740,7 @@ return array(
'iWorkingTimeComputer' => $baseDir . '/core/computing.inc.php',
'lnkTriggerAction' => $baseDir . '/core/trigger.class.inc.php',
'ormCaseLog' => $baseDir . '/core/ormcaselog.class.inc.php',
'ormCaseLogService' => $baseDir . '/core/ormcaselogservice.class.inc.php',
'ormCustomFieldsValue' => $baseDir . '/core/ormcustomfieldsvalue.class.inc.php',
'ormDocument' => $baseDir . '/core/ormdocument.class.inc.php',
'ormLinkSet' => $baseDir . '/core/ormlinkset.class.inc.php',

View File

@@ -3108,6 +3108,7 @@ class ComposerStaticInit0018331147de7601e7552f7da8e3bb8b
'iWorkingTimeComputer' => __DIR__ . '/../..' . '/core/computing.inc.php',
'lnkTriggerAction' => __DIR__ . '/../..' . '/core/trigger.class.inc.php',
'ormCaseLog' => __DIR__ . '/../..' . '/core/ormcaselog.class.inc.php',
'ormCaseLogService' => __DIR__ . '/../..' . '/core/ormcaselogservice.class.inc.php',
'ormCustomFieldsValue' => __DIR__ . '/../..' . '/core/ormcustomfieldsvalue.class.inc.php',
'ormDocument' => __DIR__ . '/../..' . '/core/ormdocument.class.inc.php',
'ormLinkSet' => __DIR__ . '/../..' . '/core/ormlinkset.class.inc.php',

View File

@@ -0,0 +1,305 @@
<?php
/*
* @copyright Copyright (C) 2010-2021 Combodo SARL
* @license http://opensource.org/licenses/AGPL-3.0
*/
namespace Combodo\iTop\Test\UnitTest\Core;
use Combodo\iTop\Test\UnitTest\ItopDataTestCase;
use ormCaseLog;
/**
* Tests of the ormCaseLog class
*
* @covers \ormCaseLog
*
* @runTestsInSeparateProcesses
* @preserveGlobalState disabled
* @backupGlobals disabled
*/
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
* @throws \CoreException
* @throws \OQLException
*/
/*public function testGetEntryCount()
{
// New log, with no entry
$oLog = new ormCaseLog();
$this->assertEquals($oLog->GetEntryCount(), 0, 'Should be no entry yet, returned '.$oLog->GetEntryCount());
// Add an entry
$oLog->AddLogEntry('First entry');
$this->assertEquals($oLog->GetEntryCount(), 1, 'Should be 1 entry, returned '.$oLog->GetEntryCount());
}*/
/*public function testGetAsArray(){
$iBug="28581";
$sLog = file_get_contents(__DIR__ . "/resources/${iBug}_log.txt");
$sIndex = file_get_contents(__DIR__ . "/resources/${iBug}_index.txt");
$aIndex = unserialize($sIndex);
$oLog = new ormCaseLog($sLog, $aIndex);
$aEntries = $oLog->GetAsArray();
foreach ($aEntries as $i => $aEntry){
echo "$i\n";
\DateTime::createFromFormat(\AttributeDateTime::GetInternalFormat(), $aEntry['date']);
}
$this->assertNotEquals([], $aEntries);
$this->assertEquals(false, \CaseLogService::IsIndexIntegrityOk($aIndex, $sLog));
$aNewIndex = \CaseLogService::RebuildIndex($sLog);
$this->assertEquals(true, \CaseLogService::IsIndexIntegrityOk($aNewIndex, $sLog));
$sTestNSBP = " ";
echo "strlen:" . strlen($sTestNSBP) . " strlen(Sanitize):" . strlen(\HTMLSanitizer::Sanitize($sTestNSBP)) . "\n";
echo "mb_strlen:" . mb_strlen($sTestNSBP). " mb_strlen(Sanitize):" . mb_strlen(\HTMLSanitizer::Sanitize($sTestNSBP)) . "\n";
$sTestNSBP = "^M";
echo "strlen:" . strlen($sTestNSBP) . " strlen(Sanitize):" . strlen(\HTMLSanitizer::Sanitize($sTestNSBP)) . "\n";
echo "mb_strlen:" . mb_strlen($sTestNSBP). " mb_strlen(Sanitize):" . mb_strlen(\HTMLSanitizer::Sanitize($sTestNSBP)) . "\n";
$this->assertEquals($aNewIndex, $aIndex);
}*/
/*public function testOrmCaseLogOperations(){
$oLog = new ormCaseLog();
$sFirstEntry = file_get_contents(__DIR__ . '/resources/log.txt');
//$sFirstEntry = "ééééééééééééééééééééééééééé";
$oLog->AddLogEntry($sFirstEntry, 'admin', 11);
$oLog->AddLogEntry("test", 'admin', 11);
$this->assertEquals(true, \CaseLogService::IsIndexIntegrityOk($oLog->GetIndex(), $oLog->__toString()));
//$sExpected = $sFirstEntry;
$sExpected = $sFirstEntry."\n". "test";
$aEntries = $oLog->GetAsArray();
foreach ($aEntries as $i => $aEntry){
echo "$i\n";
$this->assertEquals($sExpected, $aEntry['message']);
}
//$this->assertEquals([], $aEntries);
}*/
/*public function testBrokenCaseLog(){
//$sText = "é12é3é4é5éé";
$sText = file_get_contents(__DIR__ . '/resources/log.txt');
//$sSanitizedText = \HTMLSanitizer::Sanitize($sText);
$sSanitizedText=$sText;
$sDate = date(\AttributeDateTime::GetInternalFormat());
$sOnBehalfOf = "éléonore éàùèéö";
$iUserId=12;
$sSeparator = sprintf(CASELOG_SEPARATOR, $sDate, $sOnBehalfOf, $iUserId);
$iSepLength = strlen($sSeparator);
$iTextlength = strlen($sSanitizedText);
$m_sLog = $sSeparator.$sSanitizedText;
$sEntryStr = substr($m_sLog, $iSepLength, $iTextlength);
$this->assertEquals($sText, $sEntryStr);
}*/
/*public function testBreakURCaseLog() {
$this->CreateTestOrganization();
$oLog = new ormCaseLog();
$sFirstEntry = file_get_contents(__DIR__ . '/resources/log.txt');
$oLog->AddLogEntry($sFirstEntry, 'admin', 11);
$oUR = $this->createObject(\UserRequest::class,
[
'title' => 'BROKENCASELOG',
'description' => 'BROKENCASELOG',
'public_log' => $oLog,
'org_id' => $this->getTestOrgId()
]
);
$this->assertNotEquals(0, $oUR->GetKey());
$oUR = \MetaModel::GetObject(\UserRequest::class, $oUR->GetKey());
$oUR->Set('public_log', "toto1 ");
$oUR->Set('public_log', $sFirstEntry);
$oUR->Set('public_log', "titi2");
$oUR->DBWrite();
$oUR = \MetaModel::GetObject(\UserRequest::class, $oUR->GetKey());*/
/** @var ormCaseLog $oLog */
/*$oLog = $oUR->Get('public_log');
$oLog->AddLogEntry($sFirstEntry, 'hééé1', 666);
$oLog->AddLogEntry($sFirstEntry, 'hùhù2', 667);
$oUR->Set('public_log', $oLog);
$oUR->DBWrite();
$oUR = \MetaModel::GetObject(\UserRequest::class, $oUR->GetKey());*/
/** @var ormCaseLog $oLog */
/*$oLog = $oUR->Get('public_log');
$this->assertEquals(true, \CaseLogService::IsIndexIntegrityOk($oLog->GetIndex(), $oLog->__toString()));
$aEntries = $oLog->GetAsArray();
foreach ($aEntries as $i => $aEntry){
var_dump([$i => $aEntry['message']]);
}
var_dump($oLog->__toString());
}*/
public function ConstructorWithMockProvider(){
return [
'no rebuild' => [false],
'rebuild needed' => [true],
];
}
/**
* @dataProvider ConstructorWithMockProvider
*/
public function testConstructorWithMock($bRebuild){
$oOrmCaseLogService = $this->createMock(\ormCaseLogService::class);
$sLog = "aaaaa";
$aInitialIndex = ['a' => 'b'];
$aRebuiltIndex = ['c' => 'd'];
$aReturnedIndex= $bRebuild ? $aRebuiltIndex : [];
$oOrmCaseLogService->expects($this->exactly(1))
->method('RebuildIndex')
->with($sLog, $aInitialIndex)
->willReturn($aReturnedIndex);
$oLog = new ormCaseLog($sLog, $aInitialIndex, $oOrmCaseLogService);
$this->assertEquals($sLog, $oLog->GetText());
if ($bRebuild){
$this->assertEquals($aRebuiltIndex, $oLog->GetIndex());
} else {
$this->assertEquals($aInitialIndex, $oLog->GetIndex());
}
}
/**
* @dataProvider ConstructorWithMockProvider
*/
public function testAddLogEntry($bRebuild){
$_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'];
$aReturnedIndex= $bRebuild ? $aRebuiltIndex : [];
$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('RebuildIndex')
->withConsecutive(['', []], [$sExpectedLog, $aExpectedIndex])
->willReturnOnConsecutiveCalls([], $aReturnedIndex);
$oLog = new ormCaseLog('', [], $oOrmCaseLogService);
$oLog->AddLogEntry($sLog);
$this->assertEquals($sExpectedLog, $oLog->GetText());
if ($bRebuild){
$this->assertEquals($aRebuiltIndex, $oLog->GetIndex());
} else {
$this->assertEquals($aExpectedIndex, $oLog->GetIndex());
}
}
/**
* @dataProvider ConstructorWithMockProvider
*/
public function testAddLogEntryFromJSON($bRebuild){
$_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'];
$aReturnedIndex= $bRebuild ? $aRebuiltIndex : [];
$sJson = json_encode(
[
'user_login' => 'gabuzmeu',
'message' => $sLog,
]
);
$oJson = json_decode($sJson);
$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('RebuildIndex')
->withConsecutive(['', []], [$sExpectedLog, $aExpectedIndex])
->willReturnOnConsecutiveCalls([], $aReturnedIndex);
$oLog = new ormCaseLog('', [], $oOrmCaseLogService);
$oLog->AddLogEntryFromJSON($oJson, false);
$this->assertEquals($sExpectedLog, $oLog->GetText());
if ($bRebuild){
$this->assertEquals($aRebuiltIndex, $oLog->GetIndex());
} else {
$this->assertEquals($aExpectedIndex, $oLog->GetIndex());
}
}
}