From 128e7eed42aa548d07ded25bcfcde3c84fa3894e Mon Sep 17 00:00:00 2001 From: odain Date: Thu, 6 Apr 2023 17:30:06 +0200 Subject: [PATCH] =?UTF-8?q?N=C2=B05341=20-=20start=20testing=20ormcaselog?= =?UTF-8?q?=20rebuild=20when=20broken?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../unitary-tests/core/ormCaseLogTest.php | 305 ++++++++++++++++++ 1 file changed, 305 insertions(+) create mode 100644 tests/php-unit-tests/unitary-tests/core/ormCaseLogTest.php diff --git a/tests/php-unit-tests/unitary-tests/core/ormCaseLogTest.php b/tests/php-unit-tests/unitary-tests/core/ormCaseLogTest.php new file mode 100644 index 000000000..139ed0fac --- /dev/null +++ b/tests/php-unit-tests/unitary-tests/core/ormCaseLogTest.php @@ -0,0 +1,305 @@ + '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."

$sLog

"; + + $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."

$sLog

"; + + $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()); + } + } +}