diff --git a/core/ormcaselogservice.inc.php b/core/ormcaselogservice.inc.php index d1e933e3c..c884da4dd 100644 --- a/core/ormcaselogservice.inc.php +++ b/core/ormcaselogservice.inc.php @@ -17,20 +17,21 @@ class ormCaseLogService */ protected $aOrmCaseLogExtension = null; - public function __construct() + public function __construct(array $aOrmCaseLogExtensions=null) { + $this->aOrmCaseLogExtension = $aOrmCaseLogExtensions; } protected function LoadCaseLogExtensions() { if ($this->aOrmCaseLogExtension !== null) return; - $aOrmCaseLogExtension = []; - $aProviderClasses = \utils::GetClassesForInterface(iOrmCaseLogExtension::class, '', array('[\\\\/]lib[\\\\/]', '[\\\\/]node_modules[\\\\/]', '[\\\\/]test[\\\\/]', '[\\\\/]tests[\\\\/]')); - foreach($aProviderClasses as $sProviderClass) { - $aOrmCaseLogExtension[] = new $sProviderClass(); + $aOrmCaseLogExtensions = []; + $aOrmCaseLogExtensionClasses = \utils::GetClassesForInterface(iOrmCaseLogExtension::class, '', array('[\\\\/]lib[\\\\/]', '[\\\\/]node_modules[\\\\/]', '[\\\\/]test[\\\\/]', '[\\\\/]tests[\\\\/]')); + foreach($aOrmCaseLogExtensionClasses as $sOrmCaseLogExtensionClass) { + $aOrmCaseLogExtensions[] = new $sOrmCaseLogExtensionClass(); } - $this->aOrmCaseLogExtension = $aOrmCaseLogExtension; + $this->aOrmCaseLogExtension = $aOrmCaseLogExtensions; } /** diff --git a/tests/php-unit-tests/unitary-tests/core/OrmCaseLogExtensionForTest.php b/tests/php-unit-tests/unitary-tests/core/OrmCaseLogExtensionForTest.php new file mode 100644 index 000000000..ce2641fe6 --- /dev/null +++ b/tests/php-unit-tests/unitary-tests/core/OrmCaseLogExtensionForTest.php @@ -0,0 +1,24 @@ +bTouched = $bTouched; + $this->sReturnedLog = $sReturnedLog; + $this->aReturnedIndex = $aReturnedIndex; + } + + public function Rebuild(&$sLog, &$aIndex) : bool{ + $sLog = $this->sReturnedLog; + $aIndex = $this->aReturnedIndex; + return $this->bTouched; + } +} + diff --git a/tests/php-unit-tests/unitary-tests/core/ormCaseLogTest.php b/tests/php-unit-tests/unitary-tests/core/ormCaseLogTest.php index fb4e81b0f..1e456c302 100644 --- a/tests/php-unit-tests/unitary-tests/core/ormCaseLogTest.php +++ b/tests/php-unit-tests/unitary-tests/core/ormCaseLogTest.php @@ -10,7 +10,6 @@ namespace Combodo\iTop\Test\UnitTest\Core; use Combodo\iTop\Test\UnitTest\ItopDataTestCase; use ormCaseLog; - /** * Tests of the ormCaseLog class * @@ -28,7 +27,8 @@ class ormCaseLogTest extends ItopDataTestCase public function setUp() :void{ parent::setUp(); // TODO: Change the autogenerated stub - //require_once APPROOT . "core/CaseLogService.php"; + 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); @@ -117,13 +117,20 @@ class ormCaseLogTest extends ItopDataTestCase $this->assertTrue(\UserRights::Login($this->sLogin)); $sLog = "aaaaa"; - $sDate = date(\AttributeDateTime::GetInternalFormat()); + $sJson = json_encode( + [ + 'user_login' => 'gabuzmeu', + 'message' => $sLog, + ] + ); + $oJson = json_decode($sJson); + $iUserId = \UserRights::GetUserId(); $sOnBehalfOf = \UserRights::GetUserFriendlyName(); + $sDate = date(\AttributeDateTime::GetInternalFormat()); $sSeparator = sprintf(CASELOG_SEPARATOR, $sDate, $sOnBehalfOf, $iUserId); $sExpectedLog = $sSeparator."
$sLog
"; - $oOrmCaseLogService = $this->createMock(\ormCaseLogService::class); $aExpectedIndex = [ [ 'user_name' => $sOnBehalfOf, @@ -135,22 +142,20 @@ class ormCaseLogTest extends ItopDataTestCase ] ]; - $oOrmCaseLogService->expects($this->exactly(2)) - ->method('Rebuild') - ->withConsecutive(['', []], [$sExpectedLog, $aExpectedIndex]) - ->willReturnOnConsecutiveCalls(null, null); - + //create a real service with no extension to speed up treatment and avoid +1s delta in history diff + $oOrmCaseLogService = new \ormCaseLogService([]); $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 { - $sJson = json_encode( - [ - 'user_login' => 'gabuzmeu', - 'message' => $sLog, - ] - ); - $oJson = json_decode($sJson); $oLog->AddLogEntryFromJSON($oJson, false); } @@ -161,22 +166,26 @@ class ormCaseLogTest extends ItopDataTestCase /** * @dataProvider AddLogEntryProvider */ - public function testAddLogEntry($bTestAddLogEntry=true){ + public function testAddLogEntryWithRebuild($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."$sLog
"; - $aRebuiltIndex = ['c' => 'd']; $sRebuiltLog = "bbbb"; + $sLog = "aaaaa"; + $sJson = json_encode( + [ + 'user_login' => 'gabuzmeu', + 'message' => $sLog, + ] + ); + $oJson = json_decode($sJson); - $oOrmCaseLogService = $this->createMock(\ormCaseLogService::class); - + $iUserId = \UserRights::GetUserId(); + $sOnBehalfOf = \UserRights::GetUserFriendlyName(); + $sDate = date(\AttributeDateTime::GetInternalFormat()); + $sSeparator = sprintf(CASELOG_SEPARATOR, $sDate, $sOnBehalfOf, $iUserId); + $sExpectedLog = $sSeparator."$sLog
"; $aExpectedIndex = [ [ 'user_name' => $sOnBehalfOf, @@ -187,18 +196,56 @@ class ormCaseLogTest extends ItopDataTestCase 'format' => 'html', ] ]; - $oOrmCaseLogService->expects($this->exactly(2)) - ->method('Rebuild') - ->withConsecutive(['', []], [$sExpectedLog, $aExpectedIndex]) - ->willReturnOnConsecutiveCalls( - null, - new ormCaseLog($sRebuiltLog, $aRebuiltIndex) - ); + //create a real service with no extension to speed up treatment and avoid +1s delta in history diff + $oOrmCaseLogService = new \ormCaseLogService([]); $oLog = new ormCaseLog('', [], $oOrmCaseLogService); - $oLog->AddLogEntry($sLog); + + $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 = 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()); + } + } }