mirror of
https://github.com/Combodo/iTop.git
synced 2026-03-01 07:04:16 +01:00
306 lines
9.2 KiB
PHP
306 lines
9.2 KiB
PHP
<?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>";
|
||
|
||
$aExpectedIndex = [
|
||
[
|
||
'user_name' => $sOnBehalfOf,
|
||
'user_id' => $iUserId,
|
||
'date' => time(),
|
||
'text_length' => 12,
|
||
'separator_length' => strlen($sSeparator),
|
||
'format' => 'html',
|
||
]
|
||
];
|
||
|
||
$aRebuiltIndex = ['c' => 'd'];
|
||
$aReturnedIndex= $bRebuild ? $aRebuiltIndex : [];
|
||
|
||
$oOrmCaseLogService = $this->createMock(\ormCaseLogService::class);
|
||
$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>";
|
||
|
||
$aExpectedIndex = [
|
||
[
|
||
'user_name' => $sOnBehalfOf,
|
||
'user_id' => $iUserId,
|
||
'date' => time(),
|
||
'text_length' => 12,
|
||
'separator_length' => strlen($sSeparator),
|
||
'format' => 'html',
|
||
]
|
||
];
|
||
|
||
$aRebuiltIndex = ['c' => 'd'];
|
||
$aReturnedIndex= $bRebuild ? $aRebuiltIndex : [];
|
||
|
||
$oOrmCaseLogService = $this->createMock(\ormCaseLogService::class);
|
||
$oOrmCaseLogService->expects($this->exactly(2))
|
||
->method('RebuildIndex')
|
||
->withConsecutive(['', []], [$sExpectedLog, $aExpectedIndex])
|
||
->willReturnOnConsecutiveCalls([], $aReturnedIndex);
|
||
|
||
$oLog = new ormCaseLog('', [], $oOrmCaseLogService);
|
||
$sJson = json_encode(
|
||
[
|
||
'user_login' => 'gabuzmeu',
|
||
'message' => $sLog,
|
||
]
|
||
);
|
||
$oJson = json_decode($sJson);
|
||
$oLog->AddLogEntryFromJSON($oJson, false);
|
||
$this->assertEquals($sExpectedLog, $oLog->GetText());
|
||
if ($bRebuild){
|
||
$this->assertEquals($aRebuiltIndex, $oLog->GetIndex());
|
||
} else {
|
||
$this->assertEquals($aExpectedIndex, $oLog->GetIndex());
|
||
}
|
||
}
|
||
}
|