Merge remote-tracking branch 'origin/support/3.0' into support/3.1

# Conflicts:
#	tests/php-unit-tests/integration-tests/DictionariesConsistencyTest.php
#	tests/php-unit-tests/src/BaseTestCase/ItopTestCase.php
#	tests/php-unit-tests/unitary-tests/core/CMDBSource/TransactionsTest.php
#	tests/php-unit-tests/unitary-tests/sources/Application/TwigBase/Twig/TwigTest.php
This commit is contained in:
Romain Quetiez
2023-10-27 14:12:06 +02:00
23 changed files with 315 additions and 198 deletions

View File

@@ -26,6 +26,13 @@ class CMDBSourceTest extends ItopTestCase
$this->RequireOnceItopFile('/core/cmdbsource.class.inc.php');
}
protected function tearDown(): void
{
DbConnectionWrapper::SetDbConnectionMockForQuery();
parent::tearDown();
}
/**
* @covers CMDBSource::IsSameFieldTypes
* @dataProvider compareFieldTypesProvider

View File

@@ -1,6 +1,6 @@
<?php
/**
* @copyright Copyright (C) 2010-2023 Combodo SARL
* @copyright Copyright (C) 2010-2021 Combodo SARL
* @license http://opensource.org/licenses/AGPL-3.0
*/
@@ -257,30 +257,28 @@ class TransactionsTest extends ItopTestCase
/**
* @return void
* @doesNotPerformAssertions
*/
public function testTransactionOpenedThenClosed()
public function testIsInsideTransaction()
{
CMDBSource::Query('START TRANSACTION;');
CMDBSource::Query('COMMIT;');
}
static::assertFalse(CMDBSource::IsInsideTransaction(), 'Should not be already inside a transaction');
/**
* This will throw an exception in the tearDown method.
* This cannot be detected nor by `@expectedException` nor `expectException` method, so we have a specific tearDown impl
*
* @return void
* @doesNotPerformAssertions
*/
public function testTransactionOpenedNotClosed()
{
// First, with a transaction ended by a "COMMIT" statement
CMDBSource::Query('START TRANSACTION;');
static::assertTrue(CMDBSource::IsInsideTransaction(), 'Should be inside a translation');
CMDBSource::Query('COMMIT;');
static::assertFalse(CMDBSource::IsInsideTransaction(), 'Should not be inside a transaction anymore');
// Second, with a transaction ended by a "ROLLBACK" statement
CMDBSource::Query('START TRANSACTION;');
static::assertTrue(CMDBSource::IsInsideTransaction(), 'Should be inside a translation (again)');
CMDBSource::Query('ROLLBACK;');
static::assertFalse(CMDBSource::IsInsideTransaction(), 'Should not be inside a transaction anymore');
}
protected function tearDown(): void
{
try {
DbConnectionWrapper::SetDbConnectionMockForQuery(); // Else will throw error on PHP 8.1+ (see N°6848)
DbConnectionWrapper::SetDbConnectionMockForQuery();
parent::tearDown();
}
catch (MySQLTransactionNotClosedException $e) {

View File

@@ -557,7 +557,7 @@ class ExpressionEvaluateTest extends ItopDataTestCase
public function EveryTimeFormatOnDateRangeProvider()
{
return array(
'10 years, day by day' => array('2000-01-01', 'P1D', 365 * 10),
'10 years, each 17 days' => array('2000-01-01', 'P17D', 365 * 10 / 17),
'1 day, hour by hour' => array('2000-01-01 00:01:02', 'PT1H', 24),
'1 hour, minute by minute' => array('2000-01-01 00:01:02', 'PT1M', 60),
'1 minute, second by second' => array('2000-01-01 00:01:02', 'PT1S', 60),

View File

@@ -30,8 +30,6 @@ class ExceptionLogTest extends ItopDataTestCase
}
/**
* @runInSeparateProcess
*
* @dataProvider logProvider
*/
public function testLogInFile($aLevels, $aExceptions, $sChannel, $aExpectedWriteNumber, $logLevelMin, $aExpectedDbWriteNumber, $logLevelMinWriteInDb)
@@ -91,14 +89,26 @@ class ExceptionLogTest extends ItopDataTestCase
$oExpectedLastEventIssue = $this->InvokeNonPublicStaticMethod('ExceptionLog', 'GetLastEventIssue', []);
if (0 == $iExpectedDbWriteNumber) {
$this->assertNull($oExpectedLastEventIssue);
if (!is_null($oExpectedLastEventIssue)) {
$this->fail("Level '$sLevel': unexpected EventIssue");
}
} else {
$this->assertInstanceOf(\EventIssue::class, $oExpectedLastEventIssue);
$this->assertInstanceOf(\EventIssue::class, $oExpectedLastEventIssue, "Level '$sLevel': missing EventIssue");
$this->assertEquals($aExpectedFileContext, $oExpectedLastEventIssue->Get('data'));
}
}
}
/**
* @return array[]
* aLevels: log levels to iterate on (AND name of the method that will be called on the underlying FileLog)
* aExceptions: For each log level => Exception to generate
* sChannel: Expected 2nd argument to the FileLog::{Level}
* aExpectedWriteNumber: For each log level => Number of times the method FileLog::{Level} will be called
* logLevelMin: Configuration / log_level_min
* iExpectedDbWriteNumber: For each log level => 1 if at least ONE EventIssue has been recorded into the DB
* logLevelMinWriteInDb: Configuration / log_level_min.write_in_db
*/
public function logProvider()
{
return [

View File

@@ -48,12 +48,7 @@ class UserRightsTest extends ItopDataTestCase
{
parent::setUp();
try {
utils::GetConfig()->SetModuleSetting('authent-local', 'password_validation.pattern', '');
self::CreateUser('admin', 1);
}
catch (CoreCannotSaveObjectException $e) {
}
utils::GetConfig()->SetModuleSetting('authent-local', 'password_validation.pattern', '');
}
public static $aClasses = [
@@ -103,6 +98,15 @@ class UserRightsTest extends ItopDataTestCase
public function testLogin($sLogin, $bResult)
{
$_SESSION = [];
if ($sLogin == 'admin') {
// Fixture data required in this case only
try {
self::CreateUser('admin', 1);
}
catch (CoreCannotSaveObjectException $e) {
// The admin account could exist, depending on where and when the test suite is executed
}
}
$this->assertEquals($bResult, UserRights::Login($sLogin));
$this->assertEquals($bResult, UserRights::IsLoggedIn());
UserRights::Logoff();
@@ -487,8 +491,7 @@ class UserRightsTest extends ItopDataTestCase
];
}
/**
* @runInSeparateProcess
*@dataProvider NonAdminCannotListAdminProfilesProvider
* @dataProvider NonAdminCannotListAdminProfilesProvider
*/
public function testNonAdminCannotListAdminProfiles($bHideAdministrators, $iExpectedCount)
{

View File

@@ -32,7 +32,7 @@ define('UNIT_MAX_CACHE_FILES', 10);
/**
* @runTestsInSeparateProcesses
* @runTestsInSeparateProcesses Required (at least) to mock the MetaModel and utils class
*/
class apcEmulationTest extends ItopTestCase
{

View File

@@ -30,9 +30,6 @@ use Combodo\iTop\Test\UnitTest\ItopTestCase;
use Dict;
/**
* @runTestsInSeparateProcesses
*/
class dictApcuTest extends ItopTestCase
{
private $sEnvName;
@@ -45,9 +42,16 @@ class dictApcuTest extends ItopTestCase
$this->RequireOnceItopFile('core'.DIRECTORY_SEPARATOR.'apc-service.class.inc.php');
$this->sEnvName = time();
// This id will be used as path to the dictionary files
// It must be unique enough for the magic of Dict to operate (due to the use of require_once to load dictionaries)
$this->sEnvName = uniqid();
$_SESSION['itop_env'] = $this->sEnvName;
// Preserve the dictionary for test that will be executed later on
static::BackupStaticProperties('Dict');
// Reset and prepare the dictionary
static::SetNonPublicStaticProperty('Dict', 'm_aData', []);
$this->oApcService = $this->createMock(\ApcService::class);
Dict::SetApcService($this->oApcService);
Dict::EnableCache('toto');
@@ -57,7 +61,8 @@ class dictApcuTest extends ItopTestCase
$this->InitDictionnaries();
}
private function InitDictionnaries(){
private function InitDictionnaries()
{
clearstatcache();
$this->sDictionaryFolder = APPROOT."env-$this->sEnvName" . DIRECTORY_SEPARATOR . "dictionaries";
@mkdir($this->sDictionaryFolder, 0777, true);
@@ -82,7 +87,8 @@ STR;
clearstatcache();
}
private function InitDictionnary($sDictionaryFolder, $sLanguageCode, $sLanguageCodeInFilename, $sLabels){
private function InitDictionnary($sDictionaryFolder, $sLanguageCode, $sLanguageCodeInFilename, $sLabels)
{
$sContent = <<<PHP
<?php
//
@@ -95,7 +101,8 @@ PHP;
file_put_contents($sDictionaryFolder . DIRECTORY_SEPARATOR . "$sLanguageCodeInFilename.dict.php", $sContent);
}
private function InitBrokenDictionnary($sDictionaryFolder, $sLanguageCode, $sLanguageCodeInFilename){
private function InitBrokenDictionnary($sDictionaryFolder, $sLanguageCode, $sLanguageCodeInFilename)
{
$sContent = <<<PHP
<?php
//
@@ -114,10 +121,13 @@ PHP;
rmdir(APPROOT."env-$this->sEnvName".DIRECTORY_SEPARATOR."dictionaries");
rmdir(APPROOT."env-$this->sEnvName");
static::RestoreStaticProperties('Dict');
parent::tearDown();
}
public function InitLangIfNeeded_NoApcProvider(){
public function InitLangIfNeeded_NoApcProvider()
{
return [
'apcu mocked' => [ 'bApcuMocked' => true ],
'integration test - apcu service like in production - install php-apcu before' => [ 'bApcuMocked' => false ],
@@ -127,7 +137,8 @@ PHP;
/**
* @dataProvider InitLangIfNeeded_NoApcProvider
*/
public function testInitLangIfNeeded_NoApc($bApcuMocked){
public function testInitLangIfNeeded_NoApc($bApcuMocked)
{
if ($bApcuMocked) {
$this->oApcService->expects($this->any())
->method('function_exists')
@@ -163,7 +174,8 @@ PHP;
$this->assertEquals('not_defined_label', Dict::S('not_defined_label'));
}
public function testInitLangIfNeeded_Apc_LanguageMismatchDictionnary(){
public function testInitLangIfNeeded_Apc_LanguageMismatchDictionnary()
{
//language mismatch!!
$sLabels = <<<STR
'label1' => 'de1',
@@ -185,7 +197,8 @@ STR;
$this->assertEquals('label1', Dict::S('label1'));
}
public function testInitLangIfNeeded_Apc_BrokenUserDictionnary(){
public function testInitLangIfNeeded_Apc_BrokenUserDictionnary()
{
$this->InitBrokenDictionnary($this->sDictionaryFolder, 'DE DE', 'de-de');
$this->oApcService->expects($this->any())
@@ -205,7 +218,8 @@ STR;
$this->assertEquals('ru1', Dict::S('label1'));
}
public function testInitLangIfNeeded_Apc_BrokenDictionnary_UserAndDefault(){
public function testInitLangIfNeeded_Apc_BrokenDictionnary_UserAndDefault()
{
$this->InitBrokenDictionnary($this->sDictionaryFolder, 'DE DE', 'de-de');
$this->InitBrokenDictionnary($this->sDictionaryFolder, 'RU RU', 'ru-ru');
@@ -224,7 +238,8 @@ STR;
$this->assertEquals('en1', Dict::S('label1'));
}
public function testInitLangIfNeeded_Apc_BrokenDictionnary_ALL(){
public function testInitLangIfNeeded_Apc_BrokenDictionnary_ALL()
{
$this->InitBrokenDictionnary($this->sDictionaryFolder, 'DE DE', 'de-de');
$this->InitBrokenDictionnary($this->sDictionaryFolder, 'RU RU', 'ru-ru');
$this->InitBrokenDictionnary($this->sDictionaryFolder, 'EN US', 'en-us');
@@ -244,7 +259,8 @@ STR;
$this->assertEquals('label1', Dict::S('label1'));
}
public function testInitLangIfNeeded_ApcFromCache_PropertyInUserDictionnary(){
public function testInitLangIfNeeded_ApcFromCache_PropertyInUserDictionnary()
{
$this->oApcService->expects($this->any())
->method('function_exists')
->willReturn(true);
@@ -262,7 +278,8 @@ STR;
$this->assertEquals('fr1', Dict::S('label1'));
}
public function testInitLangIfNeeded_ApcStore_PropertyInUserDictionnary(){
public function testInitLangIfNeeded_ApcStore_PropertyInUserDictionnary()
{
$this->oApcService->expects($this->any())
->method('function_exists')
->willReturn(true);
@@ -341,7 +358,8 @@ STR;
$this->assertEquals('en2', Dict::S('label2'));
}
public function testInitLangIfNeeded_Apc_PropertyInDictDefaultLanguageDictionnary(){
public function testInitLangIfNeeded_Apc_PropertyInDictDefaultLanguageDictionnary()
{
$this->oApcService->expects($this->any())
->method('function_exists')
->willReturn(true);
@@ -362,7 +380,8 @@ STR;
$this->assertEquals('en3', Dict::S('label3'));
}
public function testInitLangIfNeeded_ApcCorrupted_PropertyInDictDefaultLanguageDictionnary(){
public function testInitLangIfNeeded_ApcCorrupted_PropertyInDictDefaultLanguageDictionnary()
{
$this->oApcService->expects($this->any())
->method('function_exists')
->willReturn(true);
@@ -380,7 +399,8 @@ STR;
$this->assertEquals('label3', Dict::S('label3'));
}
public function testInitLangIfNeeded_Apc_PropertyNotFound(){
public function testInitLangIfNeeded_Apc_PropertyNotFound()
{
$this->oApcService->expects($this->any())
->method('function_exists')
->willReturn(true);

View File

@@ -30,9 +30,6 @@ use Combodo\iTop\Test\UnitTest\ItopTestCase;
use Dict;
use Exception;
/**
* @runClassInSeparateProcess
*/
class dictTest extends ItopTestCase
{
private $sEnvName;
@@ -42,7 +39,9 @@ class dictTest extends ItopTestCase
$this->RequireOnceItopFile('core'.DIRECTORY_SEPARATOR.'apc-service.class.inc.php');
$this->sEnvName = time();
// This id will be used as path to the dictionary files
// It must be unique enough for the magic of Dict to operate (due to the use of require_once to load dictionaries)
$this->sEnvName = uniqid();
$sDictionaryFolder = APPROOT."env-$this->sEnvName".DIRECTORY_SEPARATOR."dictionaries";
@mkdir($sDictionaryFolder, 0777, true);
@@ -68,6 +67,9 @@ PHP;
file_put_contents($sDictionaryFolder.DIRECTORY_SEPARATOR."en-en.dict.php", $sContent);
$_SESSION['itop_env'] = $this->sEnvName;
// Preserve the dictionary for test that will be executed later on
static::BackupStaticProperties('Dict');
}
protected function tearDown(): void
@@ -78,6 +80,8 @@ PHP;
rmdir(APPROOT."env-$this->sEnvName".DIRECTORY_SEPARATOR."dictionaries");
rmdir(APPROOT."env-$this->sEnvName");
static::RestoreStaticProperties('Dict');
parent::tearDown();
}
@@ -93,6 +97,9 @@ PHP;
public function testInitLangIfNeeded_NoApc()
{
// Reset the dictionary
static::SetNonPublicStaticProperty('Dict', 'm_aData', []);
$oApcService = $this->createMock(\ApcService::class);
Dict::SetApcService($oApcService);
Dict::EnableCache('toto');