mirror of
https://github.com/Combodo/iTop.git
synced 2026-03-06 17:44:12 +01:00
271 lines
8.0 KiB
PHP
271 lines
8.0 KiB
PHP
<?php
|
|
/*
|
|
* @copyright Copyright (C) 2010-2023 Combodo SARL
|
|
* @license http://opensource.org/licenses/AGPL-3.0
|
|
*/
|
|
|
|
|
|
namespace Combodo\iTop\Test\UnitTest\Core;
|
|
|
|
use Combodo\iTop\Test\OrmCaseLogExtensionForTest;
|
|
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 __DIR__ . "/OrmCaseLogExtensionForTest.php";
|
|
require_once APPROOT . "core/ormcaselogservice.inc.php";
|
|
|
|
$oAdminProfile = \MetaModel::GetObjectFromOQL("SELECT URP_Profiles WHERE name = :name", array('name' => 'Administrator'), true);
|
|
|
|
if (is_object($oAdminProfile)) {
|
|
$this->sLogin = sprintf("admin-%s-%s", date('dmYHis'), uniqid());
|
|
|
|
$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 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";
|
|
$sJson = json_encode(
|
|
[
|
|
'user_login' => 'gabuzmeu',
|
|
'message' => $sLog,
|
|
]
|
|
);
|
|
$oJson = json_decode($sJson);
|
|
|
|
//create a real service with no extension to speed up treatment and avoid +1s delta in history diff
|
|
$oOrmCaseLogService = new \ormCaseLogService([]);
|
|
|
|
$iUserId = \UserRights::GetUserId();
|
|
$sOnBehalfOf = \UserRights::GetUserFriendlyName();
|
|
$sDate = date(\AttributeDateTime::GetInternalFormat());
|
|
$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',
|
|
]
|
|
];
|
|
|
|
$oLog = new ormCaseLog('', [], $oOrmCaseLogService);
|
|
|
|
$oOrmCaseLogService = $this->createMock(\ormCaseLogService::class);
|
|
$oOrmCaseLogService->expects($this->exactly(1))
|
|
->method('Rebuild')
|
|
->withConsecutive([$sExpectedLog, $aExpectedIndex])
|
|
->willReturnOnConsecutiveCalls(null);
|
|
$this->SetNonPublicProperty($oLog, 'oOrmCaseLogService', $oOrmCaseLogService);
|
|
|
|
if ($bTestAddLogEntry){
|
|
$oLog->AddLogEntry($sLog);
|
|
} else {
|
|
$oLog->AddLogEntryFromJSON($oJson, false);
|
|
}
|
|
|
|
$this->assertEquals($sExpectedLog, $oLog->GetText());
|
|
$this->assertEquals($aExpectedIndex, $oLog->GetIndex());
|
|
}
|
|
|
|
/**
|
|
* @dataProvider AddLogEntryProvider
|
|
*/
|
|
public function testAddLogEntryWithRebuild($bTestAddLogEntry=true){
|
|
$_SESSION = array();
|
|
$this->assertTrue(\UserRights::Login($this->sLogin));
|
|
|
|
$aRebuiltIndex = ['c' => 'd'];
|
|
$sRebuiltLog = "bbbb";
|
|
$sLog = "aaaaa";
|
|
$sJson = json_encode(
|
|
[
|
|
'user_login' => 'gabuzmeu',
|
|
'message' => $sLog,
|
|
]
|
|
);
|
|
$oJson = json_decode($sJson);
|
|
|
|
//create a real service with no extension to speed up treatment and avoid +1s delta in history diff
|
|
$oOrmCaseLogService = new \ormCaseLogService([]);
|
|
|
|
$iUserId = \UserRights::GetUserId();
|
|
$sOnBehalfOf = \UserRights::GetUserFriendlyName();
|
|
$sDate = date(\AttributeDateTime::GetInternalFormat());
|
|
$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',
|
|
]
|
|
];
|
|
|
|
$oLog = new ormCaseLog('', [], $oOrmCaseLogService);
|
|
|
|
$oOrmCaseLogService = $this->createMock(\ormCaseLogService::class);
|
|
$oOrmCaseLogService->expects($this->exactly(1))
|
|
->method('Rebuild')
|
|
->withConsecutive([$sExpectedLog, $aExpectedIndex])
|
|
->willReturnOnConsecutiveCalls(new ormCaseLog($sRebuiltLog, $aRebuiltIndex));
|
|
$this->SetNonPublicProperty($oLog, 'oOrmCaseLogService', $oOrmCaseLogService);
|
|
|
|
if ($bTestAddLogEntry){
|
|
$oLog->AddLogEntry($sLog);
|
|
} else {
|
|
$oLog->AddLogEntryFromJSON($oJson, false);
|
|
}
|
|
|
|
$this->assertEquals($sRebuiltLog, $oLog->GetText());
|
|
$this->assertEquals($aRebuiltIndex, $oLog->GetIndex());
|
|
}
|
|
|
|
public function RebuildThroughApplicationExtensionImplementation(){
|
|
return [
|
|
'caselog is declared as modified by iOrmCaseLogExtension' => [ true ],
|
|
'caselog is declared as untouched by iOrmCaseLogExtension' => [ false ],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @dataProvider RebuildThroughApplicationExtensionImplementation
|
|
*/
|
|
public function testRebuildThroughApplicationExtensionImplementation(bool $bTouched){
|
|
$sLog = "aaaaa";
|
|
$sRebuiltLog = "bbbb";
|
|
$aInitialIndex = ['a' => 'b'];
|
|
$aRebuiltIndex = ['c' => 'd'];
|
|
|
|
/*$aOrmCaseLogExtensionForTest = $this->createMock(\iOrmCaseLogExtension::class);
|
|
$aOrmCaseLogExtensionForTest->expects($this->exactly(1))
|
|
->method('Rebuild')
|
|
->with(
|
|
$this->callback(
|
|
function ($sLogParam, &$aIndexParam) use ($aInitialIndex, $sLog, $aRebuiltIndex, $sRebuiltLog, $bTouched) {
|
|
$sLogParam = $sRebuiltLog;
|
|
$aIndexParam = $aRebuiltIndex;
|
|
return ($sLog === $sLogParam)
|
|
&& ($aInitialIndex === $sLogParam);
|
|
}
|
|
)
|
|
)
|
|
->willReturn($bTouched);*/
|
|
|
|
$aOrmCaseLogExtensionForTest = new OrmCaseLogExtensionForTest();
|
|
$aOrmCaseLogExtensionForTest->Init($bTouched, $sRebuiltLog, $aRebuiltIndex);
|
|
$aOrmCaseLogExtension=[$aOrmCaseLogExtensionForTest];
|
|
$oOrmCaseLogService = new \ormCaseLogService($aOrmCaseLogExtension);
|
|
|
|
$oLog = new ormCaseLog($sLog, $aInitialIndex, $oOrmCaseLogService);
|
|
if ($bTouched){
|
|
$this->assertEquals($aRebuiltIndex, $oLog->GetIndex());
|
|
$this->assertEquals($sRebuiltLog, $oLog->GetText());
|
|
} else {
|
|
$this->assertEquals($aInitialIndex, $oLog->GetIndex());
|
|
$this->assertEquals($sLog, $oLog->GetText());
|
|
}
|
|
$this->assertEquals($bTouched, $this->GetNonPublicProperty($oLog, 'm_bModified'));
|
|
}
|
|
}
|